Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/intel/vulkan/anv_pass.c
4547 views
1
/*
2
* Copyright © 2015 Intel Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#include "anv_private.h"
25
26
#include "vk_format.h"
27
#include "vk_util.h"
28
29
static void
30
anv_render_pass_add_subpass_dep(struct anv_device *device,
31
struct anv_render_pass *pass,
32
const VkSubpassDependency2KHR *dep)
33
{
34
if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
35
pass->subpass_flushes[pass->subpass_count] |=
36
anv_pipe_invalidate_bits_for_access_flags(device, dep->dstAccessMask);
37
} else {
38
assert(dep->dstSubpass < pass->subpass_count);
39
pass->subpass_flushes[dep->dstSubpass] |=
40
anv_pipe_invalidate_bits_for_access_flags(device, dep->dstAccessMask);
41
}
42
43
if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
44
pass->subpass_flushes[0] |=
45
anv_pipe_flush_bits_for_access_flags(device, dep->srcAccessMask);
46
} else {
47
assert(dep->srcSubpass < pass->subpass_count);
48
pass->subpass_flushes[dep->srcSubpass + 1] |=
49
anv_pipe_flush_bits_for_access_flags(device, dep->srcAccessMask);
50
}
51
}
52
53
/* Do a second "compile" step on a render pass */
54
static void
55
anv_render_pass_compile(struct anv_render_pass *pass)
56
{
57
/* The CreateRenderPass code zeros the entire render pass and also uses a
58
* designated initializer for filling these out. There's no need for us to
59
* do it again.
60
*
61
* for (uint32_t i = 0; i < pass->attachment_count; i++) {
62
* pass->attachments[i].usage = 0;
63
* pass->attachments[i].first_subpass_layout = VK_IMAGE_LAYOUT_UNDEFINED;
64
* }
65
*/
66
67
VkImageUsageFlags all_usage = 0;
68
for (uint32_t i = 0; i < pass->subpass_count; i++) {
69
struct anv_subpass *subpass = &pass->subpasses[i];
70
71
/* We don't allow depth_stencil_attachment to be non-NULL and be
72
* VK_ATTACHMENT_UNUSED. This way something can just check for NULL
73
* and be guaranteed that they have a valid attachment.
74
*/
75
if (subpass->depth_stencil_attachment &&
76
subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
77
subpass->depth_stencil_attachment = NULL;
78
79
if (subpass->ds_resolve_attachment &&
80
subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
81
subpass->ds_resolve_attachment = NULL;
82
83
for (uint32_t j = 0; j < subpass->attachment_count; j++) {
84
struct anv_subpass_attachment *subpass_att = &subpass->attachments[j];
85
if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
86
continue;
87
88
struct anv_render_pass_attachment *pass_att =
89
&pass->attachments[subpass_att->attachment];
90
91
pass_att->usage |= subpass_att->usage;
92
pass_att->last_subpass_idx = i;
93
94
all_usage |= subpass_att->usage;
95
96
if (pass_att->first_subpass_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
97
pass_att->first_subpass_layout = subpass_att->layout;
98
assert(pass_att->first_subpass_layout != VK_IMAGE_LAYOUT_UNDEFINED);
99
}
100
101
if (subpass_att->usage == VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
102
subpass->depth_stencil_attachment &&
103
subpass_att->attachment == subpass->depth_stencil_attachment->attachment)
104
subpass->has_ds_self_dep = true;
105
}
106
107
/* We have to handle resolve attachments specially */
108
subpass->has_color_resolve = false;
109
if (subpass->resolve_attachments) {
110
for (uint32_t j = 0; j < subpass->color_count; j++) {
111
struct anv_subpass_attachment *color_att =
112
&subpass->color_attachments[j];
113
struct anv_subpass_attachment *resolve_att =
114
&subpass->resolve_attachments[j];
115
if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
116
continue;
117
118
subpass->has_color_resolve = true;
119
120
assert(color_att->attachment < pass->attachment_count);
121
struct anv_render_pass_attachment *color_pass_att =
122
&pass->attachments[color_att->attachment];
123
124
assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
125
assert(color_att->usage == VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
126
color_pass_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
127
}
128
}
129
130
if (subpass->ds_resolve_attachment) {
131
struct anv_subpass_attachment *ds_att =
132
subpass->depth_stencil_attachment;
133
UNUSED struct anv_subpass_attachment *resolve_att =
134
subpass->ds_resolve_attachment;
135
136
assert(ds_att->attachment < pass->attachment_count);
137
struct anv_render_pass_attachment *ds_pass_att =
138
&pass->attachments[ds_att->attachment];
139
140
assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);
141
assert(ds_att->usage == VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
142
ds_pass_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
143
}
144
145
for (uint32_t j = 0; j < subpass->attachment_count; j++)
146
assert(__builtin_popcount(subpass->attachments[j].usage) == 1);
147
}
148
149
/* From the Vulkan 1.0.39 spec:
150
*
151
* If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
152
* first subpass that uses an attachment, then an implicit subpass
153
* dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
154
* used in. The subpass dependency operates as if defined with the
155
* following parameters:
156
*
157
* VkSubpassDependency implicitDependency = {
158
* .srcSubpass = VK_SUBPASS_EXTERNAL;
159
* .dstSubpass = firstSubpass; // First subpass attachment is used in
160
* .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
161
* .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
162
* .srcAccessMask = 0;
163
* .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
164
* VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
165
* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
166
* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
167
* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
168
* .dependencyFlags = 0;
169
* };
170
*
171
* Similarly, if there is no subpass dependency from the last subpass
172
* that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
173
* subpass dependency exists from the last subpass it is used in to
174
* VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
175
* with the following parameters:
176
*
177
* VkSubpassDependency implicitDependency = {
178
* .srcSubpass = lastSubpass; // Last subpass attachment is used in
179
* .dstSubpass = VK_SUBPASS_EXTERNAL;
180
* .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
181
* .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
182
* .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
183
* VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
184
* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
185
* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
186
* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
187
* .dstAccessMask = 0;
188
* .dependencyFlags = 0;
189
* };
190
*
191
* We could implement this by walking over all of the attachments and
192
* subpasses and checking to see if any of them don't have an external
193
* dependency. Or, we could just be lazy and add a couple extra flushes.
194
* We choose to be lazy.
195
*
196
* From the documentation for vkCmdNextSubpass:
197
*
198
* "Moving to the next subpass automatically performs any multisample
199
* resolve operations in the subpass being ended. End-of-subpass
200
* multisample resolves are treated as color attachment writes for the
201
* purposes of synchronization. This applies to resolve operations for
202
* both color and depth/stencil attachments. That is, they are
203
* considered to execute in the
204
* VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT pipeline stage and
205
* their writes are synchronized with
206
* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT."
207
*
208
* Therefore, the above flags concerning color attachments also apply to
209
* color and depth/stencil resolve attachments.
210
*/
211
if (all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
212
pass->subpass_flushes[0] |=
213
ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
214
}
215
if (all_usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
216
VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
217
pass->subpass_flushes[pass->subpass_count] |=
218
ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
219
}
220
if (all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
221
pass->subpass_flushes[pass->subpass_count] |=
222
ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
223
}
224
}
225
226
static unsigned
227
num_subpass_attachments2(const VkSubpassDescription2KHR *desc)
228
{
229
const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
230
vk_find_struct_const(desc->pNext,
231
SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
232
233
return desc->inputAttachmentCount +
234
desc->colorAttachmentCount +
235
(desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
236
(desc->pDepthStencilAttachment != NULL) +
237
(ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
238
}
239
240
static bool
241
vk_image_layout_depth_only(VkImageLayout layout)
242
{
243
switch (layout) {
244
case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
245
case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
246
return true;
247
248
default:
249
return false;
250
}
251
}
252
253
/* From the Vulkan Specification 1.2.166 - VkAttachmentReference2:
254
*
255
* "If layout only specifies the layout of the depth aspect of the
256
* attachment, the layout of the stencil aspect is specified by the
257
* stencilLayout member of a VkAttachmentReferenceStencilLayout structure
258
* included in the pNext chain. Otherwise, layout describes the layout for
259
* all relevant image aspects."
260
*/
261
static VkImageLayout
262
stencil_ref_layout(const VkAttachmentReference2KHR *att_ref)
263
{
264
if (!vk_image_layout_depth_only(att_ref->layout))
265
return att_ref->layout;
266
267
const VkAttachmentReferenceStencilLayoutKHR *stencil_ref =
268
vk_find_struct_const(att_ref->pNext,
269
ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
270
if (!stencil_ref)
271
return VK_IMAGE_LAYOUT_UNDEFINED;
272
return stencil_ref->stencilLayout;
273
}
274
275
/* From the Vulkan Specification 1.2.166 - VkAttachmentDescription2:
276
*
277
* "If format is a depth/stencil format, and initialLayout only specifies
278
* the initial layout of the depth aspect of the attachment, the initial
279
* layout of the stencil aspect is specified by the stencilInitialLayout
280
* member of a VkAttachmentDescriptionStencilLayout structure included in
281
* the pNext chain. Otherwise, initialLayout describes the initial layout
282
* for all relevant image aspects."
283
*/
284
static VkImageLayout
285
stencil_desc_layout(const VkAttachmentDescription2KHR *att_desc, bool final)
286
{
287
if (!vk_format_has_stencil(att_desc->format))
288
return VK_IMAGE_LAYOUT_UNDEFINED;
289
290
const VkImageLayout main_layout =
291
final ? att_desc->finalLayout : att_desc->initialLayout;
292
if (!vk_image_layout_depth_only(main_layout))
293
return main_layout;
294
295
const VkAttachmentDescriptionStencilLayoutKHR *stencil_desc =
296
vk_find_struct_const(att_desc->pNext,
297
ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
298
assert(stencil_desc);
299
return final ?
300
stencil_desc->stencilFinalLayout :
301
stencil_desc->stencilInitialLayout;
302
}
303
304
VkResult anv_CreateRenderPass2(
305
VkDevice _device,
306
const VkRenderPassCreateInfo2KHR* pCreateInfo,
307
const VkAllocationCallbacks* pAllocator,
308
VkRenderPass* pRenderPass)
309
{
310
ANV_FROM_HANDLE(anv_device, device, _device);
311
312
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);
313
314
VK_MULTIALLOC(ma);
315
VK_MULTIALLOC_DECL(&ma, struct anv_render_pass, pass, 1);
316
VK_MULTIALLOC_DECL(&ma, struct anv_subpass, subpasses,
317
pCreateInfo->subpassCount);
318
VK_MULTIALLOC_DECL(&ma, struct anv_render_pass_attachment, attachments,
319
pCreateInfo->attachmentCount);
320
VK_MULTIALLOC_DECL(&ma, enum anv_pipe_bits, subpass_flushes,
321
pCreateInfo->subpassCount + 1);
322
323
uint32_t subpass_attachment_count = 0;
324
for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
325
subpass_attachment_count +=
326
num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
327
}
328
VK_MULTIALLOC_DECL(&ma, struct anv_subpass_attachment, subpass_attachments,
329
subpass_attachment_count);
330
331
if (!vk_object_multizalloc(&device->vk, &ma, pAllocator,
332
VK_OBJECT_TYPE_RENDER_PASS))
333
return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
334
335
/* Clear the subpasses along with the parent pass. This required because
336
* each array member of anv_subpass must be a valid pointer if not NULL.
337
*/
338
pass->attachment_count = pCreateInfo->attachmentCount;
339
pass->subpass_count = pCreateInfo->subpassCount;
340
pass->attachments = attachments;
341
pass->subpass_flushes = subpass_flushes;
342
343
for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
344
pass->attachments[i] = (struct anv_render_pass_attachment) {
345
.format = pCreateInfo->pAttachments[i].format,
346
.samples = pCreateInfo->pAttachments[i].samples,
347
.load_op = pCreateInfo->pAttachments[i].loadOp,
348
.store_op = pCreateInfo->pAttachments[i].storeOp,
349
.stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,
350
.initial_layout = pCreateInfo->pAttachments[i].initialLayout,
351
.final_layout = pCreateInfo->pAttachments[i].finalLayout,
352
353
.stencil_initial_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i],
354
false),
355
.stencil_final_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i],
356
true),
357
};
358
}
359
360
for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
361
const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];
362
struct anv_subpass *subpass = &pass->subpasses[i];
363
364
subpass->input_count = desc->inputAttachmentCount;
365
subpass->color_count = desc->colorAttachmentCount;
366
subpass->attachment_count = num_subpass_attachments2(desc);
367
subpass->attachments = subpass_attachments;
368
subpass->view_mask = desc->viewMask;
369
370
if (desc->inputAttachmentCount > 0) {
371
subpass->input_attachments = subpass_attachments;
372
subpass_attachments += desc->inputAttachmentCount;
373
374
for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
375
subpass->input_attachments[j] = (struct anv_subpass_attachment) {
376
.usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
377
.attachment = desc->pInputAttachments[j].attachment,
378
.layout = desc->pInputAttachments[j].layout,
379
.stencil_layout = stencil_ref_layout(&desc->pInputAttachments[j]),
380
};
381
}
382
}
383
384
if (desc->colorAttachmentCount > 0) {
385
subpass->color_attachments = subpass_attachments;
386
subpass_attachments += desc->colorAttachmentCount;
387
388
for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
389
subpass->color_attachments[j] = (struct anv_subpass_attachment) {
390
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
391
.attachment = desc->pColorAttachments[j].attachment,
392
.layout = desc->pColorAttachments[j].layout,
393
};
394
}
395
}
396
397
if (desc->pResolveAttachments) {
398
subpass->resolve_attachments = subpass_attachments;
399
subpass_attachments += desc->colorAttachmentCount;
400
401
for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
402
subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {
403
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
404
.attachment = desc->pResolveAttachments[j].attachment,
405
.layout = desc->pResolveAttachments[j].layout,
406
};
407
}
408
}
409
410
if (desc->pDepthStencilAttachment) {
411
subpass->depth_stencil_attachment = subpass_attachments++;
412
413
*subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {
414
.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
415
.attachment = desc->pDepthStencilAttachment->attachment,
416
.layout = desc->pDepthStencilAttachment->layout,
417
.stencil_layout = stencil_ref_layout(desc->pDepthStencilAttachment),
418
};
419
}
420
421
const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =
422
vk_find_struct_const(desc->pNext,
423
SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);
424
425
if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
426
subpass->ds_resolve_attachment = subpass_attachments++;
427
428
*subpass->ds_resolve_attachment = (struct anv_subpass_attachment) {
429
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
430
.attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
431
.layout = ds_resolve->pDepthStencilResolveAttachment->layout,
432
.stencil_layout = stencil_ref_layout(ds_resolve->pDepthStencilResolveAttachment),
433
};
434
subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
435
subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
436
}
437
}
438
439
for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
440
anv_render_pass_add_subpass_dep(device, pass,
441
&pCreateInfo->pDependencies[i]);
442
}
443
444
vk_foreach_struct(ext, pCreateInfo->pNext) {
445
switch (ext->sType) {
446
default:
447
anv_debug_ignored_stype(ext->sType);
448
}
449
}
450
451
anv_render_pass_compile(pass);
452
453
*pRenderPass = anv_render_pass_to_handle(pass);
454
455
return VK_SUCCESS;
456
}
457
458
void anv_DestroyRenderPass(
459
VkDevice _device,
460
VkRenderPass _pass,
461
const VkAllocationCallbacks* pAllocator)
462
{
463
ANV_FROM_HANDLE(anv_device, device, _device);
464
ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
465
466
if (!pass)
467
return;
468
469
vk_object_free(&device->vk, pAllocator, pass);
470
}
471
472
void anv_GetRenderAreaGranularity(
473
VkDevice device,
474
VkRenderPass renderPass,
475
VkExtent2D* pGranularity)
476
{
477
ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
478
479
/* This granularity satisfies HiZ fast clear alignment requirements
480
* for all sample counts.
481
*/
482
for (unsigned i = 0; i < pass->subpass_count; ++i) {
483
if (pass->subpasses[i].depth_stencil_attachment) {
484
*pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
485
return;
486
}
487
}
488
489
*pGranularity = (VkExtent2D) { 1, 1 };
490
}
491
492