radv: Fix descriptor set allocation failure.
[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 }
153
154 static unsigned
155 radv_num_subpass_attachments(const VkSubpassDescription *desc)
156 {
157 return desc->inputAttachmentCount +
158 desc->colorAttachmentCount +
159 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
160 (desc->pDepthStencilAttachment != NULL);
161 }
162
163 VkResult radv_CreateRenderPass(
164 VkDevice _device,
165 const VkRenderPassCreateInfo* pCreateInfo,
166 const VkAllocationCallbacks* pAllocator,
167 VkRenderPass* pRenderPass)
168 {
169 RADV_FROM_HANDLE(radv_device, device, _device);
170 struct radv_render_pass *pass;
171 size_t size;
172 size_t attachments_offset;
173 VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
174
175 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
176
177 size = sizeof(*pass);
178 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
179 attachments_offset = size;
180 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
181
182 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
183 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
184 if (pass == NULL)
185 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
186
187 memset(pass, 0, size);
188 pass->attachment_count = pCreateInfo->attachmentCount;
189 pass->subpass_count = pCreateInfo->subpassCount;
190 pass->attachments = (void *) pass + attachments_offset;
191
192 vk_foreach_struct(ext, pCreateInfo->pNext) {
193 switch(ext->sType) {
194 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
195 multiview_info = (VkRenderPassMultiviewCreateInfo*)ext;
196 break;
197 default:
198 break;
199 }
200 }
201
202 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
203 struct radv_render_pass_attachment *att = &pass->attachments[i];
204
205 att->format = pCreateInfo->pAttachments[i].format;
206 att->samples = pCreateInfo->pAttachments[i].samples;
207 att->load_op = pCreateInfo->pAttachments[i].loadOp;
208 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
209 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
210 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
211 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
212 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
213 }
214 uint32_t subpass_attachment_count = 0;
215 struct radv_subpass_attachment *p;
216 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
217 subpass_attachment_count +=
218 radv_num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
219 }
220
221 if (subpass_attachment_count) {
222 pass->subpass_attachments =
223 vk_alloc2(&device->alloc, pAllocator,
224 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
225 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
226 if (pass->subpass_attachments == NULL) {
227 vk_free2(&device->alloc, pAllocator, pass);
228 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
229 }
230 } else
231 pass->subpass_attachments = NULL;
232
233 p = pass->subpass_attachments;
234 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
235 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
236 struct radv_subpass *subpass = &pass->subpasses[i];
237
238 subpass->input_count = desc->inputAttachmentCount;
239 subpass->color_count = desc->colorAttachmentCount;
240 subpass->attachment_count = radv_num_subpass_attachments(desc);
241 subpass->attachments = p;
242
243 if (multiview_info)
244 subpass->view_mask = multiview_info->pViewMasks[i];
245
246 if (desc->inputAttachmentCount > 0) {
247 subpass->input_attachments = p;
248 p += desc->inputAttachmentCount;
249
250 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
251 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
252 .attachment = desc->pInputAttachments[j].attachment,
253 .layout = desc->pInputAttachments[j].layout,
254 };
255 }
256 }
257
258 if (desc->colorAttachmentCount > 0) {
259 subpass->color_attachments = p;
260 p += desc->colorAttachmentCount;
261
262 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
263 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
264 .attachment = desc->pColorAttachments[j].attachment,
265 .layout = desc->pColorAttachments[j].layout,
266 };
267 }
268 }
269
270 if (desc->pResolveAttachments) {
271 subpass->resolve_attachments = p;
272 p += desc->colorAttachmentCount;
273
274 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
275 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
276 .attachment = desc->pResolveAttachments[j].attachment,
277 .layout = desc->pResolveAttachments[j].layout,
278 };
279 }
280 }
281
282 if (desc->pDepthStencilAttachment) {
283 subpass->depth_stencil_attachment = p++;
284
285 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
286 .attachment = desc->pDepthStencilAttachment->attachment,
287 .layout = desc->pDepthStencilAttachment->layout,
288 };
289 }
290 }
291
292 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
293 /* Convert to a Dependency2KHR */
294 struct VkSubpassDependency2KHR dep2 = {
295 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
296 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
297 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
298 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
299 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
300 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
301 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
302 };
303 radv_render_pass_add_subpass_dep(pass, &dep2);
304 }
305
306 radv_render_pass_compile(pass);
307
308 *pRenderPass = radv_render_pass_to_handle(pass);
309
310 return VK_SUCCESS;
311 }
312
313 static unsigned
314 radv_num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
315 {
316 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
317 vk_find_struct_const(desc->pNext,
318 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
319
320 return desc->inputAttachmentCount +
321 desc->colorAttachmentCount +
322 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
323 (desc->pDepthStencilAttachment != NULL) +
324 (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
325 }
326
327 VkResult radv_CreateRenderPass2KHR(
328 VkDevice _device,
329 const VkRenderPassCreateInfo2KHR* pCreateInfo,
330 const VkAllocationCallbacks* pAllocator,
331 VkRenderPass* pRenderPass)
332 {
333 RADV_FROM_HANDLE(radv_device, device, _device);
334 struct radv_render_pass *pass;
335 size_t size;
336 size_t attachments_offset;
337
338 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
339
340 size = sizeof(*pass);
341 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
342 attachments_offset = size;
343 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
344
345 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
346 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
347 if (pass == NULL)
348 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
349
350 memset(pass, 0, size);
351 pass->attachment_count = pCreateInfo->attachmentCount;
352 pass->subpass_count = pCreateInfo->subpassCount;
353 pass->attachments = (void *) pass + attachments_offset;
354
355 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
356 struct radv_render_pass_attachment *att = &pass->attachments[i];
357
358 att->format = pCreateInfo->pAttachments[i].format;
359 att->samples = pCreateInfo->pAttachments[i].samples;
360 att->load_op = pCreateInfo->pAttachments[i].loadOp;
361 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
362 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
363 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
364 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
365 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
366 }
367 uint32_t subpass_attachment_count = 0;
368 struct radv_subpass_attachment *p;
369 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
370 subpass_attachment_count +=
371 radv_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
372 }
373
374 if (subpass_attachment_count) {
375 pass->subpass_attachments =
376 vk_alloc2(&device->alloc, pAllocator,
377 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
378 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
379 if (pass->subpass_attachments == NULL) {
380 vk_free2(&device->alloc, pAllocator, pass);
381 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
382 }
383 } else
384 pass->subpass_attachments = NULL;
385
386 p = pass->subpass_attachments;
387 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
388 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
389 struct radv_subpass *subpass = &pass->subpasses[i];
390
391 subpass->input_count = desc->inputAttachmentCount;
392 subpass->color_count = desc->colorAttachmentCount;
393 subpass->attachment_count = radv_num_subpass_attachments2(desc);
394 subpass->attachments = p;
395 subpass->view_mask = desc->viewMask;
396
397 if (desc->inputAttachmentCount > 0) {
398 subpass->input_attachments = p;
399 p += desc->inputAttachmentCount;
400
401 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
402 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
403 .attachment = desc->pInputAttachments[j].attachment,
404 .layout = desc->pInputAttachments[j].layout,
405 };
406 }
407 }
408
409 if (desc->colorAttachmentCount > 0) {
410 subpass->color_attachments = p;
411 p += desc->colorAttachmentCount;
412
413 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
414 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
415 .attachment = desc->pColorAttachments[j].attachment,
416 .layout = desc->pColorAttachments[j].layout,
417 };
418 }
419 }
420
421 if (desc->pResolveAttachments) {
422 subpass->resolve_attachments = p;
423 p += desc->colorAttachmentCount;
424
425 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
426 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
427 .attachment = desc->pResolveAttachments[j].attachment,
428 .layout = desc->pResolveAttachments[j].layout,
429 };
430 }
431 }
432
433 if (desc->pDepthStencilAttachment) {
434 subpass->depth_stencil_attachment = p++;
435
436 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
437 .attachment = desc->pDepthStencilAttachment->attachment,
438 .layout = desc->pDepthStencilAttachment->layout,
439 };
440 }
441
442 const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
443 vk_find_struct_const(desc->pNext,
444 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
445
446 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
447 subpass->ds_resolve_attachment = p++;
448
449 *subpass->ds_resolve_attachment = (struct radv_subpass_attachment) {
450 .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
451 .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
452 };
453
454 subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
455 subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
456 }
457 }
458
459 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
460 radv_render_pass_add_subpass_dep(pass,
461 &pCreateInfo->pDependencies[i]);
462 }
463
464 radv_render_pass_compile(pass);
465
466 *pRenderPass = radv_render_pass_to_handle(pass);
467
468 return VK_SUCCESS;
469 }
470
471 void radv_DestroyRenderPass(
472 VkDevice _device,
473 VkRenderPass _pass,
474 const VkAllocationCallbacks* pAllocator)
475 {
476 RADV_FROM_HANDLE(radv_device, device, _device);
477 RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
478
479 if (!_pass)
480 return;
481 vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
482 vk_free2(&device->alloc, pAllocator, pass);
483 }
484
485 void radv_GetRenderAreaGranularity(
486 VkDevice device,
487 VkRenderPass renderPass,
488 VkExtent2D* pGranularity)
489 {
490 pGranularity->width = 1;
491 pGranularity->height = 1;
492 }
493