2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
27 #include "radv_private.h"
32 radv_render_pass_compile(struct radv_render_pass
*pass
)
34 for (uint32_t i
= 0; i
< pass
->subpass_count
; i
++) {
35 struct radv_subpass
*subpass
= &pass
->subpasses
[i
];
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
42 if (subpass
->depth_stencil_attachment
&&
43 subpass
->depth_stencil_attachment
->attachment
== VK_ATTACHMENT_UNUSED
)
44 subpass
->depth_stencil_attachment
= NULL
;
49 radv_num_subpass_attachments(const VkSubpassDescription
*desc
)
51 return desc
->inputAttachmentCount
+
52 desc
->colorAttachmentCount
+
53 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
54 (desc
->pDepthStencilAttachment
!= NULL
);
57 VkResult
radv_CreateRenderPass(
59 const VkRenderPassCreateInfo
* pCreateInfo
,
60 const VkAllocationCallbacks
* pAllocator
,
61 VkRenderPass
* pRenderPass
)
63 RADV_FROM_HANDLE(radv_device
, device
, _device
);
64 struct radv_render_pass
*pass
;
66 size_t attachments_offset
;
67 VkRenderPassMultiviewCreateInfo
*multiview_info
= NULL
;
69 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
72 size
+= pCreateInfo
->subpassCount
* sizeof(pass
->subpasses
[0]);
73 attachments_offset
= size
;
74 size
+= pCreateInfo
->attachmentCount
* sizeof(pass
->attachments
[0]);
76 pass
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
77 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
79 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
81 memset(pass
, 0, size
);
82 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
83 pass
->subpass_count
= pCreateInfo
->subpassCount
;
84 pass
->attachments
= (void *) pass
+ attachments_offset
;
86 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
88 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO
:
89 multiview_info
= (VkRenderPassMultiviewCreateInfo
*)ext
;
96 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
97 struct radv_render_pass_attachment
*att
= &pass
->attachments
[i
];
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;
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
]);
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
);
125 pass
->subpass_attachments
= NULL
;
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
];
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
;
139 subpass
->view_mask
= multiview_info
->pViewMasks
[i
];
141 if (desc
->inputAttachmentCount
> 0) {
142 subpass
->input_attachments
= p
;
143 p
+= desc
->inputAttachmentCount
;
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
,
153 if (desc
->colorAttachmentCount
> 0) {
154 subpass
->color_attachments
= p
;
155 p
+= desc
->colorAttachmentCount
;
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
,
162 if (desc
->pColorAttachments
[j
].attachment
!= VK_ATTACHMENT_UNUSED
) {
163 color_sample_count
= pCreateInfo
->pAttachments
[desc
->pColorAttachments
[j
].attachment
].samples
;
168 subpass
->has_resolve
= false;
169 if (desc
->pResolveAttachments
) {
170 subpass
->resolve_attachments
= p
;
171 p
+= desc
->colorAttachmentCount
;
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
,
179 if (a
!= VK_ATTACHMENT_UNUSED
) {
180 subpass
->has_resolve
= true;
185 if (desc
->pDepthStencilAttachment
) {
186 subpass
->depth_stencil_attachment
= p
++;
188 *subpass
->depth_stencil_attachment
= (struct radv_subpass_attachment
) {
189 .attachment
= desc
->pDepthStencilAttachment
->attachment
,
190 .layout
= desc
->pDepthStencilAttachment
->layout
,
192 if (desc
->pDepthStencilAttachment
->attachment
!= VK_ATTACHMENT_UNUSED
) {
193 depth_sample_count
= pCreateInfo
->pAttachments
[desc
->pDepthStencilAttachment
->attachment
].samples
;
197 subpass
->max_sample_count
= MAX2(color_sample_count
,
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
;
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.
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
;
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
;
224 radv_render_pass_compile(pass
);
226 *pRenderPass
= radv_render_pass_to_handle(pass
);
232 radv_num_subpass_attachments2(const VkSubpassDescription2KHR
*desc
)
234 return desc
->inputAttachmentCount
+
235 desc
->colorAttachmentCount
+
236 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
237 (desc
->pDepthStencilAttachment
!= NULL
);
240 VkResult
radv_CreateRenderPass2KHR(
242 const VkRenderPassCreateInfo2KHR
* pCreateInfo
,
243 const VkAllocationCallbacks
* pAllocator
,
244 VkRenderPass
* pRenderPass
)
246 RADV_FROM_HANDLE(radv_device
, device
, _device
);
247 struct radv_render_pass
*pass
;
249 size_t attachments_offset
;
251 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR
);
253 size
= sizeof(*pass
);
254 size
+= pCreateInfo
->subpassCount
* sizeof(pass
->subpasses
[0]);
255 attachments_offset
= size
;
256 size
+= pCreateInfo
->attachmentCount
* sizeof(pass
->attachments
[0]);
258 pass
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
259 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
261 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
263 memset(pass
, 0, size
);
264 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
265 pass
->subpass_count
= pCreateInfo
->subpassCount
;
266 pass
->attachments
= (void *) pass
+ attachments_offset
;
268 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
269 struct radv_render_pass_attachment
*att
= &pass
->attachments
[i
];
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;
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
]);
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
);
297 pass
->subpass_attachments
= NULL
;
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
];
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
;
311 if (desc
->inputAttachmentCount
> 0) {
312 subpass
->input_attachments
= p
;
313 p
+= desc
->inputAttachmentCount
;
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
,
323 if (desc
->colorAttachmentCount
> 0) {
324 subpass
->color_attachments
= p
;
325 p
+= desc
->colorAttachmentCount
;
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
,
332 if (desc
->pColorAttachments
[j
].attachment
!= VK_ATTACHMENT_UNUSED
) {
333 color_sample_count
= pCreateInfo
->pAttachments
[desc
->pColorAttachments
[j
].attachment
].samples
;
338 subpass
->has_resolve
= false;
339 if (desc
->pResolveAttachments
) {
340 subpass
->resolve_attachments
= p
;
341 p
+= desc
->colorAttachmentCount
;
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
,
349 if (a
!= VK_ATTACHMENT_UNUSED
) {
350 subpass
->has_resolve
= true;
355 if (desc
->pDepthStencilAttachment
) {
356 subpass
->depth_stencil_attachment
= p
++;
358 *subpass
->depth_stencil_attachment
= (struct radv_subpass_attachment
) {
359 .attachment
= desc
->pDepthStencilAttachment
->attachment
,
360 .layout
= desc
->pDepthStencilAttachment
->layout
,
362 if (desc
->pDepthStencilAttachment
->attachment
!= VK_ATTACHMENT_UNUSED
) {
363 depth_sample_count
= pCreateInfo
->pAttachments
[desc
->pDepthStencilAttachment
->attachment
].samples
;
367 subpass
->max_sample_count
= MAX2(color_sample_count
,
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
;
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.
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
;
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
;
394 radv_render_pass_compile(pass
);
396 *pRenderPass
= radv_render_pass_to_handle(pass
);
401 void radv_DestroyRenderPass(
404 const VkAllocationCallbacks
* pAllocator
)
406 RADV_FROM_HANDLE(radv_device
, device
, _device
);
407 RADV_FROM_HANDLE(radv_render_pass
, pass
, _pass
);
411 vk_free2(&device
->alloc
, pAllocator
, pass
->subpass_attachments
);
412 vk_free2(&device
->alloc
, pAllocator
, pass
);
415 void radv_GetRenderAreaGranularity(
417 VkRenderPass renderPass
,
418 VkExtent2D
* pGranularity
)
420 pGranularity
->width
= 1;
421 pGranularity
->height
= 1;