summaryrefslogtreecommitdiff
path: root/src/effects
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-16 10:00:30 +0100
committerskal <pascal.massimino@gmail.com>2026-02-16 10:00:30 +0100
commita126caf9e52b8f7c5be8e09b303e741aa9a410cb (patch)
tree12a74c3dfc4ca0eba5807a323564118713b957a8 /src/effects
parentce246924ac12da5639cce049c7bb2e29de7ed637 (diff)
feat(sequence): port particles_effect to v2
- Add ParticlesEffectV2 with compute + render passes - Create particle_compute_v2.wgsl and particle_render_v2.wgsl - Use UniformsSequenceParams for beat-synchronized particles - Update timeline_v2.seq particles sequence (simplified 2-effect chain) - Add shader exports to shaders.{h,cc} - All 36 tests passing handoff(Claude): Particles v2 complete, rotating_cube next
Diffstat (limited to 'src/effects')
-rw-r--r--src/effects/particles_effect_v2.cc94
-rw-r--r--src/effects/particles_effect_v2.h35
2 files changed, 129 insertions, 0 deletions
diff --git a/src/effects/particles_effect_v2.cc b/src/effects/particles_effect_v2.cc
new file mode 100644
index 0000000..5a1a07c
--- /dev/null
+++ b/src/effects/particles_effect_v2.cc
@@ -0,0 +1,94 @@
+// This file is part of the 64k demo project.
+// It implements the ParticlesEffectV2.
+
+#include "effects/particles_effect_v2.h"
+#include "gpu/gpu.h"
+#include "gpu/shaders.h"
+#include <vector>
+
+ParticlesEffectV2::ParticlesEffectV2(const GpuContext& ctx,
+ const std::vector<std::string>& inputs,
+ const std::vector<std::string>& outputs)
+ : EffectV2(ctx, inputs, outputs) {
+ // Initialize uniforms
+ uniforms_.init(ctx_.device);
+
+ // Initialize particles buffer
+ std::vector<Particle> init_p(NUM_PARTICLES);
+ for (int i = 0; i < NUM_PARTICLES; ++i) {
+ float x = (float)(i % 100) / 50.0f - 1.0f;
+ float y = (float)(i / 100) / 100.0f * 3.0f - 1.5f;
+ init_p[i].pos[0] = x;
+ init_p[i].pos[1] = y;
+ init_p[i].pos[2] = ((float)i / NUM_PARTICLES) * 0.5f;
+ init_p[i].pos[3] = 1.0f;
+ init_p[i].vel[0] = 0.0f;
+ init_p[i].vel[1] = 0.0f;
+ init_p[i].vel[2] = 0.0f;
+ init_p[i].vel[3] = 0.0f;
+ init_p[i].rot[0] = 0.0f;
+ init_p[i].rot[1] = ((float)(i % 7) / 7.0f) * 3.14159f;
+ init_p[i].rot[2] = 0.0f;
+ init_p[i].rot[3] = 0.0f;
+ float hue = (float)(i % 100) / 100.0f;
+ init_p[i].color[0] = 0.5f + 0.5f * hue;
+ init_p[i].color[1] = 0.3f + 0.7f * (1.0f - hue);
+ init_p[i].color[2] = 0.8f;
+ init_p[i].color[3] = 0.8f;
+ }
+
+ particles_buffer_ = gpu_create_buffer(
+ ctx_.device, sizeof(Particle) * NUM_PARTICLES,
+ WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data());
+
+ // Create compute shader (particle simulation)
+ ResourceBinding compute_bindings[] = {
+ {particles_buffer_, WGPUBufferBindingType_Storage},
+ {uniforms_.get(), WGPUBufferBindingType_Uniform}};
+ compute_pass_ = gpu_create_compute_pass(ctx_.device, particle_compute_v2_wgsl,
+ compute_bindings, 2);
+ compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64;
+
+ // Create render shader (particle rendering)
+ ResourceBinding render_bindings[] = {
+ {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage},
+ {uniforms_.get(), WGPUBufferBindingType_Uniform}};
+ render_pass_ = gpu_create_render_pass(ctx_.device, ctx_.format, particle_render_v2_wgsl,
+ render_bindings, 2);
+ render_pass_.vertex_count = 6;
+ render_pass_.instance_count = NUM_PARTICLES;
+}
+
+void ParticlesEffectV2::render(WGPUCommandEncoder encoder,
+ const UniformsSequenceParams& params,
+ NodeRegistry& nodes) {
+ // Update uniforms
+ uniforms_.update(ctx_.queue, params);
+
+ // Run compute pass (particle simulation)
+ WGPUComputePassEncoder compute = wgpuCommandEncoderBeginComputePass(encoder, nullptr);
+ wgpuComputePassEncoderSetPipeline(compute, compute_pass_.pipeline);
+ wgpuComputePassEncoderSetBindGroup(compute, 0, compute_pass_.bind_group, 0, nullptr);
+ wgpuComputePassEncoderDispatchWorkgroups(compute, compute_pass_.workgroup_size_x, 1, 1);
+ wgpuComputePassEncoderEnd(compute);
+
+ // Run render pass (draw particles to output)
+ WGPUTextureView output_view = nodes.get_view(output_nodes_[0]);
+
+ WGPURenderPassColorAttachment color_attachment = {
+ .view = output_view,
+ .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED,
+ .loadOp = WGPULoadOp_Clear,
+ .storeOp = WGPUStoreOp_Store,
+ .clearValue = {0.0, 0.0, 0.0, 1.0}};
+
+ WGPURenderPassDescriptor render_desc = {
+ .colorAttachmentCount = 1,
+ .colorAttachments = &color_attachment};
+
+ WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_desc);
+ wgpuRenderPassEncoderSetPipeline(pass, render_pass_.pipeline);
+ wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0, nullptr);
+ wgpuRenderPassEncoderDraw(pass, render_pass_.vertex_count, render_pass_.instance_count, 0, 0);
+ wgpuRenderPassEncoderEnd(pass);
+}
diff --git a/src/effects/particles_effect_v2.h b/src/effects/particles_effect_v2.h
new file mode 100644
index 0000000..f0f260c
--- /dev/null
+++ b/src/effects/particles_effect_v2.h
@@ -0,0 +1,35 @@
+// This file is part of the 64k demo project.
+// It declares the ParticlesEffectV2.
+
+#pragma once
+
+#include "gpu/effect_v2.h"
+#include "gpu/gpu.h"
+#include "gpu/uniform_helper.h"
+#include <vector>
+
+// Particle structure
+static const int NUM_PARTICLES = 10000;
+
+struct Particle {
+ float pos[4];
+ float vel[4];
+ float rot[4];
+ float color[4];
+};
+
+class ParticlesEffectV2 : public EffectV2 {
+ public:
+ ParticlesEffectV2(const GpuContext& ctx,
+ const std::vector<std::string>& inputs,
+ const std::vector<std::string>& outputs);
+ void render(WGPUCommandEncoder encoder,
+ const UniformsSequenceParams& params,
+ NodeRegistry& nodes) override;
+
+ private:
+ ComputePass compute_pass_;
+ RenderPass render_pass_;
+ GpuBuffer particles_buffer_;
+ UniformBuffer<UniformsSequenceParams> uniforms_;
+};