radv: use the common base object type for VkDevice
[mesa.git] / src / amd / vulkan / radv_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 DEALINGS
25 * IN THE SOFTWARE.
26 */
27 #include "radv_private.h"
28
29 #include "vk_util.h"
30
31 static void
32 radv_render_pass_add_subpass_dep(struct radv_render_pass *pass,
33 const VkSubpassDependency2 *dep)
34 {
35 uint32_t src = dep->srcSubpass;
36 uint32_t dst = dep->dstSubpass;
37
38 /* Ignore subpass self-dependencies as they allow the app to call
39 * vkCmdPipelineBarrier() inside the render pass and the driver should
40 * only do the barrier when called, not when starting the render pass.
41 */
42 if (src == dst)
43 return;
44
45 /* Accumulate all ingoing external dependencies to the first subpass. */
46 if (src == VK_SUBPASS_EXTERNAL)
47 dst = 0;
48
49 if (dst == VK_SUBPASS_EXTERNAL) {
50 if (dep->dstStageMask != VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT)
51 pass->end_barrier.src_stage_mask |= dep->srcStageMask;
52 pass->end_barrier.src_access_mask |= dep->srcAccessMask;
53 pass->end_barrier.dst_access_mask |= dep->dstAccessMask;
54 } else {
55 if (dep->dstStageMask != VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT)
56 pass->subpasses[dst].start_barrier.src_stage_mask |= dep->srcStageMask;
57 pass->subpasses[dst].start_barrier.src_access_mask |= dep->srcAccessMask;
58 pass->subpasses[dst].start_barrier.dst_access_mask |= dep->dstAccessMask;
59 }
60 }
61
62 static bool
63 radv_pass_has_layout_transitions(const struct radv_render_pass *pass)
64 {
65 for (unsigned i = 0; i < pass->subpass_count; i++) {
66 const struct radv_subpass *subpass = &pass->subpasses[i];
67 for (unsigned j = 0; j < subpass->attachment_count; j++) {
68 const uint32_t a = subpass->attachments[j].attachment;
69 if (a == VK_ATTACHMENT_UNUSED)
70 continue;
71
72 uint32_t initial_layout = pass->attachments[a].initial_layout;
73 uint32_t stencil_initial_layout = pass->attachments[a].stencil_initial_layout;
74 uint32_t final_layout = pass->attachments[a].final_layout;
75 uint32_t stencil_final_layout = pass->attachments[a].stencil_final_layout;
76
77 if (subpass->attachments[j].layout != initial_layout ||
78 subpass->attachments[j].layout != stencil_initial_layout ||
79 subpass->attachments[j].layout != final_layout ||
80 subpass->attachments[j].layout != stencil_final_layout)
81 return true;
82 }
83 }
84
85 return false;
86 }
87
88 static void
89 radv_render_pass_add_implicit_deps(struct radv_render_pass *pass,
90 bool has_ingoing_dep, bool has_outgoing_dep)
91 {
92 /* From the Vulkan 1.0.39 spec:
93 *
94 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
95 * first subpass that uses an attachment, then an implicit subpass
96 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
97 * used in. The implicit subpass dependency only exists if there
98 * exists an automatic layout transition away from initialLayout.
99 * The subpass dependency operates as if defined with the
100 * following parameters:
101 *
102 * VkSubpassDependency implicitDependency = {
103 * .srcSubpass = VK_SUBPASS_EXTERNAL;
104 * .dstSubpass = firstSubpass; // First subpass attachment is used in
105 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
106 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
107 * .srcAccessMask = 0;
108 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
109 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
110 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
111 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
112 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
113 * .dependencyFlags = 0;
114 * };
115 *
116 * Similarly, if there is no subpass dependency from the last subpass
117 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
118 * subpass dependency exists from the last subpass it is used in to
119 * VK_SUBPASS_EXTERNAL. The implicit subpass dependency only exists
120 * if there exists an automatic layout transition into finalLayout.
121 * The subpass dependency operates as if defined with the following
122 * parameters:
123 *
124 * VkSubpassDependency implicitDependency = {
125 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
126 * .dstSubpass = VK_SUBPASS_EXTERNAL;
127 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
128 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
129 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
130 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
131 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
132 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
133 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
134 * .dstAccessMask = 0;
135 * .dependencyFlags = 0;
136 * };
137 */
138
139 /* Implicit subpass dependencies only make sense if automatic layout
140 * transitions are performed.
141 */
142 if (!radv_pass_has_layout_transitions(pass))
143 return;
144
145 if (!has_ingoing_dep) {
146 const VkSubpassDependency2KHR implicit_ingoing_dep = {
147 .srcSubpass = VK_SUBPASS_EXTERNAL,
148 .dstSubpass = 0,
149 .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
150 .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
151 .srcAccessMask = 0,
152 .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
153 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
154 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
155 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
156 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
157 .dependencyFlags = 0,
158 };
159
160 radv_render_pass_add_subpass_dep(pass, &implicit_ingoing_dep);
161 }
162
163 if (!has_outgoing_dep) {
164 const VkSubpassDependency2KHR implicit_outgoing_dep = {
165 .srcSubpass = 0,
166 .dstSubpass = VK_SUBPASS_EXTERNAL,
167 .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
168 .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
169 .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
170 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
171 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
172 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
173 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
174 .dstAccessMask = 0,
175 .dependencyFlags = 0,
176 };
177
178 radv_render_pass_add_subpass_dep(pass, &implicit_outgoing_dep);
179 }
180 }
181
182 static void
183 radv_render_pass_compile(struct radv_render_pass *pass)
184 {
185 for (uint32_t i = 0; i < pass->subpass_count; i++) {
186 struct radv_subpass *subpass = &pass->subpasses[i];
187
188 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
189 struct radv_subpass_attachment *subpass_att =
190 &subpass->attachments[j];
191 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
192 continue;
193
194 struct radv_render_pass_attachment *pass_att =
195 &pass->attachments[subpass_att->attachment];
196
197 pass_att->first_subpass_idx = UINT32_MAX;
198 }
199 }
200
201 for (uint32_t i = 0; i < pass->subpass_count; i++) {
202 struct radv_subpass *subpass = &pass->subpasses[i];
203 uint32_t color_sample_count = 1, depth_sample_count = 1;
204
205 /* We don't allow depth_stencil_attachment to be non-NULL and
206 * be VK_ATTACHMENT_UNUSED. This way something can just check
207 * for NULL and be guaranteed that they have a valid
208 * attachment.
209 */
210 if (subpass->depth_stencil_attachment &&
211 subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
212 subpass->depth_stencil_attachment = NULL;
213
214 if (subpass->ds_resolve_attachment &&
215 subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
216 subpass->ds_resolve_attachment = NULL;
217
218 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
219 struct radv_subpass_attachment *subpass_att =
220 &subpass->attachments[j];
221 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
222 continue;
223
224 struct radv_render_pass_attachment *pass_att =
225 &pass->attachments[subpass_att->attachment];
226
227 if (i < pass_att->first_subpass_idx)
228 pass_att->first_subpass_idx = i;
229 pass_att->last_subpass_idx = i;
230 }
231
232 subpass->has_color_att = false;
233 for (uint32_t j = 0; j < subpass->color_count; j++) {
234 struct radv_subpass_attachment *subpass_att =
235 &subpass->color_attachments[j];
236 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
237 continue;
238
239 subpass->has_color_att = true;
240
241 struct radv_render_pass_attachment *pass_att =
242 &pass->attachments[subpass_att->attachment];
243
244 color_sample_count = pass_att->samples;
245 }
246
247 if (subpass->depth_stencil_attachment) {
248 const uint32_t a =
249 subpass->depth_stencil_attachment->attachment;
250 struct radv_render_pass_attachment *pass_att =
251 &pass->attachments[a];
252 depth_sample_count = pass_att->samples;
253 }
254
255 subpass->max_sample_count = MAX2(color_sample_count,
256 depth_sample_count);
257 subpass->color_sample_count = color_sample_count;
258 subpass->depth_sample_count = depth_sample_count;
259
260 /* We have to handle resolve attachments specially */
261 subpass->has_color_resolve = false;
262 if (subpass->resolve_attachments) {
263 for (uint32_t j = 0; j < subpass->color_count; j++) {
264 struct radv_subpass_attachment *resolve_att =
265 &subpass->resolve_attachments[j];
266
267 if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
268 continue;
269
270 subpass->has_color_resolve = true;
271 }
272 }
273
274 for (uint32_t j = 0; j < subpass->input_count; ++j) {
275 if (subpass->input_attachments[j].attachment == VK_ATTACHMENT_UNUSED)
276 continue;
277
278 for (uint32_t k = 0; k < subpass->color_count; ++k) {
279 if (subpass->color_attachments[k].attachment == subpass->input_attachments[j].attachment) {
280 subpass->input_attachments[j].in_render_loop = true;
281 subpass->color_attachments[k].in_render_loop = true;
282 }
283 }
284
285 if (subpass->depth_stencil_attachment &&
286 subpass->depth_stencil_attachment->attachment == subpass->input_attachments[j].attachment) {
287 subpass->input_attachments[j].in_render_loop = true;
288 subpass->depth_stencil_attachment->in_render_loop = true;
289 }
290 }
291 }
292 }
293
294 static unsigned
295 radv_num_subpass_attachments(const VkSubpassDescription *desc)
296 {
297 return desc->inputAttachmentCount +
298 desc->colorAttachmentCount +
299 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
300 (desc->pDepthStencilAttachment != NULL);
301 }
302
303 VkResult radv_CreateRenderPass(
304 VkDevice _device,
305 const VkRenderPassCreateInfo* pCreateInfo,
306 const VkAllocationCallbacks* pAllocator,
307 VkRenderPass* pRenderPass)
308 {
309 RADV_FROM_HANDLE(radv_device, device, _device);
310 struct radv_render_pass *pass;
311 size_t size;
312 size_t attachments_offset;
313 VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
314
315 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
316
317 size = sizeof(*pass);
318 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
319 attachments_offset = size;
320 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
321
322 pass = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
323 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
324 if (pass == NULL)
325 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
326
327 memset(pass, 0, size);
328 pass->attachment_count = pCreateInfo->attachmentCount;
329 pass->subpass_count = pCreateInfo->subpassCount;
330 pass->attachments = (void *) pass + attachments_offset;
331
332 vk_foreach_struct(ext, pCreateInfo->pNext) {
333 switch(ext->sType) {
334 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
335 multiview_info = (VkRenderPassMultiviewCreateInfo*)ext;
336 break;
337 default:
338 break;
339 }
340 }
341
342 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
343 struct radv_render_pass_attachment *att = &pass->attachments[i];
344
345 att->format = pCreateInfo->pAttachments[i].format;
346 att->samples = pCreateInfo->pAttachments[i].samples;
347 att->load_op = pCreateInfo->pAttachments[i].loadOp;
348 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
349 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
350 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
351 att->stencil_initial_layout = pCreateInfo->pAttachments[i].initialLayout;
352 att->stencil_final_layout = pCreateInfo->pAttachments[i].finalLayout;
353 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
354 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
355 }
356 uint32_t subpass_attachment_count = 0;
357 struct radv_subpass_attachment *p;
358 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
359 subpass_attachment_count +=
360 radv_num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
361 }
362
363 if (subpass_attachment_count) {
364 pass->subpass_attachments =
365 vk_alloc2(&device->vk.alloc, pAllocator,
366 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
367 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
368 if (pass->subpass_attachments == NULL) {
369 vk_free2(&device->vk.alloc, pAllocator, pass);
370 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
371 }
372 } else
373 pass->subpass_attachments = NULL;
374
375 p = pass->subpass_attachments;
376 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
377 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
378 struct radv_subpass *subpass = &pass->subpasses[i];
379
380 subpass->input_count = desc->inputAttachmentCount;
381 subpass->color_count = desc->colorAttachmentCount;
382 subpass->attachment_count = radv_num_subpass_attachments(desc);
383 subpass->attachments = p;
384
385 if (multiview_info)
386 subpass->view_mask = multiview_info->pViewMasks[i];
387
388 if (desc->inputAttachmentCount > 0) {
389 subpass->input_attachments = p;
390 p += desc->inputAttachmentCount;
391
392 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
393 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
394 .attachment = desc->pInputAttachments[j].attachment,
395 .layout = desc->pInputAttachments[j].layout,
396 .stencil_layout = desc->pInputAttachments[j].layout,
397 };
398 }
399 }
400
401 if (desc->colorAttachmentCount > 0) {
402 subpass->color_attachments = p;
403 p += desc->colorAttachmentCount;
404
405 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
406 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
407 .attachment = desc->pColorAttachments[j].attachment,
408 .layout = desc->pColorAttachments[j].layout,
409 };
410 }
411 }
412
413 if (desc->pResolveAttachments) {
414 subpass->resolve_attachments = p;
415 p += desc->colorAttachmentCount;
416
417 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
418 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
419 .attachment = desc->pResolveAttachments[j].attachment,
420 .layout = desc->pResolveAttachments[j].layout,
421 .stencil_layout = desc->pResolveAttachments[j].layout,
422 };
423 }
424 }
425
426 if (desc->pDepthStencilAttachment) {
427 subpass->depth_stencil_attachment = p++;
428
429 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
430 .attachment = desc->pDepthStencilAttachment->attachment,
431 .layout = desc->pDepthStencilAttachment->layout,
432 .stencil_layout = desc->pDepthStencilAttachment->layout,
433 };
434 }
435 }
436
437 bool has_ingoing_dep = false;
438 bool has_outgoing_dep = false;
439
440 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
441 /* Convert to a Dependency2 */
442 struct VkSubpassDependency2 dep2 = {
443 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
444 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
445 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
446 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
447 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
448 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
449 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
450 };
451 radv_render_pass_add_subpass_dep(pass, &dep2);
452
453 /* Determine if the subpass has explicit dependencies from/to
454 * VK_SUBPASS_EXTERNAL.
455 */
456 if (pCreateInfo->pDependencies[i].srcSubpass == VK_SUBPASS_EXTERNAL)
457 has_ingoing_dep = true;
458 if (pCreateInfo->pDependencies[i].dstSubpass == VK_SUBPASS_EXTERNAL)
459 has_outgoing_dep = true;
460 }
461
462 radv_render_pass_add_implicit_deps(pass,
463 has_ingoing_dep, has_outgoing_dep);
464
465 radv_render_pass_compile(pass);
466
467 *pRenderPass = radv_render_pass_to_handle(pass);
468
469 return VK_SUCCESS;
470 }
471
472 static unsigned
473 radv_num_subpass_attachments2(const VkSubpassDescription2 *desc)
474 {
475 const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
476 vk_find_struct_const(desc->pNext,
477 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
478
479 return desc->inputAttachmentCount +
480 desc->colorAttachmentCount +
481 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
482 (desc->pDepthStencilAttachment != NULL) +
483 (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
484 }
485
486 VkResult radv_CreateRenderPass2(
487 VkDevice _device,
488 const VkRenderPassCreateInfo2* pCreateInfo,
489 const VkAllocationCallbacks* pAllocator,
490 VkRenderPass* pRenderPass)
491 {
492 RADV_FROM_HANDLE(radv_device, device, _device);
493 struct radv_render_pass *pass;
494 size_t size;
495 size_t attachments_offset;
496
497 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2);
498
499 size = sizeof(*pass);
500 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
501 attachments_offset = size;
502 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
503
504 pass = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
505 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
506 if (pass == NULL)
507 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
508
509 memset(pass, 0, size);
510 pass->attachment_count = pCreateInfo->attachmentCount;
511 pass->subpass_count = pCreateInfo->subpassCount;
512 pass->attachments = (void *) pass + attachments_offset;
513
514 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
515 struct radv_render_pass_attachment *att = &pass->attachments[i];
516 const VkAttachmentDescriptionStencilLayoutKHR *stencil_layout =
517 vk_find_struct_const(pCreateInfo->pAttachments[i].pNext,
518 ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
519
520 att->format = pCreateInfo->pAttachments[i].format;
521 att->samples = pCreateInfo->pAttachments[i].samples;
522 att->load_op = pCreateInfo->pAttachments[i].loadOp;
523 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
524 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
525 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
526 att->stencil_initial_layout = (stencil_layout ?
527 stencil_layout->stencilInitialLayout :
528 pCreateInfo->pAttachments[i].initialLayout);
529 att->stencil_final_layout = (stencil_layout ?
530 stencil_layout->stencilFinalLayout :
531 pCreateInfo->pAttachments[i].finalLayout);
532 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
533 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
534 }
535 uint32_t subpass_attachment_count = 0;
536 struct radv_subpass_attachment *p;
537 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
538 subpass_attachment_count +=
539 radv_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
540 }
541
542 if (subpass_attachment_count) {
543 pass->subpass_attachments =
544 vk_alloc2(&device->vk.alloc, pAllocator,
545 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
546 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
547 if (pass->subpass_attachments == NULL) {
548 vk_free2(&device->vk.alloc, pAllocator, pass);
549 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
550 }
551 } else
552 pass->subpass_attachments = NULL;
553
554 p = pass->subpass_attachments;
555 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
556 const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
557 struct radv_subpass *subpass = &pass->subpasses[i];
558
559 subpass->input_count = desc->inputAttachmentCount;
560 subpass->color_count = desc->colorAttachmentCount;
561 subpass->attachment_count = radv_num_subpass_attachments2(desc);
562 subpass->attachments = p;
563 subpass->view_mask = desc->viewMask;
564
565 if (desc->inputAttachmentCount > 0) {
566 subpass->input_attachments = p;
567 p += desc->inputAttachmentCount;
568
569 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
570 const VkAttachmentReferenceStencilLayoutKHR *stencil_attachment =
571 vk_find_struct_const(desc->pInputAttachments[j].pNext,
572 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
573
574 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
575 .attachment = desc->pInputAttachments[j].attachment,
576 .layout = desc->pInputAttachments[j].layout,
577 .stencil_layout = (stencil_attachment ?
578 stencil_attachment->stencilLayout :
579 desc->pInputAttachments[j].layout),
580 };
581 }
582 }
583
584 if (desc->colorAttachmentCount > 0) {
585 subpass->color_attachments = p;
586 p += desc->colorAttachmentCount;
587
588 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
589 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
590 .attachment = desc->pColorAttachments[j].attachment,
591 .layout = desc->pColorAttachments[j].layout,
592 };
593 }
594 }
595
596 if (desc->pResolveAttachments) {
597 subpass->resolve_attachments = p;
598 p += desc->colorAttachmentCount;
599
600 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
601 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
602 .attachment = desc->pResolveAttachments[j].attachment,
603 .layout = desc->pResolveAttachments[j].layout,
604 };
605 }
606 }
607
608 if (desc->pDepthStencilAttachment) {
609 subpass->depth_stencil_attachment = p++;
610
611 const VkAttachmentReferenceStencilLayoutKHR *stencil_attachment =
612 vk_find_struct_const(desc->pDepthStencilAttachment->pNext,
613 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
614
615 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
616 .attachment = desc->pDepthStencilAttachment->attachment,
617 .layout = desc->pDepthStencilAttachment->layout,
618 .stencil_layout = (stencil_attachment ?
619 stencil_attachment->stencilLayout :
620 desc->pDepthStencilAttachment->layout),
621 };
622 }
623
624 const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
625 vk_find_struct_const(desc->pNext,
626 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
627
628 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
629 subpass->ds_resolve_attachment = p++;
630
631 const VkAttachmentReferenceStencilLayoutKHR *stencil_resolve_attachment =
632 vk_find_struct_const(ds_resolve->pDepthStencilResolveAttachment->pNext,
633 ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
634
635 *subpass->ds_resolve_attachment = (struct radv_subpass_attachment) {
636 .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
637 .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
638 .stencil_layout = (stencil_resolve_attachment ?
639 stencil_resolve_attachment->stencilLayout :
640 ds_resolve->pDepthStencilResolveAttachment->layout),
641 };
642
643 subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
644 subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
645 }
646 }
647
648 bool has_ingoing_dep = false;
649 bool has_outgoing_dep = false;
650
651 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
652 radv_render_pass_add_subpass_dep(pass,
653 &pCreateInfo->pDependencies[i]);
654
655 /* Determine if the subpass has explicit dependencies from/to
656 * VK_SUBPASS_EXTERNAL.
657 */
658 if (pCreateInfo->pDependencies[i].srcSubpass == VK_SUBPASS_EXTERNAL)
659 has_ingoing_dep = true;
660 if (pCreateInfo->pDependencies[i].dstSubpass == VK_SUBPASS_EXTERNAL)
661 has_outgoing_dep = true;
662 }
663
664 radv_render_pass_add_implicit_deps(pass,
665 has_ingoing_dep, has_outgoing_dep);
666
667 radv_render_pass_compile(pass);
668
669 *pRenderPass = radv_render_pass_to_handle(pass);
670
671 return VK_SUCCESS;
672 }
673
674 void radv_DestroyRenderPass(
675 VkDevice _device,
676 VkRenderPass _pass,
677 const VkAllocationCallbacks* pAllocator)
678 {
679 RADV_FROM_HANDLE(radv_device, device, _device);
680 RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
681
682 if (!_pass)
683 return;
684 vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
685 vk_free2(&device->vk.alloc, pAllocator, pass);
686 }
687
688 void radv_GetRenderAreaGranularity(
689 VkDevice device,
690 VkRenderPass renderPass,
691 VkExtent2D* pGranularity)
692 {
693 pGranularity->width = 1;
694 pGranularity->height = 1;
695 }
696