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