forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparticlefire.cpp
428 lines (367 loc) · 17.8 KB
/
particlefire.cpp
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
/*
* Vulkan Example - CPU based fire particle system
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <vulkanExampleBase.h>
#define PARTICLE_COUNT 512
#define PARTICLE_SIZE 10.0f
#define FLAME_RADIUS 8.0f
#define PARTICLE_TYPE_FLAME 0
#define PARTICLE_TYPE_SMOKE 1
struct Particle {
glm::vec3 pos;
glm::vec3 color;
float alpha;
float size;
float rotation;
uint32_t type;
// Attributes not used in shader
glm::vec3 vel;
float rotationSpeed;
};
// Vertex layout for this example
class VulkanExample : public vkx::ExampleBase {
public:
struct {
struct {
vks::texture::Texture2D smoke;
vks::texture::Texture2D fire;
// We use a custom sampler to change some sampler
// attributes required for rotation the uv coordinates
// inside the shader for alpha blended textures
vk::Sampler sampler;
} particles;
struct {
vks::texture::Texture2D colorMap;
vks::texture::Texture2D normalMap;
} floor;
} textures;
struct {
vks::model::Model environment;
vk::DescriptorSet descriptorSet;
vks::model::VertexLayout vertexLayout{ {
vks::model::VERTEX_COMPONENT_POSITION,
vks::model::VERTEX_COMPONENT_UV,
vks::model::VERTEX_COMPONENT_NORMAL,
vks::model::VERTEX_COMPONENT_TANGENT,
vks::model::VERTEX_COMPONENT_BITANGENT,
} };
} meshes;
glm::vec3 emitterPos = glm::vec3(0.0f, -FLAME_RADIUS + 2.0f, 0.0f);
glm::vec3 minVel = glm::vec3(-3.0f, 0.5f, -3.0f);
glm::vec3 maxVel = glm::vec3(3.0f, 7.0f, 3.0f);
struct {
vks::Buffer buffer;
vks::model::VertexLayout vertexLayout{ {
vks::model::VERTEX_COMPONENT_POSITION,
vks::model::VERTEX_COMPONENT_COLOR,
vks::model::VERTEX_COMPONENT_DUMMY_FLOAT, // alpha
vks::model::VERTEX_COMPONENT_DUMMY_FLOAT, // size
vks::model::VERTEX_COMPONENT_DUMMY_FLOAT, // rotaton
vks::model::VERTEX_COMPONENT_DUMMY_INT, // type
vks::model::VERTEX_COMPONENT_DUMMY_VEC4,
} };
} particles;
struct {
vks::Buffer fire;
vks::Buffer environment;
} uniformData;
struct {
glm::mat4 projection;
glm::mat4 model;
glm::vec2 viewportDim;
float pointSize = PARTICLE_SIZE;
} uboVS;
struct {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 normal;
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
glm::vec4 cameraPos;
} uboEnv;
struct {
vk::Pipeline particles;
vk::Pipeline environment;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
std::vector<Particle> particleBuffer;
VulkanExample() {
camera.setRotation({ -15.0f, 45.0f, 0.0f });
camera.dolly(-90.0f);
title = "Vulkan Example - Particle system";
zoomSpeed *= 1.5f;
timerSpeed *= 8.0f;
srand((uint32_t)time(NULL));
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
textures.particles.smoke.destroy();
textures.particles.fire.destroy();
textures.floor.colorMap.destroy();
textures.floor.normalMap.destroy();
device.destroyPipeline(pipelines.particles);
device.destroyPipeline(pipelines.environment);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
particles.buffer.destroy();
uniformData.fire.destroy();
uniformData.environment.destroy();
meshes.environment.destroy();
device.destroySampler(textures.particles.sampler);
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) override {
cmdBuffer.setViewport(0, vks::util::viewport(size));
cmdBuffer.setScissor(0, vks::util::rect2D(size));
// Environment
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.environment);
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, meshes.descriptorSet, nullptr);
cmdBuffer.bindVertexBuffers(0, meshes.environment.vertices.buffer, vk::DeviceSize());
cmdBuffer.bindIndexBuffer(meshes.environment.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(meshes.environment.indexCount, 1, 0, 0, 0);
// Particle system
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.particles);
cmdBuffer.bindVertexBuffers(0, particles.buffer.buffer, { 0 });
cmdBuffer.draw(PARTICLE_COUNT, 1, 0, 0);
}
float rnd(float range) { return range * (rand() / float(RAND_MAX)); }
void initParticle(Particle* particle, glm::vec3 emitterPos) {
particle->vel = glm::vec4(0.0f, minVel.y + rnd(maxVel.y - minVel.y), 0.0f, 0.0f);
particle->alpha = rnd(0.75f);
particle->size = 1.0f + rnd(0.5f);
particle->color = glm::vec4(1.0f);
particle->type = PARTICLE_TYPE_FLAME;
particle->rotation = rnd(2.0f * (float)M_PI);
particle->rotationSpeed = rnd(2.0f) - rnd(2.0f);
// Get random sphere point
float theta = rnd(2 * (float)M_PI);
float phi = rnd((float)M_PI) - (float)M_PI / 2;
float r = rnd(FLAME_RADIUS);
particle->pos.x = r * cos(theta) * cos(phi);
particle->pos.y = r * sin(phi);
particle->pos.z = r * sin(theta) * cos(phi);
particle->pos += emitterPos;
}
void transitionParticle(Particle* particle) {
switch (particle->type) {
case PARTICLE_TYPE_FLAME:
// Flame particles have a chance of turning into smoke
if (rnd(1.0f) < 0.05f) {
particle->alpha = 0.0f;
particle->color = glm::vec4(0.25f + rnd(0.25f));
particle->pos.x *= 0.5f;
particle->pos.z *= 0.5f;
particle->vel = glm::vec4(rnd(1.0f) - rnd(1.0f), (minVel.y * 2) + rnd(maxVel.y - minVel.y), rnd(1.0f) - rnd(1.0f), 0.0f);
particle->size = 1.0f + rnd(0.5f);
particle->rotationSpeed = rnd(1.0f) - rnd(1.0f);
particle->type = PARTICLE_TYPE_SMOKE;
} else {
initParticle(particle, emitterPos);
}
break;
case PARTICLE_TYPE_SMOKE:
// Respawn at end of life
initParticle(particle, emitterPos);
break;
}
}
void prepareParticles() {
particleBuffer.resize(PARTICLE_COUNT);
for (auto& particle : particleBuffer) {
initParticle(&particle, emitterPos);
particle.alpha = 1.0f - (abs(particle.pos.y) / (FLAME_RADIUS * 2.0f));
}
particles.buffer =
context.createBuffer(vk::BufferUsageFlagBits::eVertexBuffer, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
sizeof(Particle) * particleBuffer.size());
particles.buffer.map();
particles.buffer.copy(particleBuffer);
}
void updateParticles() {
float particleTimer = frameTimer * 0.45f;
for (auto& particle : particleBuffer) {
switch (particle.type) {
case PARTICLE_TYPE_FLAME:
particle.pos.y -= particle.vel.y * particleTimer * 3.5f;
particle.alpha += particleTimer * 2.5f;
particle.size -= particleTimer * 0.5f;
break;
case PARTICLE_TYPE_SMOKE:
particle.pos -= particle.vel * frameTimer * 1.0f;
particle.alpha += particleTimer * 1.25f;
particle.size += particleTimer * 0.125f;
particle.color -= particleTimer * 0.05f;
break;
}
particle.rotation += particleTimer * particle.rotationSpeed;
// Transition particle state
if (particle.alpha > 2.0f) {
transitionParticle(&particle);
}
}
particles.buffer.copy(particleBuffer);
}
void loadAssets() override {
meshes.environment.loadFromFile(context, getAssetPath() + "models/fireplace.obj", meshes.vertexLayout, 10.0f);
// Floor
textures.floor.colorMap.loadFromFile(context, getAssetPath() + "textures/fireplace_colormap_bc3_unorm.ktx", vk::Format::eBc3UnormBlock);
textures.floor.normalMap.loadFromFile(context, getAssetPath() + "textures/fireplace_normalmap_bc3_unorm.ktx", vk::Format::eBc3UnormBlock);
// Particles
textures.particles.smoke.loadFromFile(context, getAssetPath() + "textures/particle_smoke.ktx", vk::Format::eB8G8R8A8Unorm);
textures.particles.fire.loadFromFile(context, getAssetPath() + "textures/particle_fire.ktx", vk::Format::eB8G8R8A8Unorm);
// Create a custom sampler to be used with the particle textures
// Create sampler
vk::SamplerCreateInfo samplerCreateInfo;
samplerCreateInfo.magFilter = vk::Filter::eLinear;
samplerCreateInfo.minFilter = vk::Filter::eLinear;
samplerCreateInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
// Different address mode
samplerCreateInfo.addressModeU = vk::SamplerAddressMode::eClampToBorder;
samplerCreateInfo.addressModeV = samplerCreateInfo.addressModeU;
samplerCreateInfo.addressModeW = samplerCreateInfo.addressModeU;
samplerCreateInfo.mipLodBias = 0.0f;
samplerCreateInfo.compareOp = vk::CompareOp::eNever;
samplerCreateInfo.minLod = 0.0f;
// Both particle textures have the same number of mip maps
samplerCreateInfo.maxLod = (float)textures.particles.fire.mipLevels;
// Enable anisotropic filtering
samplerCreateInfo.maxAnisotropy = 8;
samplerCreateInfo.anisotropyEnable = VK_TRUE;
// Use a different border color (than the normal texture loader) for additive blending
samplerCreateInfo.borderColor = vk::BorderColor::eFloatTransparentBlack;
textures.particles.sampler = device.createSampler(samplerCreateInfo);
}
void setupDescriptorPool() {
// Example uses one ubo and one image sampler
std::vector<vk::DescriptorPoolSize> poolSizes = { vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, 2),
vk::DescriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 4) };
descriptorPool = device.createDescriptorPool({ {}, 2, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings{
// Binding 0 : Vertex shader uniform buffer
{ 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
// Binding 1 : Fragment shader image sampler
{ 1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
// Binding 1 : Fragment shader image sampler
{ 2, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSets() {
vk::DescriptorSetAllocateInfo allocInfo{ descriptorPool, 1, &descriptorSetLayout };
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
// vk::Image descriptor for the color map texture
vk::DescriptorImageInfo texDescriptorSmoke{ textures.particles.sampler, textures.particles.smoke.view, vk::ImageLayout::eGeneral };
vk::DescriptorImageInfo texDescriptorFire{ textures.particles.sampler, textures.particles.fire.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
vk::WriteDescriptorSet{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.fire.descriptor },
// Binding 1 : Smoke texture
vk::WriteDescriptorSet{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorSmoke },
// Binding 1 : Fire texture array
vk::WriteDescriptorSet{ descriptorSet, 2, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorFire },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
// Environment
meshes.descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
vk::DescriptorImageInfo texDescriptorColorMap{ textures.floor.colorMap.sampler, textures.floor.colorMap.view, vk::ImageLayout::eGeneral };
vk::DescriptorImageInfo texDescriptorNormalMap{ textures.floor.normalMap.sampler, textures.floor.normalMap.view, vk::ImageLayout::eGeneral };
writeDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
vk::WriteDescriptorSet{ meshes.descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.environment.descriptor },
// Binding 1 : Color map
vk::WriteDescriptorSet{ meshes.descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorColorMap },
// Binding 2 : Normal map
vk::WriteDescriptorSet{ meshes.descriptorSet, 2, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorNormalMap },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
}
void preparePipelines() {
// Environment rendering pipeline (normal mapped)
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.rasterizationState.frontFace = vk::FrontFace::eClockwise;
pipelineBuilder.vertexInputState.appendVertexLayout(meshes.vertexLayout);
pipelineBuilder.loadShader(getAssetPath() + "shaders/particlefire/normalmap.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/particlefire/normalmap.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.environment = pipelineBuilder.create(context.pipelineCache);
pipelineBuilder.destroyShaderModules();
// Particle pipeline, read depth, but do not write it.
// Premulitplied alpha
pipelineBuilder.inputAssemblyState.topology = vk::PrimitiveTopology::ePointList;
pipelineBuilder.depthStencilState.depthWriteEnable = VK_FALSE;
auto& blendAttachmentState = pipelineBuilder.colorBlendState.blendAttachmentStates[0];
blendAttachmentState.blendEnable = VK_TRUE;
blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eOne;
blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blendAttachmentState.colorBlendOp = vk::BlendOp::eAdd;
blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOne;
blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
blendAttachmentState.alphaBlendOp = vk::BlendOp::eAdd;
// Reset the vertex input state
pipelineBuilder.vertexInputState = {};
pipelineBuilder.vertexInputState.appendVertexLayout(particles.vertexLayout);
// Load shaders
pipelineBuilder.loadShader(getAssetPath() + "shaders/particlefire/particle.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/particlefire/particle.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.particles = pipelineBuilder.create(context.pipelineCache);
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.fire = context.createUniformBuffer(uboVS);
// Vertex shader uniform buffer block
uniformData.environment = context.createUniformBuffer(uboEnv);
updateUniformBuffers();
}
void updateUniformBufferLight() {
// Environment
uboEnv.lightPos.x = sin(timer * 2 * (float)M_PI) * 1.5f;
uboEnv.lightPos.y = 0.0f;
uboEnv.lightPos.z = cos(timer * 2 * (float)M_PI) * 1.5f;
uniformData.environment.copy(uboEnv);
}
void updateUniformBuffers() {
// Vertex shader
glm::mat4 viewMatrix = glm::mat4();
uboVS.projection = camera.matrices.perspective;
uboVS.model = camera.matrices.view;
uboVS.viewportDim = glm::vec2(size.width, size.height);
uniformData.fire.copy(uboVS);
// Environment
uboEnv.projection = uboVS.projection;
uboEnv.model = uboVS.model;
uboEnv.normal = glm::inverseTranspose(uboEnv.model);
uboEnv.cameraPos = glm::vec4(0.0, 0.0, camera.position.z, 0.0);
uniformData.environment.copy(uboEnv);
}
void prepare() override {
ExampleBase::prepare();
prepareParticles();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSets();
buildCommandBuffers();
prepared = true;
}
void render() override {
if (!prepared)
return;
draw();
if (!paused) {
updateUniformBufferLight();
updateParticles();
}
}
void viewChanged() override { updateUniformBuffers(); }
};
RUN_EXAMPLE(VulkanExample)