summaryrefslogtreecommitdiff
path: root/src/gpu/effects/particle_spray_effect.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-01 01:33:47 +0100
committerskal <pascal.massimino@gmail.com>2026-02-01 01:33:47 +0100
commit709067df5303ddf0715f56903ccfdb877cb4db7e (patch)
tree767fbbf284ebf4084d73c8f7553487316c44a7d0 /src/gpu/effects/particle_spray_effect.cc
parente82ba031c138eb8ee72ed976b270217228a42c64 (diff)
Refactor: Split demo effects and shaders into individual files
Split src/gpu/demo_effects.cc into individual .cc files for each effect and created separate files for post-processing helpers and WGSL shaders. Updated src/gpu/demo_effects.h to be a central header for all effect-related declarations and adjusted CMakeLists.txt accordingly.
Diffstat (limited to 'src/gpu/effects/particle_spray_effect.cc')
-rw-r--r--src/gpu/effects/particle_spray_effect.cc54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/gpu/effects/particle_spray_effect.cc b/src/gpu/effects/particle_spray_effect.cc
new file mode 100644
index 0000000..a5e4292
--- /dev/null
+++ b/src/gpu/effects/particle_spray_effect.cc
@@ -0,0 +1,54 @@
+// This file is part of the 64k demo project.
+// It implements the ParticleSprayEffect.
+
+#include "gpu/demo_effects.h"
+#include "gpu/gpu.h"
+#include <vector>
+
+// --- ParticleSprayEffect ---
+ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue,
+ WGPUTextureFormat format)
+ : queue_(queue) {
+ uniforms_ =
+ gpu_create_buffer(device, sizeof(float) * 4,
+ WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
+ std::vector<Particle> init_p(NUM_PARTICLES);
+ for (Particle& p : init_p)
+ p.pos[3] = 0.0f;
+ particles_buffer_ = gpu_create_buffer(
+ device, sizeof(Particle) * NUM_PARTICLES,
+ WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data());
+ ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage},
+ {uniforms_, WGPUBufferBindingType_Uniform}};
+ compute_pass_ =
+ gpu_create_compute_pass(device, particle_spray_compute_wgsl, cb, 2);
+ compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64;
+ ResourceBinding rb[] = {
+ {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage},
+ {uniforms_, WGPUBufferBindingType_Uniform}};
+ render_pass_ =
+ gpu_create_render_pass(device, format, particle_render_wgsl, rb, 2);
+ render_pass_.vertex_count = 6;
+ render_pass_.instance_count = NUM_PARTICLES;
+}
+void ParticleSprayEffect::compute(WGPUCommandEncoder e, float t, float b,
+ float i, float a) {
+ struct {
+ float i, a, t, b;
+ } u = {i, a, t, b};
+ wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u));
+ WGPUComputePassEncoder pass = wgpuCommandEncoderBeginComputePass(e, nullptr);
+ wgpuComputePassEncoderSetPipeline(pass, compute_pass_.pipeline);
+ wgpuComputePassEncoderSetBindGroup(pass, 0, compute_pass_.bind_group, 0,
+ nullptr);
+ wgpuComputePassEncoderDispatchWorkgroups(pass, compute_pass_.workgroup_size_x,
+ 1, 1);
+ wgpuComputePassEncoderEnd(pass);
+}
+void ParticleSprayEffect::render(WGPURenderPassEncoder pass, float t, float b,
+ float i, float a) {
+ wgpuRenderPassEncoderSetPipeline(pass, render_pass_.pipeline);
+ wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0,
+ nullptr);
+ wgpuRenderPassEncoderDraw(pass, 6, NUM_PARTICLES, 0, 0);
+}