-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathRenderPass.cpp
More file actions
570 lines (485 loc) · 28.3 KB
/
RenderPass.cpp
File metadata and controls
570 lines (485 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/core/Exception.h>
#include <vsg/core/ScratchMemory.h>
#include <vsg/io/Logger.h>
#include <vsg/vk/RenderPass.h>
#include <array>
using namespace vsg;
inline VkSampleCountFlagBits computeMaxSamples(const RenderPass::Attachments& attachments)
{
VkSampleCountFlagBits maxSamples = VK_SAMPLE_COUNT_1_BIT;
for (const auto& attachment : attachments)
{
if (attachment.samples > maxSamples) maxSamples = attachment.samples;
}
return maxSamples;
}
RenderPass::RenderPass(Device* in_device, const Attachments& in_attachments, const Subpasses& in_subpasses, const Dependencies& in_dependencies, const CorrelatedViewMasks& in_correlatedViewMasks) :
device(in_device),
attachments(in_attachments),
subpasses(in_subpasses),
dependencies(in_dependencies),
correlatedViewMasks(in_correlatedViewMasks),
maxSamples(computeMaxSamples(in_attachments))
{
auto scratchMemory = ScratchMemory::create(1024);
// vkCreateRenderPass2 is supported with the VK_KHR_create_renderpass2 device extension, or in Vulkan core 1.2 and later
auto extensions = device->getExtensions();
if (extensions->vkCreateRenderPass2)
{
// vkCreateRenderPass2 code path
auto copyAttachmentDescriptions = [&scratchMemory](const Attachments& attachmentDescriptions) -> VkAttachmentDescription2* {
if (attachmentDescriptions.empty()) return nullptr;
auto vk_attachmentDescriptions = scratchMemory->allocate<VkAttachmentDescription2>(attachmentDescriptions.size());
for (size_t i = 0; i < attachmentDescriptions.size(); ++i)
{
auto& src = attachmentDescriptions[i];
auto& dst = vk_attachmentDescriptions[i];
dst.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
dst.pNext = nullptr;
dst.flags = src.flags;
dst.format = src.format;
dst.samples = src.samples;
dst.loadOp = src.loadOp;
dst.storeOp = src.storeOp;
dst.stencilLoadOp = src.stencilLoadOp;
dst.stencilStoreOp = src.stencilStoreOp;
dst.initialLayout = src.initialLayout;
dst.finalLayout = src.finalLayout;
}
return vk_attachmentDescriptions;
};
auto copySubpasses = [&scratchMemory](const Subpasses& subpassDescriptions) -> VkSubpassDescription2* {
if (subpassDescriptions.empty()) return nullptr;
auto copyAttachmentReference = [&scratchMemory](const std::vector<AttachmentReference>& attachmentReferences) -> VkAttachmentReference2* {
if (attachmentReferences.empty()) return nullptr;
auto vk_attachments = scratchMemory->allocate<VkAttachmentReference2>(attachmentReferences.size());
for (size_t i = 0; i < attachmentReferences.size(); ++i)
{
auto& src = attachmentReferences[i];
auto& dst = vk_attachments[i];
dst.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2;
dst.pNext = nullptr;
dst.attachment = src.attachment;
dst.layout = src.layout;
dst.aspectMask = src.aspectMask;
}
return vk_attachments;
};
auto vk_subpassDescription = scratchMemory->allocate<VkSubpassDescription2>(subpassDescriptions.size());
for (size_t i = 0; i < subpassDescriptions.size(); ++i)
{
auto& src = subpassDescriptions[i];
auto& dst = vk_subpassDescription[i];
dst.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2;
dst.pNext = nullptr;
dst.flags = src.flags;
dst.pipelineBindPoint = src.pipelineBindPoint;
dst.viewMask = src.viewMask;
dst.inputAttachmentCount = static_cast<uint32_t>(src.inputAttachments.size());
dst.pInputAttachments = copyAttachmentReference(src.inputAttachments);
dst.colorAttachmentCount = static_cast<uint32_t>(src.colorAttachments.size());
dst.pColorAttachments = copyAttachmentReference(src.colorAttachments);
dst.pResolveAttachments = copyAttachmentReference(src.resolveAttachments);
dst.pDepthStencilAttachment = copyAttachmentReference(src.depthStencilAttachments);
dst.preserveAttachmentCount = static_cast<uint32_t>(src.preserveAttachments.size());
dst.pPreserveAttachments = src.preserveAttachments.empty() ? nullptr : src.preserveAttachments.data();
if (!src.depthStencilResolveAttachments.empty())
{
auto depthStencilResolve = scratchMemory->allocate<VkSubpassDescriptionDepthStencilResolve>();
depthStencilResolve->sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE;
depthStencilResolve->pNext = nullptr;
depthStencilResolve->depthResolveMode = src.depthResolveMode;
depthStencilResolve->stencilResolveMode = src.stencilResolveMode;
depthStencilResolve->pDepthStencilResolveAttachment = copyAttachmentReference(src.depthStencilResolveAttachments);
;
dst.pNext = depthStencilResolve;
}
}
return vk_subpassDescription;
};
auto copySubpassDependency = [&scratchMemory](const Dependencies& subpassDependency) -> VkSubpassDependency2* {
if (subpassDependency.empty()) return nullptr;
auto vk_subpassDependencies = scratchMemory->allocate<VkSubpassDependency2>(subpassDependency.size());
for (size_t i = 0; i < subpassDependency.size(); ++i)
{
auto& src = subpassDependency[i];
auto& dst = vk_subpassDependencies[i];
dst.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
dst.pNext = nullptr;
dst.srcSubpass = src.srcSubpass;
dst.dstSubpass = src.dstSubpass;
dst.srcStageMask = src.srcStageMask;
dst.dstStageMask = src.dstStageMask;
dst.srcAccessMask = src.srcAccessMask;
dst.dstAccessMask = src.dstAccessMask;
dst.dependencyFlags = src.dependencyFlags;
dst.viewOffset = src.viewOffset;
}
return vk_subpassDependencies;
};
VkRenderPassCreateInfo2 renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassInfo.pAttachments = copyAttachmentDescriptions(attachments);
renderPassInfo.subpassCount = static_cast<uint32_t>(subpasses.size());
renderPassInfo.pSubpasses = copySubpasses(subpasses);
renderPassInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
renderPassInfo.pDependencies = copySubpassDependency(dependencies);
renderPassInfo.correlatedViewMaskCount = static_cast<uint32_t>(correlatedViewMasks.size());
renderPassInfo.pCorrelatedViewMasks = correlatedViewMasks.empty() ? nullptr : correlatedViewMasks.data();
VkResult result = extensions->vkCreateRenderPass2(*device, &renderPassInfo, device->getAllocationCallbacks(), &_renderPass);
if (result != VK_SUCCESS)
{
throw Exception{"Error: vsg::RenderPass::create(...) Failed to create VkRenderPass.", result};
}
}
else
{
// Vulkan 1.0 vkCreateRenderPass code path
auto copyAttachmentDescriptions = [&scratchMemory](const Attachments& attachmentDescriptions) -> VkAttachmentDescription* {
if (attachmentDescriptions.empty()) return nullptr;
auto vk_attachmentDescriptions = scratchMemory->allocate<VkAttachmentDescription>(attachmentDescriptions.size());
for (size_t i = 0; i < attachmentDescriptions.size(); ++i)
{
auto& src = attachmentDescriptions[i];
auto& dst = vk_attachmentDescriptions[i];
dst.flags = src.flags;
dst.format = src.format;
dst.samples = src.samples;
dst.loadOp = src.loadOp;
dst.storeOp = src.storeOp;
dst.stencilLoadOp = src.stencilLoadOp;
dst.stencilStoreOp = src.stencilStoreOp;
dst.initialLayout = src.initialLayout;
dst.finalLayout = src.finalLayout;
}
return vk_attachmentDescriptions;
};
auto copySubpasses = [&scratchMemory](const Subpasses& subpassDescriptions) -> VkSubpassDescription* {
if (subpassDescriptions.empty()) return nullptr;
auto copyAttachmentReference = [&scratchMemory](const std::vector<AttachmentReference>& attachmentReferences) -> VkAttachmentReference* {
if (attachmentReferences.empty()) return nullptr;
auto vk_attachments = scratchMemory->allocate<VkAttachmentReference>(attachmentReferences.size());
for (size_t i = 0; i < attachmentReferences.size(); ++i)
{
auto& src = attachmentReferences[i];
auto& dst = vk_attachments[i];
dst.attachment = src.attachment;
dst.layout = src.layout;
// dst.aspectMask = src.aspectMask; // only VkAttachmentReference2
}
return vk_attachments;
};
auto vk_subpassDescription = scratchMemory->allocate<VkSubpassDescription>(subpassDescriptions.size());
for (size_t i = 0; i < subpassDescriptions.size(); ++i)
{
auto& src = subpassDescriptions[i];
if (!src.depthStencilResolveAttachments.empty())
vsg::warn("vsg::RenderPass::create(...): Subpass includes depthStencilResolveAttachments but vkCreateRenderPass2 is not enabled. Set WindowTraits::vulkanVersion to at least VK_API_VERSION_1_2 or add VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME to WindowTraits::deviceExtensionNames.");
auto& dst = vk_subpassDescription[i];
dst.flags = src.flags;
dst.pipelineBindPoint = src.pipelineBindPoint;
dst.inputAttachmentCount = static_cast<uint32_t>(src.inputAttachments.size());
dst.pInputAttachments = copyAttachmentReference(src.inputAttachments);
dst.colorAttachmentCount = static_cast<uint32_t>(src.colorAttachments.size());
dst.pColorAttachments = copyAttachmentReference(src.colorAttachments);
dst.pResolveAttachments = copyAttachmentReference(src.resolveAttachments);
dst.pDepthStencilAttachment = copyAttachmentReference(src.depthStencilAttachments);
dst.preserveAttachmentCount = static_cast<uint32_t>(src.preserveAttachments.size());
dst.pPreserveAttachments = src.preserveAttachments.empty() ? nullptr : src.preserveAttachments.data();
}
return vk_subpassDescription;
};
auto copySubpassDependency = [&scratchMemory](const Dependencies& subpassDependency) -> VkSubpassDependency* {
if (subpassDependency.empty()) return nullptr;
auto vk_subpassDependencies = scratchMemory->allocate<VkSubpassDependency>(subpassDependency.size());
for (size_t i = 0; i < subpassDependency.size(); ++i)
{
auto& src = subpassDependency[i];
auto& dst = vk_subpassDependencies[i];
dst.srcSubpass = src.srcSubpass;
dst.dstSubpass = src.dstSubpass;
dst.srcStageMask = src.srcStageMask;
dst.dstStageMask = src.dstStageMask;
dst.srcAccessMask = src.srcAccessMask;
dst.dstAccessMask = src.dstAccessMask;
dst.dependencyFlags = src.dependencyFlags;
//dst.viewOffset = src.viewOffset; // only VkSubpassDependency2
}
return vk_subpassDependencies;
};
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassInfo.pAttachments = copyAttachmentDescriptions(attachments);
renderPassInfo.subpassCount = static_cast<uint32_t>(subpasses.size());
renderPassInfo.pSubpasses = copySubpasses(subpasses);
renderPassInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
renderPassInfo.pDependencies = copySubpassDependency(dependencies);
VkResult result = vkCreateRenderPass(*device, &renderPassInfo, device->getAllocationCallbacks(), &_renderPass);
if (result != VK_SUCCESS)
{
throw Exception{"Error: vsg::RenderPass::create(...) Failed to create VkRenderPass.", result};
}
}
}
RenderPass::~RenderPass()
{
if (_renderPass)
{
vkDestroyRenderPass(*device, _renderPass, device->getAllocationCallbacks());
}
}
AttachmentDescription vsg::defaultColorAttachment(VkFormat imageFormat)
{
AttachmentDescription colorAttachment = {};
colorAttachment.format = imageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
return colorAttachment;
}
AttachmentDescription vsg::defaultDepthAttachment(VkFormat depthFormat)
{
AttachmentDescription depthAttachment = {};
depthAttachment.format = depthFormat;
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
return depthAttachment;
}
ref_ptr<RenderPass> vsg::createRenderPass(Device* device, VkFormat imageFormat, VkFormat depthFormat, bool requiresDepthRead)
{
auto colorAttachment = defaultColorAttachment(imageFormat);
auto depthAttachment = defaultDepthAttachment(depthFormat);
if (requiresDepthRead)
{
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
}
RenderPass::Attachments attachments{colorAttachment, depthAttachment};
AttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
AttachmentReference depthAttachmentRef = {};
depthAttachmentRef.attachment = 1;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
SubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachments.emplace_back(colorAttachmentRef);
subpass.depthStencilAttachments.emplace_back(depthAttachmentRef);
RenderPass::Subpasses subpasses{subpass};
// image layout transition
SubpassDependency colorDependency = {};
colorDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
colorDependency.dstSubpass = 0;
colorDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
colorDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
colorDependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
colorDependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
colorDependency.dependencyFlags = 0;
// depth buffer is shared between swap chain images
SubpassDependency depthDependency = {};
depthDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
depthDependency.dstSubpass = 0;
depthDependency.srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT |
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
depthDependency.dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT |
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
depthDependency.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
depthDependency.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
depthDependency.dependencyFlags = 0;
RenderPass::Dependencies dependencies{colorDependency, depthDependency};
return RenderPass::create(device, attachments, subpasses, dependencies);
}
ref_ptr<RenderPass> vsg::createMultisampledRenderPass(Device* device, VkFormat imageFormat, VkFormat depthFormat, VkSampleCountFlagBits samples, bool requiresDepthRead)
{
if (samples == VK_SAMPLE_COUNT_1_BIT)
{
return createRenderPass(device, imageFormat, depthFormat, requiresDepthRead);
}
// First attachment is multisampled target.
AttachmentDescription colorAttachment = {};
colorAttachment.format = imageFormat;
colorAttachment.samples = samples;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// Second attachment is the resolved image which will be presented.
AttachmentDescription resolveAttachment = {};
resolveAttachment.format = imageFormat;
resolveAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
resolveAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
resolveAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
resolveAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
resolveAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
resolveAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
resolveAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
// Multisampled depth attachment. It won't be resolved.
AttachmentDescription depthAttachment = {};
depthAttachment.format = depthFormat;
depthAttachment.samples = samples;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
RenderPass::Attachments attachments{colorAttachment, resolveAttachment, depthAttachment};
if (requiresDepthRead)
{
AttachmentDescription depthResolveAttachment = {};
depthResolveAttachment.format = depthFormat;
depthResolveAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthResolveAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthResolveAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
depthResolveAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthResolveAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthResolveAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthResolveAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachments.push_back(depthResolveAttachment);
}
AttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
AttachmentReference resolveAttachmentRef = {};
resolveAttachmentRef.attachment = 1;
resolveAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
AttachmentReference depthAttachmentRef = {};
depthAttachmentRef.attachment = 2;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
SubpassDescription subpass;
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachments.emplace_back(colorAttachmentRef);
subpass.resolveAttachments.emplace_back(resolveAttachmentRef);
subpass.depthStencilAttachments.emplace_back(depthAttachmentRef);
if (requiresDepthRead)
{
AttachmentReference depthResolveAttachmentRef = {};
depthResolveAttachmentRef.attachment = 3;
depthResolveAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
subpass.depthResolveMode = VK_RESOLVE_MODE_AVERAGE_BIT;
subpass.stencilResolveMode = VK_RESOLVE_MODE_NONE;
subpass.depthStencilResolveAttachments.emplace_back(depthResolveAttachmentRef);
}
RenderPass::Subpasses subpasses{subpass};
SubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependency.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
SubpassDependency dependency2 = {};
dependency2.srcSubpass = 0;
dependency2.dstSubpass = VK_SUBPASS_EXTERNAL;
dependency2.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
dependency2.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
dependency2.dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependency2.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependency2.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
RenderPass::Dependencies dependencies{dependency, dependency2};
return RenderPass::create(device, attachments, subpasses, dependencies);
}
ref_ptr<RenderPass> vsg::createRenderPass(Device* device, VkFormat imageFormat)
{
auto colorAttachment = defaultColorAttachment(imageFormat);
RenderPass::Attachments attachments{colorAttachment};
AttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
SubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachments.emplace_back(colorAttachmentRef);
RenderPass::Subpasses subpasses{subpass};
// image layout transition
SubpassDependency colorDependency = {};
colorDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
colorDependency.dstSubpass = 0;
colorDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
colorDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
colorDependency.srcAccessMask = 0;
colorDependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
colorDependency.dependencyFlags = 0;
RenderPass::Dependencies dependencies{colorDependency};
return RenderPass::create(device, attachments, subpasses, dependencies);
}
ref_ptr<RenderPass> vsg::createMultisampledRenderPass(Device* device, VkFormat imageFormat, VkSampleCountFlagBits samples)
{
if (samples == VK_SAMPLE_COUNT_1_BIT)
{
return createRenderPass(device, imageFormat);
}
// First attachment is multisampled target.
AttachmentDescription colorAttachment = {};
colorAttachment.format = imageFormat;
colorAttachment.samples = samples;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// Second attachment is the resolved image which will be presented.
AttachmentDescription resolveAttachment = {};
resolveAttachment.format = imageFormat;
resolveAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
resolveAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
resolveAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
resolveAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
resolveAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
resolveAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
resolveAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
RenderPass::Attachments attachments{colorAttachment, resolveAttachment};
AttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
AttachmentReference resolveAttachmentRef = {};
resolveAttachmentRef.attachment = 1;
resolveAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
SubpassDescription subpass;
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachments.emplace_back(colorAttachmentRef);
subpass.resolveAttachments.emplace_back(resolveAttachmentRef);
RenderPass::Subpasses subpasses{subpass};
SubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependency.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
SubpassDependency dependency2 = {};
dependency2.srcSubpass = 0;
dependency2.dstSubpass = VK_SUBPASS_EXTERNAL;
dependency2.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency2.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependency2.dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependency2.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependency2.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
RenderPass::Dependencies dependencies{dependency, dependency2};
return RenderPass::create(device, attachments, subpasses, dependencies);
}