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 num_subpass_attachments(const VkSubpassDescription
*desc
)
31 return desc
->inputAttachmentCount
+
32 desc
->colorAttachmentCount
+
33 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
34 (desc
->pDepthStencilAttachment
!= NULL
);
38 init_first_subpass_layout(struct anv_render_pass_attachment
* const att
,
39 const VkAttachmentReference att_ref
)
41 if (att
->first_subpass_layout
== VK_IMAGE_LAYOUT_UNDEFINED
) {
42 att
->first_subpass_layout
= att_ref
.layout
;
43 assert(att
->first_subpass_layout
!= VK_IMAGE_LAYOUT_UNDEFINED
);
47 VkResult
anv_CreateRenderPass(
49 const VkRenderPassCreateInfo
* pCreateInfo
,
50 const VkAllocationCallbacks
* pAllocator
,
51 VkRenderPass
* pRenderPass
)
53 ANV_FROM_HANDLE(anv_device
, device
, _device
);
55 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
57 struct anv_render_pass
*pass
;
58 struct anv_subpass
*subpasses
;
59 struct anv_render_pass_attachment
*attachments
;
60 enum anv_pipe_bits
*subpass_flushes
;
63 anv_multialloc_add(&ma
, &pass
, 1);
64 anv_multialloc_add(&ma
, &subpasses
, pCreateInfo
->subpassCount
);
65 anv_multialloc_add(&ma
, &attachments
, pCreateInfo
->attachmentCount
);
66 anv_multialloc_add(&ma
, &subpass_flushes
, pCreateInfo
->subpassCount
+ 1);
68 VkAttachmentReference
*subpass_attachments
;
69 uint32_t subpass_attachment_count
= 0;
70 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
71 subpass_attachment_count
+=
72 num_subpass_attachments(&pCreateInfo
->pSubpasses
[i
]);
74 anv_multialloc_add(&ma
, &subpass_attachments
, subpass_attachment_count
);
76 enum anv_subpass_usage
*subpass_usages
;
77 anv_multialloc_add(&ma
, &subpass_usages
,
78 pCreateInfo
->subpassCount
* pCreateInfo
->attachmentCount
);
80 if (!anv_multialloc_alloc2(&ma
, &device
->alloc
, pAllocator
,
81 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
))
82 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
84 /* Clear the subpasses along with the parent pass. This required because
85 * each array member of anv_subpass must be a valid pointer if not NULL.
87 memset(pass
, 0, ma
.size
);
88 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
89 pass
->subpass_count
= pCreateInfo
->subpassCount
;
90 pass
->attachments
= attachments
;
91 pass
->subpass_flushes
= subpass_flushes
;
93 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
94 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
96 att
->format
= pCreateInfo
->pAttachments
[i
].format
;
97 att
->samples
= pCreateInfo
->pAttachments
[i
].samples
;
99 att
->load_op
= pCreateInfo
->pAttachments
[i
].loadOp
;
100 att
->store_op
= pCreateInfo
->pAttachments
[i
].storeOp
;
101 att
->stencil_load_op
= pCreateInfo
->pAttachments
[i
].stencilLoadOp
;
102 att
->initial_layout
= pCreateInfo
->pAttachments
[i
].initialLayout
;
103 att
->final_layout
= pCreateInfo
->pAttachments
[i
].finalLayout
;
104 att
->first_subpass_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
105 att
->subpass_usage
= subpass_usages
;
106 subpass_usages
+= pass
->subpass_count
;
109 bool has_color
= false, has_depth
= false, has_input
= false;
110 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
111 const VkSubpassDescription
*desc
= &pCreateInfo
->pSubpasses
[i
];
112 struct anv_subpass
*subpass
= &pass
->subpasses
[i
];
114 subpass
->input_count
= desc
->inputAttachmentCount
;
115 subpass
->color_count
= desc
->colorAttachmentCount
;
116 subpass
->attachment_count
= num_subpass_attachments(desc
);
117 subpass
->attachments
= subpass_attachments
;
118 subpass
->view_mask
= 0;
120 if (desc
->inputAttachmentCount
> 0) {
121 subpass
->input_attachments
= subpass_attachments
;
122 subpass_attachments
+= desc
->inputAttachmentCount
;
124 for (uint32_t j
= 0; j
< desc
->inputAttachmentCount
; j
++) {
125 uint32_t a
= desc
->pInputAttachments
[j
].attachment
;
126 subpass
->input_attachments
[j
] = desc
->pInputAttachments
[j
];
127 if (a
!= VK_ATTACHMENT_UNUSED
) {
129 pass
->attachments
[a
].usage
|= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
;
130 pass
->attachments
[a
].subpass_usage
[i
] |= ANV_SUBPASS_USAGE_INPUT
;
131 pass
->attachments
[a
].last_subpass_idx
= i
;
133 init_first_subpass_layout(&pass
->attachments
[a
],
134 desc
->pInputAttachments
[j
]);
135 if (desc
->pDepthStencilAttachment
&&
136 a
== desc
->pDepthStencilAttachment
->attachment
)
137 subpass
->has_ds_self_dep
= true;
142 if (desc
->colorAttachmentCount
> 0) {
143 subpass
->color_attachments
= subpass_attachments
;
144 subpass_attachments
+= desc
->colorAttachmentCount
;
146 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
147 uint32_t a
= desc
->pColorAttachments
[j
].attachment
;
148 subpass
->color_attachments
[j
] = desc
->pColorAttachments
[j
];
149 if (a
!= VK_ATTACHMENT_UNUSED
) {
151 pass
->attachments
[a
].usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
152 pass
->attachments
[a
].subpass_usage
[i
] |= ANV_SUBPASS_USAGE_DRAW
;
153 pass
->attachments
[a
].last_subpass_idx
= i
;
155 init_first_subpass_layout(&pass
->attachments
[a
],
156 desc
->pColorAttachments
[j
]);
161 subpass
->has_resolve
= false;
162 if (desc
->pResolveAttachments
) {
163 subpass
->resolve_attachments
= subpass_attachments
;
164 subpass_attachments
+= desc
->colorAttachmentCount
;
166 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
167 uint32_t a
= desc
->pResolveAttachments
[j
].attachment
;
168 subpass
->resolve_attachments
[j
] = desc
->pResolveAttachments
[j
];
169 if (a
!= VK_ATTACHMENT_UNUSED
) {
170 subpass
->has_resolve
= true;
171 uint32_t color_att
= desc
->pColorAttachments
[j
].attachment
;
172 pass
->attachments
[color_att
].usage
|=
173 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
174 pass
->attachments
[a
].usage
|= VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
176 pass
->attachments
[color_att
].subpass_usage
[i
] |=
177 ANV_SUBPASS_USAGE_RESOLVE_SRC
;
178 pass
->attachments
[a
].subpass_usage
[i
] |=
179 ANV_SUBPASS_USAGE_RESOLVE_DST
;
180 pass
->attachments
[a
].last_subpass_idx
= i
;
182 init_first_subpass_layout(&pass
->attachments
[a
],
183 desc
->pResolveAttachments
[j
]);
188 if (desc
->pDepthStencilAttachment
) {
189 uint32_t a
= desc
->pDepthStencilAttachment
->attachment
;
190 *subpass_attachments
++ = subpass
->depth_stencil_attachment
=
191 *desc
->pDepthStencilAttachment
;
192 if (a
!= VK_ATTACHMENT_UNUSED
) {
194 pass
->attachments
[a
].usage
|=
195 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
196 pass
->attachments
[a
].subpass_usage
[i
] |= ANV_SUBPASS_USAGE_DRAW
;
197 pass
->attachments
[a
].last_subpass_idx
= i
;
199 init_first_subpass_layout(&pass
->attachments
[a
],
200 *desc
->pDepthStencilAttachment
);
203 subpass
->depth_stencil_attachment
.attachment
= VK_ATTACHMENT_UNUSED
;
204 subpass
->depth_stencil_attachment
.layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
208 for (uint32_t i
= 0; i
< pCreateInfo
->dependencyCount
; i
++) {
209 const VkSubpassDependency
*dep
= &pCreateInfo
->pDependencies
[i
];
210 if (dep
->dstSubpass
== VK_SUBPASS_EXTERNAL
) {
211 pass
->subpass_flushes
[pass
->subpass_count
] |=
212 anv_pipe_invalidate_bits_for_access_flags(dep
->dstAccessMask
);
214 assert(dep
->dstSubpass
< pass
->subpass_count
);
215 pass
->subpass_flushes
[dep
->dstSubpass
] |=
216 anv_pipe_invalidate_bits_for_access_flags(dep
->dstAccessMask
);
219 if (dep
->srcSubpass
== VK_SUBPASS_EXTERNAL
) {
220 pass
->subpass_flushes
[0] |=
221 anv_pipe_flush_bits_for_access_flags(dep
->srcAccessMask
);
223 assert(dep
->srcSubpass
< pass
->subpass_count
);
224 pass
->subpass_flushes
[dep
->srcSubpass
+ 1] |=
225 anv_pipe_flush_bits_for_access_flags(dep
->srcAccessMask
);
229 /* From the Vulkan 1.0.39 spec:
231 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
232 * first subpass that uses an attachment, then an implicit subpass
233 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
234 * used in. The subpass dependency operates as if defined with the
235 * following parameters:
237 * VkSubpassDependency implicitDependency = {
238 * .srcSubpass = VK_SUBPASS_EXTERNAL;
239 * .dstSubpass = firstSubpass; // First subpass attachment is used in
240 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
241 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
242 * .srcAccessMask = 0;
243 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
244 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
245 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
246 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
247 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
248 * .dependencyFlags = 0;
251 * Similarly, if there is no subpass dependency from the last subpass
252 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
253 * subpass dependency exists from the last subpass it is used in to
254 * VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
255 * with the following parameters:
257 * VkSubpassDependency implicitDependency = {
258 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
259 * .dstSubpass = VK_SUBPASS_EXTERNAL;
260 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
261 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
262 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
263 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
264 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
265 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
266 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
267 * .dstAccessMask = 0;
268 * .dependencyFlags = 0;
271 * We could implement this by walking over all of the attachments and
272 * subpasses and checking to see if any of them don't have an external
273 * dependency. Or, we could just be lazy and add a couple extra flushes.
274 * We choose to be lazy.
277 pass
->subpass_flushes
[0] |=
278 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
281 pass
->subpass_flushes
[pass
->subpass_count
] |=
282 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
285 pass
->subpass_flushes
[pass
->subpass_count
] |=
286 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
289 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
290 switch (ext
->sType
) {
291 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHX
: {
292 VkRenderPassMultiviewCreateInfoKHX
*mv
= (void *)ext
;
294 for (uint32_t i
= 0; i
< mv
->subpassCount
; i
++) {
295 pass
->subpasses
[i
].view_mask
= mv
->pViewMasks
[i
];
301 anv_debug_ignored_stype(ext
->sType
);
305 *pRenderPass
= anv_render_pass_to_handle(pass
);
310 void anv_DestroyRenderPass(
313 const VkAllocationCallbacks
* pAllocator
)
315 ANV_FROM_HANDLE(anv_device
, device
, _device
);
316 ANV_FROM_HANDLE(anv_render_pass
, pass
, _pass
);
318 vk_free2(&device
->alloc
, pAllocator
, pass
);
321 void anv_GetRenderAreaGranularity(
323 VkRenderPass renderPass
,
324 VkExtent2D
* pGranularity
)
326 ANV_FROM_HANDLE(anv_render_pass
, pass
, renderPass
);
328 /* This granularity satisfies HiZ fast clear alignment requirements
329 * for all sample counts.
331 for (unsigned i
= 0; i
< pass
->subpass_count
; ++i
) {
332 if (pass
->subpasses
[i
].depth_stencil_attachment
.attachment
!=
333 VK_ATTACHMENT_UNUSED
) {
334 *pGranularity
= (VkExtent2D
) { .width
= 8, .height
= 4 };
339 *pGranularity
= (VkExtent2D
) { 1, 1 };