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
|
#include "xe_render_system.hpp"
namespace xe {
RenderSystem::RenderSystem(
Engine &xeEngine,
std::string vert,
std::string frag,
std::map<uint32_t, uint32_t> uniformBindings,
std::map<uint32_t, Image*> imageBindings,
std::map<uint32_t, std::vector<Image*>> imageArrayBindings,
uint32_t pushCunstantDataSize,
bool cullingEnabled,
std::vector<VkVertexInputAttributeDescription> attributeDescptions,
uint32_t vertexSize
) : xeDevice{xeEngine.xeDevice},
xeRenderer{xeEngine.xeRenderer},
xeDescriptorPool{xeEngine.xeDescriptorPool},
pushCunstantDataSize{pushCunstantDataSize},
uniformBindings{uniformBindings},
imageBindings{imageBindings},
imageArrayBindings{imageArrayBindings} {
createDescriptorSetLayout();
createUniformBuffers();
createDescriptorSets();
createPipelineLayout();
createPipeline(xeRenderer.getSwapChainRenderPass(), vert, frag, cullingEnabled, attributeDescptions, vertexSize);
}
RenderSystem::~RenderSystem() {
vkDestroyPipelineLayout(xeDevice.device(), pipelineLayout, nullptr);
};
void RenderSystem::createDescriptorSetLayout() {
DescriptorSetLayout::Builder builder{xeDevice};
for ( const auto &[binding, size]: uniformBindings) {
builder.addBinding(binding, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, nullptr, 1);
}
for ( const auto &[binding, image]: imageBindings) {
builder.addBinding(binding, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, &(image->textureSampler), 1);
}
for ( const auto &[binding, images]: imageArrayBindings) {
builder.addBinding(binding, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0, images.size());
}
xeDescriptorSetLayout = builder.build();
}
void RenderSystem::createUniformBuffers() {
for ( const auto &[binding, bufferSize]: uniformBindings) {
uboBuffers[binding] = std::vector<std::unique_ptr<Buffer>>(SwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < uboBuffers[binding].size(); i++) {
uboBuffers[binding][i] = std::make_unique<Buffer>(
xeDevice,
bufferSize,
SwapChain::MAX_FRAMES_IN_FLIGHT,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
uboBuffers[binding][i]->map();
}
}
}
void RenderSystem::createDescriptorSets() {
descriptorSets = std::vector<VkDescriptorSet>(SwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < descriptorSets.size(); i++) {
updateDescriptorSet(i, true);
}
}
void RenderSystem::updateDescriptorSet(int frameIndex, bool allocate) {
DescriptorWriter writer{*xeDescriptorSetLayout, *xeDescriptorPool};
std::vector<VkDescriptorBufferInfo> bufferInfos{};
int i = 0;
for ( const auto &[binding, size]: uniformBindings) {
bufferInfos.push_back(uboBuffers[binding][frameIndex]->descriptorInfo());
writer.writeBuffer(binding, &bufferInfos[i]);
i++;
}
for ( const auto &[binding, image]: imageBindings) {
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = image->textureImageView;
imageInfo.sampler = image->textureSampler;
writer.writeImage(binding, &imageInfo);
}
std::vector<VkDescriptorImageInfo> imageInfos{};
for ( const auto &[binding, images]: imageArrayBindings) {
for( const auto &image: images) {
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = image->textureImageView;
imageInfo.sampler = image->textureSampler;
imageInfos.push_back(imageInfo);
}
writer.writeImageArray(binding, &imageInfos);
}
if (allocate) {
writer.build(descriptorSets[frameIndex]);
} else {
writer.overwrite(descriptorSets[frameIndex]);
}
}
void RenderSystem::createPipelineLayout() {
VkPushConstantRange pushConstantRange;
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = pushCunstantDataSize;
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{xeDescriptorSetLayout->getDescriptorSetLayout()};
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
if (pushCunstantDataSize > 0) {
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
} else {
pipelineLayoutInfo.pushConstantRangeCount = 0;
pipelineLayoutInfo.pPushConstantRanges = nullptr;
}
if(vkCreatePipelineLayout(xeDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
std::runtime_error("failed to create pipeline layout!");
}
}
void RenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, std::string frag, bool cullingEnabled, std::vector<VkVertexInputAttributeDescription> attributeDescptions, uint32_t vertexSize) {
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
Pipeline::defaultPipelineConfigInfo(pipelineConfig, xeDevice);
if (cullingEnabled) {
pipelineConfig.rasterizationInfo.cullMode = VK_CULL_MODE_BACK_BIT;
}
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
xePipeline = std::make_unique<Pipeline>(
xeDevice,
vert,
frag,
pipelineConfig,
attributeDescptions,
vertexSize
);
}
void RenderSystem::start() {
xeRenderer.beginSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
xePipeline->bind(xeRenderer.getCurrentCommandBuffer());
if(descriptorSets.size() > 0) {
vkCmdBindDescriptorSets(
xeRenderer.getCurrentCommandBuffer(),
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
&descriptorSets[xeRenderer.getFrameIndex()],
0,
nullptr);
}
}
void RenderSystem::loadPushConstant(void *pushConstantData) {
vkCmdPushConstants(
xeRenderer.getCurrentCommandBuffer(),
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
pushCunstantDataSize,
pushConstantData);
}
void RenderSystem::loadUniformObject(uint32_t binding, void *uniformBufferData) {
uboBuffers[binding][xeRenderer.getFrameIndex()]->writeToBuffer(uniformBufferData);
}
void RenderSystem::loadTexture(uint32_t binding, Image *image) {
imageBindings[binding] = image;
updateDescriptorSet(xeRenderer.getFrameIndex(), false);
}
void RenderSystem::loadTextureArray(uint32_t binding, std::vector<Image*>& images) {
imageArrayBindings[binding] = images;
updateDescriptorSet(xeRenderer.getFrameIndex(), false);
}
void RenderSystem::render(GameObject &gameObject) {
if(gameObject.model == nullptr) return;
gameObject.model->bind(xeRenderer.getCurrentCommandBuffer());
gameObject.model->draw(xeRenderer.getCurrentCommandBuffer());
}
void RenderSystem::stop() {
xeRenderer.endSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
}
}
|