Compute Shaders & GPU Particles

⚡ Vulkan Compute Shaders & GPU Particle Simulation

While graphics pipelines output colored pixels to a swapchain framebuffer, Compute Pipelines (VkComputePipelineCreateInfo) execute general-purpose parallel math directly on GPU cores without rasterizer overhead.

In modern engines, compute shaders simulate 1,000,000+ physics particles, GPU culling, procedural mesh generation, and audio DSP.


🏛️ Compute Dispatch Architecture

[ CPU: vkCmdDispatch(X=1024, Y=1, Z=1) ]
       │
       ▼
[ GPU Compute Units / Warps / Wavefronts ]
  - Workgroup 0: 64 Threads (Particles 0 .. 63)
  - Workgroup 1: 64 Threads (Particles 64 .. 127)
  - Workgroup N: 64 Threads (Particles N*64 .. (N+1)*64-1)
       │
       ▼
[ SSBO: Particle Buffer in Device-Local VRAM ]

💻 1. Particle Simulation Compute Shader (GLSL)

#version 460

layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;

struct Particle {
    vec4 position; // xyz = position, w = lifetime
    vec4 velocity; // xyz = velocity, w = mass
};

layout(std430, set = 0, binding = 0) buffer Particle_Buffer {
    Particle particles[];
};

layout(push_constant) uniform Push_Constants {
    float delta_time;
    vec3 gravity;
} pc;

void main() {
    uint index = gl_GlobalInvocationID.x;
    if (index >= particles.length()) return;

    // Update lifetime
    particles[index].position.w -= pc.delta_time;

    // Apply gravity and update position
    particles[index].velocity.xyz += pc.gravity * pc.delta_time;
    particles[index].position.xyz += particles[index].velocity.xyz * pc.delta_time;
}