Skip to content

Commit dad1543

Browse files
authored
Big Performance Increases (#68)
* save 32 bytes of memory per particle * handle initial particle position in gpu * only send in one random value * Update SmokePipeline.cpp * Update Smoke.comp * use named constants * Update Smoke.comp * performance improvements * reduce calls to random * Update SmokePipeline.cpp * Update SmokePipeline.cpp * allow changing spawn position per system * reduce particle buffer size even more * Update VulkanEngine.cpp * Update Smoke.comp * Update Smoke.comp * Update Smoke.comp * Update SmokePipeline.cpp * Update SmokePipeline.cpp * Update SmokePipeline.cpp * Update SmokePipeline.cpp * remove identity matrix multiplication * have uniform buffers store their size rather than passing again during update * Update TexturedPlane.cpp * Update UniformBuffer.cpp * Update UniformBuffer.cpp * fix ttl setting; reduce number of particles per system * better random on first load * Update VulkanEngine.cpp
1 parent 0430389 commit dad1543

22 files changed

+204
-161
lines changed

source/VulkanEngine.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,19 @@ void VulkanEngine::renderGraphicsPipelines(const VkCommandBuffer& commandBuffer,
578578
dotsPipeline->render(&renderInfo, nullptr);
579579
}
580580

581+
ImGui::Begin("Smoke");
582+
ImGui::Separator();
581583
for (const auto& system : smokeSystems)
582584
{
585+
ImGui::PushID(&system);
583586
system->displayGui();
587+
ImGui::PopID();
588+
589+
ImGui::Separator();
590+
584591
system->render(&renderInfo, nullptr);
585592
}
593+
ImGui::End();
586594
}
587595

588596
void VulkanEngine::createNewFrame()
@@ -597,7 +605,7 @@ void VulkanEngine::createDescriptorPool()
597605
{
598606
const std::array<VkDescriptorPoolSize, 3> poolSizes {{
599607
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, logicalDevice->getMaxFramesInFlight() * 30},
600-
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, logicalDevice->getMaxFramesInFlight() * 20},
608+
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, logicalDevice->getMaxFramesInFlight() * 50},
601609
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, logicalDevice->getMaxFramesInFlight() * 10}
602610
}};
603611

source/objects/RenderObject.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void RenderObject::updateUniformBuffer(const uint32_t currentFrame, const glm::m
4646
.proj = projectionMatrix
4747
};
4848

49-
transformUniform->update(currentFrame, &transformUBO, sizeof(TransformUniform));
49+
transformUniform->update(currentFrame, &transformUBO);
5050
}
5151

5252
void RenderObject::setPosition(const glm::vec3 position)

