anv/cmd_buffer: Restrict fast clears in the GENERAL layout
[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 unsigned
29 num_subpass_attachments(const VkSubpassDescription *desc)
30 {
31 return desc->inputAttachmentCount +
32 desc->colorAttachmentCount +
33 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
34 (desc->pDepthStencilAttachment != NULL);
35 }
36
37 static void
38 init_first_subpass_layout(struct anv_render_pass_attachment * const att,
39 const VkAttachmentReference att_ref)
40 {
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);
44 }
45 }
46
47 VkResult anv_CreateRenderPass(
48 VkDevice _device,
49 const VkRenderPassCreateInfo* pCreateInfo,
50 const VkAllocationCallbacks* pAllocator,
51 VkRenderPass* pRenderPass)
52 {
53 ANV_FROM_HANDLE(anv_device, device, _device);
54
55 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
56
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;
61
62 ANV_MULTIALLOC(ma);
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);
67
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]);
73 }
74 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
75
76 enum anv_subpass_usage *subpass_usages;
77 anv_multialloc_add(&ma, &subpass_usages,
78 pCreateInfo->subpassCount * pCreateInfo->attachmentCount);
79
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);
83
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.
86 */
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;
92
93 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
94 struct anv_render_pass_attachment *att = &pass->attachments[i];
95
96 att->format = pCreateInfo->pAttachments[i].format;
97 att->samples = pCreateInfo->pAttachments[i].samples;
98 att->usage = 0;
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;
107 }
108
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];
113
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;
119
120 if (desc->inputAttachmentCount > 0) {
121 subpass->input_attachments = subpass_attachments;
122 subpass_attachments += desc->inputAttachmentCount;
123
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) {
128 has_input = true;
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;
132
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;
138 }
139 }
140 }
141
142 if (desc->colorAttachmentCount > 0) {
143 subpass->color_attachments = subpass_attachments;
144 subpass_attachments += desc->colorAttachmentCount;
145
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) {
150 has_color = true;
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;
154
155 init_first_subpass_layout(&pass->attachments[a],
156 desc->pColorAttachments[j]);
157 }
158 }
159 }
160
161 subpass->has_resolve = false;
162 if (desc->pResolveAttachments) {
163 subpass->resolve_attachments = subpass_attachments;
164 subpass_attachments += desc->colorAttachmentCount;
165
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;
175
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;
181
182 init_first_subpass_layout(&pass->attachments[a],
183 desc->pResolveAttachments[j]);
184 }
185 }
186 }
187
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) {
193 has_depth = true;
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;
198
199 init_first_subpass_layout(&pass->attachments[a],
200 *desc->pDepthStencilAttachment);
201 }
202 } else {
203 subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
204 subpass->depth_stencil_attachment.layout = VK_IMAGE_LAYOUT_UNDEFINED;
205 }
206 }
207
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);
213 } else {
214 assert(dep->dstSubpass < pass->subpass_count);
215 pass->subpass_flushes[dep->dstSubpass] |=
216 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
217 }
218
219 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
220 pass->subpass_flushes[0] |=
221 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
222 } else {
223 assert(dep->srcSubpass < pass->subpass_count);
224 pass->subpass_flushes[dep->srcSubpass + 1] |=
225 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
226 }
227 }
228
229 /* From the Vulkan 1.0.39 spec:
230 *
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:
236 *
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;
249 * };
250 *
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:
256 *
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;
269 * };
270 *
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.
275 */
276 if (has_input) {
277 pass->subpass_flushes[0] |=
278 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
279 }
280 if (has_color) {
281 pass->subpass_flushes[pass->subpass_count] |=
282 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
283 }
284 if (has_depth) {
285 pass->subpass_flushes[pass->subpass_count] |=
286 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
287 }
288
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;
293
294 for (uint32_t i = 0; i < mv->subpassCount; i++) {
295 pass->subpasses[i].view_mask = mv->pViewMasks[i];
296 }
297 break;
298 }
299
300 default:
301 anv_debug_ignored_stype(ext->sType);
302 }
303 }
304
305 *pRenderPass = anv_render_pass_to_handle(pass);
306
307 return VK_SUCCESS;
308 }
309
310 void anv_DestroyRenderPass(
311 VkDevice _device,
312 VkRenderPass _pass,
313 const VkAllocationCallbacks* pAllocator)
314 {
315 ANV_FROM_HANDLE(anv_device, device, _device);
316 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
317
318 vk_free2(&device->alloc, pAllocator, pass);
319 }
320
321 void anv_GetRenderAreaGranularity(
322 VkDevice device,
323 VkRenderPass renderPass,
324 VkExtent2D* pGranularity)
325 {
326 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
327
328 /* This granularity satisfies HiZ fast clear alignment requirements
329 * for all sample counts.
330 */
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 };
335 return;
336 }
337 }
338
339 *pGranularity = (VkExtent2D) { 1, 1 };
340 }