diff options
Diffstat (limited to 'src/effects/particles_effect.cc')
| -rw-r--r-- | src/effects/particles_effect.cc | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/src/effects/particles_effect.cc b/src/effects/particles_effect.cc index b9e2b9c..3c9feb7 100644 --- a/src/effects/particles_effect.cc +++ b/src/effects/particles_effect.cc @@ -1,21 +1,20 @@ // This file is part of the 64k demo project. -// It implements the ParticlesEffect. +// It implements the Particles. -#include "util/fatal_error.h" #include "effects/particles_effect.h" +#include "effects/shaders.h" #include "gpu/gpu.h" -#include "gpu/shaders.h" +#include "util/fatal_error.h" #include <vector> -ParticlesEffect::ParticlesEffect(const GpuContext& ctx, - const std::vector<std::string>& inputs, - const std::vector<std::string>& outputs) - : Effect(ctx, inputs, outputs) { - // Headless mode: skip GPU resource creation (compiled out in STRIP_ALL) +Particles::Particles(const GpuContext& ctx, + const std::vector<std::string>& inputs, + const std::vector<std::string>& outputs, float start_time, + float end_time) + : Effect(ctx, inputs, outputs, start_time, end_time) { HEADLESS_RETURN_IF_NULL(ctx_.device); - // Initialize uniforms - uniforms_.init(ctx_.device); + init_uniforms_buffer(); // Initialize particles buffer std::vector<Particle> init_p(NUM_PARTICLES); @@ -48,51 +47,53 @@ ParticlesEffect::ParticlesEffect(const GpuContext& ctx, // 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, + {uniforms_buffer_.get(), WGPUBufferBindingType_Uniform}}; + compute_pass_ = gpu_create_compute_pass(ctx_.device, particle_compute_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, WGPUTextureFormat_RGBA8Unorm, - particle_render_v2_wgsl, render_bindings, 2); + {uniforms_buffer_.get(), WGPUBufferBindingType_Uniform}}; + render_pass_ = + gpu_create_render_pass(ctx_.device, WGPUTextureFormat_RGBA8Unorm, + particle_render_wgsl, render_bindings, 2); render_pass_.vertex_count = 6; render_pass_.instance_count = NUM_PARTICLES; } -void ParticlesEffect::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { +void Particles::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { // Update uniforms - uniforms_.update(ctx_.queue, params); + uniforms_buffer_.update(ctx_.queue, params); // Run compute pass (particle simulation) - WGPUComputePassEncoder compute = wgpuCommandEncoderBeginComputePass(encoder, nullptr); + 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); + 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}}; + WGPURenderPassColorAttachment color_attachment = {}; + gpu_init_color_attachment(color_attachment, output_view); WGPURenderPassDescriptor render_desc = { - .colorAttachmentCount = 1, - .colorAttachments = &color_attachment}; + .colorAttachmentCount = 1, .colorAttachments = &color_attachment}; - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_desc); + 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); + wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0, + nullptr); + wgpuRenderPassEncoderDraw(pass, render_pass_.vertex_count, + render_pass_.instance_count, 0, 0); wgpuRenderPassEncoderEnd(pass); } |
