-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderpass.cppm
More file actions
224 lines (193 loc) · 10.2 KB
/
renderpass.cppm
File metadata and controls
224 lines (193 loc) · 10.2 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
module;
#include <vulkan/vulkan.h>
#include <print>
#include <span>
#include <vector>
export module vk:renderpass;
export import :types;
export import :utilities;
export namespace vk {
inline namespace v1 {
/**
* @name renderpass
*
* @param p_device is the logical device to associate the creation of
* renderpasses
* @param p_renderpass_attachment is a vk::attachment to specify the
* individual attachment operation that handle in creating
* VkAttachmentDescription, VkAttachmentReference, and VkSubpassDescription
*
* @param p_enable_subpass because subpasses are optional, this is a boolean
* to enable if we want to apply subpasses
*/
class renderpass {
public:
renderpass() = default;
renderpass(const VkDevice& p_device,
std::span<attachment> p_renderpass_attachments,
bool p_enable_subpasses = true) : m_device(p_device) {
create(p_renderpass_attachments, p_enable_subpasses);
}
void create(std::span<const attachment> p_renderpass_attachments, bool p_enable_subpasses = true) {
// 1. Specifically for setting up the attachment description
std::vector<VkAttachmentDescription> attachment_description(
p_renderpass_attachments.size());
// color attachment reference slots to set to help the
// VkAttachmentReference know which color attachment they correspond to
std::vector<uint32_t> color_attachment_indices;
// depth attachment reference slots to set to help the
// VkAttachmentReference know which depth attachment they correspond to
std::vector<uint32_t> depth_attachment_indices;
for (size_t i = 0; i < attachment_description.size(); i++) {
attachment attachment_spec = p_renderpass_attachments[i];
attachment_description[i] = {
.flags = 0,
.format = attachment_spec.format,
// .samples = VK_SAMPLE_COUNT_1_BIT,
.samples = static_cast<VkSampleCountFlagBits>(attachment_spec.samples),
// .loadOp = to_attachment_load(attachment_spec.load),
.loadOp = static_cast<VkAttachmentLoadOp>(attachment_spec.load),
// .storeOp = to_attachment_store(attachment_spec.store),
.storeOp = static_cast<VkAttachmentStoreOp>(attachment_spec.store),
.stencilLoadOp =
static_cast<VkAttachmentLoadOp>(attachment_spec.stencil_load),
.stencilStoreOp =
static_cast<VkAttachmentStoreOp>(attachment_spec.stencil_store),
.initialLayout = static_cast<VkImageLayout>(attachment_spec.initial_layout),
.finalLayout = static_cast<VkImageLayout>(attachment_spec.final_layout)
};
// I do a check here to save the slots for specifying the
// VkAttachmentReference Since .attachment is the slot index for
// corresponding which attachment layout is to which
if (has_depth_specified(attachment_spec.layout)) {
depth_attachment_indices.emplace_back(i);
}
else {
color_attachment_indices.emplace_back(i);
}
}
// 2. Setting up the color attachment reference to specifying specific
// attachments they correspond to (using the indices)
std::vector<VkAttachmentReference> color_attachment_references(
color_attachment_indices.size());
for (size_t i = 0; i < color_attachment_indices.size(); i++) {
uint32_t slot = color_attachment_indices[i];
color_attachment_references[i] = {
.attachment = slot,
// .layout = to_image_layout(p_renderpass_attachments[slot].layout)
.layout = static_cast<VkImageLayout>(p_renderpass_attachments[slot].layout)
};
}
// 3. Setting up the depth attachment reference to specifying specific
// attachments they correspond to (using the indices)
std::vector<VkAttachmentReference> depth_attachment_references(
depth_attachment_indices.size());
for (size_t i = 0; i < depth_attachment_indices.size(); i++) {
uint32_t slot = depth_attachment_indices[i];
depth_attachment_references[i] = {
.attachment = slot,
// .layout = to_image_layout(p_renderpass_attachments[slot].layout)
.layout = static_cast<VkImageLayout>(p_renderpass_attachments[slot].layout)
};
}
// 4. Setting up subpass descriptions that may/may not be applied to
// this renderpass based on the p_enable_subpass is set to true
// TODO: VkSubpassDescription is deprecated in vulkan 1.2+, we should
// change to VkSubpassDescription2
// TODO: Change from VkRenderPassCreateInfo to VkRenderPassCreateInfo2
// since they have different parameter modifications
VkSubpassDescription subpass_description = {
.flags = 0,
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.inputAttachmentCount = 0,
.pInputAttachments = nullptr,
.colorAttachmentCount =
static_cast<uint32_t>(color_attachment_references.size()),
.pColorAttachments = color_attachment_references.data(),
.pResolveAttachments = nullptr,
.pDepthStencilAttachment = depth_attachment_references.data(),
.preserveAttachmentCount = 0,
.pPreserveAttachments = nullptr
};
std::array<VkSubpassDescription, 1> subpasses;
if (p_enable_subpasses) {
subpasses = { subpass_description };
}
VkRenderPassCreateInfo renderpass_ci = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.attachmentCount =
static_cast<uint32_t>(attachment_description.size()),
.pAttachments = attachment_description.data(),
.subpassCount = static_cast<uint32_t>(subpasses.size()),
.pSubpasses = subpasses.data(),
.dependencyCount = 0,
.pDependencies = nullptr
};
vk_check(
vkCreateRenderPass(m_device, &renderpass_ci, nullptr, &m_renderpass),
"vkCreateRenderPass");
}
[[nodiscard]] bool alive() const { return m_renderpass; }
void begin(const renderpass_begin_params& p_begin_info) {
// TODO: Move VkViewport and VkScissor to vk::swapchain since these are
// information more closely set by the swapchain
VkViewport viewport = {
.x = 0.0f,
.y = 0.0f,
.width = static_cast<float>(p_begin_info.extent.width),
.height = static_cast<float>(p_begin_info.extent.height),
.minDepth = 0.0f,
.maxDepth = 1.0f,
};
vkCmdSetViewport(p_begin_info.current_command, 0, 1, &viewport);
VkRect2D scissor = {
.offset = { 0, 0 },
.extent = { p_begin_info.extent.width, p_begin_info.extent.height },
};
vkCmdSetScissor(p_begin_info.current_command, 0, 1, &scissor);
// setting color for this specific renderpass
VkClearColorValue renderpass_color = { { p_begin_info.color.at(0),
p_begin_info.color.at(1),
p_begin_info.color.at(2),
p_begin_info.color.at(3) } };
std::array<VkClearValue, 2> clear_values = {};
clear_values[0].color = renderpass_color;
clear_values[1].depthStencil = { 1.f, 0 };
VkRenderPassBeginInfo renderpass_begin_params = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.pNext = nullptr,
.renderPass = m_renderpass,
.framebuffer = p_begin_info.current_framebuffer,
.renderArea = {
.offset = {
.x = 0,
.y = 0
},
.extent = {
.width = p_begin_info.extent.width,
.height = p_begin_info.extent.height
},
},
.clearValueCount = static_cast<uint32_t>(clear_values.size()),
.pClearValues = clear_values.data(),
};
vkCmdBeginRenderPass(p_begin_info.current_command,
&renderpass_begin_params,
static_cast<VkSubpassContents>(p_begin_info.subpass));
}
void end(const VkCommandBuffer& p_current) {
vkCmdEndRenderPass(p_current);
}
void destroy() {
vkDestroyRenderPass(m_device, m_renderpass, nullptr);
}
operator VkRenderPass() const { return m_renderpass; }
operator VkRenderPass() { return m_renderpass; }
private:
VkDevice m_device = nullptr;
VkRenderPass m_renderpass = nullptr;
};
};
};