source/objects/UniformBuffer.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
UniformBuffer::UniformBuffer(const std::shared_ptr<LogicalDevice>& logicalDevice,
88
const std::shared_ptr<PhysicalDevice>& physicalDevice, const VkDeviceSize bufferSize)
9-
: logicalDevice(logicalDevice)
9+
: logicalDevice(logicalDevice), bufferSize(bufferSize)
1010
{
1111
uniformBuffers.resize(logicalDevice->getMaxFramesInFlight());
1212
uniformBuffersMemory.resize(logicalDevice->getMaxFramesInFlight());
@@ -34,7 +34,8 @@ UniformBuffer::UniformBuffer(const std::shared_ptr<LogicalDevice>& logicalDevice
3434
poolSize.descriptorCount = logicalDevice->getMaxFramesInFlight();
3535
}
3636

37-
UniformBuffer::~UniformBuffer() {
37+
UniformBuffer::~UniformBuffer()
38+
{
3839
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
3940
{
4041
if (uniformBuffersMapped[i] != VK_NULL_HANDLE)
@@ -51,7 +52,8 @@ VkDescriptorPoolSize UniformBuffer::getDescriptorPoolSize() const
5152
return poolSize;
5253
}
5354

54-
VkWriteDescriptorSet UniformBuffer::getDescriptorSet(const uint32_t binding, const VkDescriptorSet& dstSet, const size_t frame) const
55+
VkWriteDescriptorSet UniformBuffer::getDescriptorSet(const uint32_t binding, const VkDescriptorSet& dstSet,
56+
const size_t frame) const
5557
{
5658
const VkWriteDescriptorSet uniformDescriptorSet {
5759
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
@@ -66,7 +68,7 @@ VkWriteDescriptorSet UniformBuffer::getDescriptorSet(const uint32_t binding, con
6668
return uniformDescriptorSet;
6769
}
6870

69-
void UniformBuffer::update(const uint32_t frame, const void* data, const size_t size) const
71+
void UniformBuffer::update(const uint32_t frame, const void* data) const
7072
{
71-
memcpy(uniformBuffersMapped[frame], data, size);
73+
memcpy(uniformBuffersMapped[frame], data, bufferSize);
7274
}

source/objects/UniformBuffer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UniformBuffer {
1818

1919
[[nodiscard]] VkWriteDescriptorSet getDescriptorSet(uint32_t binding, const VkDescriptorSet& dstSet, size_t frame) const;
2020

21-
void update(uint32_t frame, const void* data, size_t size) const;
21+
void update(uint32_t frame, const void* data) const;
2222

2323
protected:
2424
std::shared_ptr<LogicalDevice> logicalDevice;
@@ -29,6 +29,8 @@ class UniformBuffer {
2929

3030
std::vector<VkDescriptorBufferInfo> bufferInfos;
3131
VkDescriptorPoolSize poolSize{};
32+
33+
VkDeviceSize bufferSize;
3234
};
3335

3436

source/pipelines/SmokeParticle.h

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
#include <glm/vec4.hpp>
88

99
struct SmokeParticle {
10-
glm::vec3 position;
11-
float ttl;
12-
glm::vec3 velocity;
13-
float padding;
14-
glm::vec4 color;
15-
glm::vec3 initialPosition;
16-
float padding2;
17-
glm::vec3 initialVelocity;
18-
float padding3;
10+
glm::vec4 positionTtl;
11+
glm::vec4 velocityColor;
1912

2013
static constexpr VkVertexInputBindingDescription getBindingDescription()
2114
{
@@ -32,14 +25,14 @@ struct SmokeParticle {
3225
{
3326
.location = 0,
3427
.binding = 0,
35-
.format = VK_FORMAT_R32G32B32_SFLOAT,
36-
.offset = offsetof(SmokeParticle, position)
28+
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
29+
.offset = offsetof(SmokeParticle, positionTtl)
3730
},
3831
{
3932
.location = 1,
4033
.binding = 0,
4134
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
42-
.offset = offsetof(SmokeParticle, color)
35+
.offset = offsetof(SmokeParticle, velocityColor)
4336
}
4437
}};
4538
}

source/pipelines/custom/BumpyCurtain.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void BumpyCurtain::updateLightUniforms(const std::vector<std::shared_ptr<Light>>
206206

207207
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
208208
{
209-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
209+
lightMetadataUniform->update(i, &lightMetadataUBO);
210210

211211
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
212212
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -224,7 +224,7 @@ void BumpyCurtain::updateLightUniforms(const std::vector<std::shared_ptr<Light>>
224224
lightUniforms[i] = lights[i]->getUniform();
225225
}
226226

227-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
227+
lightsUniform->update(currentFrame, lightUniforms.data());
228228
}
229229

230230
void BumpyCurtain::updateUniformVariables(const RenderInfo* renderInfo)
@@ -234,11 +234,11 @@ void BumpyCurtain::updateUniformVariables(const RenderInfo* renderInfo)
234234
const CameraUniform cameraUBO {
235235
.position = renderInfo->viewPosition
236236
};
237-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
237+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
238238

239-
curtainUniform->update(renderInfo->currentFrame, &curtainUBO, sizeof(CurtainUniform));
239+
curtainUniform->update(renderInfo->currentFrame, &curtainUBO);
240240

241-
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO, sizeof(NoiseOptionsUniform));
241+
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO);
242242
}
243243

244244
void BumpyCurtain::bindDescriptorSet(const RenderInfo* renderInfo)

source/pipelines/custom/CrossesPipeline.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void CrossesPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
204204

205205
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
206206
{
207-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
207+
lightMetadataUniform->update(i, &lightMetadataUBO);
208208

209209
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
210210
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -222,7 +222,7 @@ void CrossesPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
222222
lightUniforms[i] = lights[i]->getUniform();
223223
}
224224

225-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
225+
lightsUniform->update(currentFrame, lightUniforms.data());
226226
}
227227

228228
void CrossesPipeline::updateUniformVariables(const RenderInfo* renderInfo)
@@ -231,11 +231,11 @@ void CrossesPipeline::updateUniformVariables(const RenderInfo* renderInfo)
231231

232232
updateLightUniforms(renderInfo->lights, renderInfo->currentFrame);
233233

234-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
234+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
235235

236-
chromaDepthUniform->update(renderInfo->currentFrame, &chromaDepthUBO, sizeof(ChromaDepthUniform));
236+
chromaDepthUniform->update(renderInfo->currentFrame, &chromaDepthUBO);
237237

238-
crossesUniform->update(renderInfo->currentFrame, &crossesUBO, sizeof(CrossesUniform));
238+
crossesUniform->update(renderInfo->currentFrame, &crossesUBO);
239239
}
240240

241241
void CrossesPipeline::bindDescriptorSet(const RenderInfo* renderInfo)

source/pipelines/custom/CubeMapPipeline.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ void CubeMapPipeline::updateUniformVariables(const RenderInfo *renderInfo)
197197
const CameraUniform cameraUBO {
198198
.position = renderInfo->viewPosition
199199
};
200-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
200+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
201201

202-
cubeMapUniform->update(renderInfo->currentFrame, &cubeMapUBO, sizeof(CubeMapUniform));
202+
cubeMapUniform->update(renderInfo->currentFrame, &cubeMapUBO);
203203

204-
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO, sizeof(NoiseOptionsUniform));
204+
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO);
205205
}
206206

