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_add_subpass_dep(struct radv_render_pass
*pass
,
33 const VkSubpassDependency2KHR
*dep
)
35 uint32_t src
= dep
->srcSubpass
;
36 uint32_t dst
= dep
->dstSubpass
;
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.
45 /* Accumulate all ingoing external dependencies to the first subpass. */
46 if (src
== VK_SUBPASS_EXTERNAL
)
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
;
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
;
63 radv_render_pass_compile(struct radv_render_pass
*pass
)
65 for (uint32_t i
= 0; i
< pass
->subpass_count
; i
++) {
66 struct radv_subpass
*subpass
= &pass
->subpasses
[i
];
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
)
74 struct radv_render_pass_attachment
*pass_att
=
75 &pass
->attachments
[subpass_att
->attachment
];
77 pass_att
->first_subpass_idx
= UINT32_MAX
;
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;
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
90 if (subpass
->depth_stencil_attachment
&&
91 subpass
->depth_stencil_attachment
->attachment
== VK_ATTACHMENT_UNUSED
)
92 subpass
->depth_stencil_attachment
= NULL
;
94 if (subpass
->ds_resolve_attachment
&&
95 subpass
->ds_resolve_attachment
->attachment
== VK_ATTACHMENT_UNUSED
)
96 subpass
->ds_resolve_attachment
= NULL
;
98 for (uint32_t j
= 0; j
< subpass
->attachment_count
; j
++) {
99 struct radv_subpass_attachment
*subpass_att
=
100 &subpass
->attachments
[j
];
101 if (subpass_att
->attachment
== VK_ATTACHMENT_UNUSED
)
104 struct radv_render_pass_attachment
*pass_att
=
105 &pass
->attachments
[subpass_att
->attachment
];
107 if (i
< pass_att
->first_subpass_idx
)
108 pass_att
->first_subpass_idx
= i
;
109 pass_att
->last_subpass_idx
= i
;
112 subpass
->has_color_att
= false;
113 for (uint32_t j
= 0; j
< subpass
->color_count
; j
++) {
114 struct radv_subpass_attachment
*subpass_att
=
115 &subpass
->color_attachments
[j
];
116 if (subpass_att
->attachment
== VK_ATTACHMENT_UNUSED
)
119 subpass
->has_color_att
= true;
121 struct radv_render_pass_attachment
*pass_att
=
122 &pass
->attachments
[subpass_att
->attachment
];
124 color_sample_count
= pass_att
->samples
;
127 if (subpass
->depth_stencil_attachment
) {
129 subpass
->depth_stencil_attachment
->attachment
;
130 struct radv_render_pass_attachment
*pass_att
=
131 &pass
->attachments
[a
];
132 depth_sample_count
= pass_att
->samples
;
135 subpass
->max_sample_count
= MAX2(color_sample_count
,
138 /* We have to handle resolve attachments specially */
139 subpass
->has_color_resolve
= false;
140 if (subpass
->resolve_attachments
) {
141 for (uint32_t j
= 0; j
< subpass
->color_count
; j
++) {
142 struct radv_subpass_attachment
*resolve_att
=
143 &subpass
->resolve_attachments
[j
];
145 if (resolve_att
->attachment
== VK_ATTACHMENT_UNUSED
)
148 subpass
->has_color_resolve
= true;
155 radv_num_subpass_attachments(const VkSubpassDescription
*desc
)
157 return desc
->inputAttachmentCount
+
158 desc
->colorAttachmentCount
+
159 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
160 (desc
->pDepthStencilAttachment
!= NULL
);
163 VkResult
radv_CreateRenderPass(
165 const VkRenderPassCreateInfo
* pCreateInfo
,
166 const VkAllocationCallbacks
* pAllocator
,
167 VkRenderPass
* pRenderPass
)
169 RADV_FROM_HANDLE(radv_device
, device
, _device
);
170 struct radv_render_pass
*pass
;
172 size_t attachments_offset
;
173 VkRenderPassMultiviewCreateInfo
*multiview_info
= NULL
;
175 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
177 size
= sizeof(*pass
);
178 size
+= pCreateInfo
->subpassCount
* sizeof(pass
->subpasses
[0]);
179 attachments_offset
= size
;
180 size
+= pCreateInfo
->attachmentCount
* sizeof(pass
->attachments
[0]);
182 pass
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
183 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
185 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
187 memset(pass
, 0, size
);
188 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
189 pass
->subpass_count
= pCreateInfo
->subpassCount
;
190 pass
->attachments
= (void *) pass
+ attachments_offset
;
192 vk_foreach_struct(ext
, pCreateInfo
->pNext
) {
194 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO
:
195 multiview_info
= (VkRenderPassMultiviewCreateInfo
*)ext
;
202 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
203 struct radv_render_pass_attachment
*att
= &pass
->attachments
[i
];
205 att
->format
= pCreateInfo
->pAttachments
[i
].format
;
206 att
->samples
= pCreateInfo
->pAttachments
[i
].samples
;
207 att
->load_op
= pCreateInfo
->pAttachments
[i
].loadOp
;
208 att
->stencil_load_op
= pCreateInfo
->pAttachments
[i
].stencilLoadOp
;
209 att
->initial_layout
= pCreateInfo
->pAttachments
[i
].initialLayout
;
210 att
->final_layout
= pCreateInfo
->pAttachments
[i
].finalLayout
;
211 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
212 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
214 uint32_t subpass_attachment_count
= 0;
215 struct radv_subpass_attachment
*p
;
216 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
217 subpass_attachment_count
+=
218 radv_num_subpass_attachments(&pCreateInfo
->pSubpasses
[i
]);
221 if (subpass_attachment_count
) {
222 pass
->subpass_attachments
=
223 vk_alloc2(&device
->alloc
, pAllocator
,
224 subpass_attachment_count
* sizeof(struct radv_subpass_attachment
), 8,
225 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
226 if (pass
->subpass_attachments
== NULL
) {
227 vk_free2(&device
->alloc
, pAllocator
, pass
);
228 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
231 pass
->subpass_attachments
= NULL
;
233 p
= pass
->subpass_attachments
;
234 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
235 const VkSubpassDescription
*desc
= &pCreateInfo
->pSubpasses
[i
];
236 struct radv_subpass
*subpass
= &pass
->subpasses
[i
];
238 subpass
->input_count
= desc
->inputAttachmentCount
;
239 subpass
->color_count
= desc
->colorAttachmentCount
;
240 subpass
->attachment_count
= radv_num_subpass_attachments(desc
);
241 subpass
->attachments
= p
;
244 subpass
->view_mask
= multiview_info
->pViewMasks
[i
];
246 if (desc
->inputAttachmentCount
> 0) {
247 subpass
->input_attachments
= p
;
248 p
+= desc
->inputAttachmentCount
;
250 for (uint32_t j
= 0; j
< desc
->inputAttachmentCount
; j
++) {
251 subpass
->input_attachments
[j
] = (struct radv_subpass_attachment
) {
252 .attachment
= desc
->pInputAttachments
[j
].attachment
,
253 .layout
= desc
->pInputAttachments
[j
].layout
,
258 if (desc
->colorAttachmentCount
> 0) {
259 subpass
->color_attachments
= p
;
260 p
+= desc
->colorAttachmentCount
;
262 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
263 subpass
->color_attachments
[j
] = (struct radv_subpass_attachment
) {
264 .attachment
= desc
->pColorAttachments
[j
].attachment
,
265 .layout
= desc
->pColorAttachments
[j
].layout
,
270 if (desc
->pResolveAttachments
) {
271 subpass
->resolve_attachments
= p
;
272 p
+= desc
->colorAttachmentCount
;
274 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
275 subpass
->resolve_attachments
[j
] = (struct radv_subpass_attachment
) {
276 .attachment
= desc
->pResolveAttachments
[j
].attachment
,
277 .layout
= desc
->pResolveAttachments
[j
].layout
,
282 if (desc
->pDepthStencilAttachment
) {
283 subpass
->depth_stencil_attachment
= p
++;
285 *subpass
->depth_stencil_attachment
= (struct radv_subpass_attachment
) {
286 .attachment
= desc
->pDepthStencilAttachment
->attachment
,
287 .layout
= desc
->pDepthStencilAttachment
->layout
,
292 for (unsigned i
= 0; i
< pCreateInfo
->dependencyCount
; ++i
) {
293 /* Convert to a Dependency2KHR */
294 struct VkSubpassDependency2KHR dep2
= {
295 .srcSubpass
= pCreateInfo
->pDependencies
[i
].srcSubpass
,
296 .dstSubpass
= pCreateInfo
->pDependencies
[i
].dstSubpass
,
297 .srcStageMask
= pCreateInfo
->pDependencies
[i
].srcStageMask
,
298 .dstStageMask
= pCreateInfo
->pDependencies
[i
].dstStageMask
,
299 .srcAccessMask
= pCreateInfo
->pDependencies
[i
].srcAccessMask
,
300 .dstAccessMask
= pCreateInfo
->pDependencies
[i
].dstAccessMask
,
301 .dependencyFlags
= pCreateInfo
->pDependencies
[i
].dependencyFlags
,
303 radv_render_pass_add_subpass_dep(pass
, &dep2
);
306 radv_render_pass_compile(pass
);
308 *pRenderPass
= radv_render_pass_to_handle(pass
);
314 radv_num_subpass_attachments2(const VkSubpassDescription2KHR
*desc
)
316 const VkSubpassDescriptionDepthStencilResolveKHR
*ds_resolve
=
317 vk_find_struct_const(desc
->pNext
,
318 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR
);
320 return desc
->inputAttachmentCount
+
321 desc
->colorAttachmentCount
+
322 (desc
->pResolveAttachments
? desc
->colorAttachmentCount
: 0) +
323 (desc
->pDepthStencilAttachment
!= NULL
) +
324 (ds_resolve
&& ds_resolve
->pDepthStencilResolveAttachment
);
327 VkResult
radv_CreateRenderPass2KHR(
329 const VkRenderPassCreateInfo2KHR
* pCreateInfo
,
330 const VkAllocationCallbacks
* pAllocator
,
331 VkRenderPass
* pRenderPass
)
333 RADV_FROM_HANDLE(radv_device
, device
, _device
);
334 struct radv_render_pass
*pass
;
336 size_t attachments_offset
;
338 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR
);
340 size
= sizeof(*pass
);
341 size
+= pCreateInfo
->subpassCount
* sizeof(pass
->subpasses
[0]);
342 attachments_offset
= size
;
343 size
+= pCreateInfo
->attachmentCount
* sizeof(pass
->attachments
[0]);
345 pass
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
346 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
348 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
350 memset(pass
, 0, size
);
351 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
352 pass
->subpass_count
= pCreateInfo
->subpassCount
;
353 pass
->attachments
= (void *) pass
+ attachments_offset
;
355 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
356 struct radv_render_pass_attachment
*att
= &pass
->attachments
[i
];
358 att
->format
= pCreateInfo
->pAttachments
[i
].format
;
359 att
->samples
= pCreateInfo
->pAttachments
[i
].samples
;
360 att
->load_op
= pCreateInfo
->pAttachments
[i
].loadOp
;
361 att
->stencil_load_op
= pCreateInfo
->pAttachments
[i
].stencilLoadOp
;
362 att
->initial_layout
= pCreateInfo
->pAttachments
[i
].initialLayout
;
363 att
->final_layout
= pCreateInfo
->pAttachments
[i
].finalLayout
;
364 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
365 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
367 uint32_t subpass_attachment_count
= 0;
368 struct radv_subpass_attachment
*p
;
369 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
370 subpass_attachment_count
+=
371 radv_num_subpass_attachments2(&pCreateInfo
->pSubpasses
[i
]);
374 if (subpass_attachment_count
) {
375 pass
->subpass_attachments
=
376 vk_alloc2(&device
->alloc
, pAllocator
,
377 subpass_attachment_count
* sizeof(struct radv_subpass_attachment
), 8,
378 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
379 if (pass
->subpass_attachments
== NULL
) {
380 vk_free2(&device
->alloc
, pAllocator
, pass
);
381 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
384 pass
->subpass_attachments
= NULL
;
386 p
= pass
->subpass_attachments
;
387 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
388 const VkSubpassDescription2KHR
*desc
= &pCreateInfo
->pSubpasses
[i
];
389 struct radv_subpass
*subpass
= &pass
->subpasses
[i
];
391 subpass
->input_count
= desc
->inputAttachmentCount
;
392 subpass
->color_count
= desc
->colorAttachmentCount
;
393 subpass
->attachment_count
= radv_num_subpass_attachments2(desc
);
394 subpass
->attachments
= p
;
395 subpass
->view_mask
= desc
->viewMask
;
397 if (desc
->inputAttachmentCount
> 0) {
398 subpass
->input_attachments
= p
;
399 p
+= desc
->inputAttachmentCount
;
401 for (uint32_t j
= 0; j
< desc
->inputAttachmentCount
; j
++) {
402 subpass
->input_attachments
[j
] = (struct radv_subpass_attachment
) {
403 .attachment
= desc
->pInputAttachments
[j
].attachment
,
404 .layout
= desc
->pInputAttachments
[j
].layout
,
409 if (desc
->colorAttachmentCount
> 0) {
410 subpass
->color_attachments
= p
;
411 p
+= desc
->colorAttachmentCount
;
413 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
414 subpass
->color_attachments
[j
] = (struct radv_subpass_attachment
) {
415 .attachment
= desc
->pColorAttachments
[j
].attachment
,
416 .layout
= desc
->pColorAttachments
[j
].layout
,
421 if (desc
->pResolveAttachments
) {
422 subpass
->resolve_attachments
= p
;
423 p
+= desc
->colorAttachmentCount
;
425 for (uint32_t j
= 0; j
< desc
->colorAttachmentCount
; j
++) {
426 subpass
->resolve_attachments
[j
] = (struct radv_subpass_attachment
) {
427 .attachment
= desc
->pResolveAttachments
[j
].attachment
,
428 .layout
= desc
->pResolveAttachments
[j
].layout
,
433 if (desc
->pDepthStencilAttachment
) {
434 subpass
->depth_stencil_attachment
= p
++;
436 *subpass
->depth_stencil_attachment
= (struct radv_subpass_attachment
) {
437 .attachment
= desc
->pDepthStencilAttachment
->attachment
,
438 .layout
= desc
->pDepthStencilAttachment
->layout
,
442 const VkSubpassDescriptionDepthStencilResolveKHR
*ds_resolve
=
443 vk_find_struct_const(desc
->pNext
,
444 SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR
);
446 if (ds_resolve
&& ds_resolve
->pDepthStencilResolveAttachment
) {
447 subpass
->ds_resolve_attachment
= p
++;
449 *subpass
->ds_resolve_attachment
= (struct radv_subpass_attachment
) {
450 .attachment
= ds_resolve
->pDepthStencilResolveAttachment
->attachment
,
451 .layout
= ds_resolve
->pDepthStencilResolveAttachment
->layout
,
454 subpass
->depth_resolve_mode
= ds_resolve
->depthResolveMode
;
455 subpass
->stencil_resolve_mode
= ds_resolve
->stencilResolveMode
;
459 for (unsigned i
= 0; i
< pCreateInfo
->dependencyCount
; ++i
) {
460 radv_render_pass_add_subpass_dep(pass
,
461 &pCreateInfo
->pDependencies
[i
]);
464 radv_render_pass_compile(pass
);
466 *pRenderPass
= radv_render_pass_to_handle(pass
);
471 void radv_DestroyRenderPass(
474 const VkAllocationCallbacks
* pAllocator
)
476 RADV_FROM_HANDLE(radv_device
, device
, _device
);
477 RADV_FROM_HANDLE(radv_render_pass
, pass
, _pass
);
481 vk_free2(&device
->alloc
, pAllocator
, pass
->subpass_attachments
);
482 vk_free2(&device
->alloc
, pAllocator
, pass
);
485 void radv_GetRenderAreaGranularity(
487 VkRenderPass renderPass
,
488 VkExtent2D
* pGranularity
)
490 pGranularity
->width
= 1;
491 pGranularity
->height
= 1;