2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "anv_private.h"
29 anv_render_pass_add_subpass_dep(struct anv_render_pass *pass,
30 const VkSubpassDependency2KHR *dep)
32 if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
33 pass->subpass_flushes[pass->subpass_count] |=
34 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
36 assert(dep->dstSubpass < pass->subpass_count);
37 pass->subpass_flushes[dep->dstSubpass] |=
38 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
41 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
42 pass->subpass_flushes[0] |=
43 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
45 assert(dep->srcSubpass < pass->subpass_count);
46 pass->subpass_flushes[dep->srcSubpass + 1] |=
47 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
51 /* Do a second "compile" step on a render pass */
53 anv_render_pass_compile(struct anv_render_pass *pass)
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
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;
65 VkImageUsageFlags all_usage = 0;
66 for (uint32_t i = 0; i < pass->subpass_count; i++) {
67 struct anv_subpass *subpass = &pass->subpasses[i];
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.
73 if (subpass->depth_stencil_attachment &&
74 subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
75 subpass->depth_stencil_attachment = NULL;
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)
82 struct anv_render_pass_attachment *pass_att =
83 &pass->attachments[subpass_att->attachment];
85 assert(__builtin_popcount(subpass_att->usage) == 1);
86 pass_att->usage |= subpass_att->usage;
87 pass_att->last_subpass_idx = i;
89 all_usage |= subpass_att->usage;
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);
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;
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)
113 subpass->has_resolve = true;
115 assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
116 color_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
121 /* From the Vulkan 1.0.39 spec:
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:
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;
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:
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;
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.
168 * From the documentation for vkCmdNextSubpass:
170 * "Moving to the next subpass automatically performs any multisample
171 * resolve operations in the subpass being ended. End-of-subpass
172 * multisample resolves are treated as color attachment writes for the
173 * purposes of synchronization. This applies to resolve operations for
174 * both color and depth/stencil attachments. That is, they are
175 * considered to execute in the
176 * VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT pipeline stage and
177 * their writes are synchronized with
178 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT."
180 * Therefore, the above flags concerning color attachments also apply to
181 * color and depth/stencil resolve attachments.
183 if (all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
184 pass->subpass_flushes[0] |=
185 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
187 if (all_usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
188 VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
189 pass->subpass_flushes[pass->subpass_count] |=
190 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
192 if (all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
193 pass->subpass_flushes[pass->subpass_count] |=
194 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
199 num_subpass_attachments(const VkSubpassDescription *desc)
201 return desc->inputAttachmentCount +
202 desc->colorAttachmentCount +
203 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
204 (desc->pDepthStencilAttachment != NULL);
207 VkResult anv_CreateRenderPass(
209 const VkRenderPassCreateInfo* pCreateInfo,
210 const VkAllocationCallbacks* pAllocator,
211 VkRenderPass* pRenderPass)
213 ANV_FROM_HANDLE(anv_device, device, _device);
215 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
217 struct anv_render_pass *pass;
218 struct anv_subpass *subpasses;
219 struct anv_render_pass_attachment *attachments;
220 enum anv_pipe_bits *subpass_flushes;
223 anv_multialloc_add(&ma, &pass, 1);
224 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
225 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
226 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
228 struct anv_subpass_attachment *subpass_attachments;
229 uint32_t subpass_attachment_count = 0;
230 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
231 subpass_attachment_count +=
232 num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
234 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
236 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
237 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
238 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
240 /* Clear the subpasses along with the parent pass. This required because
241 * each array member of anv_subpass must be a valid pointer if not NULL.
243 memset(pass, 0, ma.size);
244 pass->attachment_count = pCreateInfo->attachmentCount;
245 pass->subpass_count = pCreateInfo->subpassCount;
246 pass->attachments = attachments;
247 pass->subpass_flushes = subpass_flushes;
249 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
250 pass->attachments[i] = (struct anv_render_pass_attachment) {
251 .format = pCreateInfo->pAttachments[i].format,
252 .samples = pCreateInfo->pAttachments[i].samples,
253 .load_op = pCreateInfo->pAttachments[i].loadOp,
254 .store_op = pCreateInfo->pAttachments[i].storeOp,
255 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
256 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
257 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
261 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
262 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
263 struct anv_subpass *subpass = &pass->subpasses[i];
265 subpass->input_count = desc->inputAttachmentCount;
266 subpass->color_count = desc->colorAttachmentCount;
267 subpass->attachment_count = num_subpass_attachments(desc);
268 subpass->attachments = subpass_attachments;
269 subpass->view_mask = 0;
271 if (desc->inputAttachmentCount > 0) {
272 subpass->input_attachments = subpass_attachments;
273 subpass_attachments += desc->inputAttachmentCount;
275 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
276 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
277 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
278 .attachment = desc->pInputAttachments[j].attachment,
279 .layout = desc->pInputAttachments[j].layout,
284 if (desc->colorAttachmentCount > 0) {
285 subpass->color_attachments = subpass_attachments;
286 subpass_attachments += desc->colorAttachmentCount;
288 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
289 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
290 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
291 .attachment = desc->pColorAttachments[j].attachment,
292 .layout = desc->pColorAttachments[j].layout,
297 if (desc->pResolveAttachments) {
298 subpass->resolve_attachments = subpass_attachments;
299 subpass_attachments += desc->colorAttachmentCount;
301 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
302 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
303 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
304 .attachment = desc->pResolveAttachments[j].attachment,
305 .layout = desc->pResolveAttachments[j].layout,
310 if (desc->pDepthStencilAttachment) {
311 subpass->depth_stencil_attachment = subpass_attachments++;
313 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
314 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
315 .attachment = desc->pDepthStencilAttachment->attachment,
316 .layout = desc->pDepthStencilAttachment->layout,
321 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
322 /* Convert to a Dependency2KHR */
323 struct VkSubpassDependency2KHR dep2 = {
324 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
325 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
326 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
327 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
328 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
329 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
330 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
332 anv_render_pass_add_subpass_dep(pass, &dep2);
335 vk_foreach_struct(ext, pCreateInfo->pNext) {
336 switch (ext->sType) {
337 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHR: {
338 VkRenderPassMultiviewCreateInfoKHR *mv = (void *)ext;
340 for (uint32_t i = 0; i < mv->subpassCount; i++) {
341 pass->subpasses[i].view_mask = mv->pViewMasks[i];
347 anv_debug_ignored_stype(ext->sType);
351 anv_render_pass_compile(pass);
353 *pRenderPass = anv_render_pass_to_handle(pass);
359 num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
361 return desc->inputAttachmentCount +
362 desc->colorAttachmentCount +
363 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
364 (desc->pDepthStencilAttachment != NULL);
367 VkResult anv_CreateRenderPass2KHR(
369 const VkRenderPassCreateInfo2KHR* pCreateInfo,
370 const VkAllocationCallbacks* pAllocator,
371 VkRenderPass* pRenderPass)
373 ANV_FROM_HANDLE(anv_device, device, _device);
375 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
377 struct anv_render_pass *pass;
378 struct anv_subpass *subpasses;
379 struct anv_render_pass_attachment *attachments;
380 enum anv_pipe_bits *subpass_flushes;
383 anv_multialloc_add(&ma, &pass, 1);
384 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
385 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
386 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
388 struct anv_subpass_attachment *subpass_attachments;
389 uint32_t subpass_attachment_count = 0;
390 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
391 subpass_attachment_count +=
392 num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
394 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
396 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
397 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
398 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
400 /* Clear the subpasses along with the parent pass. This required because
401 * each array member of anv_subpass must be a valid pointer if not NULL.
403 memset(pass, 0, ma.size);
404 pass->attachment_count = pCreateInfo->attachmentCount;
405 pass->subpass_count = pCreateInfo->subpassCount;
406 pass->attachments = attachments;
407 pass->subpass_flushes = subpass_flushes;
409 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
410 pass->attachments[i] = (struct anv_render_pass_attachment) {
411 .format = pCreateInfo->pAttachments[i].format,
412 .samples = pCreateInfo->pAttachments[i].samples,
413 .load_op = pCreateInfo->pAttachments[i].loadOp,
414 .store_op = pCreateInfo->pAttachments[i].storeOp,
415 .stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
416 .initial_layout = pCreateInfo->pAttachments[i].initialLayout,
417 .final_layout = pCreateInfo->pAttachments[i].finalLayout,
421 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
422 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
423 struct anv_subpass *subpass = &pass->subpasses[i];
425 subpass->input_count = desc->inputAttachmentCount;
426 subpass->color_count = desc->colorAttachmentCount;
427 subpass->attachment_count = num_subpass_attachments2(desc);
428 subpass->attachments = subpass_attachments;
429 subpass->view_mask = desc->viewMask;
431 if (desc->inputAttachmentCount > 0) {
432 subpass->input_attachments = subpass_attachments;
433 subpass_attachments += desc->inputAttachmentCount;
435 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
436 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
437 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
438 .attachment = desc->pInputAttachments[j].attachment,
439 .layout = desc->pInputAttachments[j].layout,
444 if (desc->colorAttachmentCount > 0) {
445 subpass->color_attachments = subpass_attachments;
446 subpass_attachments += desc->colorAttachmentCount;
448 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
449 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
450 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
451 .attachment = desc->pColorAttachments[j].attachment,
452 .layout = desc->pColorAttachments[j].layout,
457 if (desc->pResolveAttachments) {
458 subpass->resolve_attachments = subpass_attachments;
459 subpass_attachments += desc->colorAttachmentCount;
461 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
462 subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
463 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
464 .attachment = desc->pResolveAttachments[j].attachment,
465 .layout = desc->pResolveAttachments[j].layout,
470 if (desc->pDepthStencilAttachment) {
471 subpass->depth_stencil_attachment = subpass_attachments++;
473 *subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
474 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
475 .attachment = desc->pDepthStencilAttachment->attachment,
476 .layout = desc->pDepthStencilAttachment->layout,
481 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++)
482 anv_render_pass_add_subpass_dep(pass, &pCreateInfo->pDependencies[i]);
484 vk_foreach_struct(ext, pCreateInfo->pNext) {
485 switch (ext->sType) {
487 anv_debug_ignored_stype(ext->sType);
491 anv_render_pass_compile(pass);
493 *pRenderPass = anv_render_pass_to_handle(pass);
498 void anv_DestroyRenderPass(
501 const VkAllocationCallbacks* pAllocator)
503 ANV_FROM_HANDLE(anv_device, device, _device);
504 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
506 vk_free2(&device->alloc, pAllocator, pass);
509 void anv_GetRenderAreaGranularity(
511 VkRenderPass renderPass,
512 VkExtent2D* pGranularity)
514 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
516 /* This granularity satisfies HiZ fast clear alignment requirements
517 * for all sample counts.
519 for (unsigned i = 0; i < pass->subpass_count; ++i) {
520 if (pass->subpasses[i].depth_stencil_attachment) {
521 *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
526 *pGranularity = (VkExtent2D) { 1, 1 };