207207
void CubeMapPipeline::bindDescriptorSet(const RenderInfo *renderInfo)

source/pipelines/custom/CurtainPipeline.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void CurtainPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
179179

180180
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
181181
{
182-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
182+
lightMetadataUniform->update(i, &lightMetadataUBO);
183183

184184
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
185185
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -197,19 +197,19 @@ void CurtainPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
197197
lightUniforms[i] = lights[i]->getUniform();
198198
}
199199

200-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
200+
lightsUniform->update(currentFrame, lightUniforms.data());
201201
}
202202

203203
void CurtainPipeline::updateUniformVariables(const RenderInfo *renderInfo)
204204
{
205205
const CameraUniform cameraUBO {
206206
.position = renderInfo->viewPosition
207207
};
208-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
208+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
209209

210210
updateLightUniforms(renderInfo->lights, renderInfo->currentFrame);
211211

212-
curtainUniform->update(renderInfo->currentFrame, &curtainUBO, sizeof(CurtainUniform));
212+
curtainUniform->update(renderInfo->currentFrame, &curtainUBO);
213213
}
214214

215215
void CurtainPipeline::bindDescriptorSet(const RenderInfo *renderInfo)

source/pipelines/custom/DotsPipeline.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,5 @@ void DotsPipeline::updateUniformVariables(const RenderInfo* renderInfo)
246246

247247
const DeltaTimeUniform deltaTimeUBO{dotSpeed * dt};
248248

249-
deltaTimeUniform->update(renderInfo->currentFrame, &deltaTimeUBO, sizeof(DeltaTimeUniform));
249+
deltaTimeUniform->update(renderInfo->currentFrame, &deltaTimeUBO);
250250
}

