• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  * SPDX-License-Identifier: MIT
5  *
6  * based in part on anv driver which is:
7  * Copyright © 2015 Intel Corporation
8  */
9 
10 #include "tu_pass.h"
11 
12 #include "vk_util.h"
13 #include "vk_render_pass.h"
14 
15 #include "tu_cmd_buffer.h"
16 #include "tu_device.h"
17 #include "tu_image.h"
18 
19 static void
tu_render_pass_add_subpass_dep(struct tu_render_pass * pass,const VkSubpassDependency2 * dep)20 tu_render_pass_add_subpass_dep(struct tu_render_pass *pass,
21                                const VkSubpassDependency2 *dep)
22 {
23    uint32_t src = dep->srcSubpass;
24    uint32_t dst = dep->dstSubpass;
25 
26    /* Ignore subpass self-dependencies as they allow the app to call
27     * vkCmdPipelineBarrier() inside the render pass and the driver should only
28     * do the barrier when called, not when starting the render pass.
29     *
30     * We cannot decide whether to allow gmem rendering before a barrier
31     * is actually emitted, so we delay the decision until then.
32     */
33    if (src == dst)
34       return;
35 
36    /* From the Vulkan 1.2.195 spec:
37     *
38     * "If an instance of VkMemoryBarrier2 is included in the pNext chain, srcStageMask,
39     *  dstStageMask, srcAccessMask, and dstAccessMask parameters are ignored. The synchronization
40     *  and access scopes instead are defined by the parameters of VkMemoryBarrier2."
41     */
42    const VkMemoryBarrier2 *barrier =
43       vk_find_struct_const(dep->pNext, MEMORY_BARRIER_2);
44    VkPipelineStageFlags2 src_stage_mask = barrier ? barrier->srcStageMask : dep->srcStageMask;
45    VkAccessFlags2 src_access_mask = barrier ? barrier->srcAccessMask : dep->srcAccessMask;
46    VkPipelineStageFlags2 dst_stage_mask = barrier ? barrier->dstStageMask : dep->dstStageMask;
47    VkAccessFlags2 dst_access_mask = barrier ? barrier->dstAccessMask : dep->dstAccessMask;
48 
49    /* We can conceptually break down the process of rewriting a sysmem
50     * renderpass into a gmem one into two parts:
51     *
52     * 1. Split each draw and multisample resolve into N copies, one for each
53     * bin. (If hardware binning, add one more copy where the FS is disabled
54     * for the binning pass). This is always allowed because the vertex stage
55     * is allowed to run an arbitrary number of times and there are no extra
56     * ordering constraints within a draw.
57     * 2. Take the last copy of the second-to-last draw and slide it down to
58     * before the last copy of the last draw. Repeat for each earlier draw
59     * until the draw pass for the last bin is complete, then repeat for each
60     * earlier bin until we finish with the first bin.
61     *
62     * During this rearranging process, we can't slide draws past each other in
63     * a way that breaks the subpass dependencies. For each draw, we must slide
64     * it past (copies of) the rest of the draws in the renderpass. We can
65     * slide a draw past another if there isn't a dependency between them, or
66     * if the dependenc(ies) are dependencies between framebuffer-space stages
67     * only with the BY_REGION bit set. Note that this includes
68     * self-dependencies, since these may result in pipeline barriers that also
69     * break the rearranging process.
70     */
71 
72    if (!vk_subpass_dependency_is_fb_local(dep, src_stage_mask, dst_stage_mask)) {
73       perf_debug((struct tu_device *)pass->base.device, "Disabling gmem rendering due to invalid subpass dependency");
74       for (int i = 0; i < ARRAY_SIZE(pass->gmem_pixels); i++)
75          pass->gmem_pixels[i] = 0;
76    }
77 
78    struct tu_subpass_barrier *dst_barrier;
79    if (dst == VK_SUBPASS_EXTERNAL) {
80       dst_barrier = &pass->end_barrier;
81    } else {
82       dst_barrier = &pass->subpasses[dst].start_barrier;
83    }
84 
85    dst_barrier->src_stage_mask |= src_stage_mask;
86    dst_barrier->dst_stage_mask |= dst_stage_mask;
87    dst_barrier->src_access_mask |= src_access_mask;
88    dst_barrier->dst_access_mask |= dst_access_mask;
89 }
90 
91 /* We currently only care about undefined layouts, because we have to
92  * flush/invalidate CCU for those. PREINITIALIZED is the same thing as
93  * UNDEFINED for anything not linear tiled, but we don't know yet whether the
94  * images used are tiled, so just assume they are.
95  */
96 
97 static bool
layout_undefined(VkImageLayout layout)98 layout_undefined(VkImageLayout layout)
99 {
100    return layout == VK_IMAGE_LAYOUT_UNDEFINED ||
101           layout == VK_IMAGE_LAYOUT_PREINITIALIZED;
102 }
103 
104 /* This implements the following bit of spec text:
105  *
106  *    If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
107  *    first subpass that uses an attachment, then an implicit subpass
108  *    dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
109  *    used in. The implicit subpass dependency only exists if there
110  *    exists an automatic layout transition away from initialLayout.
111  *    The subpass dependency operates as if defined with the
112  *    following parameters:
113  *
114  *    VkSubpassDependency implicitDependency = {
115  *        .srcSubpass = VK_SUBPASS_EXTERNAL;
116  *        .dstSubpass = firstSubpass; // First subpass attachment is used in
117  *        .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
118  *        .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
119  *        .srcAccessMask = 0;
120  *        .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
121  *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
122  *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
123  *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
124  *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
125  *        .dependencyFlags = 0;
126  *    };
127  *
128  *    Similarly, if there is no subpass dependency from the last subpass
129  *    that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
130  *    subpass dependency exists from the last subpass it is used in to
131  *    VK_SUBPASS_EXTERNAL. The implicit subpass dependency only exists
132  *    if there exists an automatic layout transition into finalLayout.
133  *    The subpass dependency operates as if defined with the following
134  *    parameters:
135  *
136  *    VkSubpassDependency implicitDependency = {
137  *        .srcSubpass = lastSubpass; // Last subpass attachment is used in
138  *        .dstSubpass = VK_SUBPASS_EXTERNAL;
139  *        .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
140  *        .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
141  *        .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
142  *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
143  *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
144  *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
145  *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
146  *        .dstAccessMask = 0;
147  *        .dependencyFlags = 0;
148  *    };
149  *
150  * Note: currently this is the only use we have for layout transitions,
151  * besides needing to invalidate CCU at the beginning, so we also flag
152  * transitions from UNDEFINED here.
153  */
154 static void
tu_render_pass_add_implicit_deps(struct tu_render_pass * pass,const VkRenderPassCreateInfo2 * info)155 tu_render_pass_add_implicit_deps(struct tu_render_pass *pass,
156                                  const VkRenderPassCreateInfo2 *info)
157 {
158    const VkAttachmentDescription2* att = info->pAttachments;
159    bool has_external_src[info->subpassCount];
160    bool has_external_dst[info->subpassCount];
161    bool att_used[pass->attachment_count];
162 
163    memset(has_external_src, 0, sizeof(has_external_src));
164    memset(has_external_dst, 0, sizeof(has_external_dst));
165 
166    for (uint32_t i = 0; i < info->dependencyCount; i++) {
167       uint32_t src = info->pDependencies[i].srcSubpass;
168       uint32_t dst = info->pDependencies[i].dstSubpass;
169 
170       if (src == dst)
171          continue;
172 
173       if (src == VK_SUBPASS_EXTERNAL)
174          has_external_src[dst] = true;
175       if (dst == VK_SUBPASS_EXTERNAL)
176          has_external_dst[src] = true;
177    }
178 
179    memset(att_used, 0, sizeof(att_used));
180 
181    for (unsigned i = 0; i < info->subpassCount; i++) {
182       const VkSubpassDescription2 *subpass = &info->pSubpasses[i];
183       bool src_implicit_dep = false;
184 
185       for (unsigned j = 0; j < subpass->inputAttachmentCount; j++) {
186          uint32_t a = subpass->pInputAttachments[j].attachment;
187 
188          if (a == VK_ATTACHMENT_UNUSED)
189             continue;
190 
191          uint32_t stencil_layout = vk_format_has_stencil(att[a].format) ?
192                vk_att_ref_stencil_layout(&subpass->pInputAttachments[j], att) :
193                VK_IMAGE_LAYOUT_UNDEFINED;
194          uint32_t stencil_initial_layout = vk_att_desc_stencil_layout(&att[a], false);
195 
196          if ((att[a].initialLayout != subpass->pInputAttachments[j].layout ||
197              stencil_initial_layout != stencil_layout) &&
198              !att_used[a] && !has_external_src[i])
199             src_implicit_dep = true;
200          att_used[a] = true;
201       }
202 
203       for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
204          uint32_t a = subpass->pColorAttachments[j].attachment;
205          if (a == VK_ATTACHMENT_UNUSED)
206             continue;
207          if (att[a].initialLayout != subpass->pColorAttachments[j].layout &&
208              !att_used[a] && !has_external_src[i])
209             src_implicit_dep = true;
210          att_used[a] = true;
211       }
212 
213       if (subpass->pDepthStencilAttachment &&
214           subpass->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
215          uint32_t a = subpass->pDepthStencilAttachment->attachment;
216          uint32_t stencil_layout = vk_att_ref_stencil_layout(subpass->pDepthStencilAttachment, att);
217          uint32_t stencil_initial_layout = vk_att_desc_stencil_layout(&att[a], false);
218 
219          if ((att[a].initialLayout != subpass->pDepthStencilAttachment->layout ||
220              stencil_initial_layout != stencil_layout) &&
221              !att_used[a] && !has_external_src[i]) {
222             src_implicit_dep = true;
223          }
224          att_used[a] = true;
225       }
226 
227       if (subpass->pResolveAttachments) {
228          for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
229             uint32_t a = subpass->pResolveAttachments[j].attachment;
230             if (a == VK_ATTACHMENT_UNUSED)
231                continue;
232             if (att[a].initialLayout != subpass->pResolveAttachments[j].layout &&
233                !att_used[a] && !has_external_src[i])
234                src_implicit_dep = true;
235             att_used[a] = true;
236          }
237       }
238 
239       const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
240          vk_find_struct_const(subpass->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
241 
242       if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
243           ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) {
244             uint32_t a = ds_resolve->pDepthStencilResolveAttachment->attachment;
245             uint32_t stencil_layout = vk_att_ref_stencil_layout(ds_resolve->pDepthStencilResolveAttachment, att);
246             uint32_t stencil_initial_layout = vk_att_desc_stencil_layout(&att[a], false);
247 
248             if ((att[a].initialLayout != subpass->pDepthStencilAttachment->layout ||
249                 stencil_initial_layout != stencil_layout) &&
250                 !att_used[a] && !has_external_src[i])
251                src_implicit_dep = true;
252             att_used[a] = true;
253       }
254 
255       if (src_implicit_dep) {
256          const VkSubpassDependency2 dep = {
257             .srcSubpass = VK_SUBPASS_EXTERNAL,
258             .dstSubpass = i,
259             .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
260             .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
261             .srcAccessMask = 0,
262             .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
263                              VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
264                              VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
265                              VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
266                              VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
267             .dependencyFlags = 0,
268          };
269 
270          tu_render_pass_add_subpass_dep(pass, &dep);
271       }
272    }
273 
274    memset(att_used, 0, sizeof(att_used));
275 
276    for (int i = info->subpassCount - 1; i >= 0; i--) {
277       const VkSubpassDescription2 *subpass = &info->pSubpasses[i];
278       bool dst_implicit_dep = false;
279 
280       for (unsigned j = 0; j < subpass->inputAttachmentCount; j++) {
281          uint32_t a = subpass->pInputAttachments[j].attachment;
282          if (a == VK_ATTACHMENT_UNUSED)
283             continue;
284 
285          uint32_t stencil_layout = vk_format_has_stencil(att[a].format) ?
286                vk_att_ref_stencil_layout(&subpass->pInputAttachments[j], att) :
287                VK_IMAGE_LAYOUT_UNDEFINED;
288          uint32_t stencil_final_layout = vk_att_desc_stencil_layout(&att[a], true);
289 
290          if ((att[a].finalLayout != subpass->pInputAttachments[j].layout ||
291              stencil_final_layout != stencil_layout) &&
292              !att_used[a] && !has_external_dst[i])
293             dst_implicit_dep = true;
294          att_used[a] = true;
295       }
296 
297       for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
298          uint32_t a = subpass->pColorAttachments[j].attachment;
299          if (a == VK_ATTACHMENT_UNUSED)
300             continue;
301          if (att[a].finalLayout != subpass->pColorAttachments[j].layout &&
302              !att_used[a] && !has_external_dst[i])
303             dst_implicit_dep = true;
304          att_used[a] = true;
305       }
306 
307       if (subpass->pDepthStencilAttachment &&
308           subpass->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
309          uint32_t a = subpass->pDepthStencilAttachment->attachment;
310          uint32_t stencil_layout = vk_att_ref_stencil_layout(subpass->pDepthStencilAttachment, att);
311          uint32_t stencil_final_layout = vk_att_desc_stencil_layout(&att[a], true);
312 
313          if ((att[a].finalLayout != subpass->pDepthStencilAttachment->layout ||
314              stencil_final_layout != stencil_layout) &&
315              !att_used[a] && !has_external_dst[i]) {
316             dst_implicit_dep = true;
317          }
318          att_used[a] = true;
319       }
320 
321       if (subpass->pResolveAttachments) {
322          for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
323             uint32_t a = subpass->pResolveAttachments[j].attachment;
324             if (a == VK_ATTACHMENT_UNUSED)
325                continue;
326             if (att[a].finalLayout != subpass->pResolveAttachments[j].layout &&
327                 !att_used[a] && !has_external_dst[i])
328                dst_implicit_dep = true;
329             att_used[a] = true;
330          }
331       }
332 
333       const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
334          vk_find_struct_const(subpass->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
335 
336       if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
337           ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) {
338             uint32_t a = ds_resolve->pDepthStencilResolveAttachment->attachment;
339             uint32_t stencil_layout = vk_att_ref_stencil_layout(ds_resolve->pDepthStencilResolveAttachment, att);
340             uint32_t stencil_final_layout = vk_att_desc_stencil_layout(&att[a], true);
341 
342             if ((att[a].finalLayout != subpass->pDepthStencilAttachment->layout ||
343                 stencil_final_layout != stencil_layout) &&
344                 !att_used[a] && !has_external_src[i])
345                dst_implicit_dep = true;
346             att_used[a] = true;
347       }
348 
349       if (dst_implicit_dep) {
350          VkSubpassDependency2 dep = {
351             .srcSubpass = i,
352             .dstSubpass = VK_SUBPASS_EXTERNAL,
353             .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
354             .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
355             .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
356                              VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
357                              VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
358                              VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
359                              VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
360             .dstAccessMask = 0,
361             .dependencyFlags = 0,
362          };
363          tu_render_pass_add_subpass_dep(pass, &dep);
364       }
365    }
366 
367    /* Handle UNDEFINED transitions, similar to the handling in tu_barrier().
368     * Assume that if an attachment has an initial layout of UNDEFINED, it gets
369     * transitioned eventually.
370     */
371    for (unsigned i = 0; i < info->attachmentCount; i++) {
372       if (layout_undefined(att[i].initialLayout)) {
373          if (vk_format_is_depth_or_stencil(att[i].format)) {
374             pass->subpasses[0].start_barrier.incoherent_ccu_depth = true;
375          } else {
376             pass->subpasses[0].start_barrier.incoherent_ccu_color = true;
377          }
378       }
379    }
380 }
381 
382 /* If an input attachment is used without an intervening write to the same
383  * attachment, then we can just use the original image, even in GMEM mode.
384  * This is an optimization, but it's also important because it allows us to
385  * avoid having to invalidate UCHE at the beginning of each tile due to it
386  * becoming invalid. The only reads of GMEM via UCHE should be after an
387  * earlier subpass modified it, which only works if there's already an
388  * appropriate dependency that will add the CACHE_INVALIDATE anyway. We
389  * don't consider this in the dependency code, so this is also required for
390  * correctness.
391  */
392 static void
tu_render_pass_patch_input_gmem(struct tu_render_pass * pass)393 tu_render_pass_patch_input_gmem(struct tu_render_pass *pass)
394 {
395    bool written[pass->attachment_count];
396 
397    memset(written, 0, sizeof(written));
398 
399    for (unsigned i = 0; i < pass->subpass_count; i++) {
400       struct tu_subpass *subpass = &pass->subpasses[i];
401 
402       for (unsigned j = 0; j < subpass->input_count; j++) {
403          uint32_t a = subpass->input_attachments[j].attachment;
404          if (a == VK_ATTACHMENT_UNUSED)
405             continue;
406          subpass->input_attachments[j].patch_input_gmem = written[a];
407       }
408 
409       for (unsigned j = 0; j < subpass->color_count; j++) {
410          uint32_t a = subpass->color_attachments[j].attachment;
411          if (a == VK_ATTACHMENT_UNUSED)
412             continue;
413          written[a] = true;
414 
415          for (unsigned k = 0; k < subpass->input_count; k++) {
416             if (subpass->input_attachments[k].attachment == a &&
417                 !subpass->input_attachments[k].patch_input_gmem) {
418                /* For render feedback loops, we have no idea whether the use
419                 * as a color attachment or input attachment will come first,
420                 * so we have to always use GMEM in case the color attachment
421                 * comes first and defensively invalidate UCHE in case the
422                 * input attachment comes first.
423                 */
424                subpass->feedback_invalidate = true;
425                subpass->input_attachments[k].patch_input_gmem = true;
426             }
427          }
428       }
429 
430       for (unsigned j = 0; j < subpass->resolve_count; j++) {
431          uint32_t a = subpass->resolve_attachments[j].attachment;
432          if (a == VK_ATTACHMENT_UNUSED)
433             continue;
434          written[a] = true;
435       }
436 
437       if (subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
438          written[subpass->depth_stencil_attachment.attachment] = true;
439          for (unsigned k = 0; k < subpass->input_count; k++) {
440             if (subpass->input_attachments[k].attachment ==
441                 subpass->depth_stencil_attachment.attachment &&
442                 !subpass->input_attachments[k].patch_input_gmem) {
443                subpass->feedback_invalidate = true;
444                subpass->input_attachments[k].patch_input_gmem = true;
445             }
446          }
447       }
448    }
449 }
450 
451 static void
tu_render_pass_check_feedback_loop(struct tu_render_pass * pass)452 tu_render_pass_check_feedback_loop(struct tu_render_pass *pass)
453 {
454    for (unsigned i = 0; i < pass->subpass_count; i++) {
455       struct tu_subpass *subpass = &pass->subpasses[i];
456 
457       for (unsigned j = 0; j < subpass->color_count; j++) {
458          uint32_t a = subpass->color_attachments[j].attachment;
459          if (a == VK_ATTACHMENT_UNUSED)
460             continue;
461          for (unsigned k = 0; k < subpass->input_count; k++) {
462             if (subpass->input_attachments[k].attachment == a) {
463                subpass->feedback_loop_color = true;
464                break;
465             }
466          }
467       }
468 
469       if (subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
470          for (unsigned k = 0; k < subpass->input_count; k++) {
471             if (subpass->input_attachments[k].attachment ==
472                 subpass->depth_stencil_attachment.attachment) {
473                subpass->feedback_loop_ds = true;
474                break;
475             }
476          }
477       }
478    }
479 }
480 
update_samples(struct tu_subpass * subpass,VkSampleCountFlagBits samples)481 static void update_samples(struct tu_subpass *subpass,
482                            VkSampleCountFlagBits samples)
483 {
484    assert(subpass->samples == 0 || subpass->samples == samples);
485    subpass->samples = samples;
486 }
487 
488 static void
tu_render_pass_calc_views(struct tu_render_pass * pass)489 tu_render_pass_calc_views(struct tu_render_pass *pass)
490 {
491    uint32_t view_mask = 0;
492    for (unsigned i = 0; i < pass->subpass_count; i++)
493       view_mask |= pass->subpasses[i].multiview_mask;
494    pass->num_views = util_last_bit(view_mask);
495 }
496 
497 /* If there are any multisample attachments with a load op other than
498  * clear/don't-care/none and store op other than don't-care/none, then we'd
499  * have to load/store a scaled multisample image which doesn't make much
500  * sense. Just disable fragment_density_map in this case.
501  */
502 static bool
tu_render_pass_disable_fdm(struct tu_render_pass * pass)503 tu_render_pass_disable_fdm(struct tu_render_pass *pass)
504 {
505    for (uint32_t i = 0; i < pass->attachment_count; i++) {
506       struct tu_render_pass_attachment *att = &pass->attachments[i];
507 
508       if (att->samples > 1 &&
509           (att->load || att->load_stencil ||
510            att->store || att->store_stencil)) {
511          return true;
512       }
513    }
514 
515    return false;
516 }
517 
518 static void
tu_render_pass_calc_hash(struct tu_render_pass * pass)519 tu_render_pass_calc_hash(struct tu_render_pass *pass)
520 {
521    #define HASH(hash, data) XXH64(&(data), sizeof(data), hash)
522 
523    uint64_t hash = HASH(0, pass->attachment_count);
524    hash = XXH64(pass->attachments,
525          pass->attachment_count * sizeof(pass->attachments[0]), hash);
526    hash = HASH(hash, pass->subpass_count);
527    for (unsigned i = 0; i < pass->subpass_count; i++) {
528       hash = HASH(hash, pass->subpasses[i].samples);
529       hash = HASH(hash, pass->subpasses[i].input_count);
530       hash = HASH(hash, pass->subpasses[i].color_count);
531       hash = HASH(hash, pass->subpasses[i].resolve_count);
532    }
533 
534    pass->autotune_hash = hash;
535 
536    #undef HASH
537 }
538 
539 static void
tu_render_pass_cond_config(struct tu_render_pass * pass)540 tu_render_pass_cond_config(struct tu_render_pass *pass)
541 {
542    for (uint32_t i = 0; i < pass->attachment_count; i++) {
543       struct tu_render_pass_attachment *att = &pass->attachments[i];
544 
545       /* When there is no geometry in a tile, and there is no other operations to
546        * read/write the tile, we can skip load/store.
547        *
548        * The only other operations are clear and resolve, which disable
549        * conditional load/store.
550        */
551       att->cond_load_allowed =
552          (att->load || att->load_stencil) && !att->clear_mask && !att->will_be_resolved;
553       att->cond_store_allowed =
554          (att->store || att->store_stencil) && !att->clear_mask;
555 
556       pass->has_cond_load_store |=
557          att->cond_load_allowed | att->cond_store_allowed;
558    }
559 }
560 
561 static void
tu_render_pass_gmem_config(struct tu_render_pass * pass,const struct tu_physical_device * phys_dev)562 tu_render_pass_gmem_config(struct tu_render_pass *pass,
563                            const struct tu_physical_device *phys_dev)
564 {
565    for (enum tu_gmem_layout layout = (enum tu_gmem_layout) 0;
566         layout < TU_GMEM_LAYOUT_COUNT;
567         layout = (enum tu_gmem_layout)(layout + 1)) {
568       /* log2(gmem_align/(tile_align_w*tile_align_h)) */
569       uint32_t block_align_shift = 3;
570       uint32_t tile_align_w = phys_dev->info->tile_align_w;
571       uint32_t gmem_align = (1 << block_align_shift) * tile_align_w *
572                             phys_dev->info->tile_align_h;
573 
574       /* calculate total bytes per pixel */
575       uint32_t cpp_total = 0;
576       uint32_t min_cpp = UINT32_MAX;
577       for (uint32_t i = 0; i < pass->attachment_count; i++) {
578          struct tu_render_pass_attachment *att = &pass->attachments[i];
579          bool cpp1 = (att->cpp == 1);
580          if (att->gmem) {
581             cpp_total += att->cpp;
582             min_cpp = MIN2(min_cpp, att->cpp);
583 
584             /* take into account the separate stencil: */
585             if (att->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
586                min_cpp = MIN2(min_cpp, att->samples);
587                cpp1 = (att->samples == 1);
588                cpp_total += att->samples;
589             }
590 
591             /* texture pitch must be aligned to 64, use a tile_align_w that is
592              * a multiple of 64 for cpp==1 attachment to work as input
593              * attachment
594              */
595             if (cpp1 && tile_align_w % 64 != 0) {
596                tile_align_w *= 2;
597                block_align_shift -= 1;
598             }
599          }
600       }
601 
602       pass->tile_align_w = tile_align_w;
603       pass->min_cpp = min_cpp;
604 
605       /* no gmem attachments */
606       if (cpp_total == 0) {
607          /* any value non-zero value so tiling config works with no
608           * attachments
609           */
610          pass->gmem_pixels[layout] = 1024 * 1024;
611          continue;
612       }
613 
614       /* TODO: this algorithm isn't optimal
615        * for example, two attachments with cpp = {1, 4}
616        * result:  nblocks = {12, 52}, pixels = 196608
617        * optimal: nblocks = {13, 51}, pixels = 208896
618        */
619       uint32_t gmem_size = layout == TU_GMEM_LAYOUT_FULL
620                               ? phys_dev->usable_gmem_size_gmem
621                               : phys_dev->ccu_offset_gmem;
622       uint32_t gmem_blocks = gmem_size / gmem_align;
623       uint32_t offset = 0, pixels = ~0u, i;
624       for (i = 0; i < pass->attachment_count; i++) {
625          struct tu_render_pass_attachment *att = &pass->attachments[i];
626          if (!att->gmem)
627             continue;
628 
629          att->gmem_offset[layout] = offset;
630 
631          uint32_t align = MAX2(1, att->cpp >> block_align_shift);
632          uint32_t nblocks =
633             MAX2((gmem_blocks * att->cpp / cpp_total) & ~(align - 1), align);
634 
635          if (nblocks > gmem_blocks)
636             break;
637 
638          gmem_blocks -= nblocks;
639          cpp_total -= att->cpp;
640          offset += nblocks * gmem_align;
641          pixels = MIN2(pixels, nblocks * gmem_align / att->cpp);
642 
643          /* repeat the same for separate stencil */
644          if (att->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
645             att->gmem_offset_stencil[layout] = offset;
646 
647             /* note: for s8_uint, block align is always 1 */
648             uint32_t nblocks = gmem_blocks * att->samples / cpp_total;
649             if (nblocks > gmem_blocks)
650                break;
651 
652             gmem_blocks -= nblocks;
653             cpp_total -= att->samples;
654             offset += nblocks * gmem_align;
655             pixels = MIN2(pixels, nblocks * gmem_align / att->samples);
656          }
657       }
658 
659       /* if the loop didn't complete then the gmem config is impossible */
660       if (i == pass->attachment_count)
661          pass->gmem_pixels[layout] = pixels;
662    }
663 }
664 
665 static void
tu_render_pass_bandwidth_config(struct tu_render_pass * pass)666 tu_render_pass_bandwidth_config(struct tu_render_pass *pass)
667 {
668    pass->gmem_bandwidth_per_pixel = 0;
669    pass->sysmem_bandwidth_per_pixel = 0;
670 
671    for (uint32_t i = 0; i < pass->attachment_count; i++) {
672       const struct tu_render_pass_attachment *att = &pass->attachments[i];
673 
674       /* approximate tu_load_gmem_attachment */
675       if (att->load)
676          pass->gmem_bandwidth_per_pixel += att->cpp;
677 
678       /* approximate tu_store_gmem_attachment */
679       if (att->store)
680          pass->gmem_bandwidth_per_pixel += att->cpp;
681 
682       /* approximate tu_clear_sysmem_attachment */
683       if (att->clear_mask)
684          pass->sysmem_bandwidth_per_pixel += att->cpp;
685 
686       /* approximate tu6_emit_sysmem_resolves */
687       if (att->will_be_resolved) {
688          pass->sysmem_bandwidth_per_pixel +=
689             att->cpp + att->cpp / att->samples;
690       }
691    }
692 }
693 
694 static void
attachment_set_ops(struct tu_device * device,struct tu_render_pass_attachment * att,VkAttachmentLoadOp load_op,VkAttachmentLoadOp stencil_load_op,VkAttachmentStoreOp store_op,VkAttachmentStoreOp stencil_store_op)695 attachment_set_ops(struct tu_device *device,
696                    struct tu_render_pass_attachment *att,
697                    VkAttachmentLoadOp load_op,
698                    VkAttachmentLoadOp stencil_load_op,
699                    VkAttachmentStoreOp store_op,
700                    VkAttachmentStoreOp stencil_store_op)
701 {
702    if (unlikely(device->instance->dont_care_as_load)) {
703       if (load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
704          load_op = VK_ATTACHMENT_LOAD_OP_LOAD;
705       if (stencil_load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
706          stencil_load_op = VK_ATTACHMENT_LOAD_OP_LOAD;
707    }
708 
709    /* load/store ops */
710    att->clear_mask =
711       (load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) ? VK_IMAGE_ASPECT_COLOR_BIT : 0;
712    att->load = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD);
713    att->store = (store_op == VK_ATTACHMENT_STORE_OP_STORE);
714 
715    bool stencil_clear = (stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR);
716    bool stencil_load = (stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD);
717    bool stencil_store = (stencil_store_op == VK_ATTACHMENT_STORE_OP_STORE);
718 
719    switch (att->format) {
720    case VK_FORMAT_D24_UNORM_S8_UINT: /* || stencil load/store */
721       if (att->clear_mask)
722          att->clear_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
723       if (stencil_clear)
724          att->clear_mask |= VK_IMAGE_ASPECT_STENCIL_BIT;
725       if (stencil_load)
726          att->load = true;
727       if (stencil_store)
728          att->store = true;
729       break;
730    case VK_FORMAT_S8_UINT: /* replace load/store with stencil load/store */
731       att->clear_mask = stencil_clear ? VK_IMAGE_ASPECT_COLOR_BIT : 0;
732       att->load = stencil_load;
733       att->store = stencil_store;
734       break;
735    case VK_FORMAT_D32_SFLOAT_S8_UINT: /* separate stencil */
736       if (att->clear_mask)
737          att->clear_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
738       if (stencil_clear)
739          att->clear_mask |= VK_IMAGE_ASPECT_STENCIL_BIT;
740       if (stencil_load)
741          att->load_stencil = true;
742       if (stencil_store)
743          att->store_stencil = true;
744       break;
745    default:
746       break;
747    }
748 }
749 
750 static bool
is_depth_stencil_resolve_enabled(const VkSubpassDescriptionDepthStencilResolve * depth_stencil_resolve)751 is_depth_stencil_resolve_enabled(const VkSubpassDescriptionDepthStencilResolve *depth_stencil_resolve)
752 {
753    if (depth_stencil_resolve &&
754        depth_stencil_resolve->pDepthStencilResolveAttachment &&
755        depth_stencil_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) {
756       return true;
757    }
758    return false;
759 }
760 
761 static void
tu_subpass_use_attachment(struct tu_render_pass * pass,int i,uint32_t a,const VkRenderPassCreateInfo2 * pCreateInfo)762 tu_subpass_use_attachment(struct tu_render_pass *pass, int i, uint32_t a, const VkRenderPassCreateInfo2 *pCreateInfo)
763 {
764    struct tu_subpass *subpass = &pass->subpasses[i];
765    struct tu_render_pass_attachment *att = &pass->attachments[a];
766 
767    att->gmem = true;
768    update_samples(subpass, pCreateInfo->pAttachments[a].samples);
769    att->clear_views |= subpass->multiview_mask;
770 
771    /* Loads and clears are emitted at the start of the subpass that needs them. */
772    att->first_subpass_idx = MIN2(i, att->first_subpass_idx);
773 
774    /* Stores are emitted at vkEndRenderPass() time. */
775    if (att->store || att->store_stencil)
776       att->last_subpass_idx = pass->subpass_count - 1;
777    else
778       att->last_subpass_idx = MAX2(i, att->last_subpass_idx);
779 }
780 
781 static void
tu_subpass_resolve_attachment(struct tu_render_pass * pass,int i,uint32_t dst_a,uint32_t src_a)782 tu_subpass_resolve_attachment(struct tu_render_pass *pass, int i, uint32_t dst_a, uint32_t src_a)
783 {
784    if (src_a != VK_ATTACHMENT_UNUSED && dst_a != VK_ATTACHMENT_UNUSED) {
785       struct tu_render_pass_attachment *src_att = &pass->attachments[src_a];
786       struct tu_render_pass_attachment *dst_att = &pass->attachments[dst_a];
787       src_att->will_be_resolved = true;
788 
789       src_att->first_subpass_idx = MIN2(i, src_att->first_subpass_idx);
790       src_att->last_subpass_idx = MAX2(i, src_att->last_subpass_idx);
791       dst_att->first_subpass_idx = MIN2(i, dst_att->first_subpass_idx);
792       dst_att->last_subpass_idx = MAX2(i, dst_att->last_subpass_idx);
793    }
794 }
795 
796 VKAPI_ATTR VkResult VKAPI_CALL
tu_CreateRenderPass2(VkDevice _device,const VkRenderPassCreateInfo2 * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkRenderPass * pRenderPass)797 tu_CreateRenderPass2(VkDevice _device,
798                      const VkRenderPassCreateInfo2 *pCreateInfo,
799                      const VkAllocationCallbacks *pAllocator,
800                      VkRenderPass *pRenderPass)
801 {
802    TU_FROM_HANDLE(tu_device, device, _device);
803 
804    if (TU_DEBUG(DYNAMIC))
805       return vk_common_CreateRenderPass2(_device, pCreateInfo, pAllocator,
806                                          pRenderPass);
807 
808    struct tu_render_pass *pass;
809    size_t size;
810    size_t attachments_offset;
811 
812    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2);
813 
814    size = sizeof(*pass);
815    size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
816    attachments_offset = size;
817    size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
818 
819    pass = (struct tu_render_pass *) vk_object_zalloc(
820       &device->vk, pAllocator, size, VK_OBJECT_TYPE_RENDER_PASS);
821    if (pass == NULL)
822       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
823 
824    pass->attachment_count = pCreateInfo->attachmentCount;
825    pass->subpass_count = pCreateInfo->subpassCount;
826    pass->attachments =
827       (struct tu_render_pass_attachment *) ((char *) pass +
828                                             attachments_offset);
829 
830    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
831       struct tu_render_pass_attachment *att = &pass->attachments[i];
832 
833       att->format = pCreateInfo->pAttachments[i].format;
834       att->samples = pCreateInfo->pAttachments[i].samples;
835       /* for d32s8, cpp is for the depth image, and
836        * att->samples will be used as the cpp for the stencil image
837        */
838       if (att->format == VK_FORMAT_D32_SFLOAT_S8_UINT)
839          att->cpp = 4 * att->samples;
840       else
841          att->cpp = vk_format_get_blocksize(att->format) * att->samples;
842       /* Initially not allocated into gmem, tu_subpass_use_attachment() will move it there. */
843       att->gmem = false;
844 
845       VkAttachmentLoadOp loadOp = pCreateInfo->pAttachments[i].loadOp;
846       VkAttachmentLoadOp stencilLoadOp = pCreateInfo->pAttachments[i].stencilLoadOp;
847 
848       attachment_set_ops(device, att, loadOp, stencilLoadOp,
849                          pCreateInfo->pAttachments[i].storeOp,
850                          pCreateInfo->pAttachments[i].stencilStoreOp);
851 
852       att->first_subpass_idx = VK_SUBPASS_EXTERNAL;
853       att->last_subpass_idx = 0;
854    }
855    uint32_t subpass_attachment_count = 0;
856    struct tu_subpass_attachment *p;
857    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
858       const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
859       const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
860          vk_find_struct_const(desc->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
861 
862       subpass_attachment_count +=
863          desc->inputAttachmentCount + desc->colorAttachmentCount +
864          (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
865          (is_depth_stencil_resolve_enabled(ds_resolve) ? 1 : 0);
866    }
867 
868    if (subpass_attachment_count) {
869       pass->subpass_attachments = (struct tu_subpass_attachment *) vk_alloc2(
870          &device->vk.alloc, pAllocator,
871          subpass_attachment_count * sizeof(struct tu_subpass_attachment), 8,
872          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
873       if (pass->subpass_attachments == NULL) {
874          vk_object_free(&device->vk, pAllocator, pass);
875          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
876       }
877    } else
878       pass->subpass_attachments = NULL;
879 
880    const VkRenderPassFragmentDensityMapCreateInfoEXT *fdm_info =
881       vk_find_struct_const(pCreateInfo->pNext,
882                            RENDER_PASS_FRAGMENT_DENSITY_MAP_CREATE_INFO_EXT);
883    if (fdm_info && !tu_render_pass_disable_fdm(pass)) {
884       pass->fragment_density_map.attachment =
885          fdm_info->fragmentDensityMapAttachment.attachment;
886       pass->has_fdm = true;
887    } else {
888       pass->fragment_density_map.attachment = VK_ATTACHMENT_UNUSED;
889    }
890 
891    if (TU_DEBUG(FDM) && !tu_render_pass_disable_fdm(pass))
892       pass->has_fdm = true;
893 
894    p = pass->subpass_attachments;
895    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
896       const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
897       const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
898          vk_find_struct_const(desc->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
899       struct tu_subpass *subpass = &pass->subpasses[i];
900 
901       subpass->input_count = desc->inputAttachmentCount;
902       subpass->color_count = desc->colorAttachmentCount;
903       subpass->resolve_count = 0;
904       subpass->resolve_depth_stencil = is_depth_stencil_resolve_enabled(ds_resolve);
905       subpass->samples = (VkSampleCountFlagBits) 0;
906       subpass->srgb_cntl = 0;
907 
908       const BITMASK_ENUM(VkSubpassDescriptionFlagBits) raster_order_access_bits =
909          VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_COLOR_ACCESS_BIT_EXT |
910          VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_EXT |
911          VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_EXT;
912 
913       subpass->raster_order_attachment_access = raster_order_access_bits & desc->flags;
914 
915       subpass->multiview_mask = desc->viewMask;
916 
917       if (desc->inputAttachmentCount > 0) {
918          subpass->input_attachments = p;
919          p += desc->inputAttachmentCount;
920 
921          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
922             uint32_t a = desc->pInputAttachments[j].attachment;
923             subpass->input_attachments[j].attachment = a;
924             if (a != VK_ATTACHMENT_UNUSED) {
925                struct tu_render_pass_attachment *att = &pass->attachments[a];
926                /* Note: attachments only used as input attachments will be read
927                 * directly instead of through gmem, so we don't mark input
928                 * attachments as needing gmem.
929                 */
930                att->first_subpass_idx = MIN2(i, att->first_subpass_idx);
931                att->last_subpass_idx = MAX2(i, att->last_subpass_idx);
932             }
933          }
934       }
935 
936       if (desc->colorAttachmentCount > 0) {
937          subpass->color_attachments = p;
938          p += desc->colorAttachmentCount;
939 
940          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
941             uint32_t a = desc->pColorAttachments[j].attachment;
942             subpass->color_attachments[j].attachment = a;
943 
944             if (a != VK_ATTACHMENT_UNUSED) {
945                tu_subpass_use_attachment(pass, i, a, pCreateInfo);
946 
947                if (vk_format_is_srgb(pass->attachments[a].format))
948                   subpass->srgb_cntl |= 1 << j;
949             }
950          }
951       }
952 
953       subpass->resolve_attachments = (desc->pResolveAttachments || subpass->resolve_depth_stencil) ? p : NULL;
954       if (desc->pResolveAttachments) {
955          p += desc->colorAttachmentCount;
956          subpass->resolve_count += desc->colorAttachmentCount;
957          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
958             uint32_t a = desc->pResolveAttachments[j].attachment;
959             uint32_t src_a = desc->pColorAttachments[j].attachment;
960             subpass->resolve_attachments[j].attachment = a;
961 
962             tu_subpass_resolve_attachment(pass, i, a, src_a);
963          }
964       }
965 
966       if (subpass->resolve_depth_stencil) {
967          p++;
968          subpass->resolve_count++;
969          uint32_t a = ds_resolve->pDepthStencilResolveAttachment->attachment;
970          uint32_t src_a = desc->pDepthStencilAttachment->attachment;
971          subpass->resolve_attachments[subpass->resolve_count - 1].attachment = a;
972 
973          tu_subpass_resolve_attachment(pass, i, a, src_a);
974       }
975 
976       uint32_t a = desc->pDepthStencilAttachment ?
977          desc->pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
978       subpass->depth_stencil_attachment.attachment = a;
979       if (a != VK_ATTACHMENT_UNUSED)
980          tu_subpass_use_attachment(pass, i, a, pCreateInfo);
981    }
982 
983    tu_render_pass_patch_input_gmem(pass);
984 
985    tu_render_pass_check_feedback_loop(pass);
986 
987    /* disable unused attachments */
988    for (uint32_t i = 0; i < pass->attachment_count; i++) {
989       struct tu_render_pass_attachment *att = &pass->attachments[i];
990       if (!att->gmem) {
991          att->clear_mask = 0;
992          att->load = false;
993       }
994    }
995 
996    tu_render_pass_cond_config(pass);
997    tu_render_pass_gmem_config(pass, device->physical_device);
998    tu_render_pass_bandwidth_config(pass);
999    tu_render_pass_calc_views(pass);
1000    tu_render_pass_calc_hash(pass);
1001 
1002    for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
1003       tu_render_pass_add_subpass_dep(pass, &pCreateInfo->pDependencies[i]);
1004    }
1005 
1006    tu_render_pass_add_implicit_deps(pass, pCreateInfo);
1007 
1008    *pRenderPass = tu_render_pass_to_handle(pass);
1009 
1010    return VK_SUCCESS;
1011 }
1012 
1013 VKAPI_ATTR void VKAPI_CALL
tu_DestroyRenderPass(VkDevice _device,VkRenderPass _pass,const VkAllocationCallbacks * pAllocator)1014 tu_DestroyRenderPass(VkDevice _device,
1015                      VkRenderPass _pass,
1016                      const VkAllocationCallbacks *pAllocator)
1017 {
1018    TU_FROM_HANDLE(tu_device, device, _device);
1019 
1020    if (TU_DEBUG(DYNAMIC)) {
1021       vk_common_DestroyRenderPass(_device, _pass, pAllocator);
1022       return;
1023    }
1024 
1025    TU_FROM_HANDLE(tu_render_pass, pass, _pass);
1026 
1027    if (!_pass)
1028       return;
1029 
1030    vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
1031    vk_object_free(&device->vk, pAllocator, pass);
1032 }
1033 
1034 static void
tu_setup_dynamic_attachment(struct tu_render_pass_attachment * att,struct tu_image_view * view)1035 tu_setup_dynamic_attachment(struct tu_render_pass_attachment *att,
1036                             struct tu_image_view *view)
1037 {
1038    *att = {};
1039    att->format = view->vk.format;
1040    att->samples = (VkSampleCountFlagBits) view->image->layout->nr_samples;
1041 
1042    /* for d32s8, cpp is for the depth image, and
1043     * att->samples will be used as the cpp for the stencil image
1044     */
1045    if (att->format == VK_FORMAT_D32_SFLOAT_S8_UINT)
1046       att->cpp = 4 * att->samples;
1047    else
1048       att->cpp = vk_format_get_blocksize(att->format) * att->samples;
1049 }
1050 
1051 void
tu_setup_dynamic_render_pass(struct tu_cmd_buffer * cmd_buffer,const VkRenderingInfo * info)1052 tu_setup_dynamic_render_pass(struct tu_cmd_buffer *cmd_buffer,
1053                              const VkRenderingInfo *info)
1054 {
1055    struct tu_device *device = cmd_buffer->device;
1056    struct tu_render_pass *pass = &cmd_buffer->dynamic_pass;
1057    struct tu_subpass *subpass = &cmd_buffer->dynamic_subpass;
1058 
1059    *pass = {};
1060    *subpass = {};
1061 
1062    pass->subpass_count = 1;
1063    pass->attachments = cmd_buffer->dynamic_rp_attachments;
1064 
1065    subpass->color_count = subpass->resolve_count = info->colorAttachmentCount;
1066    subpass->color_attachments = cmd_buffer->dynamic_color_attachments;
1067    subpass->resolve_attachments = cmd_buffer->dynamic_resolve_attachments;
1068    subpass->multiview_mask = info->viewMask;
1069 
1070    uint32_t a = 0;
1071    for (uint32_t i = 0; i < info->colorAttachmentCount; i++) {
1072       struct tu_render_pass_attachment *att = &pass->attachments[a];
1073       const VkRenderingAttachmentInfo *att_info = &info->pColorAttachments[i];
1074 
1075       if (att_info->imageView == VK_NULL_HANDLE) {
1076          subpass->color_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
1077          subpass->resolve_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
1078          continue;
1079       }
1080 
1081       TU_FROM_HANDLE(tu_image_view, view, att_info->imageView);
1082       tu_setup_dynamic_attachment(att, view);
1083       att->gmem = true;
1084       att->clear_views = info->viewMask;
1085       attachment_set_ops(device, att, att_info->loadOp,
1086                          VK_ATTACHMENT_LOAD_OP_DONT_CARE, att_info->storeOp,
1087                          VK_ATTACHMENT_STORE_OP_DONT_CARE);
1088       subpass->color_attachments[i].attachment = a++;
1089 
1090       subpass->samples = (VkSampleCountFlagBits) view->image->layout->nr_samples;
1091 
1092       if (vk_format_is_srgb(view->vk.format))
1093          subpass->srgb_cntl |= 1 << i;
1094 
1095       if (att_info->resolveMode != VK_RESOLVE_MODE_NONE) {
1096          struct tu_render_pass_attachment *resolve_att = &pass->attachments[a];
1097          TU_FROM_HANDLE(tu_image_view, resolve_view, att_info->resolveImageView);
1098          tu_setup_dynamic_attachment(resolve_att, resolve_view);
1099          resolve_att->gmem = false;
1100          attachment_set_ops(
1101             device, resolve_att, VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1102             VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_STORE,
1103             VK_ATTACHMENT_STORE_OP_DONT_CARE);
1104          subpass->resolve_attachments[i].attachment = a++;
1105          att->will_be_resolved = true;
1106       } else {
1107          subpass->resolve_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
1108          att->will_be_resolved = false;
1109       }
1110    }
1111 
1112    if (info->pDepthAttachment || info->pStencilAttachment) {
1113       const struct VkRenderingAttachmentInfo *common_info =
1114          (info->pDepthAttachment &&
1115           info->pDepthAttachment->imageView != VK_NULL_HANDLE) ?
1116          info->pDepthAttachment :
1117          info->pStencilAttachment;
1118 
1119       if (common_info && common_info->imageView != VK_NULL_HANDLE) {
1120          TU_FROM_HANDLE(tu_image_view, view, common_info->imageView);
1121 
1122          struct tu_render_pass_attachment *att = &pass->attachments[a];
1123          tu_setup_dynamic_attachment(att, view);
1124          att->gmem = true;
1125          att->clear_views = info->viewMask;
1126          subpass->depth_stencil_attachment.attachment = a++;
1127 
1128          attachment_set_ops(
1129             device, att,
1130             info->pDepthAttachment ? info->pDepthAttachment->loadOp
1131                                    : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1132             info->pStencilAttachment ? info->pStencilAttachment->loadOp
1133                                      : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1134             info->pDepthAttachment ? info->pDepthAttachment->storeOp
1135                                    : VK_ATTACHMENT_STORE_OP_DONT_CARE,
1136             info->pStencilAttachment ? info->pStencilAttachment->storeOp
1137                                      : VK_ATTACHMENT_STORE_OP_DONT_CARE);
1138 
1139          subpass->samples = (VkSampleCountFlagBits) view->image->layout->nr_samples;
1140 
1141          if (common_info->resolveMode != VK_RESOLVE_MODE_NONE) {
1142             unsigned i = subpass->resolve_count++;
1143             struct tu_render_pass_attachment *resolve_att = &pass->attachments[a];
1144             TU_FROM_HANDLE(tu_image_view, resolve_view,
1145                            common_info->resolveImageView);
1146             tu_setup_dynamic_attachment(resolve_att, resolve_view);
1147             resolve_att->gmem = false;
1148             attachment_set_ops(device, resolve_att,
1149                                VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1150                                VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1151                                VK_ATTACHMENT_STORE_OP_STORE,
1152                                VK_ATTACHMENT_STORE_OP_STORE);
1153             subpass->resolve_attachments[i].attachment = a++;
1154             att->will_be_resolved = true;
1155             subpass->resolve_depth_stencil = true;
1156          } else {
1157             att->will_be_resolved = false;
1158          }
1159       } else {
1160          subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
1161       }
1162    } else {
1163       subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
1164    }
1165 
1166    pass->attachment_count = a;
1167 
1168    const VkRenderingFragmentDensityMapAttachmentInfoEXT *fdm_info =
1169       vk_find_struct_const(info->pNext,
1170                            RENDERING_FRAGMENT_DENSITY_MAP_ATTACHMENT_INFO_EXT);
1171    if (fdm_info && fdm_info->imageView != VK_NULL_HANDLE &&
1172        !tu_render_pass_disable_fdm(pass)) {
1173       TU_FROM_HANDLE(tu_image_view, view, fdm_info->imageView);
1174 
1175       struct tu_render_pass_attachment *att = &pass->attachments[a];
1176       tu_setup_dynamic_attachment(att, view);
1177       pass->fragment_density_map.attachment = a++;
1178       attachment_set_ops(device, att,
1179                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1180                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1181                          VK_ATTACHMENT_STORE_OP_DONT_CARE,
1182                          VK_ATTACHMENT_STORE_OP_DONT_CARE);
1183       pass->has_fdm = true;
1184    } else {
1185       pass->fragment_density_map.attachment = VK_ATTACHMENT_UNUSED;
1186       pass->has_fdm = false;
1187    }
1188 
1189    if (TU_DEBUG(FDM) && !tu_render_pass_disable_fdm(pass))
1190       pass->has_fdm = true;
1191 
1192    pass->attachment_count = a;
1193 
1194    tu_render_pass_cond_config(pass);
1195    tu_render_pass_gmem_config(pass, device->physical_device);
1196    tu_render_pass_bandwidth_config(pass);
1197    tu_render_pass_calc_views(pass);
1198    tu_render_pass_calc_hash(pass);
1199 }
1200 
1201 void
tu_setup_dynamic_inheritance(struct tu_cmd_buffer * cmd_buffer,const VkCommandBufferInheritanceRenderingInfo * info)1202 tu_setup_dynamic_inheritance(struct tu_cmd_buffer *cmd_buffer,
1203                              const VkCommandBufferInheritanceRenderingInfo *info)
1204 {
1205    struct tu_render_pass *pass = &cmd_buffer->dynamic_pass;
1206    struct tu_subpass *subpass = &cmd_buffer->dynamic_subpass;
1207 
1208    pass->subpass_count = 1;
1209    pass->attachments = cmd_buffer->dynamic_rp_attachments;
1210    pass->fragment_density_map.attachment = VK_ATTACHMENT_UNUSED;
1211 
1212    subpass->color_count = info->colorAttachmentCount;
1213    subpass->resolve_count = 0;
1214    subpass->resolve_depth_stencil = false;
1215    subpass->color_attachments = cmd_buffer->dynamic_color_attachments;
1216    subpass->resolve_attachments = NULL;
1217    subpass->feedback_invalidate = false;
1218    subpass->feedback_loop_ds = subpass->feedback_loop_color = false;
1219    subpass->input_count = 0;
1220    subpass->samples = (VkSampleCountFlagBits) 0;
1221    subpass->srgb_cntl = 0;
1222    subpass->raster_order_attachment_access = false;
1223    subpass->multiview_mask = info->viewMask;
1224    subpass->samples = info->rasterizationSamples;
1225 
1226    unsigned a = 0;
1227    for (unsigned i = 0; i < info->colorAttachmentCount; i++) {
1228       struct tu_render_pass_attachment *att = &pass->attachments[a];
1229       VkFormat format = info->pColorAttachmentFormats[i];
1230 
1231       if (format == VK_FORMAT_UNDEFINED) {
1232          subpass->color_attachments[i].attachment = VK_ATTACHMENT_UNUSED;
1233          continue;
1234       }
1235 
1236       att->format = format;
1237       att->samples = info->rasterizationSamples;
1238       subpass->samples = info->rasterizationSamples;
1239       subpass->color_attachments[i].attachment = a++;
1240 
1241       /* conservatively assume that the attachment may be conditionally
1242        * loaded/stored.
1243        */
1244       att->cond_load_allowed = att->cond_store_allowed = true;
1245    }
1246 
1247    if (info->depthAttachmentFormat != VK_FORMAT_UNDEFINED ||
1248        info->stencilAttachmentFormat != VK_FORMAT_UNDEFINED) {
1249       struct tu_render_pass_attachment *att = &pass->attachments[a];
1250       att->format = info->depthAttachmentFormat != VK_FORMAT_UNDEFINED ?
1251          info->depthAttachmentFormat : info->stencilAttachmentFormat;
1252       att->samples = info->rasterizationSamples;
1253       subpass->depth_stencil_attachment.attachment = a++;
1254       att->cond_load_allowed = att->cond_store_allowed = true;
1255    } else {
1256       subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
1257    }
1258 
1259    tu_render_pass_calc_views(pass);
1260 }
1261 
1262 VKAPI_ATTR void VKAPI_CALL
tu_GetRenderAreaGranularity(VkDevice _device,VkRenderPass renderPass,VkExtent2D * pGranularity)1263 tu_GetRenderAreaGranularity(VkDevice _device,
1264                             VkRenderPass renderPass,
1265                             VkExtent2D *pGranularity)
1266 {
1267    TU_FROM_HANDLE(tu_device, device, _device);
1268    pGranularity->width = device->physical_device->info->gmem_align_w;
1269    pGranularity->height = device->physical_device->info->gmem_align_h;
1270 }
1271 
1272 VKAPI_ATTR void VKAPI_CALL
tu_GetRenderingAreaGranularityKHR(VkDevice _device,const VkRenderingAreaInfoKHR * pRenderingAreaInfo,VkExtent2D * pGranularity)1273 tu_GetRenderingAreaGranularityKHR(VkDevice _device,
1274                                   const VkRenderingAreaInfoKHR *pRenderingAreaInfo,
1275                                   VkExtent2D *pGranularity)
1276 {
1277    TU_FROM_HANDLE(tu_device, device, _device);
1278    pGranularity->width = device->physical_device->info->gmem_align_w;
1279    pGranularity->height = device->physical_device->info->gmem_align_h;
1280 }
1281 
1282 uint32_t
tu_subpass_get_attachment_to_resolve(const struct tu_subpass * subpass,uint32_t index)1283 tu_subpass_get_attachment_to_resolve(const struct tu_subpass *subpass, uint32_t index)
1284 {
1285    if (subpass->resolve_depth_stencil &&
1286        index == (subpass->resolve_count - 1))
1287       return subpass->depth_stencil_attachment.attachment;
1288 
1289    return subpass->color_attachments[index].attachment;
1290 }
1291