anv: Add support for VK_KHR_create_renderpass2
[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 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
78 struct anv_subpass_attachment *subpass_att = &subpass->attachments[j];
79 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
80 continue;
81
82 struct anv_render_pass_attachment *pass_att =
83 &pass->attachments[subpass_att->attachment];
84
85 assert(__builtin_popcount(subpass_att->usage) == 1);
86 pass_att->usage |= subpass_att->usage;
87 pass_att->last_subpass_idx = i;
88
89 all_usage |= subpass_att->usage;
90
91 if (pass_att->first_subpass_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
92 pass_att->first_subpass_layout = subpass_att->layout;
93 assert(pass_att->first_subpass_layout != VK_IMAGE_LAYOUT_UNDEFINED);
94 }
95
96 if (subpass_att->usage == VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
97 subpass->depth_stencil_attachment &&
98 subpass_att->attachment == subpass->depth_stencil_attachment->attachment)
99 subpass->has_ds_self_dep = true;
100 }
101
102 /* We have to handle resolve attachments specially */
103 subpass->has_resolve = false;
104 if (subpass->resolve_attachments) {
105 for (uint32_t j = 0; j < subpass->color_count; j++) {
106 struct anv_subpass_attachment *color_att =
107 &subpass->color_attachments[j];
108 struct anv_subpass_attachment *resolve_att =
109 &subpass->resolve_attachments[j];
110 if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
111 continue;
112
113 subpass->has_resolve = true;
114
115 assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
116 color_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
117 }
118 }
119 }
120
121 /* From the Vulkan 1.0.39 spec:
122 *
123 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
124 * first subpass that uses an attachment, then an implicit subpass
125 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
126 * used in. The subpass dependency operates as if defined with the
127 * following parameters:
128 *
129 * VkSubpassDependency implicitDependency = {
130 * .srcSubpass = VK_SUBPASS_EXTERNAL;
131 * .dstSubpass = firstSubpass; // First subpass attachment is used in
132 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
133 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
134 * .srcAccessMask = 0;
135 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
136 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
137 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
138 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
139 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
140 * .dependencyFlags = 0;
141 * };
142 *
143 * Similarly, if there is no subpass dependency from the last subpass
144 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
145 * subpass dependency exists from the last subpass it is used in to
146 * VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
147 * with the following parameters:
148 *
149 * VkSubpassDependency implicitDependency = {
150 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
151 * .dstSubpass = VK_SUBPASS_EXTERNAL;
152 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
153 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
154 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
155 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
156 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
157 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
158 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
159 * .dstAccessMask = 0;
160 * .dependencyFlags = 0;
161 * };
162 *
163 * We could implement this by walking over all of the attachments and
164 * subpasses and checking to see if any of them don't have an external
165 * dependency. Or, we could just be lazy and add a couple extra flushes.
166 * We choose to be lazy.
167 */
168 if (all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
169 pass->subpass_flushes[0] |=
170 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
171 }
172 if (all_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
173 pass->subpass_flushes[pass->subpass_count] |=
174 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
175 }
176 if (all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
177 pass->subpass_flushes[pass->subpass_count] |=
178 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
179 }
180 }
181
182 static unsigned
183 num_subpass_attachments(const VkSubpassDescription *desc)
184 {
185 return desc->inputAttachmentCount +
186 desc->colorAttachmentCount +
187 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
188 (desc->pDepthStencilAttachment != NULL);
189 }
190
191 VkResult anv_CreateRenderPass(
192 VkDevice _device,
193 const VkRenderPassCreateInfo* pCreateInfo,
194 const VkAllocationCallbacks* pAllocator,
195 VkRenderPass* pRenderPass)
196 {
197 ANV_FROM_HANDLE(anv_device, device, _device);
198
199 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
200
201 struct anv_render_pass *pass;
202 struct anv_subpass *subpasses;
203 struct anv_render_pass_attachment *attachments;
204 enum anv_pipe_bits *subpass_flushes;
205
206 ANV_MULTIALLOC(ma);
207 anv_multialloc_add(&ma, &pass, 1);
208 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
209 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
210 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
211
212 struct anv_subpass_attachment *subpass_attachments;
213 uint32_t subpass_attachment_count = 0;
214 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
215 subpass_attachment_count +=
216 num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
217 }
218 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
219
220 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
221 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
222 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
223
224 /* Clear the subpasses along with the parent pass. This required because
225 * each array member of anv_subpass must be a valid pointer if not NULL.
226 */
227 memset(pass, 0, ma.size);
228 pass->attachment_count = pCreateInfo->attachmentCount;
229 pass->subpass_count = pCreateInfo->subpassCount;
230 pass->attachments = attachments;
231 pass->subpass_flushes = subpass_flushes;
232
233 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
234 pass->attachments[i] = (struct anv_render_pass_attachment) {
235 .format = pCreateInfo->pAttachments[i].format,
236 .samples = pCreateInfo->pAttachments[i].samples,
237 .load_op = pCreateInfo->pAttachments[i].loadOp,
238 .store_op = pCreateInfo->pAttachments[i].storeOp,
239 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
240 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
241 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
242 };
243 }
244
245 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
246 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
247 struct anv_subpass *subpass = &pass->subpasses[i];
248
249 subpass->input_count = desc->inputAttachmentCount;
250 subpass->color_count = desc->colorAttachmentCount;
251 subpass->attachment_count = num_subpass_attachments(desc);
252 subpass->attachments = subpass_attachments;
253 subpass->view_mask = 0;
254
255 if (desc->inputAttachmentCount > 0) {
256 subpass->input_attachments = subpass_attachments;
257 subpass_attachments += desc->inputAttachmentCount;
258
259 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
260 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
261 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
262 .attachment = desc->pInputAttachments[j].attachment,
263 .layout = desc->pInputAttachments[j].layout,
264 };
265 }
266 }
267
268 if (desc->colorAttachmentCount > 0) {
269 subpass->color_attachments = subpass_attachments;
270 subpass_attachments += desc->colorAttachmentCount;
271
272 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
273 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
274 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
275 .attachment = desc->pColorAttachments[j].attachment,
276 .layout = desc->pColorAttachments[j].layout,
277 };
278 }
279 }
280
281 if (desc->pResolveAttachments) {
282 subpass->resolve_attachments = subpass_attachments;
283 subpass_attachments += desc->colorAttachmentCount;
284
285 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
286 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
287 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
288 .attachment = desc->pResolveAttachments[j].attachment,
289 .layout = desc->pResolveAttachments[j].layout,
290 };
291 }
292 }
293
294 if (desc->pDepthStencilAttachment) {
295 subpass->depth_stencil_attachment = subpass_attachments++;
296
297 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
298 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
299 .attachment = desc->pDepthStencilAttachment->attachment,
300 .layout = desc->pDepthStencilAttachment->layout,
301 };
302 }
303 }
304
305 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
306 /* Convert to a Dependency2KHR */
307 struct VkSubpassDependency2KHR dep2 = {
308 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
309 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
310 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
311 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
312 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
313 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
314 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
315 };
316 anv_render_pass_add_subpass_dep(pass, &dep2);
317 }
318
319 vk_foreach_struct(ext, pCreateInfo->pNext) {
320 switch (ext->sType) {
321 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHR: {
322 VkRenderPassMultiviewCreateInfoKHR *mv = (void *)ext;
323
324 for (uint32_t i = 0; i < mv->subpassCount; i++) {
325 pass->subpasses[i].view_mask = mv->pViewMasks[i];
326 }
327 break;
328 }
329
330 default:
331 anv_debug_ignored_stype(ext->sType);
332 }
333 }
334
335 anv_render_pass_compile(pass);
336
337 *pRenderPass = anv_render_pass_to_handle(pass);
338
339 return VK_SUCCESS;
340 }
341
342 static unsigned
343 num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
344 {
345 return desc->inputAttachmentCount +
346 desc->colorAttachmentCount +
347 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
348 (desc->pDepthStencilAttachment != NULL);
349 }
350
351 VkResult anv_CreateRenderPass2KHR(
352 VkDevice _device,
353 const VkRenderPassCreateInfo2KHR* pCreateInfo,
354 const VkAllocationCallbacks* pAllocator,
355 VkRenderPass* pRenderPass)
356 {
357 ANV_FROM_HANDLE(anv_device, device, _device);
358
359 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
360
361 struct anv_render_pass *pass;
362 struct anv_subpass *subpasses;
363 struct anv_render_pass_attachment *attachments;
364 enum anv_pipe_bits *subpass_flushes;
365
366 ANV_MULTIALLOC(ma);
367 anv_multialloc_add(&ma, &pass, 1);
368 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
369 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
370 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
371
372 struct anv_subpass_attachment *subpass_attachments;
373 uint32_t subpass_attachment_count = 0;
374 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
375 subpass_attachment_count +=
376 num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
377 }
378 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
379
380 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
381 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
382 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
383
384 /* Clear the subpasses along with the parent pass. This required because
385 * each array member of anv_subpass must be a valid pointer if not NULL.
386 */
387 memset(pass, 0, ma.size);
388 pass->attachment_count = pCreateInfo->attachmentCount;
389 pass->subpass_count = pCreateInfo->subpassCount;
390 pass->attachments = attachments;
391 pass->subpass_flushes = subpass_flushes;
392
393 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
394 pass->attachments[i] = (struct anv_render_pass_attachment) {
395 .format = pCreateInfo->pAttachments[i].format,
396 .samples = pCreateInfo->pAttachments[i].samples,
397 .load_op = pCreateInfo->pAttachments[i].loadOp,
398 .store_op = pCreateInfo->pAttachments[i].storeOp,
399 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
400 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
401 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
402 };
403 }
404
405 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
406 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
407 struct anv_subpass *subpass = &pass->subpasses[i];
408
409 subpass->input_count = desc->inputAttachmentCount;
410 subpass->color_count = desc->colorAttachmentCount;
411 subpass->attachment_count = num_subpass_attachments2(desc);
412 subpass->attachments = subpass_attachments;
413 subpass->view_mask = desc->viewMask;
414
415 if (desc->inputAttachmentCount > 0) {
416 subpass->input_attachments = subpass_attachments;
417 subpass_attachments += desc->inputAttachmentCount;
418
419 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
420 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
421 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
422 .attachment = desc->pInputAttachments[j].attachment,
423 .layout = desc->pInputAttachments[j].layout,
424 };
425 }
426 }
427
428 if (desc->colorAttachmentCount > 0) {
429 subpass->color_attachments = subpass_attachments;
430 subpass_attachments += desc->colorAttachmentCount;
431
432 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
433 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
434 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
435 .attachment = desc->pColorAttachments[j].attachment,
436 .layout = desc->pColorAttachments[j].layout,
437 };
438 }
439 }
440
441 if (desc->pResolveAttachments) {
442 subpass->resolve_attachments = subpass_attachments;
443 subpass_attachments += desc->colorAttachmentCount;
444
445 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
446 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
447 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
448 .attachment = desc->pResolveAttachments[j].attachment,
449 .layout = desc->pResolveAttachments[j].layout,
450 };
451 }
452 }
453
454 if (desc->pDepthStencilAttachment) {
455 subpass->depth_stencil_attachment = subpass_attachments++;
456
457 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
458 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
459 .attachment = desc->pDepthStencilAttachment->attachment,
460 .layout = desc->pDepthStencilAttachment->layout,
461 };
462 }
463 }
464
465 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++)
466 anv_render_pass_add_subpass_dep(pass, &pCreateInfo->pDependencies[i]);
467
468 vk_foreach_struct(ext, pCreateInfo->pNext) {
469 switch (ext->sType) {
470 default:
471 anv_debug_ignored_stype(ext->sType);
472 }
473 }
474
475 anv_render_pass_compile(pass);
476
477 *pRenderPass = anv_render_pass_to_handle(pass);
478
479 return VK_SUCCESS;
480 }
481
482 void anv_DestroyRenderPass(
483 VkDevice _device,
484 VkRenderPass _pass,
485 const VkAllocationCallbacks* pAllocator)
486 {
487 ANV_FROM_HANDLE(anv_device, device, _device);
488 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
489
490 vk_free2(&device->alloc, pAllocator, pass);
491 }
492
493 void anv_GetRenderAreaGranularity(
494 VkDevice device,
495 VkRenderPass renderPass,
496 VkExtent2D* pGranularity)
497 {
498 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
499
500 /* This granularity satisfies HiZ fast clear alignment requirements
501 * for all sample counts.
502 */
503 for (unsigned i = 0; i < pass->subpass_count; ++i) {
504 if (pass->subpasses[i].depth_stencil_attachment) {
505 *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
506 return;
507 }
508 }
509
510 *pGranularity = (VkExtent2D) { 1, 1 };
511 }