2021-12-16 14:17:01 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout (constant_id = 0) const int MaxParticles = 2048;
|
|
|
|
|
|
|
|
layout (local_size_x = 1, local_size_y = 1) in;
|
|
|
|
|
|
|
|
struct Particle {
|
|
|
|
vec4 pos;
|
|
|
|
vec4 vel;
|
|
|
|
vec4 color;
|
|
|
|
float tex;
|
|
|
|
float ramp;
|
|
|
|
float scale;
|
|
|
|
float live;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Parameters {
|
|
|
|
vec4 drag; // [dx, dy, dz, grav scale]
|
|
|
|
vec4 ramp; // [rate, max, alpha rate, scale rate]
|
|
|
|
};
|
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 0, binding = 0) buffer OutStates {
|
2021-12-16 14:17:01 +00:00
|
|
|
Particle particles[];
|
2022-11-27 15:52:07 +00:00
|
|
|
} outStates;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 0, binding = 1) buffer OutParameters {
|
2021-12-16 14:17:01 +00:00
|
|
|
Parameters parameters[];
|
2022-11-27 15:52:07 +00:00
|
|
|
} outParameters;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
2021-12-16 15:18:25 +00:00
|
|
|
//doubles as VkDrawIndirectCommand
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 0, binding = 2) buffer OutSystem {
|
2021-12-16 15:18:25 +00:00
|
|
|
uint vertexCount;
|
|
|
|
uint particleCount; //instanceCount
|
|
|
|
uint firstVertex;
|
|
|
|
uint firstInstance;
|
2022-11-27 15:52:07 +00:00
|
|
|
} outSystem;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 1, binding = 0) buffer InStates {
|
2021-12-16 14:17:01 +00:00
|
|
|
Particle particles[];
|
2022-11-27 15:52:07 +00:00
|
|
|
} inStates;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 1, binding = 1) buffer InParameters {
|
2021-12-16 14:17:01 +00:00
|
|
|
Parameters parameters[];
|
2022-11-27 15:52:07 +00:00
|
|
|
} inParameters;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
2021-12-16 15:18:25 +00:00
|
|
|
//doubles as VkDrawIndirectCommand
|
2022-11-27 15:52:07 +00:00
|
|
|
layout(std140, set = 1, binding = 2) buffer InSystem {
|
2021-12-16 15:18:25 +00:00
|
|
|
uint vertexCount;
|
|
|
|
uint particleCount; //instanceCount
|
|
|
|
uint firstVertex;
|
|
|
|
uint firstInstance;
|
2022-11-27 15:52:07 +00:00
|
|
|
} inSystem;
|
2021-12-16 14:17:01 +00:00
|
|
|
|
|
|
|
layout(std140, set = 2, binding = 0) buffer NewStates {
|
|
|
|
Particle particles[];
|
|
|
|
} newStates;
|
|
|
|
|
|
|
|
layout(std140, set = 2, binding = 1) buffer NewParameters {
|
|
|
|
Parameters parameters[];
|
|
|
|
} newParameters;
|
|
|
|
|
2021-12-16 15:18:25 +00:00
|
|
|
//doubles as VkDrawIndirectCommand
|
2021-12-16 14:17:01 +00:00
|
|
|
layout(std140, set = 2, binding = 2) buffer NewSystem {
|
2021-12-16 15:18:25 +00:00
|
|
|
uint vertexCount;
|
|
|
|
uint particleCount; //instanceCount
|
|
|
|
uint firstVertex;
|
|
|
|
uint firstInstance;
|
2021-12-16 14:17:01 +00:00
|
|
|
} newSystem;
|
|
|
|
|
|
|
|
bool
|
|
|
|
is_dead (in Particle part, in Parameters parm)
|
|
|
|
{
|
|
|
|
if (part.live <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (part.ramp >= parm.ramp.y || part.color.a <= 0 || part.scale <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
main ()
|
|
|
|
{
|
|
|
|
uint j = 0;
|
|
|
|
// compact existing partles removing dead particles
|
|
|
|
for (uint i = 0; i < inSystem.particleCount; i++) {
|
2022-11-27 15:52:07 +00:00
|
|
|
if (is_dead (inStates.particles[i], inParameters.parameters[i])) {
|
2021-12-16 14:17:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-27 15:52:07 +00:00
|
|
|
outStates.particles[j] = inStates.particles[i];
|
2021-12-16 14:17:01 +00:00
|
|
|
outParameters.parameters[j] = inParameters.parameters[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
// inject any new particles that aren't DOA
|
|
|
|
for (uint i = 0; i < newSystem.particleCount && j < MaxParticles; i++) {
|
2022-11-27 15:52:07 +00:00
|
|
|
if (is_dead (newStates.particles[i], newParameters.parameters[i])) {
|
2021-12-16 14:17:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
outStates.particles[j] = newStates.particles[i];
|
|
|
|
outParameters.parameters[j] = newParameters.parameters[i];
|
|
|
|
j++;
|
|
|
|
}
|
2022-11-27 15:52:07 +00:00
|
|
|
outSystem.vertexCount = newSystem.vertexCount;
|
2021-12-16 14:17:01 +00:00
|
|
|
outSystem.particleCount = j;
|
2022-11-27 15:52:07 +00:00
|
|
|
outSystem.firstVertex = newSystem.firstVertex;
|
|
|
|
outSystem.firstInstance = newSystem.firstInstance;
|
2021-12-16 14:17:01 +00:00
|
|
|
}
|