SHADE_Y3/Assets/Shaders/ParticleUpdate_CS.glsl

153 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2023-02-27 15:14:32 +08:00
#version 450
layout(local_size_x = 128) in;
struct DrawArraysIndirectArgs
{
uint count;
uint instanceCount;
uint first;
uint baseInstance;
};
struct ParticleData
{
vec4 position;
2023-03-16 09:34:42 +08:00
vec4 orientationSpeedDecay;
2023-02-27 15:14:32 +08:00
vec4 velocity;
vec4 acceleration;
vec4 scaleAndDecay;
vec4 colorTint;
2023-02-27 15:14:32 +08:00
float life;
uint textureIndex;
2023-03-13 11:41:23 +08:00
};
2023-02-27 15:14:32 +08:00
struct GenericData
{
//! Delta time
float dt;
//! Elapsed time of the application
float elapsedTime;
//! Viewport width of the scene (excluding imgui, that means smaller than window)
uint viewportWidth;
//! Ditto but for height
uint viewportHeight;
};
layout(set = 1, binding = 0) uniform CameraData
{
vec4 position;
mat4 vpMat;
mat4 viewMat;
mat4 projMat;
} cameraData;
layout (set = 0, binding = 0) uniform GenericDataBuffer
{
GenericData data;
} genericDataBuffer;
layout (std430, set = 2, binding = 1) coherent restrict readonly buffer ParticlesInputBuffer
{
ParticleData data[];
} inputParticles;
// output buffer not needed
layout (std430, set = 2, binding = 2) coherent restrict buffer ParticlesOutputBuffer
{
ParticleData data[];
} outputParticles;
layout (std430, set = 2, binding = 3) coherent restrict buffer ParticlesFreelistBuffer
{
int freeCount;
int freeIndices[];
} freelist;
layout (std430, set = 2, binding = 4) coherent restrict buffer IndicesData
{
uint indices[];
};
2023-03-13 11:41:23 +08:00
layout (std140, set = 2, binding = 5) coherent restrict buffer IndirectDrawArgs
2023-02-27 15:14:32 +08:00
{
DrawArraysIndirectArgs indirectArgs;
2023-03-13 11:41:23 +08:00
};
2023-02-27 15:14:32 +08:00
// push constants
layout(std140, push_constant) uniform EmitterPushConstant
{
vec4 emitterPosition;
uint emissionCount;
} emitterPushConstant;
uint pcg_hash(uint seed)
{
uint state = seed * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
// Used to advance the PCG state.
uint rand_pcg(inout uint rng_state)
{
uint state = rng_state;
rng_state = rng_state * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
// Advances the prng state and returns the corresponding random float.
float rand(inout uint state)
{
uint x = rand_pcg(state);
state = x;
return float(x)*uintBitsToFloat(0x2f800004u);
}
void main()
{
uint index = gl_GlobalInvocationID.x;
ParticleData particle = inputParticles.data[index];
2023-03-13 11:41:23 +08:00
if (particle.life > 0.0f)
2023-02-27 15:14:32 +08:00
{
// update position from velocity
particle.position += particle.velocity * genericDataBuffer.data.dt;
2023-03-16 10:44:03 +08:00
particle.velocity += particle.acceleration;
particle.life -= genericDataBuffer.data.dt;
particle.orientationSpeedDecay.x += particle.orientationSpeedDecay.y;
particle.scaleAndDecay.x *= particle.scaleAndDecay.z;
particle.scaleAndDecay.y *= particle.scaleAndDecay.w;
if (particle.orientationSpeedDecay.y > 0.0f)
{
particle.orientationSpeedDecay.y -= particle.orientationSpeedDecay.z * genericDataBuffer.data.dt;
if (particle.orientationSpeedDecay.y < 0.0f)
particle.orientationSpeedDecay.y = 0.0f;
}
2023-02-27 15:14:32 +08:00
2023-03-13 11:41:23 +08:00
if (particle.life < 0.0f || particle.scaleAndDecay.x < 0.0f || particle.scaleAndDecay.y < 0.0f)
2023-02-27 15:14:32 +08:00
{
2023-03-13 11:41:23 +08:00
particle.life = 0.0f;
particle.position.x = 9999.0f;
2023-02-27 15:14:32 +08:00
outputParticles.data[index] = particle;
freelist.freeIndices[atomicAdd(freelist.freeCount, 1)] = int (index);
return;
}
uint drawIndex = atomicAdd (indirectArgs.instanceCount, 1);
indices[drawIndex] = index;
2023-03-14 19:34:26 +08:00
2023-02-27 15:14:32 +08:00
}
outputParticles.data[index] = particle;
2023-02-27 15:14:32 +08:00
}