radv: gather layer in the shader info pass
[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 VkSubpassDependency2KHR *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 void
63 radv_render_pass_compile(struct radv_render_pass *pass)
64 {
65 for (uint32_t i = 0; i < pass->subpass_count; i++) {
66 struct radv_subpass *subpass = &pass->subpasses[i];
67
68 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
69 struct radv_subpass_attachment *subpass_att =
70 &subpass->attachments[j];
71 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
72 continue;
73
74 struct radv_render_pass_attachment *pass_att =
75 &pass->attachments[subpass_att->attachment];
76
77 pass_att->first_subpass_idx = UINT32_MAX;
78 }
79 }
80
81 for (uint32_t i = 0; i < pass->subpass_count; i++) {
82 struct radv_subpass *subpass = &pass->subpasses[i];
83 uint32_t color_sample_count = 1, depth_sample_count = 1;
84
85 /* We don't allow depth_stencil_attachment to be non-NULL and
86 * be VK_ATTACHMENT_UNUSED. This way something can just check
87 * for NULL and be guaranteed that they have a valid
88 * attachment.
89 */
90 if (subpass->depth_stencil_attachment &&
91 subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
92 subpass->depth_stencil_attachment = NULL;
93
94 if (subpass->ds_resolve_attachment &&
95 subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
96 subpass->ds_resolve_attachment = NULL;
97
98 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
99 struct radv_subpass_attachment *subpass_att =
100 &subpass->attachments[j];
101 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
102 continue;
103
104 struct radv_render_pass_attachment *pass_att =
105 &pass->attachments[subpass_att->attachment];
106
107 if (i < pass_att->first_subpass_idx)
108 pass_att->first_subpass_idx = i;
109 pass_att->last_subpass_idx = i;
110 }
111
112 subpass->has_color_att = false;
113 for (uint32_t j = 0; j < subpass->color_count; j++) {
114 struct radv_subpass_attachment *subpass_att =
115 &subpass->color_attachments[j];
116 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
117 continue;
118
119 subpass->has_color_att = true;
120
121 struct radv_render_pass_attachment *pass_att =
122 &pass->attachments[subpass_att->attachment];
123
124 color_sample_count = pass_att->samples;
125 }
126
127 if (subpass->depth_stencil_attachment) {
128 const uint32_t a =
129 subpass->depth_stencil_attachment->attachment;
130 struct radv_render_pass_attachment *pass_att =
131 &pass->attachments[a];
132 depth_sample_count = pass_att->samples;
133 }
134
135 subpass->max_sample_count = MAX2(color_sample_count,
136 depth_sample_count);
137
138 /* We have to handle resolve attachments specially */
139 subpass->has_color_resolve = false;
140 if (subpass->resolve_attachments) {
141 for (uint32_t j = 0; j < subpass->color_count; j++) {
142 struct radv_subpass_attachment *resolve_att =
143 &subpass->resolve_attachments[j];
144
145 if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
146 continue;
147
148 subpass->has_color_resolve = true;
149 }
150 }
151
152 for (uint32_t j = 0; j < subpass->input_count; ++j) {
153 if (subpass->input_attachments[j].attachment == VK_ATTACHMENT_UNUSED)
154 continue;
155
156 for (uint32_t k = 0; k < subpass->color_count; ++k) {
157 if (subpass->color_attachments[k].attachment == subpass->input_attachments[j].attachment) {
158 subpass->input_attachments[j].in_render_loop = true;
159 subpass->color_attachments[k].in_render_loop = true;
160 }
161 }
162
163 if (subpass->depth_stencil_attachment &&
164 subpass->depth_stencil_attachment->attachment == subpass->input_attachments[j].attachment) {
165 subpass->input_attachments[j].in_render_loop = true;
166 subpass->depth_stencil_attachment->in_render_loop = true;
167 }
168 }
169 }
170 }
171
172 static unsigned
173 radv_num_subpass_attachments(const VkSubpassDescription *desc)
174 {
175 return desc->inputAttachmentCount +
176 desc->colorAttachmentCount +
177 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
178 (desc->pDepthStencilAttachment != NULL);
179 }
180
181 VkResult radv_CreateRenderPass(
182 VkDevice _device,
183 const VkRenderPassCreateInfo* pCreateInfo,
184 const VkAllocationCallbacks* pAllocator,
185 VkRenderPass* pRenderPass)
186 {
187 RADV_FROM_HANDLE(radv_device, device, _device);
188 struct radv_render_pass *pass;
189 size_t size;
190 size_t attachments_offset;
191 VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
192
193 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
194
195 size = sizeof(*pass);
196 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
197 attachments_offset = size;
198 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
199
200 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
201 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
202 if (pass == NULL)
203 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
204
205 memset(pass, 0, size);
206 pass->attachment_count = pCreateInfo->attachmentCount;
207 pass->subpass_count = pCreateInfo->subpassCount;
208 pass->attachments = (void *) pass + attachments_offset;
209
210 vk_foreach_struct(ext, pCreateInfo->pNext) {
211 switch(ext->sType) {
212 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
213 multiview_info = (VkRenderPassMultiviewCreateInfo*)ext;
214 break;
215 default:
216 break;
217 }
218 }
219
220 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
221 struct radv_render_pass_attachment *att = &pass->attachments[i];
222
223 att->format = pCreateInfo->pAttachments[i].format;
224 att->samples = pCreateInfo->pAttachments[i].samples;
225 att->load_op = pCreateInfo->pAttachments[i].loadOp;
226 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
227 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
228 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
229 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
230 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
231 }
232 uint32_t subpass_attachment_count = 0;
233 struct radv_subpass_attachment *p;
234 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
235 subpass_attachment_count +=
236 radv_num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
237 }
238
239 if (subpass_attachment_count) {
240 pass->subpass_attachments =
241 vk_alloc2(&device->alloc, pAllocator,
242 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
243 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
244 if (pass->subpass_attachments == NULL) {
245 vk_free2(&device->alloc, pAllocator, pass);
246 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
247 }
248 } else
249 pass->subpass_attachments = NULL;
250
251 p = pass->subpass_attachments;
252 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
253 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
254 struct radv_subpass *subpass = &pass->subpasses[i];
255
256 subpass->input_count = desc->inputAttachmentCount;
257 subpass->color_count = desc->colorAttachmentCount;
258 subpass->attachment_count = radv_num_subpass_attachments(desc);
259 subpass->attachments = p;
260
261 if (multiview_info)
262 subpass->view_mask = multiview_info->pViewMasks[i];
263
264 if (desc->inputAttachmentCount > 0) {
265 subpass->input_attachments = p;
266 p += desc->inputAttachmentCount;
267
268 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
269 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
270 .attachment = desc->pInputAttachments[j].attachment,
271 .layout = desc->pInputAttachments[j].layout,
272 };
273 }
274 }
275
276 if (desc->colorAttachmentCount > 0) {
277 subpass->color_attachments = p;
278 p += desc->colorAttachmentCount;
279
280 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
281 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
282 .attachment = desc->pColorAttachments[j].attachment,
283 .layout = desc->pColorAttachments[j].layout,
284 };
285 }
286 }
287
288 if (desc->pResolveAttachments) {
289 subpass->resolve_attachments = p;
290 p += desc->colorAttachmentCount;
291
292 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
293 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
294 .attachment = desc->pResolveAttachments[j].attachment,
295 .layout = desc->pResolveAttachments[j].layout,
296 };
297 }
298 }
299
300 if (desc->pDepthStencilAttachment) {
301 subpass->depth_stencil_attachment = p++;
302
303 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
304 .attachment = desc->pDepthStencilAttachment->attachment,
305 .layout = desc->pDepthStencilAttachment->layout,
306 };
307 }
308 }
309
310 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
311 /* Convert to a Dependency2KHR */
312 struct VkSubpassDependency2KHR dep2 = {
313 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
314 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
315 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
316 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
317 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
318 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
319 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
320 };
321 radv_render_pass_add_subpass_dep(pass, &dep2);
322 }
323
324 radv_render_pass_compile(pass);
325
326 *pRenderPass = radv_render_pass_to_handle(pass);
327
328 return VK_SUCCESS;
329 }
330
331 static unsigned
332 radv_num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
333 {
334 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
335 vk_find_struct_const(desc->pNext,
336 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
337
338 return desc->inputAttachmentCount +
339 desc->colorAttachmentCount +
340 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
341 (desc->pDepthStencilAttachment != NULL) +
342 (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
343 }
344
345 VkResult radv_CreateRenderPass2KHR(
346 VkDevice _device,
347 const VkRenderPassCreateInfo2KHR* pCreateInfo,
348 const VkAllocationCallbacks* pAllocator,
349 VkRenderPass* pRenderPass)
350 {
351 RADV_FROM_HANDLE(radv_device, device, _device);
352 struct radv_render_pass *pass;
353 size_t size;
354 size_t attachments_offset;
355
356 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
357
358 size = sizeof(*pass);
359 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
360 attachments_offset = size;
361 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
362
363 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
364 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
365 if (pass == NULL)
366 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
367
368 memset(pass, 0, size);
369 pass->attachment_count = pCreateInfo->attachmentCount;
370 pass->subpass_count = pCreateInfo->subpassCount;
371 pass->attachments = (void *) pass + attachments_offset;
372
373 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
374 struct radv_render_pass_attachment *att = &pass->attachments[i];
375
376 att->format = pCreateInfo->pAttachments[i].format;
377 att->samples = pCreateInfo->pAttachments[i].samples;
378 att->load_op = pCreateInfo->pAttachments[i].loadOp;
379 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
380 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
381 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
382 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
383 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
384 }
385 uint32_t subpass_attachment_count = 0;
386 struct radv_subpass_attachment *p;
387 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
388 subpass_attachment_count +=
389 radv_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
390 }
391
392 if (subpass_attachment_count) {
393 pass->subpass_attachments =
394 vk_alloc2(&device->alloc, pAllocator,
395 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
396 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
397 if (pass->subpass_attachments == NULL) {
398 vk_free2(&device->alloc, pAllocator, pass);
399 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
400 }
401 } else
402 pass->subpass_attachments = NULL;
403
404 p = pass->subpass_attachments;
405 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
406 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
407 struct radv_subpass *subpass = &pass->subpasses[i];
408
409 subpass->input_count = desc->inputAttachmentCount;
410 subpass->color_count = desc->colorAttachmentCount;
411 subpass->attachment_count = radv_num_subpass_attachments2(desc);
412 subpass->attachments = p;
413 subpass->view_mask = desc->viewMask;
414
415 if (desc->inputAttachmentCount > 0) {
416 subpass->input_attachments = p;
417 p += desc->inputAttachmentCount;
418
419 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
420 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
421 .attachment = desc->pInputAttachments[j].attachment,
422 .layout = desc->pInputAttachments[j].layout,
423 };
424 }
425 }
426
427 if (desc->colorAttachmentCount > 0) {
428 subpass->color_attachments = p;
429 p += desc->colorAttachmentCount;
430
431 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
432 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
433 .attachment = desc->pColorAttachments[j].attachment,
434 .layout = desc->pColorAttachments[j].layout,
435 };
436 }
437 }
438
439 if (desc->pResolveAttachments) {
440 subpass->resolve_attachments = p;
441 p += desc->colorAttachmentCount;
442
443 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
444 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
445 .attachment = desc->pResolveAttachments[j].attachment,
446 .layout = desc->pResolveAttachments[j].layout,
447 };
448 }
449 }
450
451 if (desc->pDepthStencilAttachment) {
452 subpass->depth_stencil_attachment = p++;
453
454 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
455 .attachment = desc->pDepthStencilAttachment->attachment,
456 .layout = desc->pDepthStencilAttachment->layout,
457 };
458 }
459
460 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
461 vk_find_struct_const(desc->pNext,
462 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
463
464 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
465 subpass->ds_resolve_attachment = p++;
466
467 *subpass->ds_resolve_attachment = (struct radv_subpass_attachment) {
468 .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
469 .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
470 };
471
472 subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
473 subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
474 }
475 }
476
477 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
478 radv_render_pass_add_subpass_dep(pass,
479 &pCreateInfo->pDependencies[i]);
480 }
481
482 radv_render_pass_compile(pass);
483
484 *pRenderPass = radv_render_pass_to_handle(pass);
485
486 return VK_SUCCESS;
487 }
488
489 void radv_DestroyRenderPass(
490 VkDevice _device,
491 VkRenderPass _pass,
492 const VkAllocationCallbacks* pAllocator)
493 {
494 RADV_FROM_HANDLE(radv_device, device, _device);
495 RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
496
497 if (!_pass)
498 return;
499 vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
500 vk_free2(&device->alloc, pAllocator, pass);
501 }
502
503 void radv_GetRenderAreaGranularity(
504 VkDevice device,
505 VkRenderPass renderPass,
506 VkExtent2D* pGranularity)
507 {
508 pGranularity->width = 1;
509 pGranularity->height = 1;
510 }
511