diff options
| -rw-r--r-- | cmake/DemoSourceLists.cmake | 1 | ||||
| -rw-r--r-- | src/effects/particles_effect_v2.cc | 94 | ||||
| -rw-r--r-- | src/effects/particles_effect_v2.h | 35 | ||||
| -rw-r--r-- | src/gpu/shaders.cc | 8 | ||||
| -rw-r--r-- | src/gpu/shaders.h | 2 | ||||
| -rw-r--r-- | workspaces/main/assets.txt | 2 | ||||
| -rw-r--r-- | workspaces/main/shaders/particle_compute_v2.wgsl | 31 | ||||
| -rw-r--r-- | workspaces/main/shaders/particle_render_v2.wgsl | 53 | ||||
| -rw-r--r-- | workspaces/main/timeline_v2.seq | 7 |
9 files changed, 229 insertions, 4 deletions
diff --git a/cmake/DemoSourceLists.cmake b/cmake/DemoSourceLists.cmake index bb7cdbc..5325f6a 100644 --- a/cmake/DemoSourceLists.cmake +++ b/cmake/DemoSourceLists.cmake @@ -35,6 +35,7 @@ set(COMMON_GPU_EFFECTS src/effects/placeholder_effect_v2.cc src/effects/gaussian_blur_effect_v2.cc src/effects/heptagon_effect_v2.cc + src/effects/particles_effect_v2.cc src/effects/heptagon_effect.cc src/effects/particles_effect.cc src/effects/passthrough_effect.cc 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_; +}; diff --git a/src/gpu/shaders.cc b/src/gpu/shaders.cc index 4f24705..bea1eb9 100644 --- a/src/gpu/shaders.cc +++ b/src/gpu/shaders.cc @@ -173,3 +173,11 @@ const char* gaussian_blur_v2_shader_wgsl = const char* heptagon_v2_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_HEPTAGON_V2); + +const char* particle_compute_v2_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_PARTICLE_COMPUTE_V2); + +const char* particle_render_v2_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_PARTICLE_RENDER_V2); diff --git a/src/gpu/shaders.h b/src/gpu/shaders.h index d1658fa..b7ee226 100644 --- a/src/gpu/shaders.h +++ b/src/gpu/shaders.h @@ -33,3 +33,5 @@ extern const char* gen_mask_compute_wgsl; extern const char* passthrough_v2_shader_wgsl; extern const char* gaussian_blur_v2_shader_wgsl; extern const char* heptagon_v2_shader_wgsl; +extern const char* particle_compute_v2_wgsl; +extern const char* particle_render_v2_wgsl; diff --git a/workspaces/main/assets.txt b/workspaces/main/assets.txt index de96eab..fe15cdf 100644 --- a/workspaces/main/assets.txt +++ b/workspaces/main/assets.txt @@ -33,6 +33,8 @@ SHADER_RAY_TRIANGLE, NONE, ../../common/shaders/ray_triangle.wgsl, "Ray-Triangle SHADER_MAIN, NONE, shaders/main_shader.wgsl, "Main Heptagon Shader" SHADER_PARTICLE_COMPUTE, NONE, shaders/particle_compute.wgsl, "Particle Compute Shader" SHADER_PARTICLE_RENDER, NONE, shaders/particle_render.wgsl, "Particle Render Shader" +SHADER_PARTICLE_COMPUTE_V2, NONE, shaders/particle_compute_v2.wgsl, "Particle Compute Shader V2" +SHADER_PARTICLE_RENDER_V2, NONE, shaders/particle_render_v2.wgsl, "Particle Render Shader V2" SHADER_PASSTHROUGH, NONE, ../../common/shaders/passthrough.wgsl, "Passthrough Shader" SHADER_ELLIPSE, NONE, shaders/ellipse.wgsl, "Ellipse Shader" SHADER_PARTICLE_SPRAY_COMPUTE, NONE, shaders/particle_spray_compute.wgsl, "Particle Spray Compute" diff --git a/workspaces/main/shaders/particle_compute_v2.wgsl b/workspaces/main/shaders/particle_compute_v2.wgsl new file mode 100644 index 0000000..3683826 --- /dev/null +++ b/workspaces/main/shaders/particle_compute_v2.wgsl @@ -0,0 +1,31 @@ +// Particle simulation (compute shader) - V2 +struct Particle { + pos: vec4<f32>, + vel: vec4<f32>, + rot: vec4<f32>, + color: vec4<f32>, +}; + +#include "sequence_v2_uniforms" + +@group(0) @binding(0) var<storage, read_write> particles: array<Particle>; +@group(0) @binding(1) var<uniform> uniforms: UniformsSequenceParams; + +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + let i = id.x; + if (i >= arrayLength(&particles)) { + return; + } + var p = particles[i]; + let new_pos = p.pos.xyz + p.vel.xyz * 0.016; + p.pos = vec4<f32>(new_pos, p.pos.w); + p.vel.y = p.vel.y - 0.01 * (1.0 + uniforms.audio_intensity * 5.0); + p.rot.x = p.rot.x + p.rot.y * 0.016; + if (p.pos.y < -1.5) { + p.pos.y = 1.5; + p.pos.x = (f32(i % 100u) / 50.0) - 1.0 + (uniforms.audio_intensity * 0.5); + p.vel.y = 0.0; + } + particles[i] = p; +} diff --git a/workspaces/main/shaders/particle_render_v2.wgsl b/workspaces/main/shaders/particle_render_v2.wgsl new file mode 100644 index 0000000..8663658 --- /dev/null +++ b/workspaces/main/shaders/particle_render_v2.wgsl @@ -0,0 +1,53 @@ +// Particle rendering (vertex + fragment) - V2 +struct Particle { + pos: vec4<f32>, + vel: vec4<f32>, + rot: vec4<f32>, + color: vec4<f32>, +}; + +#include "sequence_v2_uniforms" + +@group(0) @binding(0) var<storage, read> particles: array<Particle>; +@group(0) @binding(1) var<uniform> uniforms: UniformsSequenceParams; + +struct VSOut { + @builtin(position) pos: vec4<f32>, + @location(0) color: vec4<f32>, + @location(1) uv: vec2<f32>, +}; + +@vertex fn vs_main(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut { + let p = particles[ii]; + let size = 0.02 + p.pos.z * 0.01 + uniforms.audio_intensity * 0.02; + var offsets = array<vec2<f32>, 6>( + vec2<f32>(-1, -1), + vec2<f32>(1, -1), + vec2<f32>(-1, 1), + vec2<f32>(-1, 1), + vec2<f32>(1, -1), + vec2<f32>(1, 1) + ); + let offset = offsets[vi]; + let c = cos(p.rot.x); + let s = sin(p.rot.x); + let rotated_offset = vec2<f32>(offset.x * c - offset.y * s, offset.x * s + offset.y * c); + let pos = vec2<f32>(p.pos.x + rotated_offset.x * size / uniforms.aspect_ratio, p.pos.y + rotated_offset.y * size); + + // Fade based on lifetime (p.pos.w goes from 1.0 to 0.0) + let lifetime_fade = p.pos.w; + let color_with_fade = vec4<f32>(p.color.rgb * (0.5 + 0.5 * uniforms.audio_intensity), p.color.a * lifetime_fade); + + return VSOut(vec4<f32>(pos, 0.0, 1.0), color_with_fade, offset); +} + +@fragment fn fs_main(@location(0) color: vec4<f32>, @location(1) uv: vec2<f32>) -> @location(0) vec4<f32> { + // Calculate distance from center for circular shape + let dist = length(uv); + + // Smooth circular falloff (1.0 at center, 0.0 at edge) + let circle_alpha = smoothstep(1.0, 0.5, dist); + + // Apply circular fade to alpha channel + return vec4<f32>(color.rgb, color.a * circle_alpha); +} diff --git a/workspaces/main/timeline_v2.seq b/workspaces/main/timeline_v2.seq index b0ab993..2dc1720 100644 --- a/workspaces/main/timeline_v2.seq +++ b/workspaces/main/timeline_v2.seq @@ -18,10 +18,9 @@ SEQUENCE 8.00 0 "flash_cube" EFFECT + PlaceholderEffect temp1 -> sink 0.00 0.40 SEQUENCE 12.00 1 "particles" - # ParticleSpray (placeholder) -> Particles (placeholder) -> Blur -> sink - EFFECT + PlaceholderEffect source -> temp1 0.00 2.00 - EFFECT + PlaceholderEffect temp1 -> temp2 2.00 4.00 - EFFECT = GaussianBlurEffect temp2 -> sink 0.00 4.00 + # Particles -> Blur -> sink + EFFECT + ParticlesEffectV2 source -> temp1 0.00 4.00 + EFFECT = GaussianBlurEffect temp1 -> sink 0.00 4.00 SEQUENCE 16.00 2 "hybrid_heptagon" # Theme (placeholder) -> Heptagon -> ParticleSpray (placeholder) -> Particles (placeholder) -> Hybrid3D (placeholder) -> sink |
