ir3: Validate bindless samp_tex correctly
[mesa.git] / src / freedreno / vulkan / tu_pass.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27 #include "tu_private.h"
28
29 #include "vk_util.h"
30 #include "vk_format.h"
31
32 /* Return true if we have to fallback to sysmem rendering because the
33 * dependency can't be satisfied with tiled rendering.
34 */
35
36 static bool
37 dep_invalid_for_gmem(const VkSubpassDependency2 *dep)
38 {
39 /* External dependencies don't matter here. */
40 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL ||
41 dep->dstSubpass == VK_SUBPASS_EXTERNAL)
42 return false;
43
44 /* We can conceptually break down the process of rewriting a sysmem
45 * renderpass into a gmem one into two parts:
46 *
47 * 1. Split each draw and multisample resolve into N copies, one for each
48 * bin. (If hardware binning, add one more copy where the FS is disabled
49 * for the binning pass). This is always allowed because the vertex stage
50 * is allowed to run an arbitrary number of times and there are no extra
51 * ordering constraints within a draw.
52 * 2. Take the last copy of the second-to-last draw and slide it down to
53 * before the last copy of the last draw. Repeat for each earlier draw
54 * until the draw pass for the last bin is complete, then repeat for each
55 * earlier bin until we finish with the first bin.
56 *
57 * During this rearranging process, we can't slide draws past each other in
58 * a way that breaks the subpass dependencies. For each draw, we must slide
59 * it past (copies of) the rest of the draws in the renderpass. We can
60 * slide a draw past another if there isn't a dependency between them, or
61 * if the dependenc(ies) are dependencies between framebuffer-space stages
62 * only with the BY_REGION bit set. Note that this includes
63 * self-dependencies, since these may result in pipeline barriers that also
64 * break the rearranging process.
65 */
66
67 /* This is straight from the Vulkan 1.2 spec, section 6.1.4 "Framebuffer
68 * Region Dependencies":
69 */
70 const VkPipelineStageFlags framebuffer_space_stages =
71 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
72 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
73 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT |
74 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
75
76 return
77 (dep->srcStageMask & ~framebuffer_space_stages) ||
78 (dep->dstStageMask & ~framebuffer_space_stages) ||
79 !(dep->dependencyFlags & VK_DEPENDENCY_BY_REGION_BIT);
80 }
81
82 static void
83 tu_render_pass_add_subpass_dep(struct tu_render_pass *pass,
84 const VkSubpassDependency2 *dep)
85 {
86 uint32_t src = dep->srcSubpass;
87 uint32_t dst = dep->dstSubpass;
88
89 if (dep_invalid_for_gmem(dep))
90 pass->gmem_pixels = 0;
91
92 /* Ignore subpass self-dependencies as they allow the app to call
93 * vkCmdPipelineBarrier() inside the render pass and the driver should only
94 * do the barrier when called, not when starting the render pass.
95 */
96 if (src == dst)
97 return;
98
99 struct tu_subpass_barrier *src_barrier;
100 if (src == VK_SUBPASS_EXTERNAL) {
101 src_barrier = &pass->subpasses[0].start_barrier;
102 } else if (src == pass->subpass_count - 1) {
103 src_barrier = &pass->end_barrier;
104 } else {
105 src_barrier = &pass->subpasses[src + 1].start_barrier;
106 }
107
108 struct tu_subpass_barrier *dst_barrier;
109 if (dst == VK_SUBPASS_EXTERNAL) {
110 dst_barrier = &pass->end_barrier;
111 } else {
112 dst_barrier = &pass->subpasses[dst].start_barrier;
113 }
114
115 if (dep->dstStageMask != VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT)
116 src_barrier->src_stage_mask |= dep->srcStageMask;
117 src_barrier->src_access_mask |= dep->srcAccessMask;
118 dst_barrier->dst_access_mask |= dep->dstAccessMask;
119 }
120
121 /* We currently only care about undefined layouts, because we have to
122 * flush/invalidate CCU for those. PREINITIALIZED is the same thing as
123 * UNDEFINED for anything not linear tiled, but we don't know yet whether the
124 * images used are tiled, so just assume they are.
125 */
126
127 static bool
128 layout_undefined(VkImageLayout layout)
129 {
130 return layout == VK_IMAGE_LAYOUT_UNDEFINED ||
131 layout == VK_IMAGE_LAYOUT_PREINITIALIZED;
132 }
133
134 /* This implements the following bit of spec text:
135 *
136 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
137 * first subpass that uses an attachment, then an implicit subpass
138 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
139 * used in. The implicit subpass dependency only exists if there
140 * exists an automatic layout transition away from initialLayout.
141 * The subpass dependency operates as if defined with the
142 * following parameters:
143 *
144 * VkSubpassDependency implicitDependency = {
145 * .srcSubpass = VK_SUBPASS_EXTERNAL;
146 * .dstSubpass = firstSubpass; // First subpass attachment is used in
147 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
148 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
149 * .srcAccessMask = 0;
150 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
151 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
152 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
153 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
154 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
155 * .dependencyFlags = 0;
156 * };
157 *
158 * Similarly, if there is no subpass dependency from the last subpass
159 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
160 * subpass dependency exists from the last subpass it is used in to
161 * VK_SUBPASS_EXTERNAL. The implicit subpass dependency only exists
162 * if there exists an automatic layout transition into finalLayout.
163 * The subpass dependency operates as if defined with the following
164 * parameters:
165 *
166 * VkSubpassDependency implicitDependency = {
167 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
168 * .dstSubpass = VK_SUBPASS_EXTERNAL;
169 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
170 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
171 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
172 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
173 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
174 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
175 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
176 * .dstAccessMask = 0;
177 * .dependencyFlags = 0;
178 * };
179 *
180 * Note: currently this is the only use we have for layout transitions,
181 * besides needing to invalidate CCU at the beginning, so we also flag
182 * transitions from UNDEFINED here.
183 */
184 static void
185 tu_render_pass_add_implicit_deps(struct tu_render_pass *pass,
186 const VkRenderPassCreateInfo2 *info)
187 {
188 const VkAttachmentDescription2* att = info->pAttachments;
189 bool has_external_src[info->subpassCount];
190 bool has_external_dst[info->subpassCount];
191 bool att_used[pass->attachment_count];
192
193 memset(has_external_src, 0, sizeof(has_external_src));
194 memset(has_external_dst, 0, sizeof(has_external_dst));
195
196 for (uint32_t i = 0; i < info->dependencyCount; i++) {
197 uint32_t src = info->pDependencies[i].srcSubpass;
198 uint32_t dst = info->pDependencies[i].dstSubpass;
199
200 if (src == dst)
201 continue;
202
203 if (src == VK_SUBPASS_EXTERNAL)
204 has_external_src[dst] = true;
205 if (dst == VK_SUBPASS_EXTERNAL)
206 has_external_dst[src] = true;
207 }
208
209 memset(att_used, 0, sizeof(att_used));
210
211 for (unsigned i = 0; i < info->subpassCount; i++) {
212 if (!has_external_src[i])
213 continue;
214
215 const VkSubpassDescription2 *subpass = &info->pSubpasses[i];
216 bool src_implicit_dep = false;
217
218 for (unsigned j = 0; j < subpass->inputAttachmentCount; j++) {
219 uint32_t a = subpass->pInputAttachments[j].attachment;
220 if (a == VK_ATTACHMENT_UNUSED)
221 continue;
222 if (att[a].initialLayout != subpass->pInputAttachments[j].layout && !att_used[a])
223 src_implicit_dep = true;
224 att_used[a] = true;
225 }
226
227 for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
228 uint32_t a = subpass->pColorAttachments[j].attachment;
229 if (a == VK_ATTACHMENT_UNUSED)
230 continue;
231 if (att[a].initialLayout != subpass->pColorAttachments[j].layout && !att_used[a])
232 src_implicit_dep = true;
233 att_used[a] = true;
234 }
235
236 if (subpass->pResolveAttachments) {
237 for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
238 uint32_t a = subpass->pResolveAttachments[j].attachment;
239 if (a == VK_ATTACHMENT_UNUSED)
240 continue;
241 if (att[a].initialLayout != subpass->pResolveAttachments[j].layout && !att_used[a])
242 src_implicit_dep = true;
243 att_used[a] = true;
244 }
245 }
246
247 if (src_implicit_dep) {
248 tu_render_pass_add_subpass_dep(pass, &(VkSubpassDependency2KHR) {
249 .srcSubpass = VK_SUBPASS_EXTERNAL,
250 .dstSubpass = i,
251 .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
252 .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
253 .srcAccessMask = 0,
254 .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
255 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
256 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
257 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
258 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
259 .dependencyFlags = 0,
260 });
261 }
262 }
263
264 memset(att_used, 0, sizeof(att_used));
265
266 for (int i = info->subpassCount - 1; i >= 0; i--) {
267 if (!has_external_dst[i])
268 continue;
269
270 const VkSubpassDescription2 *subpass = &info->pSubpasses[i];
271 bool dst_implicit_dep = false;
272
273 for (unsigned j = 0; j < subpass->inputAttachmentCount; j++) {
274 uint32_t a = subpass->pInputAttachments[j].attachment;
275 if (a == VK_ATTACHMENT_UNUSED)
276 continue;
277 if (att[a].finalLayout != subpass->pInputAttachments[j].layout && !att_used[a])
278 dst_implicit_dep = true;
279 att_used[a] = true;
280 }
281
282 for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
283 uint32_t a = subpass->pColorAttachments[j].attachment;
284 if (a == VK_ATTACHMENT_UNUSED)
285 continue;
286 if (att[a].finalLayout != subpass->pColorAttachments[j].layout && !att_used[a])
287 dst_implicit_dep = true;
288 att_used[a] = true;
289 }
290
291 if (subpass->pResolveAttachments) {
292 for (unsigned j = 0; j < subpass->colorAttachmentCount; j++) {
293 uint32_t a = subpass->pResolveAttachments[j].attachment;
294 if (a == VK_ATTACHMENT_UNUSED)
295 continue;
296 if (att[a].finalLayout != subpass->pResolveAttachments[j].layout && !att_used[a])
297 dst_implicit_dep = true;
298 att_used[a] = true;
299 }
300 }
301
302 if (dst_implicit_dep) {
303 tu_render_pass_add_subpass_dep(pass, &(VkSubpassDependency2KHR) {
304 .srcSubpass = i,
305 .dstSubpass = VK_SUBPASS_EXTERNAL,
306 .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
307 .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
308 .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
309 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
310 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
311 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
312 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
313 .dstAccessMask = 0,
314 .dependencyFlags = 0,
315 });
316 }
317 }
318
319 /* Handle UNDEFINED transitions, similar to the handling in tu_barrier().
320 * Assume that if an attachment has an initial layout of UNDEFINED, it gets
321 * transitioned eventually.
322 */
323 for (unsigned i = 0; i < info->attachmentCount; i++) {
324 if (layout_undefined(att[i].initialLayout)) {
325 if (vk_format_is_depth_or_stencil(att[i].format)) {
326 pass->subpasses[0].start_barrier.incoherent_ccu_depth = true;
327 } else {
328 pass->subpasses[0].start_barrier.incoherent_ccu_color = true;
329 }
330 }
331 }
332 }
333
334 static void update_samples(struct tu_subpass *subpass,
335 VkSampleCountFlagBits samples)
336 {
337 assert(subpass->samples == 0 || subpass->samples == samples);
338 subpass->samples = samples;
339 }
340
341 static void
342 tu_render_pass_gmem_config(struct tu_render_pass *pass,
343 const struct tu_physical_device *phys_dev)
344 {
345 uint32_t block_align_shift = 3; /* log2(gmem_align/(tile_align_w*tile_align_h)) */
346 uint32_t tile_align_w = phys_dev->tile_align_w;
347 uint32_t gmem_align = (1 << block_align_shift) * tile_align_w * TILE_ALIGN_H;
348
349 /* calculate total bytes per pixel */
350 uint32_t cpp_total = 0;
351 for (uint32_t i = 0; i < pass->attachment_count; i++) {
352 struct tu_render_pass_attachment *att = &pass->attachments[i];
353 if (att->gmem_offset >= 0) {
354 cpp_total += att->cpp;
355 /* texture pitch must be aligned to 64, use a tile_align_w that is
356 * a multiple of 64 for cpp==1 attachment to work as input attachment
357 */
358 if (att->cpp == 1 && tile_align_w % 64 != 0) {
359 tile_align_w *= 2;
360 block_align_shift -= 1;
361 }
362 }
363 }
364
365 pass->tile_align_w = tile_align_w;
366
367 /* no gmem attachments */
368 if (cpp_total == 0) {
369 /* any value non-zero value so tiling config works with no attachments */
370 pass->gmem_pixels = 1024*1024;
371 return;
372 }
373
374 /* TODO: using ccu_offset_gmem so that BLIT_OP_SCALE resolve path
375 * doesn't break things. maybe there is a better solution?
376 * TODO: this algorithm isn't optimal
377 * for example, two attachments with cpp = {1, 4}
378 * result: nblocks = {12, 52}, pixels = 196608
379 * optimal: nblocks = {13, 51}, pixels = 208896
380 */
381 uint32_t gmem_blocks = phys_dev->ccu_offset_gmem / gmem_align;
382 uint32_t offset = 0, pixels = ~0u;
383 for (uint32_t i = 0; i < pass->attachment_count; i++) {
384 struct tu_render_pass_attachment *att = &pass->attachments[i];
385 if (att->gmem_offset < 0)
386 continue;
387
388 att->gmem_offset = offset;
389
390 uint32_t align = MAX2(1, att->cpp >> block_align_shift);
391 uint32_t nblocks = MAX2((gmem_blocks * att->cpp / cpp_total) & ~(align - 1), align);
392
393 if (nblocks > gmem_blocks) {
394 pixels = 0;
395 break;
396 }
397
398 gmem_blocks -= nblocks;
399 cpp_total -= att->cpp;
400 offset += nblocks * gmem_align;
401 pixels = MIN2(pixels, nblocks * gmem_align / att->cpp);
402 }
403
404 pass->gmem_pixels = pixels;
405 }
406
407 static void
408 attachment_set_ops(struct tu_render_pass_attachment *att,
409 VkAttachmentLoadOp load_op,
410 VkAttachmentLoadOp stencil_load_op,
411 VkAttachmentStoreOp store_op,
412 VkAttachmentStoreOp stencil_store_op)
413 {
414 /* load/store ops */
415 att->clear_mask =
416 (load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) ? VK_IMAGE_ASPECT_COLOR_BIT : 0;
417 att->load = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD);
418 att->store = (store_op == VK_ATTACHMENT_STORE_OP_STORE);
419
420 bool stencil_clear = (stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR);
421 bool stencil_load = (stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD);
422 bool stencil_store = (stencil_store_op == VK_ATTACHMENT_STORE_OP_STORE);
423
424 switch (att->format) {
425 case VK_FORMAT_D24_UNORM_S8_UINT: /* || stencil load/store */
426 if (att->clear_mask)
427 att->clear_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
428 if (stencil_clear)
429 att->clear_mask |= VK_IMAGE_ASPECT_STENCIL_BIT;
430 if (stencil_load)
431 att->load = true;
432 if (stencil_store)
433 att->store = true;
434 break;
435 case VK_FORMAT_S8_UINT: /* replace load/store with stencil load/store */
436 att->clear_mask = stencil_clear ? VK_IMAGE_ASPECT_COLOR_BIT : 0;
437 att->load = stencil_load;
438 att->store = stencil_store;
439 break;
440 default:
441 break;
442 }
443 }
444
445 static void
446 translate_references(VkAttachmentReference2 **reference_ptr,
447 const VkAttachmentReference *reference,
448 uint32_t count)
449 {
450 VkAttachmentReference2 *reference2 = *reference_ptr;
451 *reference_ptr += count;
452 for (uint32_t i = 0; i < count; i++) {
453 reference2[i] = (VkAttachmentReference2) {
454 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
455 .pNext = NULL,
456 .attachment = reference[i].attachment,
457 .layout = reference[i].layout,
458 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
459 };
460 }
461 }
462
463 VkResult
464 tu_CreateRenderPass(VkDevice device,
465 const VkRenderPassCreateInfo *pCreateInfo,
466 const VkAllocationCallbacks *pAllocator,
467 VkRenderPass *pRenderPass)
468 {
469 /* note: these counts shouldn't be excessively high, so allocating it all
470 * on the stack should be OK..
471 * also note preserve attachments aren't translated, currently unused
472 */
473 VkAttachmentDescription2 attachments[pCreateInfo->attachmentCount];
474 VkSubpassDescription2 subpasses[pCreateInfo->subpassCount];
475 VkSubpassDependency2 dependencies[pCreateInfo->dependencyCount];
476 uint32_t reference_count = 0;
477 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
478 reference_count += pCreateInfo->pSubpasses[i].inputAttachmentCount;
479 reference_count += pCreateInfo->pSubpasses[i].colorAttachmentCount;
480 if (pCreateInfo->pSubpasses[i].pResolveAttachments)
481 reference_count += pCreateInfo->pSubpasses[i].colorAttachmentCount;
482 if (pCreateInfo->pSubpasses[i].pDepthStencilAttachment)
483 reference_count += 1;
484 }
485 VkAttachmentReference2 reference[reference_count];
486 VkAttachmentReference2 *reference_ptr = reference;
487
488 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
489 attachments[i] = (VkAttachmentDescription2) {
490 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
491 .pNext = NULL,
492 .flags = pCreateInfo->pAttachments[i].flags,
493 .format = pCreateInfo->pAttachments[i].format,
494 .samples = pCreateInfo->pAttachments[i].samples,
495 .loadOp = pCreateInfo->pAttachments[i].loadOp,
496 .storeOp = pCreateInfo->pAttachments[i].storeOp,
497 .stencilLoadOp = pCreateInfo->pAttachments[i].stencilLoadOp,
498 .stencilStoreOp = pCreateInfo->pAttachments[i].stencilStoreOp,
499 .initialLayout = pCreateInfo->pAttachments[i].initialLayout,
500 .finalLayout = pCreateInfo->pAttachments[i].finalLayout,
501 };
502 }
503
504 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
505 subpasses[i] = (VkSubpassDescription2) {
506 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2,
507 .pNext = NULL,
508 .flags = pCreateInfo->pSubpasses[i].flags,
509 .pipelineBindPoint = pCreateInfo->pSubpasses[i].pipelineBindPoint,
510 .viewMask = 0,
511 .inputAttachmentCount = pCreateInfo->pSubpasses[i].inputAttachmentCount,
512 .colorAttachmentCount = pCreateInfo->pSubpasses[i].colorAttachmentCount,
513 };
514
515 subpasses[i].pInputAttachments = reference_ptr;
516 translate_references(&reference_ptr,
517 pCreateInfo->pSubpasses[i].pInputAttachments,
518 subpasses[i].inputAttachmentCount);
519 subpasses[i].pColorAttachments = reference_ptr;
520 translate_references(&reference_ptr,
521 pCreateInfo->pSubpasses[i].pColorAttachments,
522 subpasses[i].colorAttachmentCount);
523 subpasses[i].pResolveAttachments = NULL;
524 if (pCreateInfo->pSubpasses[i].pResolveAttachments) {
525 subpasses[i].pResolveAttachments = reference_ptr;
526 translate_references(&reference_ptr,
527 pCreateInfo->pSubpasses[i].pResolveAttachments,
528 subpasses[i].colorAttachmentCount);
529 }
530 subpasses[i].pDepthStencilAttachment = NULL;
531 if (pCreateInfo->pSubpasses[i].pDepthStencilAttachment) {
532 subpasses[i].pDepthStencilAttachment = reference_ptr;
533 translate_references(&reference_ptr,
534 pCreateInfo->pSubpasses[i].pDepthStencilAttachment,
535 1);
536 }
537 }
538
539 assert(reference_ptr == reference + reference_count);
540
541 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
542 dependencies[i] = (VkSubpassDependency2) {
543 .sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
544 .pNext = NULL,
545 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
546 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
547 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
548 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
549 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
550 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
551 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
552 .viewOffset = 0,
553 };
554 }
555
556 VkRenderPassCreateInfo2 create_info = {
557 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2,
558 .pNext = pCreateInfo->pNext,
559 .flags = pCreateInfo->flags,
560 .attachmentCount = pCreateInfo->attachmentCount,
561 .pAttachments = attachments,
562 .subpassCount = pCreateInfo->subpassCount,
563 .pSubpasses = subpasses,
564 .dependencyCount = pCreateInfo->dependencyCount,
565 .pDependencies = dependencies,
566 };
567
568 return tu_CreateRenderPass2(device, &create_info, pAllocator, pRenderPass);
569 }
570
571 VkResult
572 tu_CreateRenderPass2(VkDevice _device,
573 const VkRenderPassCreateInfo2KHR *pCreateInfo,
574 const VkAllocationCallbacks *pAllocator,
575 VkRenderPass *pRenderPass)
576 {
577 TU_FROM_HANDLE(tu_device, device, _device);
578 struct tu_render_pass *pass;
579 size_t size;
580 size_t attachments_offset;
581
582 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
583
584 size = sizeof(*pass);
585 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
586 attachments_offset = size;
587 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
588
589 pass = vk_object_zalloc(&device->vk, pAllocator, size,
590 VK_OBJECT_TYPE_RENDER_PASS);
591 if (pass == NULL)
592 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
593
594 pass->attachment_count = pCreateInfo->attachmentCount;
595 pass->subpass_count = pCreateInfo->subpassCount;
596 pass->attachments = (void *) pass + attachments_offset;
597
598 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
599 struct tu_render_pass_attachment *att = &pass->attachments[i];
600
601 att->format = pCreateInfo->pAttachments[i].format;
602 att->samples = pCreateInfo->pAttachments[i].samples;
603 att->cpp = vk_format_get_blocksize(att->format) * att->samples;
604 att->gmem_offset = -1;
605
606 attachment_set_ops(att,
607 pCreateInfo->pAttachments[i].loadOp,
608 pCreateInfo->pAttachments[i].stencilLoadOp,
609 pCreateInfo->pAttachments[i].storeOp,
610 pCreateInfo->pAttachments[i].stencilStoreOp);
611 }
612 uint32_t subpass_attachment_count = 0;
613 struct tu_subpass_attachment *p;
614 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
615 const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
616
617 subpass_attachment_count +=
618 desc->inputAttachmentCount + desc->colorAttachmentCount +
619 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0);
620 }
621
622 if (subpass_attachment_count) {
623 pass->subpass_attachments = vk_alloc2(
624 &device->vk.alloc, pAllocator,
625 subpass_attachment_count * sizeof(struct tu_subpass_attachment), 8,
626 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
627 if (pass->subpass_attachments == NULL) {
628 vk_object_free(&device->vk, pAllocator, pass);
629 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
630 }
631 } else
632 pass->subpass_attachments = NULL;
633
634 p = pass->subpass_attachments;
635 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
636 const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
637 struct tu_subpass *subpass = &pass->subpasses[i];
638
639 subpass->input_count = desc->inputAttachmentCount;
640 subpass->color_count = desc->colorAttachmentCount;
641 subpass->samples = 0;
642 subpass->srgb_cntl = 0;
643
644 if (desc->inputAttachmentCount > 0) {
645 subpass->input_attachments = p;
646 p += desc->inputAttachmentCount;
647
648 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
649 uint32_t a = desc->pInputAttachments[j].attachment;
650 subpass->input_attachments[j].attachment = a;
651 if (a != VK_ATTACHMENT_UNUSED)
652 pass->attachments[a].gmem_offset = 0;
653 }
654 }
655
656 if (desc->colorAttachmentCount > 0) {
657 subpass->color_attachments = p;
658 p += desc->colorAttachmentCount;
659
660 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
661 uint32_t a = desc->pColorAttachments[j].attachment;
662 subpass->color_attachments[j].attachment = a;
663
664 if (a != VK_ATTACHMENT_UNUSED) {
665 pass->attachments[a].gmem_offset = 0;
666 update_samples(subpass, pCreateInfo->pAttachments[a].samples);
667
668 if (vk_format_is_srgb(pass->attachments[a].format))
669 subpass->srgb_cntl |= 1 << j;
670 }
671 }
672 }
673
674 subpass->resolve_attachments = desc->pResolveAttachments ? p : NULL;
675 if (desc->pResolveAttachments) {
676 p += desc->colorAttachmentCount;
677 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
678 subpass->resolve_attachments[j].attachment =
679 desc->pResolveAttachments[j].attachment;
680 }
681 }
682
683
684 uint32_t a = desc->pDepthStencilAttachment ?
685 desc->pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
686 subpass->depth_stencil_attachment.attachment = a;
687 if (a != VK_ATTACHMENT_UNUSED) {
688 pass->attachments[a].gmem_offset = 0;
689 update_samples(subpass, pCreateInfo->pAttachments[a].samples);
690 }
691
692 subpass->samples = subpass->samples ?: 1;
693 }
694
695 /* disable unused attachments */
696 for (uint32_t i = 0; i < pass->attachment_count; i++) {
697 struct tu_render_pass_attachment *att = &pass->attachments[i];
698 if (att->gmem_offset < 0) {
699 att->clear_mask = 0;
700 att->load = false;
701 }
702 }
703
704 tu_render_pass_gmem_config(pass, device->physical_device);
705
706 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
707 tu_render_pass_add_subpass_dep(pass, &pCreateInfo->pDependencies[i]);
708 }
709
710 tu_render_pass_add_implicit_deps(pass, pCreateInfo);
711
712 *pRenderPass = tu_render_pass_to_handle(pass);
713
714 return VK_SUCCESS;
715 }
716
717 void
718 tu_DestroyRenderPass(VkDevice _device,
719 VkRenderPass _pass,
720 const VkAllocationCallbacks *pAllocator)
721 {
722 TU_FROM_HANDLE(tu_device, device, _device);
723 TU_FROM_HANDLE(tu_render_pass, pass, _pass);
724
725 if (!_pass)
726 return;
727
728 vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
729 vk_object_free(&device->vk, pAllocator, pass);
730 }
731
732 void
733 tu_GetRenderAreaGranularity(VkDevice _device,
734 VkRenderPass renderPass,
735 VkExtent2D *pGranularity)
736 {
737 pGranularity->width = GMEM_ALIGN_W;
738 pGranularity->height = GMEM_ALIGN_H;
739 }