radv: remove unused radv_render_pass_attachment::view_mask
[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 VkRenderPassMultiviewCreateInfo *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:
63 multiview_info = (VkRenderPassMultiviewCreateInfo*)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 }
127 }
128
129 if (desc->colorAttachmentCount > 0) {
130 subpass->color_attachments = p;
131 p += desc->colorAttachmentCount;
132
133 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
134 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
135 .attachment = desc->pColorAttachments[j].attachment,
136 .layout = desc->pColorAttachments[j].layout,
137 };
138 if (desc->pColorAttachments[j].attachment != VK_ATTACHMENT_UNUSED) {
139 color_sample_count = pCreateInfo->pAttachments[desc->pColorAttachments[j].attachment].samples;
140 }
141 }
142 }
143
144 subpass->has_resolve = false;
145 if (desc->pResolveAttachments) {
146 subpass->resolve_attachments = p;
147 p += desc->colorAttachmentCount;
148
149 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
150 uint32_t a = desc->pResolveAttachments[j].attachment;
151 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
152 .attachment = desc->pResolveAttachments[j].attachment,
153 .layout = desc->pResolveAttachments[j].layout,
154 };
155 if (a != VK_ATTACHMENT_UNUSED) {
156 subpass->has_resolve = true;
157 }
158 }
159 }
160
161 if (desc->pDepthStencilAttachment) {
162 subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
163 .attachment = desc->pDepthStencilAttachment->attachment,
164 .layout = desc->pDepthStencilAttachment->layout,
165 };
166 if (desc->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
167 depth_sample_count = pCreateInfo->pAttachments[desc->pDepthStencilAttachment->attachment].samples;
168 }
169 } else {
170 subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
171 }
172
173 subpass->max_sample_count = MAX2(color_sample_count,
174 depth_sample_count);
175 }
176
177 for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
178 uint32_t src = pCreateInfo->pDependencies[i].srcSubpass;
179 uint32_t dst = pCreateInfo->pDependencies[i].dstSubpass;
180
181 /* Ignore subpass self-dependencies as they allow the app to
182 * call vkCmdPipelineBarrier() inside the render pass and the
183 * driver should only do the barrier when called, not when
184 * starting the render pass.
185 */
186 if (src == dst)
187 continue;
188
189 if (dst == VK_SUBPASS_EXTERNAL) {
190 pass->end_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
191 pass->end_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
192 pass->end_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
193 } else {
194 pass->subpasses[dst].start_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
195 pass->subpasses[dst].start_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
196 pass->subpasses[dst].start_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
197 }
198 }
199
200 *pRenderPass = radv_render_pass_to_handle(pass);
201
202 return VK_SUCCESS;
203 }
204
205 VkResult radv_CreateRenderPass2KHR(
206 VkDevice _device,
207 const VkRenderPassCreateInfo2KHR* pCreateInfo,
208 const VkAllocationCallbacks* pAllocator,
209 VkRenderPass* pRenderPass)
210 {
211 RADV_FROM_HANDLE(radv_device, device, _device);
212 struct radv_render_pass *pass;
213 size_t size;
214 size_t attachments_offset;
215
216 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
217
218 size = sizeof(*pass);
219 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
220 attachments_offset = size;
221 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
222
223 pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
224 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
225 if (pass == NULL)
226 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
227
228 memset(pass, 0, size);
229 pass->attachment_count = pCreateInfo->attachmentCount;
230 pass->subpass_count = pCreateInfo->subpassCount;
231 pass->attachments = (void *) pass + attachments_offset;
232
233 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
234 struct radv_render_pass_attachment *att = &pass->attachments[i];
235
236 att->format = pCreateInfo->pAttachments[i].format;
237 att->samples = pCreateInfo->pAttachments[i].samples;
238 att->load_op = pCreateInfo->pAttachments[i].loadOp;
239 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
240 att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
241 att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
242 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
243 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
244 }
245 uint32_t subpass_attachment_count = 0;
246 struct radv_subpass_attachment *p;
247 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
248 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
249
250 subpass_attachment_count +=
251 desc->inputAttachmentCount +
252 desc->colorAttachmentCount +
253 (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
254 (desc->pDepthStencilAttachment != NULL);
255 }
256
257 if (subpass_attachment_count) {
258 pass->subpass_attachments =
259 vk_alloc2(&device->alloc, pAllocator,
260 subpass_attachment_count * sizeof(struct radv_subpass_attachment), 8,
261 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
262 if (pass->subpass_attachments == NULL) {
263 vk_free2(&device->alloc, pAllocator, pass);
264 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
265 }
266 } else
267 pass->subpass_attachments = NULL;
268
269 p = pass->subpass_attachments;
270 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
271 const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
272 uint32_t color_sample_count = 1, depth_sample_count = 1;
273 struct radv_subpass *subpass = &pass->subpasses[i];
274
275 subpass->input_count = desc->inputAttachmentCount;
276 subpass->color_count = desc->colorAttachmentCount;
277 subpass->view_mask = desc->viewMask;
278
279 if (desc->inputAttachmentCount > 0) {
280 subpass->input_attachments = p;
281 p += desc->inputAttachmentCount;
282
283 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
284 subpass->input_attachments[j] = (struct radv_subpass_attachment) {
285 .attachment = desc->pInputAttachments[j].attachment,
286 .layout = desc->pInputAttachments[j].layout,
287 };
288 }
289 }
290
291 if (desc->colorAttachmentCount > 0) {
292 subpass->color_attachments = p;
293 p += desc->colorAttachmentCount;
294
295 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
296 subpass->color_attachments[j] = (struct radv_subpass_attachment) {
297 .attachment = desc->pColorAttachments[j].attachment,
298 .layout = desc->pColorAttachments[j].layout,
299 };
300 if (desc->pColorAttachments[j].attachment != VK_ATTACHMENT_UNUSED) {
301 color_sample_count = pCreateInfo->pAttachments[desc->pColorAttachments[j].attachment].samples;
302 }
303 }
304 }
305
306 subpass->has_resolve = false;
307 if (desc->pResolveAttachments) {
308 subpass->resolve_attachments = p;
309 p += desc->colorAttachmentCount;
310
311 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
312 uint32_t a = desc->pResolveAttachments[j].attachment;
313 subpass->resolve_attachments[j] = (struct radv_subpass_attachment) {
314 .attachment = desc->pResolveAttachments[j].attachment,
315 .layout = desc->pResolveAttachments[j].layout,
316 };
317 if (a != VK_ATTACHMENT_UNUSED) {
318 subpass->has_resolve = true;
319 }
320 }
321 }
322
323 if (desc->pDepthStencilAttachment) {
324 subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
325 .attachment = desc->pDepthStencilAttachment->attachment,
326 .layout = desc->pDepthStencilAttachment->layout,
327 };
328 if (desc->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
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