summaryrefslogtreecommitdiff
path: root/src/effects/particles_effect.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/effects/particles_effect.cc')
-rw-r--r--src/effects/particles_effect.cc47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/effects/particles_effect.cc b/src/effects/particles_effect.cc
new file mode 100644
index 0000000..5762637
--- /dev/null
+++ b/src/effects/particles_effect.cc
@@ -0,0 +1,47 @@
+// This file is part of the 64k demo project.
+// It implements the ParticlesEffect.
+
+#include "gpu/demo_effects.h"
+#include "gpu/post_process_helper.h"
+#include "gpu/gpu.h"
+#include <vector>
+
+// --- ParticlesEffect ---
+ParticlesEffect::ParticlesEffect(const GpuContext& ctx) : Effect(ctx) {
+ std::vector<Particle> init_p(NUM_PARTICLES);
+ particles_buffer_ = gpu_create_buffer(
+ ctx_.device, sizeof(Particle) * NUM_PARTICLES,
+ WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data());
+ ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage},
+ {uniforms_.get(), WGPUBufferBindingType_Uniform}};
+ compute_pass_ =
+ gpu_create_compute_pass(ctx_.device, particle_compute_wgsl, cb, 2);
+ compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64;
+ ResourceBinding rb[] = {
+ {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage},
+ {uniforms_.get(), WGPUBufferBindingType_Uniform}};
+ render_pass_ = gpu_create_render_pass(ctx_.device, ctx_.format,
+ particle_render_wgsl, rb, 2);
+ render_pass_.vertex_count = 6;
+ render_pass_.instance_count = NUM_PARTICLES;
+}
+void ParticlesEffect::compute(WGPUCommandEncoder e,
+ const CommonPostProcessUniforms& uniforms) {
+ uniforms_.update(ctx_.queue, uniforms);
+ 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 ParticlesEffect::render(WGPURenderPassEncoder pass,
+ const CommonPostProcessUniforms& uniforms) {
+ (void)uniforms;
+ 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);
+}