xref: /aosp_15_r20/external/mesa3d/src/broadcom/vulkan/v3dv_pass.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019 Raspberry Pi Ltd
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 "v3dv_private.h"
25 
26 static uint32_t
num_subpass_attachments(const VkSubpassDescription2 * desc)27 num_subpass_attachments(const VkSubpassDescription2 *desc)
28 {
29    return desc->inputAttachmentCount +
30           desc->colorAttachmentCount +
31           (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
32           (desc->pDepthStencilAttachment != NULL);
33 }
34 
35 static void
set_try_tlb_resolve(struct v3dv_device * device,struct v3dv_render_pass_attachment * att)36 set_try_tlb_resolve(struct v3dv_device *device,
37                     struct v3dv_render_pass_attachment *att)
38 {
39    const struct v3dv_format *format = v3dv_X(device, get_format)(att->desc.format);
40    att->try_tlb_resolve = v3dv_X(device, format_supports_tlb_resolve)(format);
41 }
42 
43 static void
pass_find_subpass_range_for_attachments(struct v3dv_device * device,struct v3dv_render_pass * pass)44 pass_find_subpass_range_for_attachments(struct v3dv_device *device,
45                                         struct v3dv_render_pass *pass)
46 {
47    for (uint32_t i = 0; i < pass->attachment_count; i++) {
48       pass->attachments[i].first_subpass = pass->subpass_count - 1;
49       pass->attachments[i].last_subpass = 0;
50       if (pass->multiview_enabled) {
51          for (uint32_t j = 0; j < MAX_MULTIVIEW_VIEW_COUNT; j++) {
52             pass->attachments[i].views[j].first_subpass = pass->subpass_count - 1;
53             pass->attachments[i].views[j].last_subpass = 0;
54          }
55       }
56    }
57 
58    for (uint32_t i = 0; i < pass->subpass_count; i++) {
59       const struct v3dv_subpass *subpass = &pass->subpasses[i];
60 
61       for (uint32_t j = 0; j < subpass->color_count; j++) {
62          uint32_t attachment_idx = subpass->color_attachments[j].attachment;
63          if (attachment_idx == VK_ATTACHMENT_UNUSED)
64             continue;
65 
66          struct v3dv_render_pass_attachment *att =
67             &pass->attachments[attachment_idx];
68 
69          if (i < att->first_subpass)
70             att->first_subpass = i;
71          if (i > att->last_subpass)
72             att->last_subpass = i;
73 
74          uint32_t view_mask = subpass->view_mask;
75          while (view_mask) {
76             uint32_t view_index = u_bit_scan(&view_mask);
77             if (i < att->views[view_index].first_subpass)
78                att->views[view_index].first_subpass = i;
79             if (i > att->views[view_index].last_subpass)
80                att->views[view_index].last_subpass = i;
81          }
82 
83          if (subpass->resolve_attachments &&
84              subpass->resolve_attachments[j].attachment != VK_ATTACHMENT_UNUSED) {
85             set_try_tlb_resolve(device, att);
86          }
87       }
88 
89       uint32_t ds_attachment_idx = subpass->ds_attachment.attachment;
90       if (ds_attachment_idx != VK_ATTACHMENT_UNUSED) {
91          if (i < pass->attachments[ds_attachment_idx].first_subpass)
92             pass->attachments[ds_attachment_idx].first_subpass = i;
93          if (i > pass->attachments[ds_attachment_idx].last_subpass)
94             pass->attachments[ds_attachment_idx].last_subpass = i;
95 
96          if (subpass->ds_resolve_attachment.attachment != VK_ATTACHMENT_UNUSED)
97             set_try_tlb_resolve(device, &pass->attachments[ds_attachment_idx]);
98       }
99 
100       for (uint32_t j = 0; j < subpass->input_count; j++) {
101          uint32_t input_attachment_idx = subpass->input_attachments[j].attachment;
102          if (input_attachment_idx == VK_ATTACHMENT_UNUSED)
103             continue;
104          if (i < pass->attachments[input_attachment_idx].first_subpass)
105             pass->attachments[input_attachment_idx].first_subpass = i;
106          if (i > pass->attachments[input_attachment_idx].last_subpass)
107             pass->attachments[input_attachment_idx].last_subpass = i;
108       }
109 
110       if (subpass->resolve_attachments) {
111          for (uint32_t j = 0; j < subpass->color_count; j++) {
112             uint32_t attachment_idx = subpass->resolve_attachments[j].attachment;
113             if (attachment_idx == VK_ATTACHMENT_UNUSED)
114                continue;
115             if (i < pass->attachments[attachment_idx].first_subpass)
116                pass->attachments[attachment_idx].first_subpass = i;
117             if (i > pass->attachments[attachment_idx].last_subpass)
118                pass->attachments[attachment_idx].last_subpass = i;
119          }
120       }
121    }
122 }
123 
124 /* GFXH-1461: if depth is cleared but stencil is loaded (or vice versa),
125  * the clear might get lost. If a subpass has this then we can't emit
126  * the clear using the TLB and we have to do it as a draw call. This
127  * issue is fixed since V3D 4.3.18.
128  *
129  * FIXME: separate stencil.
130  */
131 static void
check_do_depth_stencil_clear_with_draw(struct v3dv_device * device,struct v3dv_render_pass * pass,struct v3dv_subpass * subpass)132 check_do_depth_stencil_clear_with_draw(struct v3dv_device *device,
133                                        struct v3dv_render_pass *pass,
134                                        struct v3dv_subpass *subpass)
135 {
136    if (device->devinfo.ver > 42 ||
137        subpass->ds_attachment.attachment == VK_ATTACHMENT_UNUSED) {
138       return;
139    }
140 
141    struct v3dv_render_pass_attachment *att =
142       &pass->attachments[subpass->ds_attachment.attachment];
143    if (att->desc.format != VK_FORMAT_D24_UNORM_S8_UINT)
144       return;
145 
146    if (att->desc.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR &&
147        att->desc.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
148       subpass->do_depth_clear_with_draw = true;
149    } else if (att->desc.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD &&
150               att->desc.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
151       subpass->do_stencil_clear_with_draw = true;
152    }
153 }
154 
155 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_CreateRenderPass2(VkDevice _device,const VkRenderPassCreateInfo2 * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkRenderPass * pRenderPass)156 v3dv_CreateRenderPass2(VkDevice _device,
157                        const VkRenderPassCreateInfo2 *pCreateInfo,
158                        const VkAllocationCallbacks *pAllocator,
159                        VkRenderPass *pRenderPass)
160 {
161    V3DV_FROM_HANDLE(v3dv_device, device, _device);
162    struct v3dv_render_pass *pass;
163 
164    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2);
165 
166    /* From the VK_KHR_multiview spec:
167     *
168     *   When a subpass uses a non-zero view mask, multiview functionality is
169     *   considered to be enabled. Multiview is all-or-nothing for a render
170     *   pass - that is, either all subpasses must have a non-zero view mask
171     *   (though some subpasses may have only one view) or all must be zero.
172     */
173    bool multiview_enabled = pCreateInfo->subpassCount &&
174       pCreateInfo->pSubpasses[0].viewMask;
175 
176    size_t size = sizeof(*pass);
177    size_t subpasses_offset = size;
178    size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
179    size_t attachments_offset = size;
180    size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
181 
182    pass = vk_object_zalloc(&device->vk, pAllocator, size,
183                            VK_OBJECT_TYPE_RENDER_PASS);
184    if (pass == NULL)
185       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
186 
187    pass->multiview_enabled = multiview_enabled;
188    pass->attachment_count = pCreateInfo->attachmentCount;
189    pass->attachments = (void *) pass + attachments_offset;
190    pass->subpass_count = pCreateInfo->subpassCount;
191    pass->subpasses = (void *) pass + subpasses_offset;
192 
193    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++)
194       pass->attachments[i].desc = pCreateInfo->pAttachments[i];
195 
196    uint32_t subpass_attachment_count = 0;
197    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
198       const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
199       subpass_attachment_count += num_subpass_attachments(desc);
200    }
201 
202    if (subpass_attachment_count) {
203       const size_t subpass_attachment_bytes =
204          subpass_attachment_count * sizeof(struct v3dv_subpass_attachment);
205       pass->subpass_attachments =
206          vk_alloc2(&device->vk.alloc, pAllocator, subpass_attachment_bytes, 8,
207                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
208       if (pass->subpass_attachments == NULL) {
209          vk_object_free(&device->vk, pAllocator, pass);
210          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
211       }
212    } else {
213       pass->subpass_attachments = NULL;
214    }
215 
216    struct v3dv_subpass_attachment *p = pass->subpass_attachments;
217    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
218       const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
219       struct v3dv_subpass *subpass = &pass->subpasses[i];
220 
221       subpass->input_count = desc->inputAttachmentCount;
222       subpass->color_count = desc->colorAttachmentCount;
223       subpass->view_mask = desc->viewMask;
224 
225       if (desc->inputAttachmentCount > 0) {
226          subpass->input_attachments = p;
227          p += desc->inputAttachmentCount;
228 
229          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
230             subpass->input_attachments[j] = (struct v3dv_subpass_attachment) {
231                .attachment = desc->pInputAttachments[j].attachment,
232                .layout = desc->pInputAttachments[j].layout,
233             };
234          }
235       }
236 
237       if (desc->colorAttachmentCount > 0) {
238          subpass->color_attachments = p;
239          p += desc->colorAttachmentCount;
240 
241          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
242             subpass->color_attachments[j] = (struct v3dv_subpass_attachment) {
243                .attachment = desc->pColorAttachments[j].attachment,
244                .layout = desc->pColorAttachments[j].layout,
245             };
246          }
247       }
248 
249       if (desc->pResolveAttachments) {
250          subpass->resolve_attachments = p;
251          p += desc->colorAttachmentCount;
252 
253          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
254             subpass->resolve_attachments[j] = (struct v3dv_subpass_attachment) {
255                .attachment = desc->pResolveAttachments[j].attachment,
256                .layout = desc->pResolveAttachments[j].layout,
257             };
258          }
259       }
260 
261       if (desc->pDepthStencilAttachment) {
262          subpass->ds_attachment = (struct v3dv_subpass_attachment) {
263             .attachment = desc->pDepthStencilAttachment->attachment,
264             .layout = desc->pDepthStencilAttachment->layout,
265          };
266 
267          check_do_depth_stencil_clear_with_draw(device, pass, subpass);
268 
269          /* VK_KHR_depth_stencil_resolve */
270          const VkSubpassDescriptionDepthStencilResolve *resolve_desc =
271             vk_find_struct_const(desc->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
272          const VkAttachmentReference2 *resolve_att =
273             resolve_desc && resolve_desc->pDepthStencilResolveAttachment &&
274             resolve_desc->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED ?
275                resolve_desc->pDepthStencilResolveAttachment : NULL;
276          if (resolve_att) {
277             subpass->ds_resolve_attachment = (struct v3dv_subpass_attachment) {
278                .attachment = resolve_att->attachment,
279                .layout = resolve_att->layout,
280             };
281             assert(resolve_desc->depthResolveMode == VK_RESOLVE_MODE_SAMPLE_ZERO_BIT ||
282                    resolve_desc->stencilResolveMode == VK_RESOLVE_MODE_SAMPLE_ZERO_BIT);
283             subpass->resolve_depth =
284                resolve_desc->depthResolveMode != VK_RESOLVE_MODE_NONE &&
285                resolve_att->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT;
286             subpass->resolve_stencil =
287                resolve_desc->stencilResolveMode != VK_RESOLVE_MODE_NONE &&
288                resolve_att->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT;
289          } else {
290             subpass->ds_resolve_attachment.attachment = VK_ATTACHMENT_UNUSED;
291             subpass->resolve_depth = false;
292             subpass->resolve_stencil = false;
293          }
294       } else {
295          subpass->ds_attachment.attachment = VK_ATTACHMENT_UNUSED;
296          subpass->ds_resolve_attachment.attachment = VK_ATTACHMENT_UNUSED;
297          subpass->resolve_depth = false;
298          subpass->resolve_stencil = false;
299       }
300    }
301 
302    pass_find_subpass_range_for_attachments(device, pass);
303 
304    /* FIXME: handle subpass dependencies */
305 
306    *pRenderPass = v3dv_render_pass_to_handle(pass);
307 
308    return VK_SUCCESS;
309 }
310 
311 VKAPI_ATTR void VKAPI_CALL
v3dv_DestroyRenderPass(VkDevice _device,VkRenderPass _pass,const VkAllocationCallbacks * pAllocator)312 v3dv_DestroyRenderPass(VkDevice _device,
313                        VkRenderPass _pass,
314                        const VkAllocationCallbacks *pAllocator)
315 {
316    V3DV_FROM_HANDLE(v3dv_device, device, _device);
317    V3DV_FROM_HANDLE(v3dv_render_pass, pass, _pass);
318 
319    if (!_pass)
320       return;
321 
322    vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
323    vk_object_free(&device->vk, pAllocator, pass);
324 }
325 
326 static void
get_granularity(struct v3dv_device * device,uint32_t count,const VkFormat * formats,bool msaa,VkExtent2D * granularity)327 get_granularity(struct v3dv_device *device,
328                 uint32_t count,
329                 const VkFormat *formats,
330                 bool msaa,
331                 VkExtent2D *granularity)
332 {
333    uint32_t max_internal_bpp = 0;
334    uint32_t total_color_bpp = 0;
335    for (int i = 0; i < count; i++) {
336       const struct v3dv_format *format = v3dv_X(device, get_format)(formats[i]);
337       assert(format);
338       /* We don't support rendering to YCbCr images */
339       assert(format->plane_count == 1);
340 
341       uint32_t internal_type, internal_bpp;
342       v3dv_X(device, get_internal_type_bpp_for_output_format)
343          (format->planes[0].rt_type, &internal_type, &internal_bpp);
344 
345       max_internal_bpp = MAX2(max_internal_bpp, internal_bpp);
346       total_color_bpp += 4 * v3d_internal_bpp_words(internal_bpp);
347    }
348 
349    /* If requested, double-buffer may or may not be enabled depending on
350     * heuristics so we choose a conservative granularity here, with it disabled.
351     */
352    uint32_t width, height;
353    v3d_choose_tile_size(&device->devinfo, count,
354                         max_internal_bpp, total_color_bpp, msaa,
355                         false /* double-buffer */, &width, &height);
356    *granularity = (VkExtent2D) {
357       .width = width,
358       .height = height
359    };
360 }
361 
362 static void
subpass_get_granularity(struct v3dv_device * device,struct v3dv_render_pass * pass,uint32_t subpass_idx,VkExtent2D * granularity)363 subpass_get_granularity(struct v3dv_device *device,
364                         struct v3dv_render_pass *pass,
365                         uint32_t subpass_idx,
366                         VkExtent2D *granularity)
367 {
368    /* Granularity is defined by the tile size */
369    assert(subpass_idx < pass->subpass_count);
370    struct v3dv_subpass *subpass = &pass->subpasses[subpass_idx];
371    const uint32_t subpass_color_count = subpass->color_count;
372    assert(subpass_color_count <= V3D_MAX_DRAW_BUFFERS);
373 
374    VkFormat formats[V3D_MAX_DRAW_BUFFERS];
375    bool msaa = false;
376    uint32_t color_count = 0;
377    for (uint32_t i = 0; i < subpass_color_count; i++) {
378       uint32_t attachment_idx = subpass->color_attachments[i].attachment;
379       if (attachment_idx == VK_ATTACHMENT_UNUSED)
380          continue;
381       const VkAttachmentDescription2 *desc =
382          &pass->attachments[attachment_idx].desc;
383       formats[color_count++] = desc->format;
384       if (desc->samples > VK_SAMPLE_COUNT_1_BIT)
385          msaa = true;
386    }
387 
388    get_granularity(device, color_count, formats, msaa, granularity);
389 }
390 
391 VKAPI_ATTR void VKAPI_CALL
v3dv_GetRenderAreaGranularity(VkDevice _device,VkRenderPass renderPass,VkExtent2D * pGranularity)392 v3dv_GetRenderAreaGranularity(VkDevice _device,
393                               VkRenderPass renderPass,
394                               VkExtent2D *pGranularity)
395 {
396    V3DV_FROM_HANDLE(v3dv_render_pass, pass, renderPass);
397    V3DV_FROM_HANDLE(v3dv_device, device, _device);
398 
399    *pGranularity = (VkExtent2D) {
400       .width = 64,
401       .height = 64,
402    };
403 
404    for (uint32_t i = 0; i < pass->subpass_count; i++) {
405       VkExtent2D sg;
406       subpass_get_granularity(device, pass, i, &sg);
407       pGranularity->width = MIN2(pGranularity->width, sg.width);
408       pGranularity->height = MIN2(pGranularity->height, sg.height);
409    }
410 }
411 
412 /* This is for dynamic rendering, introduced with VK_KHR_maintenance5 */
413 VKAPI_ATTR void VKAPI_CALL
v3dv_GetRenderingAreaGranularityKHR(VkDevice _device,const VkRenderingAreaInfoKHR * pRenderingAreaInfo,VkExtent2D * pGranularity)414 v3dv_GetRenderingAreaGranularityKHR(VkDevice _device,
415                                     const VkRenderingAreaInfoKHR *pRenderingAreaInfo,
416                                     VkExtent2D *pGranularity)
417 {
418    V3DV_FROM_HANDLE(v3dv_device, device, _device);
419 
420    /* Since VkRenderingAreaInfoKHR doesn't include information about MSAA we
421     * choose to compute granularity as if it was disabled which would give us
422     * the largest granularity (worst case).
423     */
424    get_granularity(device,
425                    pRenderingAreaInfo->colorAttachmentCount,
426                    pRenderingAreaInfo->pColorAttachmentFormats,
427                    false /* msaa */,
428                    pGranularity);
429 }
430 
431 /* Checks whether the render area rectangle covers a region that is aligned to
432  * tile boundaries. This means that we are writing to all pixels covered by
433  * all tiles in that area (except for pixels on edge tiles that are outside
434  * the framebuffer dimensions).
435  *
436  * When our framebuffer is aligned to tile boundaries we know we are writing
437  * valid data to all all pixels in each tile and we can apply certain
438  * optimizations, like avoiding tile loads, since we know that none of the
439  * original pixel values in each tile for that area need to be preserved.
440  * We also use this to decide if we can use TLB clears, as these clear whole
441  * tiles so we can't use them if the render area is not aligned.
442  *
443  * Note that when an image is created it will possibly include padding blocks
444  * depending on its tiling layout. When the framebuffer dimensions are not
445  * aligned to tile boundaries then edge tiles are only partially covered by the
446  * framebuffer pixels, but tile stores still seem to store full tiles
447  * writing to the padded sections. This is important when the framebuffer
448  * is aliasing a smaller section of a larger image, as in that case the edge
449  * tiles of the framebuffer would overwrite valid pixels in the larger image.
450  * In that case, we can't flag the area as being aligned.
451  */
452 bool
v3dv_subpass_area_is_tile_aligned(struct v3dv_device * device,const VkRect2D * area,struct v3dv_framebuffer * fb,struct v3dv_render_pass * pass,uint32_t subpass_idx)453 v3dv_subpass_area_is_tile_aligned(struct v3dv_device *device,
454                                   const VkRect2D *area,
455                                   struct v3dv_framebuffer *fb,
456                                   struct v3dv_render_pass *pass,
457                                   uint32_t subpass_idx)
458 {
459    assert(subpass_idx < pass->subpass_count);
460 
461    VkExtent2D granularity;
462    subpass_get_granularity(device, pass, subpass_idx, &granularity);
463 
464    return area->offset.x % granularity.width == 0 &&
465           area->offset.y % granularity.height == 0 &&
466          (area->extent.width % granularity.width == 0 ||
467           (fb->has_edge_padding &&
468            area->offset.x + area->extent.width >= fb->width)) &&
469          (area->extent.height % granularity.height == 0 ||
470           (fb->has_edge_padding &&
471            area->offset.y + area->extent.height >= fb->height));
472 }
473 
474 static void
setup_dynamic_attachment(struct v3dv_device * device,struct v3dv_render_pass_attachment * att,const VkRenderingAttachmentInfo * info,bool is_stencil,bool is_resolve)475 setup_dynamic_attachment(struct v3dv_device *device,
476                          struct v3dv_render_pass_attachment *att,
477                          const VkRenderingAttachmentInfo *info,
478                          bool is_stencil,
479                          bool is_resolve)
480 {
481    struct v3dv_image_view *view = v3dv_image_view_from_handle(info->imageView);
482 
483    VkAttachmentLoadOp load_op, stencil_load_op;
484    VkAttachmentStoreOp store_op, stencil_store_op;
485 
486    if (!is_stencil) {
487       stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
488       stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
489       if (!is_resolve) {
490          load_op = info->loadOp;
491          store_op = info->storeOp;
492       } else {
493          load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
494          store_op = VK_ATTACHMENT_STORE_OP_STORE;
495       }
496    } else {
497       load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
498       store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
499       if (!is_resolve) {
500          stencil_load_op = info->loadOp;
501          stencil_store_op = info->storeOp;
502       } else {
503          stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
504          stencil_store_op = VK_ATTACHMENT_STORE_OP_STORE;
505       }
506    }
507 
508    att->desc = (VkAttachmentDescription2) {
509       .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
510       .flags = 0,
511       .format = view->vk.format,
512       .samples = view->vk.image->samples,
513       .loadOp = load_op,
514       .storeOp = store_op,
515       .stencilLoadOp = stencil_load_op,
516       .stencilStoreOp = stencil_store_op,
517       .initialLayout = info->imageLayout,
518       .finalLayout = info->imageLayout,
519    };
520 
521    if (is_resolve)
522       set_try_tlb_resolve(device, att);
523 }
524 
525 void
v3dv_setup_dynamic_render_pass(struct v3dv_cmd_buffer * cmd_buffer,const VkRenderingInfoKHR * info)526 v3dv_setup_dynamic_render_pass(struct v3dv_cmd_buffer *cmd_buffer,
527                                const VkRenderingInfoKHR *info)
528 {
529    struct v3dv_device *device = cmd_buffer->device;
530    struct v3dv_cmd_buffer_state *state = &cmd_buffer->state;
531 
532    struct v3dv_render_pass *pass = &state->dynamic_pass;
533    struct v3dv_subpass *subpass = &state->dynamic_subpass;
534    struct v3dv_render_pass_attachment *pass_attachments =
535       &state->dynamic_attachments[0];
536    struct v3dv_subpass_attachment *subpass_attachments =
537       &state->dynamic_subpass_attachments[0];
538 
539    memset(pass, 0, sizeof(*pass));
540    memset(subpass, 0, sizeof(*subpass));
541    memset(pass_attachments, 0, sizeof(state->dynamic_subpass_attachments));
542    memset(subpass_attachments, 0, sizeof(state->dynamic_subpass_attachments));
543 
544    vk_object_base_init(&device->vk, (struct vk_object_base *) pass,
545                        VK_OBJECT_TYPE_RENDER_PASS);
546 
547    pass->attachments = pass_attachments;
548    pass->subpass_attachments = subpass_attachments;
549 
550    subpass->view_mask = info->viewMask;
551    subpass->color_count = info->colorAttachmentCount;
552    subpass->color_attachments = &subpass_attachments[0];
553    subpass->resolve_attachments = &subpass_attachments[subpass->color_count];
554 
555    pass->multiview_enabled = info->viewMask != 0;
556    pass->subpass_count = 1;
557    pass->subpasses = subpass;
558 
559    int a = 0;
560    for (int i = 0; i < info->colorAttachmentCount; i++) {
561       struct v3dv_render_pass_attachment *att = &pass->attachments[a];
562       const VkRenderingAttachmentInfo *att_info = &info->pColorAttachments[i];
563 
564       if (att_info->imageView == VK_NULL_HANDLE) {
565          subpass->color_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
566          subpass->resolve_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
567          continue;
568       }
569 
570       setup_dynamic_attachment(device, att, att_info, false, false);
571       subpass->color_attachments[i].attachment = a++;
572       subpass->color_attachments[i].layout = att_info->imageLayout;
573 
574       if (att_info->resolveMode != VK_RESOLVE_MODE_NONE) {
575          struct v3dv_render_pass_attachment *resolve_att = &pass->attachments[a];
576          setup_dynamic_attachment(device, resolve_att, att_info, false, true);
577          subpass->resolve_attachments[i].attachment = a++;
578          subpass->resolve_attachments[i].layout = att_info->resolveImageLayout;
579       } else {
580          subpass->resolve_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
581       }
582    }
583 
584    bool has_depth = info->pDepthAttachment &&
585                     info->pDepthAttachment->imageView != VK_NULL_HANDLE;
586    bool has_stencil = info->pStencilAttachment &&
587                       info->pStencilAttachment->imageView != VK_NULL_HANDLE;
588    if (has_depth || has_stencil) {
589       struct v3dv_render_pass_attachment *att = &pass->attachments[a];
590       subpass->ds_attachment.attachment = a++;
591 
592       bool has_depth_resolve = false;
593       bool has_stencil_resolve = false;
594 
595       if (has_depth) {
596          setup_dynamic_attachment(device, att, info->pDepthAttachment,
597                                   false, false);
598          subpass->ds_attachment.layout = info->pDepthAttachment->imageLayout;
599          has_depth_resolve =
600             info->pDepthAttachment->resolveMode != VK_RESOLVE_MODE_NONE;
601       }
602 
603       if (has_stencil) {
604          if (has_depth) {
605             att->desc.stencilLoadOp = info->pStencilAttachment->loadOp;
606             att->desc.stencilStoreOp = info->pStencilAttachment->storeOp;
607          } else {
608             setup_dynamic_attachment(device, att, info->pStencilAttachment,
609                                      true, false);
610             subpass->ds_attachment.layout =
611                info->pStencilAttachment->imageLayout;
612          }
613          has_stencil_resolve =
614             info->pStencilAttachment->resolveMode != VK_RESOLVE_MODE_NONE;
615       }
616 
617       if (has_depth_resolve || has_stencil_resolve) {
618          struct v3dv_render_pass_attachment *att = &pass->attachments[a];
619          subpass->ds_resolve_attachment.attachment = a++;
620          if (has_depth_resolve) {
621             setup_dynamic_attachment(device, att, info->pDepthAttachment,
622                                      false, true);
623             subpass->ds_resolve_attachment.layout =
624                info->pDepthAttachment->resolveImageLayout;
625             subpass->resolve_depth = true;
626          }
627          if (has_stencil_resolve) {
628             if (has_depth_resolve) {
629                att->desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
630                att->desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
631             } else {
632                setup_dynamic_attachment(device, att, info->pStencilAttachment,
633                                         true, true);
634                subpass->ds_resolve_attachment.layout =
635                   info->pStencilAttachment->resolveImageLayout;
636             }
637             subpass->resolve_stencil = true;
638          }
639       } else {
640          subpass->ds_resolve_attachment.attachment = VK_ATTACHMENT_UNUSED;
641       }
642    } else {
643       subpass->ds_attachment.attachment = VK_ATTACHMENT_UNUSED;
644    }
645 
646    check_do_depth_stencil_clear_with_draw(device, pass, subpass);
647 
648    pass->attachment_count = a;
649 }
650 
651 void
v3dv_setup_dynamic_render_pass_inheritance(struct v3dv_cmd_buffer * cmd_buffer,const VkCommandBufferInheritanceRenderingInfo * info)652 v3dv_setup_dynamic_render_pass_inheritance(struct v3dv_cmd_buffer *cmd_buffer,
653                                            const VkCommandBufferInheritanceRenderingInfo *info)
654 {
655    struct v3dv_device *device = cmd_buffer->device;
656    struct v3dv_cmd_buffer_state *state = &cmd_buffer->state;
657 
658    struct v3dv_render_pass *pass = &state->dynamic_pass;
659    struct v3dv_subpass *subpass = &state->dynamic_subpass;
660    struct v3dv_render_pass_attachment *pass_attachments =
661       &state->dynamic_attachments[0];
662    struct v3dv_subpass_attachment *subpass_attachments =
663       &state->dynamic_subpass_attachments[0];
664 
665    memset(pass, 0, sizeof(*pass));
666    memset(subpass, 0, sizeof(*subpass));
667    memset(pass_attachments, 0, sizeof(state->dynamic_subpass_attachments));
668    memset(subpass_attachments, 0, sizeof(state->dynamic_subpass_attachments));
669 
670    vk_object_base_init(&device->vk, (struct vk_object_base *) pass,
671                        VK_OBJECT_TYPE_RENDER_PASS);
672 
673    pass->attachments = pass_attachments;
674    pass->subpass_attachments = subpass_attachments;
675 
676    subpass->view_mask = info->viewMask;
677    subpass->color_count = info->colorAttachmentCount;
678    subpass->color_attachments = &subpass_attachments[0];
679    subpass->resolve_attachments = NULL;
680 
681    pass->multiview_enabled = info->viewMask != 0;
682    pass->subpass_count = 1;
683    pass->subpasses = subpass;
684 
685    int a = 0;
686    for (int i = 0; i < info->colorAttachmentCount; i++) {
687       struct v3dv_render_pass_attachment *att = &pass->attachments[a];
688       const VkFormat format = info->pColorAttachmentFormats[i];
689 
690       if (format == VK_FORMAT_UNDEFINED) {
691          subpass->color_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
692          continue;
693       }
694 
695       /* We don't have info about load/store, so we assume we load and we
696        * store.
697        */
698       att->desc.format = format;
699       att->desc.samples = info->rasterizationSamples;
700       att->desc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
701       att->desc.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
702       att->desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
703       att->desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
704       subpass->color_attachments[i].attachment = a++;
705    }
706 
707    if (info->depthAttachmentFormat != VK_FORMAT_UNDEFINED ||
708        info->stencilAttachmentFormat != VK_FORMAT_UNDEFINED) {
709       struct v3dv_render_pass_attachment *att = &pass->attachments[a];
710       att->desc.format = info->depthAttachmentFormat != VK_FORMAT_UNDEFINED ?
711          info->depthAttachmentFormat : info->stencilAttachmentFormat;
712       att->desc.samples = info->rasterizationSamples;
713       if (vk_format_has_depth(att->desc.format)) {
714          att->desc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
715          att->desc.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
716       } else {
717          att->desc.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
718          att->desc.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
719       }
720       if (vk_format_has_stencil(att->desc.format)) {
721          att->desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
722          att->desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
723       } else {
724          att->desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
725          att->desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
726       }
727       subpass->ds_attachment.attachment = a++;
728    } else {
729       subpass->ds_attachment.attachment = VK_ATTACHMENT_UNUSED;
730    }
731 
732    pass->attachment_count = a;
733 }
734