anv: Emit pushed UBO bounds checking code in the back-end compiler
[mesa.git] / src / intel / vulkan / anv_pass.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_private.h"
25
26 #include "vk_util.h"
27
28 static void
29 anv_render_pass_add_subpass_dep(struct anv_render_pass *pass,
30 const VkSubpassDependency2KHR *dep)
31 {
32 if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
33 pass->subpass_flushes[pass->subpass_count] |=
34 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
35 } else {
36 assert(dep->dstSubpass < pass->subpass_count);
37 pass->subpass_flushes[dep->dstSubpass] |=
38 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
39 }
40
41 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
42 pass->subpass_flushes[0] |=
43 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
44 } else {
45 assert(dep->srcSubpass < pass->subpass_count);
46 pass->subpass_flushes[dep->srcSubpass + 1] |=
47 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
48 }
49 }
50
51 /* Do a second "compile" step on a render pass */
52 static void
53 anv_render_pass_compile(struct anv_render_pass *pass)
54 {
55 /* The CreateRenderPass code zeros the entire render pass and also uses a
56 * designated initializer for filling these out. There's no need for us to
57 * do it again.
58 *
59 * for (uint32_t i = 0; i < pass->attachment_count; i++) {
60 * pass->attachments[i].usage = 0;
61 * pass->attachments[i].first_subpass_layout = VK_IMAGE_LAYOUT_UNDEFINED;
62 * }
63 */
64
65 VkImageUsageFlags all_usage = 0;
66 for (uint32_t i = 0; i < pass->subpass_count; i++) {
67 struct anv_subpass *subpass = &pass->subpasses[i];
68
69 /* We don't allow depth_stencil_attachment to be non-NULL and be
70 * VK_ATTACHMENT_UNUSED. This way something can just check for NULL
71 * and be guaranteed that they have a valid attachment.
72 */
73 if (subpass->depth_stencil_attachment &&
74 subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
75 subpass->depth_stencil_attachment = NULL;
76
77 if (subpass->ds_resolve_attachment &&
78 subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
79 subpass->ds_resolve_attachment = NULL;
80
81 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
82 struct anv_subpass_attachment *subpass_att = &subpass->attachments[j];
83 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
84 continue;
85
86 struct anv_render_pass_attachment *pass_att =
87 &pass->attachments[subpass_att->attachment];
88
89 assert(__builtin_popcount(subpass_att->usage) == 1);
90 pass_att->usage |= subpass_att->usage;
91 pass_att->last_subpass_idx = i;
92
93 all_usage |= subpass_att->usage;
94
95 if (pass_att->first_subpass_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
96 pass_att->first_subpass_layout = subpass_att->layout;
97 assert(pass_att->first_subpass_layout != VK_IMAGE_LAYOUT_UNDEFINED);
98 }
99
100 if (subpass_att->usage == VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
101 subpass->depth_stencil_attachment &&
102 subpass_att->attachment == subpass->depth_stencil_attachment->attachment)
103 subpass->has_ds_self_dep = true;
104 }
105
106 /* We have to handle resolve attachments specially */
107 subpass->has_color_resolve = false;
108 if (subpass->resolve_attachments) {
109 for (uint32_t j = 0; j < subpass->color_count; j++) {
110 struct anv_subpass_attachment *color_att =
111 &subpass->color_attachments[j];
112 struct anv_subpass_attachment *resolve_att =
113 &subpass->resolve_attachments[j];
114 if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
115 continue;
116
117 subpass->has_color_resolve = true;
118
119 assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
120 color_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
121 }
122 }
123
124 if (subpass->ds_resolve_attachment) {
125 struct anv_subpass_attachment *ds_att =
126 subpass->depth_stencil_attachment;
127 UNUSED struct anv_subpass_attachment *resolve_att =
128 subpass->ds_resolve_attachment;
129
130 assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
131 ds_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
132 }
133 }
134
135 /* From the Vulkan 1.0.39 spec:
136 *
137 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
138 * first subpass that uses an attachment, then an implicit subpass
139 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
140 * used in. The subpass dependency operates as if defined with the
141 * following parameters:
142 *
143 * VkSubpassDependency implicitDependency = {
144 * .srcSubpass = VK_SUBPASS_EXTERNAL;
145 * .dstSubpass = firstSubpass; // First subpass attachment is used in
146 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
147 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
148 * .srcAccessMask = 0;
149 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
150 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
151 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
152 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
153 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
154 * .dependencyFlags = 0;
155 * };
156 *
157 * Similarly, if there is no subpass dependency from the last subpass
158 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
159 * subpass dependency exists from the last subpass it is used in to
160 * VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
161 * with the following parameters:
162 *
163 * VkSubpassDependency implicitDependency = {
164 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
165 * .dstSubpass = VK_SUBPASS_EXTERNAL;
166 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
167 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
168 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
169 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
170 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
171 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
172 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
173 * .dstAccessMask = 0;
174 * .dependencyFlags = 0;
175 * };
176 *
177 * We could implement this by walking over all of the attachments and
178 * subpasses and checking to see if any of them don't have an external
179 * dependency. Or, we could just be lazy and add a couple extra flushes.
180 * We choose to be lazy.
181 *
182 * From the documentation for vkCmdNextSubpass:
183 *
184 * "Moving to the next subpass automatically performs any multisample
185 * resolve operations in the subpass being ended. End-of-subpass
186 * multisample resolves are treated as color attachment writes for the
187 * purposes of synchronization. This applies to resolve operations for
188 * both color and depth/stencil attachments. That is, they are
189 * considered to execute in the
190 * VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT pipeline stage and
191 * their writes are synchronized with
192 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT."
193 *
194 * Therefore, the above flags concerning color attachments also apply to
195 * color and depth/stencil resolve attachments.
196 */
197 if (all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
198 pass->subpass_flushes[0] |=
199 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
200 }
201 if (all_usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
202 VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
203 pass->subpass_flushes[pass->subpass_count] |=
204 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
205 }
206 if (all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
207 pass->subpass_flushes[pass->subpass_count] |=
208 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
209 }
210 }
211
212 static unsigned
213 num_subpass_attachments(const VkSubpassDescription *desc)
214 {
215 return desc->inputAttachmentCount +
216 desc->colorAttachmentCount +
217 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
218 (desc->pDepthStencilAttachment != NULL);
219 }
220
221 VkResult anv_CreateRenderPass(
222 VkDevice _device,
223 const VkRenderPassCreateInfo* pCreateInfo,
224 const VkAllocationCallbacks* pAllocator,
225 VkRenderPass* pRenderPass)
226 {
227 ANV_FROM_HANDLE(anv_device, device, _device);
228
229 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
230
231 struct anv_render_pass *pass;
232 struct anv_subpass *subpasses;
233 struct anv_render_pass_attachment *attachments;
234 enum anv_pipe_bits *subpass_flushes;
235
236 ANV_MULTIALLOC(ma);
237 anv_multialloc_add(&ma, &pass, 1);
238 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
239 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
240 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
241
242 struct anv_subpass_attachment *subpass_attachments;
243 uint32_t subpass_attachment_count = 0;
244 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
245 subpass_attachment_count +=
246 num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
247 }
248 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
249
250 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
251 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
252 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
253
254 /* Clear the subpasses along with the parent pass. This required because
255 * each array member of anv_subpass must be a valid pointer if not NULL.
256 */
257 memset(pass, 0, ma.size);
258 pass->attachment_count = pCreateInfo->attachmentCount;
259 pass->subpass_count = pCreateInfo->subpassCount;
260 pass->attachments = attachments;
261 pass->subpass_flushes = subpass_flushes;
262
263 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
264 pass->attachments[i] = (struct anv_render_pass_attachment) {
265 .format = pCreateInfo->pAttachments[i].format,
266 .samples = pCreateInfo->pAttachments[i].samples,
267 .load_op = pCreateInfo->pAttachments[i].loadOp,
268 .store_op = pCreateInfo->pAttachments[i].storeOp,
269 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
270 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
271 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
272
273 .stencil_initial_layout = pCreateInfo->pAttachments[i].initialLayout,
274 .stencil_final_layout = pCreateInfo->pAttachments[i].finalLayout,
275 };
276 }
277
278 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
279 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
280 struct anv_subpass *subpass = &pass->subpasses[i];
281
282 subpass->input_count = desc->inputAttachmentCount;
283 subpass->color_count = desc->colorAttachmentCount;
284 subpass->attachment_count = num_subpass_attachments(desc);
285 subpass->attachments = subpass_attachments;
286 subpass->view_mask = 0;
287
288 if (desc->inputAttachmentCount > 0) {
289 subpass->input_attachments = subpass_attachments;
290 subpass_attachments += desc->inputAttachmentCount;
291
292 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
293 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
294 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
295 .attachment = desc->pInputAttachments[j].attachment,
296 .layout = desc->pInputAttachments[j].layout,
297 .stencil_layout = desc->pInputAttachments[j].layout,
298 };
299 }
300 }
301
302 if (desc->colorAttachmentCount > 0) {
303 subpass->color_attachments = subpass_attachments;
304 subpass_attachments += desc->colorAttachmentCount;
305
306 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
307 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
308 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
309 .attachment = desc->pColorAttachments[j].attachment,
310 .layout = desc->pColorAttachments[j].layout,
311 };
312 }
313 }
314
315 if (desc->pResolveAttachments) {
316 subpass->resolve_attachments = subpass_attachments;
317 subpass_attachments += desc->colorAttachmentCount;
318
319 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
320 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
321 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
322 .attachment = desc->pResolveAttachments[j].attachment,
323 .layout = desc->pResolveAttachments[j].layout,
324 };
325 }
326 }
327
328 if (desc->pDepthStencilAttachment) {
329 subpass->depth_stencil_attachment = subpass_attachments++;
330
331 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
332 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
333 .attachment = desc->pDepthStencilAttachment->attachment,
334 .layout = desc->pDepthStencilAttachment->layout,
335 .stencil_layout = desc->pDepthStencilAttachment->layout,
336 };
337 }
338 }
339
340 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
341 /* Convert to a Dependency2KHR */
342 VkSubpassDependency2 dep2 = {
343 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
344 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
345 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
346 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
347 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
348 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
349 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
350 };
351 anv_render_pass_add_subpass_dep(pass, &dep2);
352 }
353
354 vk_foreach_struct(ext, pCreateInfo->pNext) {
355 switch (ext->sType) {
356 case VK_STRUCTURE_TYPE_RENDER_PASS_INPUT_ATTACHMENT_ASPECT_CREATE_INFO:
357 /* We don't care about this information */
358 break;
359
360 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO: {
361 VkRenderPassMultiviewCreateInfo *mv = (void *)ext;
362
363 for (uint32_t i = 0; i < mv->subpassCount; i++) {
364 pass->subpasses[i].view_mask = mv->pViewMasks[i];
365 }
366 break;
367 }
368
369 default:
370 anv_debug_ignored_stype(ext->sType);
371 }
372 }
373
374 anv_render_pass_compile(pass);
375
376 *pRenderPass = anv_render_pass_to_handle(pass);
377
378 return VK_SUCCESS;
379 }
380
381 static unsigned
382 num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
383 {
384 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
385 vk_find_struct_const(desc->pNext,
386 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
387
388 return desc->inputAttachmentCount +
389 desc->colorAttachmentCount +
390 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
391 (desc->pDepthStencilAttachment != NULL) +
392 (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
393 }
394
395 VkResult anv_CreateRenderPass2(
396 VkDevice _device,
397 const VkRenderPassCreateInfo2KHR* pCreateInfo,
398 const VkAllocationCallbacks* pAllocator,
399 VkRenderPass* pRenderPass)
400 {
401 ANV_FROM_HANDLE(anv_device, device, _device);
402
403 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
404
405 struct anv_render_pass *pass;
406 struct anv_subpass *subpasses;
407 struct anv_render_pass_attachment *attachments;
408 enum anv_pipe_bits *subpass_flushes;
409
410 ANV_MULTIALLOC(ma);
411 anv_multialloc_add(&ma, &pass, 1);
412 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
413 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
414 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
415
416 struct anv_subpass_attachment *subpass_attachments;
417 uint32_t subpass_attachment_count = 0;
418 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
419 subpass_attachment_count +=
420 num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
421 }
422 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
423
424 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
425 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
426 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
427
428 /* Clear the subpasses along with the parent pass. This required because
429 * each array member of anv_subpass must be a valid pointer if not NULL.
430 */
431 memset(pass, 0, ma.size);
432 pass->attachment_count = pCreateInfo->attachmentCount;
433 pass->subpass_count = pCreateInfo->subpassCount;
434 pass->attachments = attachments;
435 pass->subpass_flushes = subpass_flushes;
436
437 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
438 const VkAttachmentDescriptionStencilLayoutKHR *stencil_layout =
439 vk_find_struct_const(pCreateInfo->pAttachments[i].pNext,
440 ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
441
442 pass->attachments[i] = (struct anv_render_pass_attachment) {
443 .format = pCreateInfo->pAttachments[i].format,
444 .samples = pCreateInfo->pAttachments[i].samples,
445 .load_op = pCreateInfo->pAttachments[i].loadOp,
446 .store_op = pCreateInfo->pAttachments[i].storeOp,
447 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
448 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
449 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
450
451 .stencil_initial_layout = (stencil_layout ?
452 stencil_layout->stencilInitialLayout :
453 pCreateInfo->pAttachments[i].initialLayout),
454 .stencil_final_layout = (stencil_layout ?
455 stencil_layout->stencilFinalLayout :
456 pCreateInfo->pAttachments[i].finalLayout),
457 };
458 }
459
460 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
461 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
462 struct anv_subpass *subpass = &pass->subpasses[i];
463
464 subpass->input_count = desc->inputAttachmentCount;
465 subpass->color_count = desc->colorAttachmentCount;
466 subpass->attachment_count = num_subpass_attachments2(desc);
467 subpass->attachments = subpass_attachments;
468 subpass->view_mask = desc->viewMask;
469
470 if (desc->inputAttachmentCount > 0) {
471 subpass->input_attachments = subpass_attachments;
472 subpass_attachments += desc->inputAttachmentCount;
473
474 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
475 const VkAttachmentReferenceStencilLayoutKHR *stencil_layout =
476 vk_find_struct_const(desc->pInputAttachments[j].pNext,
477 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
478
479 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
480 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
481 .attachment = desc->pInputAttachments[j].attachment,
482 .layout = desc->pInputAttachments[j].layout,
483 .stencil_layout = (stencil_layout ?
484 stencil_layout->stencilLayout :
485 desc->pInputAttachments[j].layout),
486 };
487 }
488 }
489
490 if (desc->colorAttachmentCount > 0) {
491 subpass->color_attachments = subpass_attachments;
492 subpass_attachments += desc->colorAttachmentCount;
493
494 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
495 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
496 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
497 .attachment = desc->pColorAttachments[j].attachment,
498 .layout = desc->pColorAttachments[j].layout,
499 };
500 }
501 }
502
503 if (desc->pResolveAttachments) {
504 subpass->resolve_attachments = subpass_attachments;
505 subpass_attachments += desc->colorAttachmentCount;
506
507 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
508 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
509 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
510 .attachment = desc->pResolveAttachments[j].attachment,
511 .layout = desc->pResolveAttachments[j].layout,
512 };
513 }
514 }
515
516 if (desc->pDepthStencilAttachment) {
517 subpass->depth_stencil_attachment = subpass_attachments++;
518
519 const VkAttachmentReferenceStencilLayoutKHR *stencil_attachment =
520 vk_find_struct_const(desc->pDepthStencilAttachment->pNext,
521 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
522
523 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
524 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
525 .attachment = desc->pDepthStencilAttachment->attachment,
526 .layout = desc->pDepthStencilAttachment->layout,
527 .stencil_layout = stencil_attachment ?
528 stencil_attachment->stencilLayout :
529 desc->pDepthStencilAttachment->layout,
530 };
531 }
532
533 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
534 vk_find_struct_const(desc->pNext,
535 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
536
537 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
538 subpass->ds_resolve_attachment = subpass_attachments++;
539
540 const VkAttachmentReferenceStencilLayoutKHR *stencil_resolve_attachment =
541 vk_find_struct_const(ds_resolve->pDepthStencilResolveAttachment->pNext,
542 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
543
544 *subpass->ds_resolve_attachment = (struct anv_subpass_attachment) {
545 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
546 .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
547 .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
548 .stencil_layout = stencil_resolve_attachment ?
549 stencil_resolve_attachment->stencilLayout :
550 ds_resolve->pDepthStencilResolveAttachment->layout,
551 };
552 subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
553 subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
554 }
555 }
556
557 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++)
558 anv_render_pass_add_subpass_dep(pass, &pCreateInfo->pDependencies[i]);
559
560 vk_foreach_struct(ext, pCreateInfo->pNext) {
561 switch (ext->sType) {
562 default:
563 anv_debug_ignored_stype(ext->sType);
564 }
565 }
566
567 anv_render_pass_compile(pass);
568
569 *pRenderPass = anv_render_pass_to_handle(pass);
570
571 return VK_SUCCESS;
572 }
573
574 void anv_DestroyRenderPass(
575 VkDevice _device,
576 VkRenderPass _pass,
577 const VkAllocationCallbacks* pAllocator)
578 {
579 ANV_FROM_HANDLE(anv_device, device, _device);
580 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
581
582 vk_free2(&device->alloc, pAllocator, pass);
583 }
584
585 void anv_GetRenderAreaGranularity(
586 VkDevice device,
587 VkRenderPass renderPass,
588 VkExtent2D* pGranularity)
589 {
590 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
591
592 /* This granularity satisfies HiZ fast clear alignment requirements
593 * for all sample counts.
594 */
595 for (unsigned i = 0; i < pass->subpass_count; ++i) {
596 if (pass->subpasses[i].depth_stencil_attachment) {
597 *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
598 return;
599 }
600 }
601
602 *pGranularity = (VkExtent2D) { 1, 1 };
603 }