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