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