From d02366e9b8f8f7f53455580d933e550a7b06f112 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Fri, 31 Jan 2020 07:48:01 -0500 Subject: [PATCH] Add basic compute simulation (disabled) We want to use a compute shader to update the particle states. Unfortunately, they don't want to be updated. If you uncomment the line that runs the compute shader, all the particles turn into a glitchy mess. Committing this code for reproducability's sake. --- main.lua | 10 ++++++++++ shaders/particles.comp | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 shaders/particles.comp diff --git a/main.lua b/main.lua index a9476a9..9764877 100644 --- a/main.lua +++ b/main.lua @@ -8,16 +8,25 @@ local features local shaders = {} local blocks = {} local meshes = {} +local compute = {} function lovr.load() features = lovr.graphics.getFeatures() + shaders.particles = lovr.graphics.newShader("shaders/particles.vert", "shaders/particles.frag") + blocks.particles = lovr.graphics.newShaderBlock("compute", { particles = {"mat4", nParticles}, }, { readable = true, writable = true }) shaders.particles:sendBlock('particleData', blocks.particles) shaders.particles:send("batchSize", batchSize) + meshes.particle = makeParticleMesh(batchSize) + + compute.particles = lovr.graphics.newComputeShader("shaders/particles.comp") + compute.particles:sendBlock('particleData', blocks.particles) + compute.particles:send("dt", 0.0) + local initial = initialParticleData(nParticles, range(-2, 2), range(0, 4), range(-2, 2), range(0.005, 0.01)) blocks.particles:send("particles", initial) end @@ -25,6 +34,7 @@ end function lovr.draw() lovr.graphics.clear() local x, y, z = lovr.headset.getPose("head") + -- lovr.graphics.compute(compute.particles, batches) shaders.particles:send("headPos", {x, y, z}) lovr.graphics.setColor(0.6, 0.1, 0) diff --git a/shaders/particles.comp b/shaders/particles.comp new file mode 100644 index 0000000..f2553ae --- /dev/null +++ b/shaders/particles.comp @@ -0,0 +1,15 @@ +uniform float dt; +struct particle_t { + vec4 positionAndSize; + vec4 velocity; + vec4 padding2; // for LÖVR's limited ShaderBlock type :( + vec4 padding3; // for LÖVR's limited ShaderBlock type :( +}; +layout(std430) buffer particleData { particle_t particles[]; }; + +layout(local_size_x = 128) in; +void compute() { + uint particleId = gl_GlobalInvocationID.x; + particles[particleId].positionAndSize.xyz += + particles[particleId].velocity.xyz * dt; +} -- 2.47.0