radv: accumulate all ingoing external dependencies to the first subpass
[mesa.git] / src / amd / vulkan / radv_pass.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27 #include "radv_private.h"
28
29 #include "vk_util.h"
30
31 static void
32 radv_render_pass_add_subpass_dep(struct radv_render_pass *pass,
33 const VkSubpassDependency2KHR *dep)
34 {
35 uint32_t src = dep->srcSubpass;
36 uint32_t dst = dep->dstSubpass;
37
38 /* Ignore subpass self-dependencies as they allow the app to call
39 * vkCmdPipelineBarrier() inside the render pass and the driver should
40 * only do the barrier when called, not when starting the render pass.
41 */
42 if (src == dst)
43 return;
44
45 /* Accumulate all ingoing external dependencies to the first subpass. */
46 if (src == VK_SUBPASS_EXTERNAL)
47 dst = 0;
48
49 if (dst == VK_SUBPASS_EXTERNAL) {
50 pass->end_barrier.src_stage_mask |= dep->srcStageMask;
51 pass->end_barrier.src_access_mask |= dep->srcAccessMask;
52 pass->end_barrier.dst_access_mask |= dep->dstAccessMask;
53 } else {
54 pass->subpasses[dst].start_barrier.src_stage_mask |= dep->srcStageMask;
55 pass->subpasses[dst].start_barrier.src_access_mask |= dep->srcAccessMask;
56 pass->subpasses[dst].start_barrier.dst_access_mask |= dep->dstAccessMask;
57 }
58 }
59
60 static void
61 radv_render_pass_compile(struct radv_render_pass *pass)
62 {
63 for (uint32_t i = 0; i < pass->subpass_count; i++) {
64 struct radv_subpass *subpass = &pass->subpasses[i];
65 uint32_t color_sample_count = 1, depth_sample_count = 1;
66
67 /* We don't allow depth_stencil_attachment to be non-NULL and
68 * be VK_ATTACHMENT_UNUSED. This way something can just check
69 * for NULL and be guaranteed that they have a valid
70 * attachment.
71 */
72 if (subpass->depth_stencil_attachment &&
73 subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
74 subpass->depth_stencil_attachment = NULL;
75
76 for (uint32_t j = 0; j < subpass->attachment_count; j++) {
77 struct radv_subpass_attachment *subpass_att =
78 &subpass->attachments[j];
79 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
80 continue;
81
82 struct radv_render_pass_attachment *pass_att =
83 &pass->attachments[subpass_att->attachment];
84
85 pass_att->last_subpass_idx = i;
86 }
87
88 subpass->has_color_att = false;
89 for (uint32_t j = 0; j < subpass->color_count; j++) {
90 struct radv_subpass_attachment *subpass_att =
91 &subpass->color_attachments[j];
92 if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
93 continue;
94
95 subpass->has_color_att = true;
96
97 struct radv_render_pass_attachment *pass_att =
98 &pass->attachments[subpass_att->attachment];
99
100 color_sample_count = pass_att->samples;
101 }
102
103 if (subpass->depth_stencil_attachment) {
104 const uint32_t a =
105 subpass->depth_stencil_attachment->attachment;
106 struct radv_render_pass_attachment *pass_att =
107 &pass->attachments[a];
108 depth_sample_count = pass_att->samples;
109 }
110
111 subpass->max_sample_count = MAX2(color_sample_count,
112 depth_sample_count);
113
114 /* We have to handle resolve attachments specially */
115 subpass->has_resolve = false;
116 if (subpass->resolve_attachments) {
117 for (uint32_t j = 0; j < subpass->color_count; j++) {
118 struct radv_subpass_attachment *resolve_att =
119 &subpass->resolve_attachments[j];
120
121 if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
122 continue;
123
124 subpass->has_resolve = true;
125 }
126 }
127 }
128 }
129
130 static unsigned
131 radv_num_subpass_attachments(const VkSubpassDescription *desc)
132 {
133 return desc->inputAttachmentCount +
134 desc->colorAttachmentCount +
135 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
136 (desc->pDepthStencilAttachment != NULL);
137 }
138
139 VkResult radv_CreateRenderPass(
140 VkDevice _device,
141 const VkRenderPassCreateInfo* pCreateInfo,
142 const VkAllocationCallbacks* pAllocator,
143 VkRenderPass* pRenderPass)
144 {
145 RADV_FROM_HANDLE(radv_device, device, _device);
146 struct radv_render_pass *pass;
147 size_t size;
148 size_t attachments_offset;
149 VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
150
151 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
152
153 size = sizeof(*pass);
154 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
155 attachments_offset = size;
156 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
157
158 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
159 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
160 if (pass == NULL)
161 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
162
163 memset(pass, 0, size);
164 pass->attachment_count = pCreateInfo->attachmentCount;
165 pass->subpass_count = pCreateInfo->subpassCount;
166 pass->attachments = (void *) pass + attachments_offset;
167
168 vk_foreach_struct(ext, pCreateInfo->pNext) {
169 switch(ext->sType) {
170 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
171 multiview_info = (VkRenderPassMultiviewCreateInfo*)ext;
172 break;
173 default:
174 break;
175 }
176 }
177
178 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
179 struct radv_render_pass_attachment *att = &pass->attachments[i];
180
181 att->format = pCreateInfo->pAttachments[i].format;
182 att->samples = pCreateInfo->pAttachments[i].samples;
183 att->load_op = pCreateInfo->pAttachments[i].loadOp;
184 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
185 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
186 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
187 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
188 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
189 }
190 uint32_t subpass_attachment_count = 0;
191 struct radv_subpass_attachment *p;
192 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
193 subpass_attachment_count +=
194 radv_num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
195 }
196
197 if (subpass_attachment_count) {
198 pass->subpass_attachments =
199 vk_alloc2(&device->alloc, pAllocator,
200 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
201 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
202 if (pass->subpass_attachments == NULL) {
203 vk_free2(&device->alloc, pAllocator, pass);
204 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
205 }
206 } else
207 pass->subpass_attachments = NULL;
208
209 p = pass->subpass_attachments;
210 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
211 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
212 struct radv_subpass *subpass = &pass->subpasses[i];
213
214 subpass->input_count = desc->inputAttachmentCount;
215 subpass->color_count = desc->colorAttachmentCount;
216 subpass->attachment_count = radv_num_subpass_attachments(desc);
217 subpass->attachments = p;
218
219 if (multiview_info)
220 subpass->view_mask = multiview_info->pViewMasks[i];
221
222 if (desc->inputAttachmentCount > 0) {
223 subpass->input_attachments = p;
224 p += desc->inputAttachmentCount;
225
226 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
227 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
228 .attachment = desc->pInputAttachments[j].attachment,
229 .layout = desc->pInputAttachments[j].layout,
230 };
231 }
232 }
233
234 if (desc->colorAttachmentCount > 0) {
235 subpass->color_attachments = p;
236 p += desc->colorAttachmentCount;
237
238 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
239 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
240 .attachment = desc->pColorAttachments[j].attachment,
241 .layout = desc->pColorAttachments[j].layout,
242 };
243 }
244 }
245
246 if (desc->pResolveAttachments) {
247 subpass->resolve_attachments = p;
248 p += desc->colorAttachmentCount;
249
250 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
251 uint32_t a = desc->pResolveAttachments[j].attachment;
252 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
253 .attachment = desc->pResolveAttachments[j].attachment,
254 .layout = desc->pResolveAttachments[j].layout,
255 };
256 }
257 }
258
259 if (desc->pDepthStencilAttachment) {
260 subpass->depth_stencil_attachment = p++;
261
262 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
263 .attachment = desc->pDepthStencilAttachment->attachment,
264 .layout = desc->pDepthStencilAttachment->layout,
265 };
266 }
267 }
268
269 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
270 /* Convert to a Dependency2KHR */
271 struct VkSubpassDependency2KHR dep2 = {
272 .srcSubpass = pCreateInfo->pDependencies[i].srcSubpass,
273 .dstSubpass = pCreateInfo->pDependencies[i].dstSubpass,
274 .srcStageMask = pCreateInfo->pDependencies[i].srcStageMask,
275 .dstStageMask = pCreateInfo->pDependencies[i].dstStageMask,
276 .srcAccessMask = pCreateInfo->pDependencies[i].srcAccessMask,
277 .dstAccessMask = pCreateInfo->pDependencies[i].dstAccessMask,
278 .dependencyFlags = pCreateInfo->pDependencies[i].dependencyFlags,
279 };
280 radv_render_pass_add_subpass_dep(pass, &dep2);
281 }
282
283 radv_render_pass_compile(pass);
284
285 *pRenderPass = radv_render_pass_to_handle(pass);
286
287 return VK_SUCCESS;
288 }
289
290 static unsigned
291 radv_num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
292 {
293 return desc->inputAttachmentCount +
294 desc->colorAttachmentCount +
295 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
296 (desc->pDepthStencilAttachment != NULL);
297 }
298
299 VkResult radv_CreateRenderPass2KHR(
300 VkDevice _device,
301 const VkRenderPassCreateInfo2KHR* pCreateInfo,
302 const VkAllocationCallbacks* pAllocator,
303 VkRenderPass* pRenderPass)
304 {
305 RADV_FROM_HANDLE(radv_device, device, _device);
306 struct radv_render_pass *pass;
307 size_t size;
308 size_t attachments_offset;
309
310 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
311
312 size = sizeof(*pass);
313 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
314 attachments_offset = size;
315 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
316
317 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
318 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
319 if (pass == NULL)
320 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
321
322 memset(pass, 0, size);
323 pass->attachment_count = pCreateInfo->attachmentCount;
324 pass->subpass_count = pCreateInfo->subpassCount;
325 pass->attachments = (void *) pass + attachments_offset;
326
327 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
328 struct radv_render_pass_attachment *att = &pass->attachments[i];
329
330 att->format = pCreateInfo->pAttachments[i].format;
331 att->samples = pCreateInfo->pAttachments[i].samples;
332 att->load_op = pCreateInfo->pAttachments[i].loadOp;
333 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
334 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
335 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
336 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
337 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
338 }
339 uint32_t subpass_attachment_count = 0;
340 struct radv_subpass_attachment *p;
341 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
342 subpass_attachment_count +=
343 radv_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
344 }
345
346 if (subpass_attachment_count) {
347 pass->subpass_attachments =
348 vk_alloc2(&device->alloc, pAllocator,
349 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
350 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
351 if (pass->subpass_attachments == NULL) {
352 vk_free2(&device->alloc, pAllocator, pass);
353 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
354 }
355 } else
356 pass->subpass_attachments = NULL;
357
358 p = pass->subpass_attachments;
359 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
360 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
361 struct radv_subpass *subpass = &pass->subpasses[i];
362
363 subpass->input_count = desc->inputAttachmentCount;
364 subpass->color_count = desc->colorAttachmentCount;
365 subpass->attachment_count = radv_num_subpass_attachments2(desc);
366 subpass->attachments = p;
367 subpass->view_mask = desc->viewMask;
368
369 if (desc->inputAttachmentCount > 0) {
370 subpass->input_attachments = p;
371 p += desc->inputAttachmentCount;
372
373 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
374 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
375 .attachment = desc->pInputAttachments[j].attachment,
376 .layout = desc->pInputAttachments[j].layout,
377 };
378 }
379 }
380
381 if (desc->colorAttachmentCount > 0) {
382 subpass->color_attachments = p;
383 p += desc->colorAttachmentCount;
384
385 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
386 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
387 .attachment = desc->pColorAttachments[j].attachment,
388 .layout = desc->pColorAttachments[j].layout,
389 };
390 }
391 }
392
393 if (desc->pResolveAttachments) {
394 subpass->resolve_attachments = p;
395 p += desc->colorAttachmentCount;
396
397 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
398 uint32_t a = desc->pResolveAttachments[j].attachment;
399 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
400 .attachment = desc->pResolveAttachments[j].attachment,
401 .layout = desc->pResolveAttachments[j].layout,
402 };
403 }
404 }
405
406 if (desc->pDepthStencilAttachment) {
407 subpass->depth_stencil_attachment = p++;
408
409 *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
410 .attachment = desc->pDepthStencilAttachment->attachment,
411 .layout = desc->pDepthStencilAttachment->layout,
412 };
413 }
414 }
415
416 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
417 radv_render_pass_add_subpass_dep(pass,
418 &pCreateInfo->pDependencies[i]);
419 }
420
421 radv_render_pass_compile(pass);
422
423 *pRenderPass = radv_render_pass_to_handle(pass);
424
425 return VK_SUCCESS;
426 }
427
428 void radv_DestroyRenderPass(
429 VkDevice _device,
430 VkRenderPass _pass,
431 const VkAllocationCallbacks* pAllocator)
432 {
433 RADV_FROM_HANDLE(radv_device, device, _device);
434 RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
435
436 if (!_pass)
437 return;
438 vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
439 vk_free2(&device->alloc, pAllocator, pass);
440 }
441
442 void radv_GetRenderAreaGranularity(
443 VkDevice device,
444 VkRenderPass renderPass,
445 VkExtent2D* pGranularity)
446 {
447 pGranularity->width = 1;
448 pGranularity->height = 1;
449 }
450