radv: ignore subpass self-dependencies
[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 VkResult radv_CreateRenderPass(
32 VkDevice _device,
33 const VkRenderPassCreateInfo* pCreateInfo,
34 const VkAllocationCallbacks* pAllocator,
35 VkRenderPass* pRenderPass)
36 {
37 RADV_FROM_HANDLE(radv_device, device, _device);
38 struct radv_render_pass *pass;
39 size_t size;
40 size_t attachments_offset;
41 VkRenderPassMultiviewCreateInfoKHR *multiview_info = NULL;
42
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
44
45 size = sizeof(*pass);
46 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
47 attachments_offset = size;
48 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
49
50 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
51 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
52 if (pass == NULL)
53 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
54
55 memset(pass, 0, size);
56 pass->attachment_count = pCreateInfo->attachmentCount;
57 pass->subpass_count = pCreateInfo->subpassCount;
58 pass->attachments = (void *) pass + attachments_offset;
59
60 vk_foreach_struct(ext, pCreateInfo->pNext) {
61 switch(ext->sType) {
62 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHR:
63 multiview_info = ( VkRenderPassMultiviewCreateInfoKHR*)ext;
64 break;
65 default:
66 break;
67 }
68 }
69
70 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
71 struct radv_render_pass_attachment *att = &pass->attachments[i];
72
73 att->format = pCreateInfo->pAttachments[i].format;
74 att->samples = pCreateInfo->pAttachments[i].samples;
75 att->load_op = pCreateInfo->pAttachments[i].loadOp;
76 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
77 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
78 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
79 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
80 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
81 }
82 uint32_t subpass_attachment_count = 0;
83 struct radv_subpass_attachment *p;
84 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
85 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
86
87 subpass_attachment_count +=
88 desc->inputAttachmentCount +
89 desc->colorAttachmentCount +
90 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
91 (desc->pDepthStencilAttachment != NULL);
92 }
93
94 if (subpass_attachment_count) {
95 pass->subpass_attachments =
96 vk_alloc2(&device->alloc, pAllocator,
97 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
98 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
99 if (pass->subpass_attachments == NULL) {
100 vk_free2(&device->alloc, pAllocator, pass);
101 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
102 }
103 } else
104 pass->subpass_attachments = NULL;
105
106 p = pass->subpass_attachments;
107 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
108 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
109 uint32_t color_sample_count = 1, depth_sample_count = 1;
110 struct radv_subpass *subpass = &pass->subpasses[i];
111
112 subpass->input_count = desc->inputAttachmentCount;
113 subpass->color_count = desc->colorAttachmentCount;
114 if (multiview_info)
115 subpass->view_mask = multiview_info->pViewMasks[i];
116
117 if (desc->inputAttachmentCount > 0) {
118 subpass->input_attachments = p;
119 p += desc->inputAttachmentCount;
120
121 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
122 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
123 .attachment = desc->pInputAttachments[j].attachment,
124 .layout = desc->pInputAttachments[j].layout,
125 };
126 if (desc->pInputAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
127 pass->attachments[desc->pInputAttachments[j].attachment].view_mask |= subpass->view_mask;
128 }
129 }
130
131 if (desc->colorAttachmentCount > 0) {
132 subpass->color_attachments = p;
133 p += desc->colorAttachmentCount;
134
135 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
136 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
137 .attachment = desc->pColorAttachments[j].attachment,
138 .layout = desc->pColorAttachments[j].layout,
139 };
140 if (desc->pColorAttachments[j].attachment != VK_ATTACHMENT_UNUSED) {
141 pass->attachments[desc->pColorAttachments[j].attachment].view_mask |= subpass->view_mask;
142 color_sample_count = pCreateInfo->pAttachments[desc->pColorAttachments[j].attachment].samples;
143 }
144 }
145 }
146
147 subpass->has_resolve = false;
148 if (desc->pResolveAttachments) {
149 subpass->resolve_attachments = p;
150 p += desc->colorAttachmentCount;
151
152 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
153 uint32_t a = desc->pResolveAttachments[j].attachment;
154 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
155 .attachment = desc->pResolveAttachments[j].attachment,
156 .layout = desc->pResolveAttachments[j].layout,
157 };
158 if (a != VK_ATTACHMENT_UNUSED) {
159 subpass->has_resolve = true;
160 pass->attachments[desc->pResolveAttachments[j].attachment].view_mask |= subpass->view_mask;
161 }
162 }
163 }
164
165 if (desc->pDepthStencilAttachment) {
166 subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
167 .attachment = desc->pDepthStencilAttachment->attachment,
168 .layout = desc->pDepthStencilAttachment->layout,
169 };
170 if (desc->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
171 pass->attachments[desc->pDepthStencilAttachment->attachment].view_mask |= subpass->view_mask;
172 depth_sample_count = pCreateInfo->pAttachments[desc->pDepthStencilAttachment->attachment].samples;
173 }
174 } else {
175 subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
176 }
177
178 subpass->max_sample_count = MAX2(color_sample_count,
179 depth_sample_count);
180 }
181
182 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
183 uint32_t dst = pCreateInfo->pDependencies[i].dstSubpass;
184 if (dst == VK_SUBPASS_EXTERNAL) {
185 pass->end_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
186 pass->end_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
187 pass->end_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
188 } else {
189 pass->subpasses[dst].start_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
190 pass->subpasses[dst].start_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
191 pass->subpasses[dst].start_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
192 }
193 }
194
195 *pRenderPass = radv_render_pass_to_handle(pass);
196
197 return VK_SUCCESS;
198 }
199
200 VkResult radv_CreateRenderPass2KHR(
201 VkDevice _device,
202 const VkRenderPassCreateInfo2KHR* pCreateInfo,
203 const VkAllocationCallbacks* pAllocator,
204 VkRenderPass* pRenderPass)
205 {
206 RADV_FROM_HANDLE(radv_device, device, _device);
207 struct radv_render_pass *pass;
208 size_t size;
209 size_t attachments_offset;
210
211 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
212
213 size = sizeof(*pass);
214 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
215 attachments_offset = size;
216 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
217
218 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
219 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
220 if (pass == NULL)
221 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
222
223 memset(pass, 0, size);
224 pass->attachment_count = pCreateInfo->attachmentCount;
225 pass->subpass_count = pCreateInfo->subpassCount;
226 pass->attachments = (void *) pass + attachments_offset;
227
228 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
229 struct radv_render_pass_attachment *att = &pass->attachments[i];
230
231 att->format = pCreateInfo->pAttachments[i].format;
232 att->samples = pCreateInfo->pAttachments[i].samples;
233 att->load_op = pCreateInfo->pAttachments[i].loadOp;
234 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
235 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
236 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
237 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
238 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
239 }
240 uint32_t subpass_attachment_count = 0;
241 struct radv_subpass_attachment *p;
242 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
243 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
244
245 subpass_attachment_count +=
246 desc->inputAttachmentCount +
247 desc->colorAttachmentCount +
248 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
249 (desc->pDepthStencilAttachment != NULL);
250 }
251
252 if (subpass_attachment_count) {
253 pass->subpass_attachments =
254 vk_alloc2(&device->alloc, pAllocator,
255 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
256 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
257 if (pass->subpass_attachments == NULL) {
258 vk_free2(&device->alloc, pAllocator, pass);
259 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
260 }
261 } else
262 pass->subpass_attachments = NULL;
263
264 p = pass->subpass_attachments;
265 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
266 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
267 uint32_t color_sample_count = 1, depth_sample_count = 1;
268 struct radv_subpass *subpass = &pass->subpasses[i];
269
270 subpass->input_count = desc->inputAttachmentCount;
271 subpass->color_count = desc->colorAttachmentCount;
272 subpass->view_mask = desc->viewMask;
273
274 if (desc->inputAttachmentCount > 0) {
275 subpass->input_attachments = p;
276 p += desc->inputAttachmentCount;
277
278 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
279 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
280 .attachment = desc->pInputAttachments[j].attachment,
281 .layout = desc->pInputAttachments[j].layout,
282 };
283 if (desc->pInputAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
284 pass->attachments[desc->pInputAttachments[j].attachment].view_mask |= subpass->view_mask;
285 }
286 }
287
288 if (desc->colorAttachmentCount > 0) {
289 subpass->color_attachments = p;
290 p += desc->colorAttachmentCount;
291
292 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
293 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
294 .attachment = desc->pColorAttachments[j].attachment,
295 .layout = desc->pColorAttachments[j].layout,
296 };
297 if (desc->pColorAttachments[j].attachment != VK_ATTACHMENT_UNUSED) {
298 pass->attachments[desc->pColorAttachments[j].attachment].view_mask |= subpass->view_mask;
299 color_sample_count = pCreateInfo->pAttachments[desc->pColorAttachments[j].attachment].samples;
300 }
301 }
302 }
303
304 subpass->has_resolve = false;
305 if (desc->pResolveAttachments) {
306 subpass->resolve_attachments = p;
307 p += desc->colorAttachmentCount;
308
309 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
310 uint32_t a = desc->pResolveAttachments[j].attachment;
311 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
312 .attachment = desc->pResolveAttachments[j].attachment,
313 .layout = desc->pResolveAttachments[j].layout,
314 };
315 if (a != VK_ATTACHMENT_UNUSED) {
316 subpass->has_resolve = true;
317 pass->attachments[desc->pResolveAttachments[j].attachment].view_mask |= subpass->view_mask;
318 }
319 }
320 }
321
322 if (desc->pDepthStencilAttachment) {
323 subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
324 .attachment = desc->pDepthStencilAttachment->attachment,
325 .layout = desc->pDepthStencilAttachment->layout,
326 };
327 if (desc->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
328 pass->attachments[desc->pDepthStencilAttachment->attachment].view_mask |= subpass->view_mask;
329 depth_sample_count = pCreateInfo->pAttachments[desc->pDepthStencilAttachment->attachment].samples;
330 }
331 } else {
332 subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
333 }
334
335 subpass->max_sample_count = MAX2(color_sample_count,
336 depth_sample_count);
337 }
338
339 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
340 uint32_t src = pCreateInfo->pDependencies[i].srcSubpass;
341 uint32_t dst = pCreateInfo->pDependencies[i].dstSubpass;
342
343 /* Ignore subpass self-dependencies as they allow the app to
344 * call vkCmdPipelineBarrier() inside the render pass and the
345 * driver should only do the barrier when called, not when
346 * starting the render pass.
347 */
348 if (src == dst)
349 continue;
350
351 if (dst == VK_SUBPASS_EXTERNAL) {
352 pass->end_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
353 pass->end_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
354 pass->end_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
355 } else {
356 pass->subpasses[dst].start_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
357 pass->subpasses[dst].start_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
358 pass->subpasses[dst].start_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
359 }
360 }
361
362 *pRenderPass = radv_render_pass_to_handle(pass);
363
364 return VK_SUCCESS;
365 }
366
367 void radv_DestroyRenderPass(
368 VkDevice _device,
369 VkRenderPass _pass,
370 const VkAllocationCallbacks* pAllocator)
371 {
372 RADV_FROM_HANDLE(radv_device, device, _device);
373 RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
374
375 if (!_pass)
376 return;
377 vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
378 vk_free2(&device->alloc, pAllocator, pass);
379 }
380
381 void radv_GetRenderAreaGranularity(
382 VkDevice device,
383 VkRenderPass renderPass,
384 VkExtent2D* pGranularity)
385 {
386 pGranularity->width = 1;
387 pGranularity->height = 1;
388 }
389