source/pipelines/custom/EllipticalDots.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void EllipticalDots::updateLightUniforms(const std::vector<std::shared_ptr<Light
180180

181181
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
182182
{
183-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
183+
lightMetadataUniform->update(i, &lightMetadataUBO);
184184

185185
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
186186
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -198,19 +198,19 @@ void EllipticalDots::updateLightUniforms(const std::vector<std::shared_ptr<Light
198198
lightUniforms[i] = lights[i]->getUniform();
199199
}
200200

201-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
201+
lightsUniform->update(currentFrame, lightUniforms.data());
202202
}
203203

204204
void EllipticalDots::updateUniformVariables(const RenderInfo *renderInfo)
205205
{
206206
const CameraUniform cameraUBO {
207207
.position = renderInfo->viewPosition
208208
};
209-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
209+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
210210

211211
updateLightUniforms(renderInfo->lights, renderInfo->currentFrame);
212212

213-
ellipticalDotsUniform->update(renderInfo->currentFrame, &ellipticalDotsUBO, sizeof(EllipticalDotsUniform));
213+
ellipticalDotsUniform->update(renderInfo->currentFrame, &ellipticalDotsUBO);
214214
}
215215

216216
void EllipticalDots::bindDescriptorSet(const RenderInfo *renderInfo)

source/pipelines/custom/MagnifyWhirlMosaicPipeline.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ void MagnifyWhirlMosaicPipeline::updateUniformVariables(const RenderInfo *render
146146
const CameraUniform cameraUBO {
147147
.position = renderInfo->viewPosition
148148
};
149-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
149+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
150150

151-
magnifyWhirlMosaicUniform->update(renderInfo->currentFrame, &magnifyWhirlMosaicUBO, sizeof(MagnifyWhirlMosaicUniform));
151+
magnifyWhirlMosaicUniform->update(renderInfo->currentFrame, &magnifyWhirlMosaicUBO);
152152
}
153153

154154
void MagnifyWhirlMosaicPipeline::bindDescriptorSet(const RenderInfo *renderInfo)

source/pipelines/custom/NoisyEllipticalDots.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void NoisyEllipticalDots::updateLightUniforms(const std::vector<std::shared_ptr<
208208

209209
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
210210
{
211-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
211+
lightMetadataUniform->update(i, &lightMetadataUBO);
212212

213213
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
214214
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -226,21 +226,21 @@ void NoisyEllipticalDots::updateLightUniforms(const std::vector<std::shared_ptr<
226226
lightUniforms[i] = lights[i]->getUniform();
227227
}
228228

229-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
229+
lightsUniform->update(currentFrame, lightUniforms.data());
230230
}
231231

232232
void NoisyEllipticalDots::updateUniformVariables(const RenderInfo *renderInfo)
233233
{
234234
const CameraUniform cameraUBO {
235235
.position = renderInfo->viewPosition
236236
};
237-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
237+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
238238

239239
updateLightUniforms(renderInfo->lights, renderInfo->currentFrame);
240240

241-
ellipticalDotsUniform->update(renderInfo->currentFrame, &ellipticalDotsUBO, sizeof(EllipticalDotsUniform));
241+
ellipticalDotsUniform->update(renderInfo->currentFrame, &ellipticalDotsUBO);
242242

243-
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO, sizeof(NoiseOptionsUniform));
243+
noiseOptionsUniform->update(renderInfo->currentFrame, &noiseOptionsUBO);
244244
}
245245

246246
void NoisyEllipticalDots::bindDescriptorSet(const RenderInfo *renderInfo)

source/pipelines/custom/ObjectsPipeline.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void ObjectsPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
157157

158158
for (size_t i = 0; i < logicalDevice->getMaxFramesInFlight(); i++)
159159
{
160-
lightMetadataUniform->update(i, &lightMetadataUBO, sizeof(lightMetadataUBO));
160+
lightMetadataUniform->update(i, &lightMetadataUBO);
161161

162162
auto descriptorSet = lightsUniform->getDescriptorSet(5, descriptorSets[i], i);
163163
descriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
@@ -175,15 +175,15 @@ void ObjectsPipeline::updateLightUniforms(const std::vector<std::shared_ptr<Ligh
175175
lightUniforms[i] = lights[i]->getUniform();
176176
}
177177

178-
lightsUniform->update(currentFrame, lightUniforms.data(), lightsUniformBufferSize);
178+
lightsUniform->update(currentFrame, lightUniforms.data());
179179
}
180180

181181
void ObjectsPipeline::updateUniformVariables(const RenderInfo *renderInfo)
182182
{
183183
const CameraUniform cameraUBO {
184184
.position = renderInfo->viewPosition
185185
};
186-
cameraUniform->update(renderInfo->currentFrame, &cameraUBO, sizeof(CameraUniform));
186+
cameraUniform->update(renderInfo->currentFrame, &cameraUBO);
187187

188188
updateLightUniforms(renderInfo->lights, renderInfo->currentFrame);
189189
}

0 commit comments

Comments
 (0)