forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderPass.cpp
More file actions
230 lines (190 loc) · 11.1 KB
/
RenderPass.cpp
File metadata and controls
230 lines (190 loc) · 11.1 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
/* <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/io/Options.h>
#include <vsg/vk/RenderPass.h>
#include <array>
using namespace vsg;
RenderPass::RenderPass(Device* device, const Attachments& attachments, const Subpasses& subpasses, const Dependencies& dependencies) :
_device(device)
{
_maxSamples = VK_SAMPLE_COUNT_1_BIT;
for (auto& attachment : attachments)
{
if (attachment.samples > _maxSamples) _maxSamples = attachment.samples;
}
std::vector<VkSubpassDescription> vk_subpasses(subpasses.size());
for (size_t i = 0; i < subpasses.size(); ++i)
{
const SubpassDescription& src = subpasses[i];
VkSubpassDescription& dst = vk_subpasses[i];
dst.flags = src.flags;
dst.pipelineBindPoint = src.pipelineBindPoint;
dst.inputAttachmentCount = static_cast<uint32_t>(src.inputAttachments.size());
dst.pInputAttachments = src.inputAttachments.empty() ? nullptr : src.inputAttachments.data();
dst.colorAttachmentCount = static_cast<uint32_t>(src.colorAttachments.size());
dst.pColorAttachments = src.colorAttachments.empty() ? nullptr : src.colorAttachments.data();
dst.pResolveAttachments = src.depthStencilAttachments.empty() ? nullptr : src.resolveAttachments.data();
dst.pDepthStencilAttachment = (src.depthStencilAttachments.empty()) ? nullptr : src.depthStencilAttachments.data();
dst.preserveAttachmentCount = static_cast<uint32_t>(src.preserveAttachments.size());
dst.pPreserveAttachments = src.preserveAttachments.empty() ? nullptr : src.preserveAttachments.data();
}
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassInfo.pAttachments = attachments.data();
renderPassInfo.subpassCount = static_cast<uint32_t>(vk_subpasses.size());
renderPassInfo.pSubpasses = vk_subpasses.data();
renderPassInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
renderPassInfo.pDependencies = dependencies.data();
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)
{
VkAttachmentDescription 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)
{
VkAttachmentDescription 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)
{
RenderPass::Attachments attachments{
defaultColorAttachment(imageFormat),
defaultDepthAttachment(depthFormat)};
VkAttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference 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
VkSubpassDependency 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;
// depth buffer is shared between swap chain images
VkSubpassDependency 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;
depthDependency.dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_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;
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)
{
if (samples == VK_SAMPLE_COUNT_1_BIT)
{
return createRenderPass(device, imageFormat, depthFormat);
}
// First attachment is multisampled target.
VkAttachmentDescription colorAttachment = {};
colorAttachment.format = imageFormat;
colorAttachment.samples = samples;
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_COLOR_ATTACHMENT_OPTIMAL;
// Second attachment is the resolved image which will be presented.
VkAttachmentDescription 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.
VkAttachmentDescription 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};
VkAttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference resolveAttachmentRef = {};
resolveAttachmentRef.attachment = 1;
resolveAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference 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);
RenderPass::Subpasses subpasses{subpass};
VkSubpassDependency 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;
VkSubpassDependency 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);
}