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 if (all_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
) {
169 pass
->subpass_flushes
[0] |=
170 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
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
;
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
;
183 num_subpass_attachments(const VkSubpassDescription
*desc
)
185 return desc
->inputAttachmentCount
+
186 desc
->colorAttachmentCount
+
187 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
188 (desc
->pDepthStencilAttachment
!= NULL
);
191 VkResult
anv_CreateRenderPass(
193 const VkRenderPassCreateInfo
* pCreateInfo
,
194 const VkAllocationCallbacks
* pAllocator
,
195 VkRenderPass
* pRenderPass
)
197 ANV_FROM_HANDLE(anv_device
, device
, _device
);
199 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
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
;
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);
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
]);
218 anv_multialloc_add(&ma
, &subpass_attachments
, subpass_attachment_count
);
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
);
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.
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
;
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
,
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
];
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;
255 if (desc
->inputAttachmentCount
> 0) {
256 subpass
->input_attachments
= subpass_attachments
;
257 subpass_attachments
+= desc
->inputAttachmentCount
;
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
,
268 if (desc
->colorAttachmentCount
> 0) {
269 subpass
->color_attachments
= subpass_attachments
;
270 subpass_attachments
+= desc
->colorAttachmentCount
;
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
,
281 if (desc
->pResolveAttachments
) {
282 subpass
->resolve_attachments
= subpass_attachments
;
283 subpass_attachments
+= desc
->colorAttachmentCount
;
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
,
294 if (desc
->pDepthStencilAttachment
) {
295 subpass
->depth_stencil_attachment
= subpass_attachments
++;
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
,
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
,
316 anv_render_pass_add_subpass_dep(pass
, &dep2
);
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
;
324 for (uint32_t i
= 0; i
< mv
->subpassCount
; i
++) {
325 pass
->subpasses
[i
].view_mask
= mv
->pViewMasks
[i
];
331 anv_debug_ignored_stype(ext
->sType
);
335 anv_render_pass_compile(pass
);
337 *pRenderPass
= anv_render_pass_to_handle(pass
);
343 num_subpass_attachments2(const VkSubpassDescription2KHR
*desc
)
345 return desc
->inputAttachmentCount
+
346 desc
->colorAttachmentCount
+
347 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
348 (desc
->pDepthStencilAttachment
!= NULL
);
351 VkResult
anv_CreateRenderPass2KHR(
353 const VkRenderPassCreateInfo2KHR
* pCreateInfo
,
354 const VkAllocationCallbacks
* pAllocator
,
355 VkRenderPass
* pRenderPass
)
357 ANV_FROM_HANDLE(anv_device
, device
, _device
);
359 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR
);
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
;
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);
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
]);
378 anv_multialloc_add(&ma
, &subpass_attachments
, subpass_attachment_count
);
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
);
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.
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
;
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
,
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
];
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
;
415 if (desc
->inputAttachmentCount
> 0) {
416 subpass
->input_attachments
= subpass_attachments
;
417 subpass_attachments
+= desc
->inputAttachmentCount
;
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
,
428 if (desc
->colorAttachmentCount
> 0) {
429 subpass
->color_attachments
= subpass_attachments
;
430 subpass_attachments
+= desc
->colorAttachmentCount
;
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
,
441 if (desc
->pResolveAttachments
) {
442 subpass
->resolve_attachments
= subpass_attachments
;
443 subpass_attachments
+= desc
->colorAttachmentCount
;
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
,
454 if (desc
->pDepthStencilAttachment
) {
455 subpass
->depth_stencil_attachment
= subpass_attachments
++;
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
,
465 for (uint32_t i
= 0; i
< pCreateInfo
->dependencyCount
; i
++)
466 anv_render_pass_add_subpass_dep(pass
, &pCreateInfo
->pDependencies
[i
]);
468 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
469 switch (ext
->sType
) {
471 anv_debug_ignored_stype(ext
->sType
);
475 anv_render_pass_compile(pass
);
477 *pRenderPass
= anv_render_pass_to_handle(pass
);
482 void anv_DestroyRenderPass(
485 const VkAllocationCallbacks
* pAllocator
)
487 ANV_FROM_HANDLE(anv_device
, device
, _device
);
488 ANV_FROM_HANDLE(anv_render_pass
, pass
, _pass
);
490 vk_free2(&device
->alloc
, pAllocator
, pass
);
493 void anv_GetRenderAreaGranularity(
495 VkRenderPass renderPass
,
496 VkExtent2D
* pGranularity
)
498 ANV_FROM_HANDLE(anv_render_pass
, pass
, renderPass
);
500 /* This granularity satisfies HiZ fast clear alignment requirements
501 * for all sample counts.
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 };
510 *pGranularity
= (VkExtent2D
) { 1, 1 };