anv/pass: Record required pipe flushes
[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 static unsigned
27 num_subpass_attachments(const VkSubpassDescription *desc)
28 {
29 return desc->inputAttachmentCount +
30 desc->colorAttachmentCount +
31 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
32 (desc->pDepthStencilAttachment != NULL);
33 }
34
35 VkResult anv_CreateRenderPass(
36 VkDevice _device,
37 const VkRenderPassCreateInfo* pCreateInfo,
38 const VkAllocationCallbacks* pAllocator,
39 VkRenderPass* pRenderPass)
40 {
41 ANV_FROM_HANDLE(anv_device, device, _device);
42
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
44
45 struct anv_render_pass *pass;
46 struct anv_subpass *subpasses;
47 struct anv_render_pass_attachment *attachments;
48 enum anv_pipe_bits *subpass_flushes;
49
50 ANV_MULTIALLOC(ma);
51 anv_multialloc_add(&ma, &pass, 1);
52 anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
53 anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
54 anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
55
56 VkAttachmentReference *subpass_attachments;
57 uint32_t subpass_attachment_count = 0;
58 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
59 subpass_attachment_count +=
60 num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
61 }
62 anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
63
64 enum anv_subpass_usage *subpass_usages;
65 anv_multialloc_add(&ma, &subpass_usages,
66 pCreateInfo->subpassCount * pCreateInfo->attachmentCount);
67
68 if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
69 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
70 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
71
72 /* Clear the subpasses along with the parent pass. This required because
73 * each array member of anv_subpass must be a valid pointer if not NULL.
74 */
75 memset(pass, 0, ma.size);
76 pass->attachment_count = pCreateInfo->attachmentCount;
77 pass->subpass_count = pCreateInfo->subpassCount;
78 pass->attachments = attachments;
79 pass->subpass_flushes = subpass_flushes;
80
81 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
82 struct anv_render_pass_attachment *att = &pass->attachments[i];
83
84 att->format = pCreateInfo->pAttachments[i].format;
85 att->samples = pCreateInfo->pAttachments[i].samples;
86 att->usage = 0;
87 att->load_op = pCreateInfo->pAttachments[i].loadOp;
88 att->store_op = pCreateInfo->pAttachments[i].storeOp;
89 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
90 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
91 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
92 att->subpass_usage = subpass_usages;
93 subpass_usages += pass->subpass_count;
94 }
95
96 bool has_color = false, has_depth = false, has_input = false;
97 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
98 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
99 struct anv_subpass *subpass = &pass->subpasses[i];
100
101 subpass->input_count = desc->inputAttachmentCount;
102 subpass->color_count = desc->colorAttachmentCount;
103 subpass->attachment_count = num_subpass_attachments(desc);
104 subpass->attachments = subpass_attachments;
105
106 if (desc->inputAttachmentCount > 0) {
107 subpass->input_attachments = subpass_attachments;
108 subpass_attachments += desc->inputAttachmentCount;
109
110 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
111 uint32_t a = desc->pInputAttachments[j].attachment;
112 subpass->input_attachments[j] = desc->pInputAttachments[j];
113 if (a != VK_ATTACHMENT_UNUSED) {
114 has_input = true;
115 pass->attachments[a].usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
116 pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_INPUT;
117 pass->attachments[a].last_subpass_idx = i;
118
119 if (desc->pDepthStencilAttachment &&
120 a == desc->pDepthStencilAttachment->attachment)
121 subpass->has_ds_self_dep = true;
122 }
123 }
124 }
125
126 if (desc->colorAttachmentCount > 0) {
127 subpass->color_attachments = subpass_attachments;
128 subpass_attachments += desc->colorAttachmentCount;
129
130 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
131 uint32_t a = desc->pColorAttachments[j].attachment;
132 subpass->color_attachments[j] = desc->pColorAttachments[j];
133 if (a != VK_ATTACHMENT_UNUSED) {
134 has_color = true;
135 pass->attachments[a].usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
136 pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
137 pass->attachments[a].last_subpass_idx = i;
138 }
139 }
140 }
141
142 subpass->has_resolve = false;
143 if (desc->pResolveAttachments) {
144 subpass->resolve_attachments = subpass_attachments;
145 subpass_attachments += desc->colorAttachmentCount;
146
147 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
148 uint32_t a = desc->pResolveAttachments[j].attachment;
149 subpass->resolve_attachments[j] = desc->pResolveAttachments[j];
150 if (a != VK_ATTACHMENT_UNUSED) {
151 subpass->has_resolve = true;
152 uint32_t color_att = desc->pColorAttachments[j].attachment;
153 pass->attachments[color_att].usage |=
154 VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
155 pass->attachments[a].usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
156
157 pass->attachments[color_att].subpass_usage[i] |=
158 ANV_SUBPASS_USAGE_RESOLVE_SRC;
159 pass->attachments[a].subpass_usage[i] |=
160 ANV_SUBPASS_USAGE_RESOLVE_DST;
161 pass->attachments[a].last_subpass_idx = i;
162 }
163 }
164 }
165
166 if (desc->pDepthStencilAttachment) {
167 uint32_t a = desc->pDepthStencilAttachment->attachment;
168 *subpass_attachments++ = subpass->depth_stencil_attachment =
169 *desc->pDepthStencilAttachment;
170 if (a != VK_ATTACHMENT_UNUSED) {
171 has_depth = true;
172 pass->attachments[a].usage |=
173 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
174 pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
175 pass->attachments[a].last_subpass_idx = i;
176 }
177 } else {
178 subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
179 subpass->depth_stencil_attachment.layout = VK_IMAGE_LAYOUT_UNDEFINED;
180 }
181 }
182
183 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
184 const VkSubpassDependency *dep = &pCreateInfo->pDependencies[i];
185 if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
186 pass->subpass_flushes[pass->subpass_count] |=
187 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
188 } else {
189 assert(dep->dstSubpass < pass->subpass_count);
190 pass->subpass_flushes[dep->dstSubpass] |=
191 anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
192 }
193
194 if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
195 pass->subpass_flushes[0] |=
196 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
197 } else {
198 assert(dep->srcSubpass < pass->subpass_count);
199 pass->subpass_flushes[dep->srcSubpass + 1] |=
200 anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
201 }
202 }
203
204 /* From the Vulkan 1.0.39 spec:
205 *
206 * If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
207 * first subpass that uses an attachment, then an implicit subpass
208 * dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
209 * used in. The subpass dependency operates as if defined with the
210 * following parameters:
211 *
212 * VkSubpassDependency implicitDependency = {
213 * .srcSubpass = VK_SUBPASS_EXTERNAL;
214 * .dstSubpass = firstSubpass; // First subpass attachment is used in
215 * .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
216 * .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
217 * .srcAccessMask = 0;
218 * .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
219 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
220 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
221 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
222 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
223 * .dependencyFlags = 0;
224 * };
225 *
226 * Similarly, if there is no subpass dependency from the last subpass
227 * that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
228 * subpass dependency exists from the last subpass it is used in to
229 * VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
230 * with the following parameters:
231 *
232 * VkSubpassDependency implicitDependency = {
233 * .srcSubpass = lastSubpass; // Last subpass attachment is used in
234 * .dstSubpass = VK_SUBPASS_EXTERNAL;
235 * .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
236 * .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
237 * .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
238 * VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
239 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
240 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
241 * VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
242 * .dstAccessMask = 0;
243 * .dependencyFlags = 0;
244 * };
245 *
246 * We could implement this by walking over all of the attachments and
247 * subpasses and checking to see if any of them don't have an external
248 * dependency. Or, we could just be lazy and add a couple extra flushes.
249 * We choose to be lazy.
250 */
251 if (has_input) {
252 pass->subpass_flushes[0] |=
253 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
254 }
255 if (has_color) {
256 pass->subpass_flushes[pass->subpass_count] |=
257 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
258 }
259 if (has_depth) {
260 pass->subpass_flushes[pass->subpass_count] |=
261 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
262 }
263
264 *pRenderPass = anv_render_pass_to_handle(pass);
265
266 return VK_SUCCESS;
267 }
268
269 void anv_DestroyRenderPass(
270 VkDevice _device,
271 VkRenderPass _pass,
272 const VkAllocationCallbacks* pAllocator)
273 {
274 ANV_FROM_HANDLE(anv_device, device, _device);
275 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
276
277 vk_free2(&device->alloc, pAllocator, pass);
278 }
279
280 void anv_GetRenderAreaGranularity(
281 VkDevice device,
282 VkRenderPass renderPass,
283 VkExtent2D* pGranularity)
284 {
285 ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
286
287 /* This granularity satisfies HiZ fast clear alignment requirements
288 * for all sample counts.
289 */
290 for (unsigned i = 0; i < pass->subpass_count; ++i) {
291 if (pass->subpasses[i].depth_stencil_attachment.attachment !=
292 VK_ATTACHMENT_UNUSED) {
293 *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
294 return;
295 }
296 }
297
298 *pGranularity = (VkExtent2D) { 1, 1 };
299 }