• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "anv_private.h"
25 
26 #include "vk_util.h"
27 
28 static unsigned
num_subpass_attachments(const VkSubpassDescription * desc)29 num_subpass_attachments(const VkSubpassDescription *desc)
30 {
31    return desc->inputAttachmentCount +
32           desc->colorAttachmentCount +
33           (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
34           (desc->pDepthStencilAttachment != NULL);
35 }
36 
37 static void
init_first_subpass_layout(struct anv_render_pass_attachment * const att,const VkAttachmentReference att_ref)38 init_first_subpass_layout(struct anv_render_pass_attachment * const att,
39                           const VkAttachmentReference att_ref)
40 {
41    if (att->first_subpass_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
42       att->first_subpass_layout = att_ref.layout;
43       assert(att->first_subpass_layout != VK_IMAGE_LAYOUT_UNDEFINED);
44    }
45 }
46 
anv_CreateRenderPass(VkDevice _device,const VkRenderPassCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkRenderPass * pRenderPass)47 VkResult anv_CreateRenderPass(
48     VkDevice                                    _device,
49     const VkRenderPassCreateInfo*               pCreateInfo,
50     const VkAllocationCallbacks*                pAllocator,
51     VkRenderPass*                               pRenderPass)
52 {
53    ANV_FROM_HANDLE(anv_device, device, _device);
54 
55    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
56 
57    struct anv_render_pass *pass;
58    struct anv_subpass *subpasses;
59    struct anv_render_pass_attachment *attachments;
60    enum anv_pipe_bits *subpass_flushes;
61 
62    ANV_MULTIALLOC(ma);
63    anv_multialloc_add(&ma, &pass, 1);
64    anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
65    anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
66    anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
67 
68    VkAttachmentReference *subpass_attachments;
69    uint32_t subpass_attachment_count = 0;
70    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
71       subpass_attachment_count +=
72          num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
73    }
74    anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
75 
76    if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
77                               VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
78       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
79 
80    /* Clear the subpasses along with the parent pass. This required because
81     * each array member of anv_subpass must be a valid pointer if not NULL.
82     */
83    memset(pass, 0, ma.size);
84    pass->attachment_count = pCreateInfo->attachmentCount;
85    pass->subpass_count = pCreateInfo->subpassCount;
86    pass->attachments = attachments;
87    pass->subpass_flushes = subpass_flushes;
88 
89    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
90       struct anv_render_pass_attachment *att = &pass->attachments[i];
91 
92       att->format = pCreateInfo->pAttachments[i].format;
93       att->samples = pCreateInfo->pAttachments[i].samples;
94       att->usage = 0;
95       att->load_op = pCreateInfo->pAttachments[i].loadOp;
96       att->store_op = pCreateInfo->pAttachments[i].storeOp;
97       att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
98       att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
99       att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
100       att->first_subpass_layout = VK_IMAGE_LAYOUT_UNDEFINED;
101    }
102 
103    bool has_color = false, has_depth = false, has_input = false;
104    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
105       const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
106       struct anv_subpass *subpass = &pass->subpasses[i];
107 
108       subpass->input_count = desc->inputAttachmentCount;
109       subpass->color_count = desc->colorAttachmentCount;
110       subpass->attachment_count = num_subpass_attachments(desc);
111       subpass->attachments = subpass_attachments;
112       subpass->view_mask = 0;
113 
114       if (desc->inputAttachmentCount > 0) {
115          subpass->input_attachments = subpass_attachments;
116          subpass_attachments += desc->inputAttachmentCount;
117 
118          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
119             uint32_t a = desc->pInputAttachments[j].attachment;
120             subpass->input_attachments[j] = desc->pInputAttachments[j];
121             if (a != VK_ATTACHMENT_UNUSED) {
122                has_input = true;
123                pass->attachments[a].usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
124                pass->attachments[a].last_subpass_idx = i;
125 
126                init_first_subpass_layout(&pass->attachments[a],
127                                          desc->pInputAttachments[j]);
128                if (desc->pDepthStencilAttachment &&
129                    a == desc->pDepthStencilAttachment->attachment)
130                   subpass->has_ds_self_dep = true;
131             }
132          }
133       }
134 
135       if (desc->colorAttachmentCount > 0) {
136          subpass->color_attachments = subpass_attachments;
137          subpass_attachments += desc->colorAttachmentCount;
138 
139          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
140             uint32_t a = desc->pColorAttachments[j].attachment;
141             subpass->color_attachments[j] = desc->pColorAttachments[j];
142             if (a != VK_ATTACHMENT_UNUSED) {
143                has_color = true;
144                pass->attachments[a].usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
145                pass->attachments[a].last_subpass_idx = i;
146 
147                init_first_subpass_layout(&pass->attachments[a],
148                                          desc->pColorAttachments[j]);
149             }
150          }
151       }
152 
153       subpass->has_resolve = false;
154       if (desc->pResolveAttachments) {
155          subpass->resolve_attachments = subpass_attachments;
156          subpass_attachments += desc->colorAttachmentCount;
157 
158          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
159             uint32_t a = desc->pResolveAttachments[j].attachment;
160             subpass->resolve_attachments[j] = desc->pResolveAttachments[j];
161             if (a != VK_ATTACHMENT_UNUSED) {
162                subpass->has_resolve = true;
163                uint32_t color_att = desc->pColorAttachments[j].attachment;
164                pass->attachments[color_att].usage |=
165                   VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
166                pass->attachments[a].usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
167                pass->attachments[a].last_subpass_idx = i;
168 
169                init_first_subpass_layout(&pass->attachments[a],
170                                          desc->pResolveAttachments[j]);
171             }
172          }
173       }
174 
175       if (desc->pDepthStencilAttachment) {
176          uint32_t a = desc->pDepthStencilAttachment->attachment;
177          *subpass_attachments++ = subpass->depth_stencil_attachment =
178             *desc->pDepthStencilAttachment;
179          if (a != VK_ATTACHMENT_UNUSED) {
180             has_depth = true;
181             pass->attachments[a].usage |=
182                VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
183             pass->attachments[a].last_subpass_idx = i;
184 
185             init_first_subpass_layout(&pass->attachments[a],
186                                       *desc->pDepthStencilAttachment);
187          }
188       } else {
189          subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
190          subpass->depth_stencil_attachment.layout = VK_IMAGE_LAYOUT_UNDEFINED;
191       }
192    }
193 
194    for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
195       const VkSubpassDependency *dep = &pCreateInfo->pDependencies[i];
196       if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
197          pass->subpass_flushes[pass->subpass_count] |=
198             anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
199       } else {
200          assert(dep->dstSubpass < pass->subpass_count);
201          pass->subpass_flushes[dep->dstSubpass] |=
202             anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
203       }
204 
205       if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
206          pass->subpass_flushes[0] |=
207             anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
208       } else {
209          assert(dep->srcSubpass < pass->subpass_count);
210          pass->subpass_flushes[dep->srcSubpass + 1] |=
211             anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
212       }
213    }
214 
215    /* From the Vulkan 1.0.39 spec:
216     *
217     *    If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
218     *    first subpass that uses an attachment, then an implicit subpass
219     *    dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
220     *    used in. The subpass dependency operates as if defined with the
221     *    following parameters:
222     *
223     *    VkSubpassDependency implicitDependency = {
224     *        .srcSubpass = VK_SUBPASS_EXTERNAL;
225     *        .dstSubpass = firstSubpass; // First subpass attachment is used in
226     *        .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
227     *        .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
228     *        .srcAccessMask = 0;
229     *        .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
230     *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
231     *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
232     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
233     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
234     *        .dependencyFlags = 0;
235     *    };
236     *
237     *    Similarly, if there is no subpass dependency from the last subpass
238     *    that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
239     *    subpass dependency exists from the last subpass it is used in to
240     *    VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
241     *    with the following parameters:
242     *
243     *    VkSubpassDependency implicitDependency = {
244     *        .srcSubpass = lastSubpass; // Last subpass attachment is used in
245     *        .dstSubpass = VK_SUBPASS_EXTERNAL;
246     *        .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
247     *        .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
248     *        .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
249     *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
250     *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
251     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
252     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
253     *        .dstAccessMask = 0;
254     *        .dependencyFlags = 0;
255     *    };
256     *
257     * We could implement this by walking over all of the attachments and
258     * subpasses and checking to see if any of them don't have an external
259     * dependency.  Or, we could just be lazy and add a couple extra flushes.
260     * We choose to be lazy.
261     */
262    if (has_input) {
263       pass->subpass_flushes[0] |=
264          ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
265    }
266    if (has_color) {
267       pass->subpass_flushes[pass->subpass_count] |=
268          ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
269    }
270    if (has_depth) {
271       pass->subpass_flushes[pass->subpass_count] |=
272          ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
273    }
274 
275    vk_foreach_struct(ext, pCreateInfo->pNext) {
276       switch (ext->sType) {
277       case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHX: {
278          VkRenderPassMultiviewCreateInfoKHX *mv = (void *)ext;
279 
280          for (uint32_t i = 0; i < mv->subpassCount; i++) {
281             pass->subpasses[i].view_mask = mv->pViewMasks[i];
282          }
283          break;
284       }
285 
286       default:
287          anv_debug_ignored_stype(ext->sType);
288       }
289    }
290 
291    *pRenderPass = anv_render_pass_to_handle(pass);
292 
293    return VK_SUCCESS;
294 }
295 
anv_DestroyRenderPass(VkDevice _device,VkRenderPass _pass,const VkAllocationCallbacks * pAllocator)296 void anv_DestroyRenderPass(
297     VkDevice                                    _device,
298     VkRenderPass                                _pass,
299     const VkAllocationCallbacks*                pAllocator)
300 {
301    ANV_FROM_HANDLE(anv_device, device, _device);
302    ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
303 
304    vk_free2(&device->alloc, pAllocator, pass);
305 }
306 
anv_GetRenderAreaGranularity(VkDevice device,VkRenderPass renderPass,VkExtent2D * pGranularity)307 void anv_GetRenderAreaGranularity(
308     VkDevice                                    device,
309     VkRenderPass                                renderPass,
310     VkExtent2D*                                 pGranularity)
311 {
312    ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
313 
314    /* This granularity satisfies HiZ fast clear alignment requirements
315     * for all sample counts.
316     */
317    for (unsigned i = 0; i < pass->subpass_count; ++i) {
318       if (pass->subpasses[i].depth_stencil_attachment.attachment !=
319           VK_ATTACHMENT_UNUSED) {
320          *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
321          return;
322       }
323    }
324 
325    *pGranularity = (VkExtent2D) { 1, 1 };
326 }
327