anv: Make blorp update the clear color.
[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 struct anv_subpass_attachment *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 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
77 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
78 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
79
80 /* Clear the subpasses along with the parent pass. This required because
81 * each array member of anv_subpass must be a valid pointer if not NULL.
82 */
83 memset(pass, 0, ma.size);
84 pass->attachment_count = pCreateInfo->attachmentCount;
85 pass->subpass_count = pCreateInfo->subpassCount;
86 pass->attachments = attachments;
87 pass->subpass_flushes = subpass_flushes;
88
89 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
90 struct anv_render_pass_attachment *att = &pass->attachments[i];
91
92 att->format = pCreateInfo->pAttachments[i].format;
93 att->samples = pCreateInfo->pAttachments[i].samples;
94 att->usage = 0;
95 att->load_op = pCreateInfo->pAttachments[i].loadOp;
96 att->store_op = pCreateInfo->pAttachments[i].storeOp;
97 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
98 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
99 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
100 att->first_subpass_layout = VK_IMAGE_LAYOUT_UNDEFINED;
101 }
102
103 bool has_color = false, has_depth = false, has_input = false;
104 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
105 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
106 struct anv_subpass *subpass = &pass->subpasses[i];
107
108 subpass->input_count = desc->inputAttachmentCount;
109 subpass->color_count = desc->colorAttachmentCount;
110 subpass->attachment_count = num_subpass_attachments(desc);
111 subpass->attachments = subpass_attachments;
112 subpass->view_mask = 0;
113
114 if (desc->inputAttachmentCount > 0) {
115 subpass->input_attachments = subpass_attachments;
116 subpass_attachments += desc->inputAttachmentCount;
117
118 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
119 uint32_t a = desc->pInputAttachments[j].attachment;
120 subpass->input_attachments[j] = (struct anv_subpass_attachment) {
121 .usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
122 .attachment = desc->pInputAttachments[j].attachment,
123 .layout = desc->pInputAttachments[j].layout,
124 };
125 if (a != VK_ATTACHMENT_UNUSED) {
126 has_input = true;
127 pass->attachments[a].usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
128 pass->attachments[a].last_subpass_idx = i;
129
130 init_first_subpass_layout(&pass->attachments[a],
131 desc->pInputAttachments[j]);
132 if (desc->pDepthStencilAttachment &&
133 a == desc->pDepthStencilAttachment->attachment)
134 subpass->has_ds_self_dep = true;
135 }
136 }
137 }
138
139 if (desc->colorAttachmentCount > 0) {
140 subpass->color_attachments = subpass_attachments;
141 subpass_attachments += desc->colorAttachmentCount;
142
143 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
144 uint32_t a = desc->pColorAttachments[j].attachment;
145 subpass->color_attachments[j] = (struct anv_subpass_attachment) {
146 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
147 .attachment = desc->pColorAttachments[j].attachment,
148 .layout = desc->pColorAttachments[j].layout,
149 };
150 if (a != VK_ATTACHMENT_UNUSED) {
151 has_color = true;
152 pass->attachments[a].usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
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] = (struct anv_subpass_attachment) {
169 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
170 .attachment = desc->pResolveAttachments[j].attachment,
171 .layout = desc->pResolveAttachments[j].layout,
172 };
173 if (a != VK_ATTACHMENT_UNUSED) {
174 subpass->has_resolve = true;
175 uint32_t color_att = desc->pColorAttachments[j].attachment;
176 pass->attachments[color_att].usage |=
177 VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
178 pass->attachments[a].usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
179 pass->attachments[a].last_subpass_idx = i;
180
181 init_first_subpass_layout(&pass->attachments[a],
182 desc->pResolveAttachments[j]);
183 }
184 }
185 }
186
187 if (desc->pDepthStencilAttachment) {
188 uint32_t a = desc->pDepthStencilAttachment->attachment;
189 subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
190 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
191 .attachment = desc->pDepthStencilAttachment->attachment,
192 .layout = desc->pDepthStencilAttachment->layout,
193 };
194 *subpass_attachments++ = subpass->depth_stencil_attachment;
195 if (a != VK_ATTACHMENT_UNUSED) {
196 has_depth = true;
197 pass->attachments[a].usage |=
198 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
199 pass->attachments[a].last_subpass_idx = i;
200
201 init_first_subpass_layout(&pass->attachments[a],
202 *desc->pDepthStencilAttachment);
203 }
204 } else {
205 subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
206 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
207 .attachment = VK_ATTACHMENT_UNUSED,
208 .layout = VK_IMAGE_LAYOUT_UNDEFINED,
209 };
210 }
211 }
212
213 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
214 const VkSubpassDependency *dep = &pCreateInfo->pDependencies[i];
215 if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
216 pass->subpass_flushes[pass->subpass_count] |=
217 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
218 } else {
219 assert(dep->dstSubpass < pass->subpass_count);
220 pass->subpass_flushes[dep->dstSubpass] |=
221 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
222 }
223
224 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
225 pass->subpass_flushes[0] |=
226 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
227 } else {
228 assert(dep->srcSubpass < pass->subpass_count);
229 pass->subpass_flushes[dep->srcSubpass + 1] |=
230 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
231 }
232 }
233
234 /* From the Vulkan 1.0.39 spec:
235 *
236 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
237 * first subpass that uses an attachment, then an implicit subpass
238 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
239 * used in. The subpass dependency operates as if defined with the
240 * following parameters:
241 *
242 * VkSubpassDependency implicitDependency = {
243 * .srcSubpass = VK_SUBPASS_EXTERNAL;
244 * .dstSubpass = firstSubpass; // First subpass attachment is used in
245 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
246 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
247 * .srcAccessMask = 0;
248 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
249 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
250 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
251 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
252 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
253 * .dependencyFlags = 0;
254 * };
255 *
256 * Similarly, if there is no subpass dependency from the last subpass
257 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
258 * subpass dependency exists from the last subpass it is used in to
259 * VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
260 * with the following parameters:
261 *
262 * VkSubpassDependency implicitDependency = {
263 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
264 * .dstSubpass = VK_SUBPASS_EXTERNAL;
265 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
266 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
267 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
268 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
269 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
270 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
271 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
272 * .dstAccessMask = 0;
273 * .dependencyFlags = 0;
274 * };
275 *
276 * We could implement this by walking over all of the attachments and
277 * subpasses and checking to see if any of them don't have an external
278 * dependency. Or, we could just be lazy and add a couple extra flushes.
279 * We choose to be lazy.
280 */
281 if (has_input) {
282 pass->subpass_flushes[0] |=
283 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
284 }
285 if (has_color) {
286 pass->subpass_flushes[pass->subpass_count] |=
287 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
288 }
289 if (has_depth) {
290 pass->subpass_flushes[pass->subpass_count] |=
291 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
292 }
293
294 vk_foreach_struct(ext, pCreateInfo->pNext) {
295 switch (ext->sType) {
296 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHR: {
297 VkRenderPassMultiviewCreateInfoKHR *mv = (void *)ext;
298
299 for (uint32_t i = 0; i < mv->subpassCount; i++) {
300 pass->subpasses[i].view_mask = mv->pViewMasks[i];
301 }
302 break;
303 }
304
305 default:
306 anv_debug_ignored_stype(ext->sType);
307 }
308 }
309
310 *pRenderPass = anv_render_pass_to_handle(pass);
311
312 return VK_SUCCESS;
313 }
314
315 void anv_DestroyRenderPass(
316 VkDevice _device,
317 VkRenderPass _pass,
318 const VkAllocationCallbacks* pAllocator)
319 {
320 ANV_FROM_HANDLE(anv_device, device, _device);
321 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
322
323 vk_free2(&device->alloc, pAllocator, pass);
324 }
325
326 void anv_GetRenderAreaGranularity(
327 VkDevice device,
328 VkRenderPass renderPass,
329 VkExtent2D* pGranularity)
330 {
331 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
332
333 /* This granularity satisfies HiZ fast clear alignment requirements
334 * for all sample counts.
335 */
336 for (unsigned i = 0; i < pass->subpass_count; ++i) {
337 if (pass->subpasses[i].depth_stencil_attachment.attachment !=
338 VK_ATTACHMENT_UNUSED) {
339 *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
340 return;
341 }
342 }
343
344 *pGranularity = (VkExtent2D) { 1, 1 };
345 }