From b2ede3f0680edc894a54e28374cb87ab2690afa2 Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 16 Feb 2026 14:32:59 +0100 Subject: refactor: remove v2 versioning artifacts, establish Sequence as canonical system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Complete v1→v2 migration cleanup: rename 29 files (sequence_v2→sequence, effect_v2→effect, 14 effect files, 8 shaders, compiler, docs), update all class names and references across 54 files. Archive v1 timeline. System now uses standard naming with all versioning removed. 30/34 tests passing. Co-Authored-By: Claude Sonnet 4.5 --- src/app/main.cc | 6 +- src/app/test_demo.cc | 8 +- src/effects/gaussian_blur_effect.cc | 84 ++++++++++++ src/effects/gaussian_blur_effect.h | 34 +++++ src/effects/gaussian_blur_effect_v2.cc | 84 ------------ src/effects/gaussian_blur_effect_v2.h | 33 ----- src/effects/heptagon_effect.cc | 81 ++++++++++++ src/effects/heptagon_effect.h | 25 ++++ src/effects/heptagon_effect_v2.cc | 81 ------------ src/effects/heptagon_effect_v2.h | 24 ---- src/effects/hybrid3_d_effect.cc | 114 ++++++++++++++++ src/effects/hybrid3_d_effect.h | 32 +++++ src/effects/hybrid3_d_effect_v2.cc | 114 ---------------- src/effects/hybrid3_d_effect_v2.h | 32 ----- src/effects/particles_effect.cc | 96 ++++++++++++++ src/effects/particles_effect.h | 36 +++++ src/effects/particles_effect_v2.cc | 96 -------------- src/effects/particles_effect_v2.h | 35 ----- src/effects/passthrough_effect.cc | 81 ++++++++++++ src/effects/passthrough_effect.h | 21 +++ src/effects/passthrough_effect_v2.cc | 81 ------------ src/effects/passthrough_effect_v2.h | 21 --- src/effects/placeholder_effect.cc | 63 +++++++++ src/effects/placeholder_effect.h | 25 ++++ src/effects/placeholder_effect_v2.cc | 63 --------- src/effects/placeholder_effect_v2.h | 24 ---- src/effects/rotating_cube_effect.cc | 186 ++++++++++++++++++++++++++ src/effects/rotating_cube_effect.h | 49 +++++++ src/effects/rotating_cube_effect_v2.cc | 186 -------------------------- src/effects/rotating_cube_effect_v2.h | 49 ------- src/gpu/demo_effects.h | 20 +-- src/gpu/effect.cc | 11 ++ src/gpu/effect.h | 50 +++++++ src/gpu/effect_v2.cc | 11 -- src/gpu/effect_v2.h | 47 ------- src/gpu/gpu.cc | 2 +- src/gpu/sequence.cc | 235 +++++++++++++++++++++++++++++++++ src/gpu/sequence.h | 131 ++++++++++++++++++ src/gpu/sequence_v2.cc | 235 --------------------------------- src/gpu/sequence_v2.h | 128 ------------------ src/tests/gpu/test_demo_effects.cc | 38 +++--- src/tests/gpu/test_effect_base.cc | 34 ++--- src/tests/gpu/test_sequence.cc | 184 ++++++++++++++++++++++++++ src/tests/gpu/test_sequence_e2e.cc | 112 ++++++++++++++++ src/tests/gpu/test_sequence_v2.cc | 184 -------------------------- src/tests/gpu/test_sequence_v2_e2e.cc | 112 ---------------- 46 files changed, 1704 insertions(+), 1694 deletions(-) create mode 100644 src/effects/gaussian_blur_effect.cc create mode 100644 src/effects/gaussian_blur_effect.h delete mode 100644 src/effects/gaussian_blur_effect_v2.cc delete mode 100644 src/effects/gaussian_blur_effect_v2.h create mode 100644 src/effects/heptagon_effect.cc create mode 100644 src/effects/heptagon_effect.h delete mode 100644 src/effects/heptagon_effect_v2.cc delete mode 100644 src/effects/heptagon_effect_v2.h create mode 100644 src/effects/hybrid3_d_effect.cc create mode 100644 src/effects/hybrid3_d_effect.h delete mode 100644 src/effects/hybrid3_d_effect_v2.cc delete mode 100644 src/effects/hybrid3_d_effect_v2.h create mode 100644 src/effects/particles_effect.cc create mode 100644 src/effects/particles_effect.h delete mode 100644 src/effects/particles_effect_v2.cc delete mode 100644 src/effects/particles_effect_v2.h create mode 100644 src/effects/passthrough_effect.cc create mode 100644 src/effects/passthrough_effect.h delete mode 100644 src/effects/passthrough_effect_v2.cc delete mode 100644 src/effects/passthrough_effect_v2.h create mode 100644 src/effects/placeholder_effect.cc create mode 100644 src/effects/placeholder_effect.h delete mode 100644 src/effects/placeholder_effect_v2.cc delete mode 100644 src/effects/placeholder_effect_v2.h create mode 100644 src/effects/rotating_cube_effect.cc create mode 100644 src/effects/rotating_cube_effect.h delete mode 100644 src/effects/rotating_cube_effect_v2.cc delete mode 100644 src/effects/rotating_cube_effect_v2.h create mode 100644 src/gpu/effect.cc create mode 100644 src/gpu/effect.h delete mode 100644 src/gpu/effect_v2.cc delete mode 100644 src/gpu/effect_v2.h create mode 100644 src/gpu/sequence.cc create mode 100644 src/gpu/sequence.h delete mode 100644 src/gpu/sequence_v2.cc delete mode 100644 src/gpu/sequence_v2.h create mode 100644 src/tests/gpu/test_sequence.cc create mode 100644 src/tests/gpu/test_sequence_e2e.cc delete mode 100644 src/tests/gpu/test_sequence_v2.cc delete mode 100644 src/tests/gpu/test_sequence_v2_e2e.cc (limited to 'src') diff --git a/src/app/main.cc b/src/app/main.cc index 75995ad..7496e8c 100644 --- a/src/app/main.cc +++ b/src/app/main.cc @@ -20,7 +20,7 @@ #include "generated/assets.h" // Include generated asset header #include "gpu/demo_effects.h" #include "gpu/gpu.h" -#include "generated/timeline_v2.h" // For GetDemoDuration(), RenderV2Timeline() +#include "generated/timeline.h" // For GetDemoDuration(), RenderTimeline() #include "platform/platform.h" #include "util/math.h" #include @@ -109,7 +109,7 @@ int main(int argc, char** argv) { gpu_init(&platform_state); // Initialize v2 sequences - InitializeV2Sequences(*gpu_get_context(), width, height); + InitializeSequences(*gpu_get_context(), width, height); #if !defined(STRIP_ALL) // Set WAV dump backend if requested @@ -414,7 +414,7 @@ int main(int argc, char** argv) { } // Draw graphics using v2 timeline - RenderV2Timeline(gpu_get_surface(), (float)current_physical_time, width, height, + RenderTimeline(gpu_get_surface(), (float)current_physical_time, width, height, absolute_beat_time, visual_peak); last_frame_time = current_physical_time; diff --git a/src/app/test_demo.cc b/src/app/test_demo.cc index c2366c3..993ceba 100644 --- a/src/app/test_demo.cc +++ b/src/app/test_demo.cc @@ -16,7 +16,7 @@ #include // External declarations from generated test timeline (v2) -#include "generated/test_timeline_v2.h" +#include "generated/test_timeline.h" extern float GetDemoDuration(); // TODO: Port PeakMeterEffect and CNN effects to v2 @@ -226,7 +226,7 @@ int main(int argc, char** argv) { gpu_init(&platform_state); // Initialize v2 timeline from test_demo.seq - InitializeTestV2Sequences(*gpu_get_context(), width, height); + InitializeSequences(*gpu_get_context(), width, height); #if !defined(STRIP_ALL) // TODO: Port CNN and peak meter effects to v2 @@ -397,8 +397,8 @@ int main(int argc, char** argv) { // Draw graphics using v2 timeline const float graphics_frame_time = (float)current_physical_time; - RenderTestV2Timeline(gpu_get_surface(), graphics_frame_time, width, height, - absolute_beat_time, visual_peak); + RenderTimeline(gpu_get_surface(), graphics_frame_time, width, height, + absolute_beat_time, visual_peak); // Update audio systems (tracker, synth, etc.) based on audio time // progression diff --git a/src/effects/gaussian_blur_effect.cc b/src/effects/gaussian_blur_effect.cc new file mode 100644 index 0000000..17c657f --- /dev/null +++ b/src/effects/gaussian_blur_effect.cc @@ -0,0 +1,84 @@ +// Gaussian blur effect v2 implementation + +#include "effects/gaussian_blur_effect.h" +#include "gpu/post_process_helper.h" +#include "gpu/shaders.h" + +GaussianBlurEffect::GaussianBlurEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), + sampler_(nullptr) { + // Create pipeline + pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, + gaussian_blur_v2_shader_wgsl); + + // Create sampler + WGPUSamplerDescriptor sampler_desc = {}; + sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; + sampler_desc.magFilter = WGPUFilterMode_Linear; + sampler_desc.minFilter = WGPUFilterMode_Linear; + sampler_desc.maxAnisotropy = 1; + sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); + + // Init uniform buffers + params_buffer_.init(ctx_.device); + uniforms_buffer_.init(ctx_.device); +} + +void GaussianBlurEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + // Get input/output views + WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); + + // Update uniforms + uniforms_buffer_.update(ctx_.queue, params); + params_buffer_.update(ctx_.queue, blur_params_); + + // Update bind group + WGPUBindGroupEntry entries[4] = {}; + entries[0].binding = PP_BINDING_SAMPLER; + entries[0].sampler = sampler_; + entries[1].binding = PP_BINDING_TEXTURE; + entries[1].textureView = input_view; + entries[2].binding = PP_BINDING_UNIFORMS; + entries[2].buffer = uniforms_buffer_.get().buffer; + entries[2].size = sizeof(UniformsSequenceParams); + entries[3].binding = PP_BINDING_EFFECT_PARAMS; + entries[3].buffer = params_buffer_.get().buffer; + entries[3].size = sizeof(GaussianBlurParams); + + WGPUBindGroupDescriptor bg_desc = {}; + bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0); + bg_desc.entryCount = 4; + bg_desc.entries = entries; + + if (bind_group_) { + wgpuBindGroupRelease(bind_group_); + } + bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); + + // Render pass + WGPURenderPassColorAttachment color_attachment = {}; + color_attachment.view = output_view; +#if !defined(DEMO_CROSS_COMPILE_WIN32) + color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; +#endif + color_attachment.loadOp = WGPULoadOp_Clear; + color_attachment.storeOp = WGPUStoreOp_Store; + color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/gaussian_blur_effect.h b/src/effects/gaussian_blur_effect.h new file mode 100644 index 0000000..8bf34dc --- /dev/null +++ b/src/effects/gaussian_blur_effect.h @@ -0,0 +1,34 @@ +// Gaussian blur effect v2 - single-pass blur + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +struct GaussianBlurParams { + float strength = 1.0f; + float strength_audio = 0.5f; + float stretch = 1.0f; + float _pad = 0.0f; +}; +static_assert(sizeof(GaussianBlurParams) == 16, + "GaussianBlurParams must be 16 bytes"); + +class GaussianBlurEffect : public Effect { + public: + GaussianBlurEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs); + + void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + WGPURenderPipeline pipeline_; + WGPUBindGroup bind_group_; + WGPUSampler sampler_; + GaussianBlurParams blur_params_; + UniformBuffer params_buffer_; + UniformBuffer uniforms_buffer_; +}; + diff --git a/src/effects/gaussian_blur_effect_v2.cc b/src/effects/gaussian_blur_effect_v2.cc deleted file mode 100644 index 0c90fa2..0000000 --- a/src/effects/gaussian_blur_effect_v2.cc +++ /dev/null @@ -1,84 +0,0 @@ -// Gaussian blur effect v2 implementation - -#include "effects/gaussian_blur_effect_v2.h" -#include "gpu/post_process_helper.h" -#include "gpu/shaders.h" - -GaussianBlurEffectV2::GaussianBlurEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), - sampler_(nullptr) { - // Create pipeline - pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, - gaussian_blur_v2_shader_wgsl); - - // Create sampler - WGPUSamplerDescriptor sampler_desc = {}; - sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; - sampler_desc.magFilter = WGPUFilterMode_Linear; - sampler_desc.minFilter = WGPUFilterMode_Linear; - sampler_desc.maxAnisotropy = 1; - sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); - - // Init uniform buffers - params_buffer_.init(ctx_.device); - uniforms_buffer_.init(ctx_.device); -} - -void GaussianBlurEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - // Get input/output views - WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); - WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); - - // Update uniforms - uniforms_buffer_.update(ctx_.queue, params); - params_buffer_.update(ctx_.queue, blur_params_); - - // Update bind group - WGPUBindGroupEntry entries[4] = {}; - entries[0].binding = PP_BINDING_SAMPLER; - entries[0].sampler = sampler_; - entries[1].binding = PP_BINDING_TEXTURE; - entries[1].textureView = input_view; - entries[2].binding = PP_BINDING_UNIFORMS; - entries[2].buffer = uniforms_buffer_.get().buffer; - entries[2].size = sizeof(UniformsSequenceParams); - entries[3].binding = PP_BINDING_EFFECT_PARAMS; - entries[3].buffer = params_buffer_.get().buffer; - entries[3].size = sizeof(GaussianBlurParams); - - WGPUBindGroupDescriptor bg_desc = {}; - bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0); - bg_desc.entryCount = 4; - bg_desc.entries = entries; - - if (bind_group_) { - wgpuBindGroupRelease(bind_group_); - } - bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); - - // Render pass - WGPURenderPassColorAttachment color_attachment = {}; - color_attachment.view = output_view; -#if !defined(DEMO_CROSS_COMPILE_WIN32) - color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif - color_attachment.loadOp = WGPULoadOp_Clear; - color_attachment.storeOp = WGPUStoreOp_Store; - color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; - - WGPURenderPassDescriptor pass_desc = {}; - pass_desc.colorAttachmentCount = 1; - pass_desc.colorAttachments = &color_attachment; - - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); - wgpuRenderPassEncoderEnd(pass); - wgpuRenderPassEncoderRelease(pass); -} diff --git a/src/effects/gaussian_blur_effect_v2.h b/src/effects/gaussian_blur_effect_v2.h deleted file mode 100644 index c5d88ff..0000000 --- a/src/effects/gaussian_blur_effect_v2.h +++ /dev/null @@ -1,33 +0,0 @@ -// Gaussian blur effect v2 - single-pass blur - -#pragma once - -#include "gpu/effect_v2.h" -#include "gpu/uniform_helper.h" - -struct GaussianBlurParams { - float strength = 1.0f; - float strength_audio = 0.5f; - float stretch = 1.0f; - float _pad = 0.0f; -}; -static_assert(sizeof(GaussianBlurParams) == 16, - "GaussianBlurParams must be 16 bytes"); - -class GaussianBlurEffectV2 : public EffectV2 { - public: - GaussianBlurEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs); - - void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - WGPURenderPipeline pipeline_; - WGPUBindGroup bind_group_; - WGPUSampler sampler_; - GaussianBlurParams blur_params_; - UniformBuffer params_buffer_; - UniformBuffer uniforms_buffer_; -}; diff --git a/src/effects/heptagon_effect.cc b/src/effects/heptagon_effect.cc new file mode 100644 index 0000000..27d59da --- /dev/null +++ b/src/effects/heptagon_effect.cc @@ -0,0 +1,81 @@ +// Heptagon effect v2 implementation + +#include "effects/heptagon_effect.h" +#include "gpu/gpu.h" +#include "gpu/post_process_helper.h" +#include "gpu/shaders.h" + +HeptagonEffect::HeptagonEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), sampler_(nullptr) { + // Init uniforms + uniforms_buffer_.init(ctx_.device); + + // Create pipeline (standard v2 post-process, no depth) + pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, + heptagon_v2_shader_wgsl); + + // Create dummy sampler (scene effects don't use texture input) + WGPUSamplerDescriptor sampler_desc = {}; + sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; + sampler_desc.magFilter = WGPUFilterMode_Nearest; + sampler_desc.minFilter = WGPUFilterMode_Nearest; + sampler_desc.maxAnisotropy = 1; + sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); + + // Create 1×1 dummy texture + WGPUTextureDescriptor tex_desc = {}; + tex_desc.size = {1, 1, 1}; + tex_desc.format = WGPUTextureFormat_RGBA8Unorm; + tex_desc.usage = WGPUTextureUsage_TextureBinding; + tex_desc.dimension = WGPUTextureDimension_2D; + tex_desc.mipLevelCount = 1; + tex_desc.sampleCount = 1; + dummy_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc); + dummy_texture_view_ = wgpuTextureCreateView(dummy_texture_, nullptr); +} + +HeptagonEffect::~HeptagonEffect() { + if (bind_group_) wgpuBindGroupRelease(bind_group_); + if (pipeline_) wgpuRenderPipelineRelease(pipeline_); + if (sampler_) wgpuSamplerRelease(sampler_); + if (dummy_texture_view_) wgpuTextureViewRelease(dummy_texture_view_); + if (dummy_texture_) wgpuTextureRelease(dummy_texture_); +} + +void HeptagonEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + // Get output view (scene effects typically write to output, ignore input) + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); + + // Update uniforms + uniforms_buffer_.update(ctx_.queue, params); + + // Create bind group (use dummy texture for scene effect) + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, dummy_texture_view_, + uniforms_buffer_.get(), {nullptr, 0}); + + // Render pass + WGPURenderPassColorAttachment color_attachment = {}; + color_attachment.view = output_view; +#if !defined(DEMO_CROSS_COMPILE_WIN32) + color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; +#endif + color_attachment.loadOp = WGPULoadOp_Clear; + color_attachment.storeOp = WGPUStoreOp_Store; + color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 21, 1, 0, 0); // 7 triangles * 3 vertices + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/heptagon_effect.h b/src/effects/heptagon_effect.h new file mode 100644 index 0000000..9f148a1 --- /dev/null +++ b/src/effects/heptagon_effect.h @@ -0,0 +1,25 @@ +// Heptagon effect v2 - scene rendering effect + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +class HeptagonEffect : public Effect { + public: + HeptagonEffect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs); + ~HeptagonEffect(); + + void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + WGPURenderPipeline pipeline_; + WGPUBindGroup bind_group_; + WGPUSampler sampler_; + WGPUTexture dummy_texture_; + WGPUTextureView dummy_texture_view_; + UniformBuffer uniforms_buffer_; +}; + diff --git a/src/effects/heptagon_effect_v2.cc b/src/effects/heptagon_effect_v2.cc deleted file mode 100644 index 6a2135e..0000000 --- a/src/effects/heptagon_effect_v2.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Heptagon effect v2 implementation - -#include "effects/heptagon_effect_v2.h" -#include "gpu/gpu.h" -#include "gpu/post_process_helper.h" -#include "gpu/shaders.h" - -HeptagonEffectV2::HeptagonEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), sampler_(nullptr) { - // Init uniforms - uniforms_buffer_.init(ctx_.device); - - // Create pipeline (standard v2 post-process, no depth) - pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, - heptagon_v2_shader_wgsl); - - // Create dummy sampler (scene effects don't use texture input) - WGPUSamplerDescriptor sampler_desc = {}; - sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; - sampler_desc.magFilter = WGPUFilterMode_Nearest; - sampler_desc.minFilter = WGPUFilterMode_Nearest; - sampler_desc.maxAnisotropy = 1; - sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); - - // Create 1×1 dummy texture - WGPUTextureDescriptor tex_desc = {}; - tex_desc.size = {1, 1, 1}; - tex_desc.format = WGPUTextureFormat_RGBA8Unorm; - tex_desc.usage = WGPUTextureUsage_TextureBinding; - tex_desc.dimension = WGPUTextureDimension_2D; - tex_desc.mipLevelCount = 1; - tex_desc.sampleCount = 1; - dummy_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc); - dummy_texture_view_ = wgpuTextureCreateView(dummy_texture_, nullptr); -} - -HeptagonEffectV2::~HeptagonEffectV2() { - if (bind_group_) wgpuBindGroupRelease(bind_group_); - if (pipeline_) wgpuRenderPipelineRelease(pipeline_); - if (sampler_) wgpuSamplerRelease(sampler_); - if (dummy_texture_view_) wgpuTextureViewRelease(dummy_texture_view_); - if (dummy_texture_) wgpuTextureRelease(dummy_texture_); -} - -void HeptagonEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - // Get output view (scene effects typically write to output, ignore input) - WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); - - // Update uniforms - uniforms_buffer_.update(ctx_.queue, params); - - // Create bind group (use dummy texture for scene effect) - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, dummy_texture_view_, - uniforms_buffer_.get(), {nullptr, 0}); - - // Render pass - WGPURenderPassColorAttachment color_attachment = {}; - color_attachment.view = output_view; -#if !defined(DEMO_CROSS_COMPILE_WIN32) - color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif - color_attachment.loadOp = WGPULoadOp_Clear; - color_attachment.storeOp = WGPUStoreOp_Store; - color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; - - WGPURenderPassDescriptor pass_desc = {}; - pass_desc.colorAttachmentCount = 1; - pass_desc.colorAttachments = &color_attachment; - - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 21, 1, 0, 0); // 7 triangles * 3 vertices - wgpuRenderPassEncoderEnd(pass); - wgpuRenderPassEncoderRelease(pass); -} diff --git a/src/effects/heptagon_effect_v2.h b/src/effects/heptagon_effect_v2.h deleted file mode 100644 index 1737a46..0000000 --- a/src/effects/heptagon_effect_v2.h +++ /dev/null @@ -1,24 +0,0 @@ -// Heptagon effect v2 - scene rendering effect - -#pragma once - -#include "gpu/effect_v2.h" -#include "gpu/uniform_helper.h" - -class HeptagonEffectV2 : public EffectV2 { - public: - HeptagonEffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs); - ~HeptagonEffectV2(); - - void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - WGPURenderPipeline pipeline_; - WGPUBindGroup bind_group_; - WGPUSampler sampler_; - WGPUTexture dummy_texture_; - WGPUTextureView dummy_texture_view_; - UniformBuffer uniforms_buffer_; -}; diff --git a/src/effects/hybrid3_d_effect.cc b/src/effects/hybrid3_d_effect.cc new file mode 100644 index 0000000..ced5b42 --- /dev/null +++ b/src/effects/hybrid3_d_effect.cc @@ -0,0 +1,114 @@ +// This file is part of the 64k demo project. +// It implements Hybrid3DEffect (simplified v2 port). +// TODO: Full Renderer3D integration with texture manager, noise assets + +#include "effects/hybrid3_d_effect.h" +#include + +Hybrid3DEffect::Hybrid3DEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), depth_node_(outputs[0] + "_depth"), + dummy_texture_(nullptr), dummy_texture_view_(nullptr) { + // Initialize renderer (format is always RGBA8Unorm for v2) + renderer_.init(ctx_.device, ctx_.queue, WGPUTextureFormat_RGBA8Unorm); + + // Create 1×1 white dummy texture for noise/sky (Renderer3D requires these) + WGPUTextureDescriptor tex_desc = {}; + tex_desc.size = {1, 1, 1}; + tex_desc.format = WGPUTextureFormat_RGBA8Unorm; + tex_desc.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst; + tex_desc.dimension = WGPUTextureDimension_2D; + tex_desc.mipLevelCount = 1; + tex_desc.sampleCount = 1; + dummy_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc); + dummy_texture_view_ = wgpuTextureCreateView(dummy_texture_, nullptr); + + // Write white pixel + uint32_t white_pixel = 0xFFFFFFFF; +#if defined(DEMO_CROSS_COMPILE_WIN32) + WGPUImageCopyTexture dst = { + .texture = dummy_texture_, + .mipLevel = 0, + .origin = {0, 0, 0} + }; + WGPUTextureDataLayout data_layout = { + .bytesPerRow = 4, + .rowsPerImage = 1 + }; +#else + WGPUTexelCopyTextureInfo dst = { + .texture = dummy_texture_, + .mipLevel = 0, + .origin = {0, 0, 0} + }; + WGPUTexelCopyBufferLayout data_layout = { + .bytesPerRow = 4, + .rowsPerImage = 1 + }; +#endif + WGPUExtent3D size = {1, 1, 1}; + wgpuQueueWriteTexture(ctx_.queue, &dst, &white_pixel, 4, &data_layout, &size); + + renderer_.set_noise_texture(dummy_texture_view_); + renderer_.set_sky_texture(dummy_texture_view_); + + initialized_ = true; + + // Setup simple scene (1 center cube + 8 surrounding objects) + scene_.clear(); + Object3D center(ObjectType::BOX); + center.position = vec3(0, 0, 0); + center.color = vec4(1, 0, 0, 1); + scene_.add_object(center); + + for (int i = 0; i < 8; ++i) { + ObjectType type = (i % 3 == 1) ? ObjectType::TORUS : + (i % 3 == 2) ? ObjectType::BOX : ObjectType::SPHERE; + + Object3D obj(type); + float angle = (i / 8.0f) * 6.28318f; + obj.position = vec3(std::cos(angle) * 4.0f, 0, std::sin(angle) * 4.0f); + obj.scale = vec3(0.7f, 0.7f, 0.7f); + + if (type == ObjectType::SPHERE) + obj.color = vec4(0, 1, 0, 1); + else if (type == ObjectType::TORUS) + obj.color = vec4(0, 0.5f, 1, 1); + else + obj.color = vec4(1, 1, 0, 1); + + scene_.add_object(obj); + } +} + +Hybrid3DEffect::~Hybrid3DEffect() { + if (dummy_texture_view_) + wgpuTextureViewRelease(dummy_texture_view_); + if (dummy_texture_) + wgpuTextureRelease(dummy_texture_); + renderer_.shutdown(); +} + +void Hybrid3DEffect::declare_nodes(NodeRegistry& registry) { + // Declare depth buffer node + registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1); +} + +void Hybrid3DEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + // Update camera (orbiting) + float angle = params.time * 0.3f; + vec3 cam_pos = vec3(std::cos(angle) * 10.0f, 5.0f, std::sin(angle) * 10.0f); + camera_.position = cam_pos; + camera_.target = vec3(0, 0, 0); + camera_.aspect_ratio = params.aspect_ratio; + + // Get output views + WGPUTextureView color_view = nodes.get_view(output_nodes_[0]); + WGPUTextureView depth_view = nodes.get_view(depth_node_); + + // Render 3D scene + renderer_.render(scene_, camera_, params.time, color_view, depth_view); +} diff --git a/src/effects/hybrid3_d_effect.h b/src/effects/hybrid3_d_effect.h new file mode 100644 index 0000000..e6e0f49 --- /dev/null +++ b/src/effects/hybrid3_d_effect.h @@ -0,0 +1,32 @@ +// This file is part of the 64k demo project. +// It declares Hybrid3DEffect (simplified v2 port). +// TODO: Full Renderer3D integration with Scene/Camera + +#pragma once + +#include "3d/camera.h" +#include "3d/renderer.h" +#include "3d/scene.h" +#include "gpu/effect.h" + +class Hybrid3DEffect : public Effect { + public: + Hybrid3DEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs); + ~Hybrid3DEffect() override; + + void declare_nodes(NodeRegistry& registry) override; + void render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + Renderer3D renderer_; + Scene scene_; + Camera camera_; + bool initialized_ = false; + std::string depth_node_; + WGPUTexture dummy_texture_; + WGPUTextureView dummy_texture_view_; +}; diff --git a/src/effects/hybrid3_d_effect_v2.cc b/src/effects/hybrid3_d_effect_v2.cc deleted file mode 100644 index 38e4e66..0000000 --- a/src/effects/hybrid3_d_effect_v2.cc +++ /dev/null @@ -1,114 +0,0 @@ -// This file is part of the 64k demo project. -// It implements Hybrid3DEffectV2 (simplified v2 port). -// TODO: Full Renderer3D integration with texture manager, noise assets - -#include "effects/hybrid3_d_effect_v2.h" -#include - -Hybrid3DEffectV2::Hybrid3DEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), depth_node_(outputs[0] + "_depth"), - dummy_texture_(nullptr), dummy_texture_view_(nullptr) { - // Initialize renderer (format is always RGBA8Unorm for v2) - renderer_.init(ctx_.device, ctx_.queue, WGPUTextureFormat_RGBA8Unorm); - - // Create 1×1 white dummy texture for noise/sky (Renderer3D requires these) - WGPUTextureDescriptor tex_desc = {}; - tex_desc.size = {1, 1, 1}; - tex_desc.format = WGPUTextureFormat_RGBA8Unorm; - tex_desc.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst; - tex_desc.dimension = WGPUTextureDimension_2D; - tex_desc.mipLevelCount = 1; - tex_desc.sampleCount = 1; - dummy_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc); - dummy_texture_view_ = wgpuTextureCreateView(dummy_texture_, nullptr); - - // Write white pixel - uint32_t white_pixel = 0xFFFFFFFF; -#if defined(DEMO_CROSS_COMPILE_WIN32) - WGPUImageCopyTexture dst = { - .texture = dummy_texture_, - .mipLevel = 0, - .origin = {0, 0, 0} - }; - WGPUTextureDataLayout data_layout = { - .bytesPerRow = 4, - .rowsPerImage = 1 - }; -#else - WGPUTexelCopyTextureInfo dst = { - .texture = dummy_texture_, - .mipLevel = 0, - .origin = {0, 0, 0} - }; - WGPUTexelCopyBufferLayout data_layout = { - .bytesPerRow = 4, - .rowsPerImage = 1 - }; -#endif - WGPUExtent3D size = {1, 1, 1}; - wgpuQueueWriteTexture(ctx_.queue, &dst, &white_pixel, 4, &data_layout, &size); - - renderer_.set_noise_texture(dummy_texture_view_); - renderer_.set_sky_texture(dummy_texture_view_); - - initialized_ = true; - - // Setup simple scene (1 center cube + 8 surrounding objects) - scene_.clear(); - Object3D center(ObjectType::BOX); - center.position = vec3(0, 0, 0); - center.color = vec4(1, 0, 0, 1); - scene_.add_object(center); - - for (int i = 0; i < 8; ++i) { - ObjectType type = (i % 3 == 1) ? ObjectType::TORUS : - (i % 3 == 2) ? ObjectType::BOX : ObjectType::SPHERE; - - Object3D obj(type); - float angle = (i / 8.0f) * 6.28318f; - obj.position = vec3(std::cos(angle) * 4.0f, 0, std::sin(angle) * 4.0f); - obj.scale = vec3(0.7f, 0.7f, 0.7f); - - if (type == ObjectType::SPHERE) - obj.color = vec4(0, 1, 0, 1); - else if (type == ObjectType::TORUS) - obj.color = vec4(0, 0.5f, 1, 1); - else - obj.color = vec4(1, 1, 0, 1); - - scene_.add_object(obj); - } -} - -Hybrid3DEffectV2::~Hybrid3DEffectV2() { - if (dummy_texture_view_) - wgpuTextureViewRelease(dummy_texture_view_); - if (dummy_texture_) - wgpuTextureRelease(dummy_texture_); - renderer_.shutdown(); -} - -void Hybrid3DEffectV2::declare_nodes(NodeRegistry& registry) { - // Declare depth buffer node - registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1); -} - -void Hybrid3DEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - // Update camera (orbiting) - float angle = params.time * 0.3f; - vec3 cam_pos = vec3(std::cos(angle) * 10.0f, 5.0f, std::sin(angle) * 10.0f); - camera_.position = cam_pos; - camera_.target = vec3(0, 0, 0); - camera_.aspect_ratio = params.aspect_ratio; - - // Get output views - WGPUTextureView color_view = nodes.get_view(output_nodes_[0]); - WGPUTextureView depth_view = nodes.get_view(depth_node_); - - // Render 3D scene - renderer_.render(scene_, camera_, params.time, color_view, depth_view); -} diff --git a/src/effects/hybrid3_d_effect_v2.h b/src/effects/hybrid3_d_effect_v2.h deleted file mode 100644 index c8116b0..0000000 --- a/src/effects/hybrid3_d_effect_v2.h +++ /dev/null @@ -1,32 +0,0 @@ -// This file is part of the 64k demo project. -// It declares Hybrid3DEffectV2 (simplified v2 port). -// TODO: Full Renderer3D integration with Scene/Camera - -#pragma once - -#include "3d/camera.h" -#include "3d/renderer.h" -#include "3d/scene.h" -#include "gpu/effect_v2.h" - -class Hybrid3DEffectV2 : public EffectV2 { - public: - Hybrid3DEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs); - ~Hybrid3DEffectV2() override; - - void declare_nodes(NodeRegistry& registry) override; - void render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - Renderer3D renderer_; - Scene scene_; - Camera camera_; - bool initialized_ = false; - std::string depth_node_; - WGPUTexture dummy_texture_; - WGPUTextureView dummy_texture_view_; -}; diff --git a/src/effects/particles_effect.cc b/src/effects/particles_effect.cc new file mode 100644 index 0000000..9d73bf7 --- /dev/null +++ b/src/effects/particles_effect.cc @@ -0,0 +1,96 @@ +// This file is part of the 64k demo project. +// It implements the ParticlesEffect. + +#include "effects/particles_effect.h" +#include "gpu/gpu.h" +#include "gpu/shaders.h" +#include + +ParticlesEffect::ParticlesEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs) { + // Initialize uniforms + uniforms_.init(ctx_.device); + + // Initialize particles buffer + std::vector 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, WGPUTextureFormat_RGBA8Unorm, + particle_render_v2_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) { + // 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, +#if !defined(DEMO_CROSS_COMPILE_WIN32) + .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, +#endif + .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.h b/src/effects/particles_effect.h new file mode 100644 index 0000000..76c2ef4 --- /dev/null +++ b/src/effects/particles_effect.h @@ -0,0 +1,36 @@ +// This file is part of the 64k demo project. +// It declares the ParticlesEffect. + +#pragma once + +#include "gpu/effect.h" +#include "gpu/gpu.h" +#include "gpu/uniform_helper.h" +#include + +// Particle structure +static const int NUM_PARTICLES = 10000; + +struct Particle { + float pos[4]; + float vel[4]; + float rot[4]; + float color[4]; +}; + +class ParticlesEffect : public Effect { + public: + ParticlesEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs); + void render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + ComputePass compute_pass_; + RenderPass render_pass_; + GpuBuffer particles_buffer_; + UniformBuffer uniforms_; +}; + diff --git a/src/effects/particles_effect_v2.cc b/src/effects/particles_effect_v2.cc deleted file mode 100644 index 69da4da..0000000 --- a/src/effects/particles_effect_v2.cc +++ /dev/null @@ -1,96 +0,0 @@ -// 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 - -ParticlesEffectV2::ParticlesEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs) { - // Initialize uniforms - uniforms_.init(ctx_.device); - - // Initialize particles buffer - std::vector 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, WGPUTextureFormat_RGBA8Unorm, - 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, -#if !defined(DEMO_CROSS_COMPILE_WIN32) - .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, -#endif - .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 deleted file mode 100644 index f0f260c..0000000 --- a/src/effects/particles_effect_v2.h +++ /dev/null @@ -1,35 +0,0 @@ -// 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 - -// 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& inputs, - const std::vector& outputs); - void render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - ComputePass compute_pass_; - RenderPass render_pass_; - GpuBuffer particles_buffer_; - UniformBuffer uniforms_; -}; diff --git a/src/effects/passthrough_effect.cc b/src/effects/passthrough_effect.cc new file mode 100644 index 0000000..ba98657 --- /dev/null +++ b/src/effects/passthrough_effect.cc @@ -0,0 +1,81 @@ +// Passthrough effect v2 implementation + +#include "effects/passthrough_effect.h" +#include "gpu/post_process_helper.h" +#include "gpu/shaders.h" + +PassthroughEffect::PassthroughEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), + sampler_(nullptr) { + // Init uniform buffer + uniforms_buffer_.init(ctx_.device); + // Create pipeline (simple version without effect params) + pipeline_ = create_post_process_pipeline_simple(ctx_.device, WGPUTextureFormat_RGBA8Unorm, + passthrough_v2_shader_wgsl); + + // Create sampler + WGPUSamplerDescriptor sampler_desc = {}; + sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge; + sampler_desc.magFilter = WGPUFilterMode_Linear; + sampler_desc.minFilter = WGPUFilterMode_Linear; + sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Nearest; + sampler_desc.maxAnisotropy = 1; + sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); +} + +void PassthroughEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + // Get input/output views + WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); + + // Update uniforms + uniforms_buffer_.update(ctx_.queue, params); + + // Manually create bind group with only 3 entries (no effect params needed) + WGPUBindGroupEntry entries[3] = {}; + entries[0].binding = PP_BINDING_SAMPLER; + entries[0].sampler = sampler_; + entries[1].binding = PP_BINDING_TEXTURE; + entries[1].textureView = input_view; + entries[2].binding = PP_BINDING_UNIFORMS; + entries[2].buffer = uniforms_buffer_.get().buffer; + entries[2].size = sizeof(UniformsSequenceParams); + + WGPUBindGroupDescriptor bg_desc = {}; + bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0); + bg_desc.entryCount = 3; + bg_desc.entries = entries; + + if (bind_group_) { + wgpuBindGroupRelease(bind_group_); + } + bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); + + // Render pass + WGPURenderPassColorAttachment color_attachment = { + .view = output_view, +#if !defined(DEMO_CROSS_COMPILE_WIN32) + .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, +#endif + .loadOp = WGPULoadOp_Clear, + .storeOp = WGPUStoreOp_Store, + .clearValue = {0.0, 0.0, 0.0, 1.0} + }; + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); // Fullscreen triangle + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/passthrough_effect.h b/src/effects/passthrough_effect.h new file mode 100644 index 0000000..125ac5a --- /dev/null +++ b/src/effects/passthrough_effect.h @@ -0,0 +1,21 @@ +// Passthrough effect v2 - simple copy input to output + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +class PassthroughEffect : public Effect { + public: + PassthroughEffect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs); + + void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + WGPURenderPipeline pipeline_; + WGPUBindGroup bind_group_; + WGPUSampler sampler_; + UniformBuffer uniforms_buffer_; +}; diff --git a/src/effects/passthrough_effect_v2.cc b/src/effects/passthrough_effect_v2.cc deleted file mode 100644 index 38bb63a..0000000 --- a/src/effects/passthrough_effect_v2.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Passthrough effect v2 implementation - -#include "effects/passthrough_effect_v2.h" -#include "gpu/post_process_helper.h" -#include "gpu/shaders.h" - -PassthroughEffectV2::PassthroughEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), - sampler_(nullptr) { - // Init uniform buffer - uniforms_buffer_.init(ctx_.device); - // Create pipeline (simple version without effect params) - pipeline_ = create_post_process_pipeline_simple(ctx_.device, WGPUTextureFormat_RGBA8Unorm, - passthrough_v2_shader_wgsl); - - // Create sampler - WGPUSamplerDescriptor sampler_desc = {}; - sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge; - sampler_desc.magFilter = WGPUFilterMode_Linear; - sampler_desc.minFilter = WGPUFilterMode_Linear; - sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Nearest; - sampler_desc.maxAnisotropy = 1; - sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); -} - -void PassthroughEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - // Get input/output views - WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); - WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); - - // Update uniforms - uniforms_buffer_.update(ctx_.queue, params); - - // Manually create bind group with only 3 entries (no effect params needed) - WGPUBindGroupEntry entries[3] = {}; - entries[0].binding = PP_BINDING_SAMPLER; - entries[0].sampler = sampler_; - entries[1].binding = PP_BINDING_TEXTURE; - entries[1].textureView = input_view; - entries[2].binding = PP_BINDING_UNIFORMS; - entries[2].buffer = uniforms_buffer_.get().buffer; - entries[2].size = sizeof(UniformsSequenceParams); - - WGPUBindGroupDescriptor bg_desc = {}; - bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0); - bg_desc.entryCount = 3; - bg_desc.entries = entries; - - if (bind_group_) { - wgpuBindGroupRelease(bind_group_); - } - bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); - - // Render pass - WGPURenderPassColorAttachment color_attachment = { - .view = output_view, -#if !defined(DEMO_CROSS_COMPILE_WIN32) - .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, -#endif - .loadOp = WGPULoadOp_Clear, - .storeOp = WGPUStoreOp_Store, - .clearValue = {0.0, 0.0, 0.0, 1.0} - }; - - WGPURenderPassDescriptor pass_desc = {}; - pass_desc.colorAttachmentCount = 1; - pass_desc.colorAttachments = &color_attachment; - - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); // Fullscreen triangle - wgpuRenderPassEncoderEnd(pass); - wgpuRenderPassEncoderRelease(pass); -} diff --git a/src/effects/passthrough_effect_v2.h b/src/effects/passthrough_effect_v2.h deleted file mode 100644 index a272b87..0000000 --- a/src/effects/passthrough_effect_v2.h +++ /dev/null @@ -1,21 +0,0 @@ -// Passthrough effect v2 - simple copy input to output - -#pragma once - -#include "gpu/effect_v2.h" -#include "gpu/uniform_helper.h" - -class PassthroughEffectV2 : public EffectV2 { - public: - PassthroughEffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs); - - void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - WGPURenderPipeline pipeline_; - WGPUBindGroup bind_group_; - WGPUSampler sampler_; - UniformBuffer uniforms_buffer_; -}; diff --git a/src/effects/placeholder_effect.cc b/src/effects/placeholder_effect.cc new file mode 100644 index 0000000..d3308de --- /dev/null +++ b/src/effects/placeholder_effect.cc @@ -0,0 +1,63 @@ +// Placeholder effect v2 implementation - logs TODO warning once + +#include "effects/placeholder_effect.h" +#include "gpu/post_process_helper.h" +#include "gpu/shaders.h" +#include + +PlaceholderEffect::PlaceholderEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs, + const char* placeholder_name) + : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), + sampler_(nullptr), name_(placeholder_name) { + // Log once on construction + fprintf(stderr, "TODO: %s not yet ported to v2, using passthrough\n", name_); + + uniforms_buffer_.init(ctx_.device); + pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, + passthrough_v2_shader_wgsl); + + WGPUSamplerDescriptor sampler_desc = {}; + sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; + sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge; + sampler_desc.magFilter = WGPUFilterMode_Linear; + sampler_desc.minFilter = WGPUFilterMode_Linear; + sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Nearest; + sampler_desc.maxAnisotropy = 1; + sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); +} + +void PlaceholderEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); + + uniforms_buffer_.update(ctx_.queue, params); + + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, + uniforms_buffer_.get(), {nullptr, 0}); + + WGPURenderPassColorAttachment color_attachment = { + .view = output_view, +#if !defined(DEMO_CROSS_COMPILE_WIN32) + .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, +#endif + .loadOp = WGPULoadOp_Clear, + .storeOp = WGPUStoreOp_Store, + .clearValue = {0.0, 0.0, 0.0, 1.0} + }; + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/placeholder_effect.h b/src/effects/placeholder_effect.h new file mode 100644 index 0000000..f7917ab --- /dev/null +++ b/src/effects/placeholder_effect.h @@ -0,0 +1,25 @@ +// Placeholder effect v2 - temporary passthrough for unported effects +// TODO: Replace with actual effect implementations + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +class PlaceholderEffect : public Effect { + public: + PlaceholderEffect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs, + const char* placeholder_name = "UnknownEffect"); + + void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + WGPURenderPipeline pipeline_; + WGPUBindGroup bind_group_; + WGPUSampler sampler_; + UniformBuffer uniforms_buffer_; + const char* name_; +}; + diff --git a/src/effects/placeholder_effect_v2.cc b/src/effects/placeholder_effect_v2.cc deleted file mode 100644 index d1fa212..0000000 --- a/src/effects/placeholder_effect_v2.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Placeholder effect v2 implementation - logs TODO warning once - -#include "effects/placeholder_effect_v2.h" -#include "gpu/post_process_helper.h" -#include "gpu/shaders.h" -#include - -PlaceholderEffectV2::PlaceholderEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs, - const char* placeholder_name) - : EffectV2(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), - sampler_(nullptr), name_(placeholder_name) { - // Log once on construction - fprintf(stderr, "TODO: %s not yet ported to v2, using passthrough\n", name_); - - uniforms_buffer_.init(ctx_.device); - pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm, - passthrough_v2_shader_wgsl); - - WGPUSamplerDescriptor sampler_desc = {}; - sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge; - sampler_desc.magFilter = WGPUFilterMode_Linear; - sampler_desc.minFilter = WGPUFilterMode_Linear; - sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Nearest; - sampler_desc.maxAnisotropy = 1; - sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); -} - -void PlaceholderEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); - WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); - - uniforms_buffer_.update(ctx_.queue, params); - - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, - uniforms_buffer_.get(), {nullptr, 0}); - - WGPURenderPassColorAttachment color_attachment = { - .view = output_view, -#if !defined(DEMO_CROSS_COMPILE_WIN32) - .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, -#endif - .loadOp = WGPULoadOp_Clear, - .storeOp = WGPUStoreOp_Store, - .clearValue = {0.0, 0.0, 0.0, 1.0} - }; - - WGPURenderPassDescriptor pass_desc = {}; - pass_desc.colorAttachmentCount = 1; - pass_desc.colorAttachments = &color_attachment; - - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); - wgpuRenderPassEncoderEnd(pass); - wgpuRenderPassEncoderRelease(pass); -} diff --git a/src/effects/placeholder_effect_v2.h b/src/effects/placeholder_effect_v2.h deleted file mode 100644 index aa9ed75..0000000 --- a/src/effects/placeholder_effect_v2.h +++ /dev/null @@ -1,24 +0,0 @@ -// Placeholder effect v2 - temporary passthrough for unported effects -// TODO: Replace with actual effect implementations - -#pragma once - -#include "gpu/effect_v2.h" -#include "gpu/uniform_helper.h" - -class PlaceholderEffectV2 : public EffectV2 { - public: - PlaceholderEffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs, - const char* placeholder_name = "UnknownEffect"); - - void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - WGPURenderPipeline pipeline_; - WGPUBindGroup bind_group_; - WGPUSampler sampler_; - UniformBuffer uniforms_buffer_; - const char* name_; -}; diff --git a/src/effects/rotating_cube_effect.cc b/src/effects/rotating_cube_effect.cc new file mode 100644 index 0000000..3f1d445 --- /dev/null +++ b/src/effects/rotating_cube_effect.cc @@ -0,0 +1,186 @@ +// This file is part of the 64k demo project. +// It implements RotatingCubeEffect (simplified v2 port). + +#include "effects/rotating_cube_effect.h" +#include "gpu/bind_group_builder.h" +#include "gpu/gpu.h" +#include "gpu/shaders.h" + +RotatingCubeEffect::RotatingCubeEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), depth_node_(outputs[0] + "_depth") { + // Create uniform buffers + uniform_buffer_ = + gpu_create_buffer(ctx_.device, sizeof(Uniforms), + WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + object_buffer_ = + gpu_create_buffer(ctx_.device, sizeof(ObjectData), + WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst); + + // Create bind group layout + WGPUBindGroupLayout bgl = + BindGroupLayoutBuilder() + .uniform(0, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, + sizeof(Uniforms)) + .storage(1, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, + sizeof(ObjectData)) + .build(ctx_.device); + + const WGPUBindGroupLayout bgls[] = {bgl}; + const WGPUPipelineLayoutDescriptor pl_desc = { + .bindGroupLayoutCount = 1, + .bindGroupLayouts = bgls, + }; + WGPUPipelineLayout pipeline_layout = + wgpuDeviceCreatePipelineLayout(ctx_.device, &pl_desc); + + // Load shader (TODO: create rotating_cube_v2.wgsl) + WGPUShaderSourceWGSL wgsl_src = {}; + wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; + wgsl_src.code = str_view(rotating_cube_v2_wgsl); + + WGPUShaderModuleDescriptor shader_desc = {}; + shader_desc.nextInChain = &wgsl_src.chain; + WGPUShaderModule shader_module = + wgpuDeviceCreateShaderModule(ctx_.device, &shader_desc); + + const WGPUColorTargetState color_target = { + .format = WGPUTextureFormat_RGBA8Unorm, + .writeMask = WGPUColorWriteMask_All, + }; + + const WGPUDepthStencilState depth_stencil = { + .format = WGPUTextureFormat_Depth24Plus, + .depthWriteEnabled = WGPUOptionalBool_True, + .depthCompare = WGPUCompareFunction_Less, + }; + + WGPUFragmentState fragment = {}; + fragment.module = shader_module; + fragment.entryPoint = str_view("fs_main"); + fragment.targetCount = 1; + fragment.targets = &color_target; + + WGPURenderPipelineDescriptor pipeline_desc = {}; + pipeline_desc.layout = pipeline_layout; + pipeline_desc.vertex.module = shader_module; + pipeline_desc.vertex.entryPoint = str_view("vs_main"); + pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; + pipeline_desc.primitive.cullMode = WGPUCullMode_Back; + pipeline_desc.depthStencil = &depth_stencil; + pipeline_desc.multisample.count = 1; + pipeline_desc.multisample.mask = 0xFFFFFFFF; + pipeline_desc.fragment = &fragment; + + pipeline_ = wgpuDeviceCreateRenderPipeline(ctx_.device, &pipeline_desc); + wgpuShaderModuleRelease(shader_module); + wgpuPipelineLayoutRelease(pipeline_layout); + + // Create bind group + const WGPUBindGroupEntry entries[] = { + {.binding = 0, + .buffer = uniform_buffer_.buffer, + .size = sizeof(Uniforms)}, + {.binding = 1, + .buffer = object_buffer_.buffer, + .size = sizeof(ObjectData)}, + }; + + const WGPUBindGroupDescriptor bg_desc = { + .layout = bgl, + .entryCount = 2, + .entries = entries, + }; + bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); + wgpuBindGroupLayoutRelease(bgl); +} + +RotatingCubeEffect::~RotatingCubeEffect() { + if (bind_group_) + wgpuBindGroupRelease(bind_group_); + if (pipeline_) + wgpuRenderPipelineRelease(pipeline_); +} + +void RotatingCubeEffect::declare_nodes(NodeRegistry& registry) { + // Declare depth buffer node + registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1); +} + +void RotatingCubeEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + rotation_ += 0.016f * 1.5f; + + // Camera setup + const vec3 camera_pos = vec3(0, 0, 5); + const vec3 target = vec3(0, 0, 0); + const vec3 up = vec3(0, 1, 0); + + const mat4 view = mat4::look_at(camera_pos, target, up); + const float fov = 60.0f * 3.14159f / 180.0f; + const mat4 proj = mat4::perspective(fov, params.aspect_ratio, 0.1f, 100.0f); + const mat4 view_proj = proj * view; + + // Cube transform + const quat rot = quat::from_axis(vec3(0.3f, 1.0f, 0.2f), rotation_); + const mat4 T = mat4::translate(vec3(0, 0, 0)); + const mat4 R = rot.to_mat(); + const mat4 S = mat4::scale(vec3(1.5f, 1.5f, 1.5f)); + const mat4 model = T * R * S; + + // Update uniforms + const Uniforms uniforms = { + .view_proj = view_proj, + .inv_view_proj = view_proj.inverse(), + .camera_pos_time = vec4(camera_pos.x, camera_pos.y, camera_pos.z, params.time), + .params = vec4(1.0f, 0.0f, 0.0f, 0.0f), + .resolution = params.resolution, + .aspect_ratio = params.aspect_ratio, + }; + + const ObjectData obj_data = { + .model = model, + .inv_model = model.inverse(), + .color = vec4(0.8f, 0.4f, 0.2f, 1.0f), + .params = vec4(1.0f, 0.0f, 0.0f, 0.0f), + }; + + wgpuQueueWriteBuffer(ctx_.queue, uniform_buffer_.buffer, 0, &uniforms, + sizeof(Uniforms)); + wgpuQueueWriteBuffer(ctx_.queue, object_buffer_.buffer, 0, &obj_data, + sizeof(ObjectData)); + + // Get output views + WGPUTextureView color_view = nodes.get_view(output_nodes_[0]); + WGPUTextureView depth_view = nodes.get_view(depth_node_); + + // Render pass with depth + WGPURenderPassColorAttachment color_attachment = { + .view = color_view, +#if !defined(DEMO_CROSS_COMPILE_WIN32) + .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, +#endif + .loadOp = WGPULoadOp_Clear, + .storeOp = WGPUStoreOp_Store, + .clearValue = {0.0, 0.0, 0.0, 1.0}}; + + WGPURenderPassDepthStencilAttachment depth_attachment = { + .view = depth_view, + .depthLoadOp = WGPULoadOp_Clear, + .depthStoreOp = WGPUStoreOp_Discard, + .depthClearValue = 1.0f}; + + WGPURenderPassDescriptor pass_desc = { + .colorAttachmentCount = 1, + .colorAttachments = &color_attachment, + .depthStencilAttachment = &depth_attachment}; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 36, 1, 0, 0); // 36 vertices for cube + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/rotating_cube_effect.h b/src/effects/rotating_cube_effect.h new file mode 100644 index 0000000..1c0155a --- /dev/null +++ b/src/effects/rotating_cube_effect.h @@ -0,0 +1,49 @@ +// This file is part of the 64k demo project. +// It declares RotatingCubeEffect (simplified v2 port). + +#pragma once + +#include "gpu/effect.h" +#include "gpu/gpu.h" +#include "gpu/uniform_helper.h" +#include "util/mini_math.h" + +class RotatingCubeEffect : public Effect { + public: + RotatingCubeEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs); + ~RotatingCubeEffect() override; + + void declare_nodes(NodeRegistry& registry) override; + void render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + struct Uniforms { + mat4 view_proj; + mat4 inv_view_proj; + vec4 camera_pos_time; + vec4 params; + vec2 resolution; + float aspect_ratio; + float _pad; + }; + static_assert(sizeof(Uniforms) == 176, "Uniforms size mismatch"); + + struct ObjectData { + mat4 model; + mat4 inv_model; + vec4 color; + vec4 params; + }; + static_assert(sizeof(ObjectData) == 160, "ObjectData size mismatch"); + + WGPURenderPipeline pipeline_ = nullptr; + WGPUBindGroup bind_group_ = nullptr; + GpuBuffer uniform_buffer_; + GpuBuffer object_buffer_; + float rotation_ = 0.0f; + std::string depth_node_; +}; diff --git a/src/effects/rotating_cube_effect_v2.cc b/src/effects/rotating_cube_effect_v2.cc deleted file mode 100644 index 1a28cad..0000000 --- a/src/effects/rotating_cube_effect_v2.cc +++ /dev/null @@ -1,186 +0,0 @@ -// This file is part of the 64k demo project. -// It implements RotatingCubeEffectV2 (simplified v2 port). - -#include "effects/rotating_cube_effect_v2.h" -#include "gpu/bind_group_builder.h" -#include "gpu/gpu.h" -#include "gpu/shaders.h" - -RotatingCubeEffectV2::RotatingCubeEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), depth_node_(outputs[0] + "_depth") { - // Create uniform buffers - uniform_buffer_ = - gpu_create_buffer(ctx_.device, sizeof(Uniforms), - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); - object_buffer_ = - gpu_create_buffer(ctx_.device, sizeof(ObjectData), - WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst); - - // Create bind group layout - WGPUBindGroupLayout bgl = - BindGroupLayoutBuilder() - .uniform(0, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, - sizeof(Uniforms)) - .storage(1, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, - sizeof(ObjectData)) - .build(ctx_.device); - - const WGPUBindGroupLayout bgls[] = {bgl}; - const WGPUPipelineLayoutDescriptor pl_desc = { - .bindGroupLayoutCount = 1, - .bindGroupLayouts = bgls, - }; - WGPUPipelineLayout pipeline_layout = - wgpuDeviceCreatePipelineLayout(ctx_.device, &pl_desc); - - // Load shader (TODO: create rotating_cube_v2.wgsl) - WGPUShaderSourceWGSL wgsl_src = {}; - wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(rotating_cube_v2_wgsl); - - WGPUShaderModuleDescriptor shader_desc = {}; - shader_desc.nextInChain = &wgsl_src.chain; - WGPUShaderModule shader_module = - wgpuDeviceCreateShaderModule(ctx_.device, &shader_desc); - - const WGPUColorTargetState color_target = { - .format = WGPUTextureFormat_RGBA8Unorm, - .writeMask = WGPUColorWriteMask_All, - }; - - const WGPUDepthStencilState depth_stencil = { - .format = WGPUTextureFormat_Depth24Plus, - .depthWriteEnabled = WGPUOptionalBool_True, - .depthCompare = WGPUCompareFunction_Less, - }; - - WGPUFragmentState fragment = {}; - fragment.module = shader_module; - fragment.entryPoint = str_view("fs_main"); - fragment.targetCount = 1; - fragment.targets = &color_target; - - WGPURenderPipelineDescriptor pipeline_desc = {}; - pipeline_desc.layout = pipeline_layout; - pipeline_desc.vertex.module = shader_module; - pipeline_desc.vertex.entryPoint = str_view("vs_main"); - pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; - pipeline_desc.primitive.cullMode = WGPUCullMode_Back; - pipeline_desc.depthStencil = &depth_stencil; - pipeline_desc.multisample.count = 1; - pipeline_desc.multisample.mask = 0xFFFFFFFF; - pipeline_desc.fragment = &fragment; - - pipeline_ = wgpuDeviceCreateRenderPipeline(ctx_.device, &pipeline_desc); - wgpuShaderModuleRelease(shader_module); - wgpuPipelineLayoutRelease(pipeline_layout); - - // Create bind group - const WGPUBindGroupEntry entries[] = { - {.binding = 0, - .buffer = uniform_buffer_.buffer, - .size = sizeof(Uniforms)}, - {.binding = 1, - .buffer = object_buffer_.buffer, - .size = sizeof(ObjectData)}, - }; - - const WGPUBindGroupDescriptor bg_desc = { - .layout = bgl, - .entryCount = 2, - .entries = entries, - }; - bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); - wgpuBindGroupLayoutRelease(bgl); -} - -RotatingCubeEffectV2::~RotatingCubeEffectV2() { - if (bind_group_) - wgpuBindGroupRelease(bind_group_); - if (pipeline_) - wgpuRenderPipelineRelease(pipeline_); -} - -void RotatingCubeEffectV2::declare_nodes(NodeRegistry& registry) { - // Declare depth buffer node - registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1); -} - -void RotatingCubeEffectV2::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { - rotation_ += 0.016f * 1.5f; - - // Camera setup - const vec3 camera_pos = vec3(0, 0, 5); - const vec3 target = vec3(0, 0, 0); - const vec3 up = vec3(0, 1, 0); - - const mat4 view = mat4::look_at(camera_pos, target, up); - const float fov = 60.0f * 3.14159f / 180.0f; - const mat4 proj = mat4::perspective(fov, params.aspect_ratio, 0.1f, 100.0f); - const mat4 view_proj = proj * view; - - // Cube transform - const quat rot = quat::from_axis(vec3(0.3f, 1.0f, 0.2f), rotation_); - const mat4 T = mat4::translate(vec3(0, 0, 0)); - const mat4 R = rot.to_mat(); - const mat4 S = mat4::scale(vec3(1.5f, 1.5f, 1.5f)); - const mat4 model = T * R * S; - - // Update uniforms - const Uniforms uniforms = { - .view_proj = view_proj, - .inv_view_proj = view_proj.inverse(), - .camera_pos_time = vec4(camera_pos.x, camera_pos.y, camera_pos.z, params.time), - .params = vec4(1.0f, 0.0f, 0.0f, 0.0f), - .resolution = params.resolution, - .aspect_ratio = params.aspect_ratio, - }; - - const ObjectData obj_data = { - .model = model, - .inv_model = model.inverse(), - .color = vec4(0.8f, 0.4f, 0.2f, 1.0f), - .params = vec4(1.0f, 0.0f, 0.0f, 0.0f), - }; - - wgpuQueueWriteBuffer(ctx_.queue, uniform_buffer_.buffer, 0, &uniforms, - sizeof(Uniforms)); - wgpuQueueWriteBuffer(ctx_.queue, object_buffer_.buffer, 0, &obj_data, - sizeof(ObjectData)); - - // Get output views - WGPUTextureView color_view = nodes.get_view(output_nodes_[0]); - WGPUTextureView depth_view = nodes.get_view(depth_node_); - - // Render pass with depth - WGPURenderPassColorAttachment color_attachment = { - .view = color_view, -#if !defined(DEMO_CROSS_COMPILE_WIN32) - .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, -#endif - .loadOp = WGPULoadOp_Clear, - .storeOp = WGPUStoreOp_Store, - .clearValue = {0.0, 0.0, 0.0, 1.0}}; - - WGPURenderPassDepthStencilAttachment depth_attachment = { - .view = depth_view, - .depthLoadOp = WGPULoadOp_Clear, - .depthStoreOp = WGPUStoreOp_Discard, - .depthClearValue = 1.0f}; - - WGPURenderPassDescriptor pass_desc = { - .colorAttachmentCount = 1, - .colorAttachments = &color_attachment, - .depthStencilAttachment = &depth_attachment}; - - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 36, 1, 0, 0); // 36 vertices for cube - wgpuRenderPassEncoderEnd(pass); - wgpuRenderPassEncoderRelease(pass); -} diff --git a/src/effects/rotating_cube_effect_v2.h b/src/effects/rotating_cube_effect_v2.h deleted file mode 100644 index 19ef410..0000000 --- a/src/effects/rotating_cube_effect_v2.h +++ /dev/null @@ -1,49 +0,0 @@ -// This file is part of the 64k demo project. -// It declares RotatingCubeEffectV2 (simplified v2 port). - -#pragma once - -#include "gpu/effect_v2.h" -#include "gpu/gpu.h" -#include "gpu/uniform_helper.h" -#include "util/mini_math.h" - -class RotatingCubeEffectV2 : public EffectV2 { - public: - RotatingCubeEffectV2(const GpuContext& ctx, - const std::vector& inputs, - const std::vector& outputs); - ~RotatingCubeEffectV2() override; - - void declare_nodes(NodeRegistry& registry) override; - void render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) override; - - private: - struct Uniforms { - mat4 view_proj; - mat4 inv_view_proj; - vec4 camera_pos_time; - vec4 params; - vec2 resolution; - float aspect_ratio; - float _pad; - }; - static_assert(sizeof(Uniforms) == 176, "Uniforms size mismatch"); - - struct ObjectData { - mat4 model; - mat4 inv_model; - vec4 color; - vec4 params; - }; - static_assert(sizeof(ObjectData) == 160, "ObjectData size mismatch"); - - WGPURenderPipeline pipeline_ = nullptr; - WGPUBindGroup bind_group_ = nullptr; - GpuBuffer uniform_buffer_; - GpuBuffer object_buffer_; - float rotation_ = 0.0f; - std::string depth_node_; -}; diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index beccd46..b837ffe 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -9,21 +9,21 @@ #include "3d/scene.h" // Base effect classes (v2) -#include "gpu/effect_v2.h" +#include "gpu/effect.h" #include "gpu/post_process_helper.h" -#include "gpu/sequence_v2.h" +#include "gpu/sequence.h" #include "gpu/shaders.h" #include "gpu/texture_manager.h" #include "gpu/uniform_helper.h" // Individual Effect Headers (v2) -#include "effects/gaussian_blur_effect_v2.h" -#include "effects/heptagon_effect_v2.h" -#include "effects/hybrid3_d_effect_v2.h" -#include "effects/particles_effect_v2.h" -#include "effects/passthrough_effect_v2.h" -#include "effects/placeholder_effect_v2.h" -#include "effects/rotating_cube_effect_v2.h" +#include "effects/gaussian_blur_effect.h" +#include "effects/heptagon_effect.h" +#include "effects/hybrid3_d_effect.h" +#include "effects/particles_effect.h" +#include "effects/passthrough_effect.h" +#include "effects/placeholder_effect.h" +#include "effects/rotating_cube_effect.h" // TODO: Port CNN effects to v2 // #include "../../cnn_v1/src/cnn_v1_effect.h" // #include "../../cnn_v2/src/cnn_v2_effect.h" @@ -31,7 +31,7 @@ #include // Auto-generated functions from sequence compiler v2 -// See generated/timeline_v2.h for: +// See generated/timeline.h for: // - InitializeV2Sequences() // - GetActiveV2Sequence() // - RenderV2Timeline() diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc new file mode 100644 index 0000000..e4d3a90 --- /dev/null +++ b/src/gpu/effect.cc @@ -0,0 +1,11 @@ +// Effect implementation + +#include "gpu/effect.h" +#include "util/fatal_error.h" + +Effect::Effect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs) + : ctx_(ctx), input_nodes_(inputs), output_nodes_(outputs) { + FATAL_CHECK(!inputs.empty(), "Effect must have at least one input\n"); + FATAL_CHECK(!outputs.empty(), "Effect must have at least one output\n"); +} diff --git a/src/gpu/effect.h b/src/gpu/effect.h new file mode 100644 index 0000000..d40e750 --- /dev/null +++ b/src/gpu/effect.h @@ -0,0 +1,50 @@ +// Effect: Base class for effects with multi-input/multi-output support + +#ifndef EFFECT_H +#define EFFECT_H +#pragma once + +#include "gpu/gpu.h" +#include "gpu/sequence.h" +#include +#include + +class NodeRegistry; + +class Effect { + public: + Effect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs); + virtual ~Effect() = default; + + // Optional: Declare temporary nodes (e.g., multi-pass intermediate buffers) + virtual void declare_nodes(NodeRegistry& registry) { + (void)registry; + } + + // Render effect (multi-input/multi-output) + virtual void render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) = 0; + + // Resize notification + virtual void resize(int width, int height) { + width_ = width; + height_ = height; + } + + const std::vector& input_nodes() const { + return input_nodes_; + } + const std::vector& output_nodes() const { + return output_nodes_; + } + + protected: + const GpuContext& ctx_; + std::vector input_nodes_; + std::vector output_nodes_; + int width_ = 1280; + int height_ = 720; +}; +#endif // EFFECT_H diff --git a/src/gpu/effect_v2.cc b/src/gpu/effect_v2.cc deleted file mode 100644 index 7ecdfbd..0000000 --- a/src/gpu/effect_v2.cc +++ /dev/null @@ -1,11 +0,0 @@ -// EffectV2 implementation - -#include "gpu/effect_v2.h" -#include "util/fatal_error.h" - -EffectV2::EffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs) - : ctx_(ctx), input_nodes_(inputs), output_nodes_(outputs) { - FATAL_CHECK(!inputs.empty(), "Effect must have at least one input\n"); - FATAL_CHECK(!outputs.empty(), "Effect must have at least one output\n"); -} diff --git a/src/gpu/effect_v2.h b/src/gpu/effect_v2.h deleted file mode 100644 index 0d4e18d..0000000 --- a/src/gpu/effect_v2.h +++ /dev/null @@ -1,47 +0,0 @@ -// EffectV2: Base class for v2 effects with multi-input/multi-output support - -#pragma once - -#include "gpu/gpu.h" -#include "gpu/sequence_v2.h" -#include -#include - -class NodeRegistry; - -class EffectV2 { - public: - EffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs); - virtual ~EffectV2() = default; - - // Optional: Declare temporary nodes (e.g., multi-pass intermediate buffers) - virtual void declare_nodes(NodeRegistry& registry) { - (void)registry; - } - - // Render effect (multi-input/multi-output) - virtual void render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) = 0; - - // Resize notification - virtual void resize(int width, int height) { - width_ = width; - height_ = height; - } - - const std::vector& input_nodes() const { - return input_nodes_; - } - const std::vector& output_nodes() const { - return output_nodes_; - } - - protected: - const GpuContext& ctx_; - std::vector input_nodes_; - std::vector output_nodes_; - int width_ = 1280; - int height_ = 720; -}; diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 647833c..805e555 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -3,7 +3,7 @@ // Driven by audio peaks for synchronized visual effects. #include "gpu.h" -#include "generated/timeline_v2.h" +#include "generated/timeline.h" #include "gpu/shader_composer.h" #include "gpu/shaders.h" #include "platform/platform.h" diff --git a/src/gpu/sequence.cc b/src/gpu/sequence.cc new file mode 100644 index 0000000..d0a925f --- /dev/null +++ b/src/gpu/sequence.cc @@ -0,0 +1,235 @@ +// Sequence implementation + +#include "gpu/sequence.h" +#include "gpu/effect.h" +#include "util/fatal_error.h" +#include + +// NodeRegistry implementation + +NodeRegistry::NodeRegistry(WGPUDevice device, int default_width, + int default_height) + : device_(device), default_width_(default_width), + default_height_(default_height) { + // Create source/sink nodes with actual textures + Node source_node = {}; + source_node.type = NodeType::U8X4_NORM; + source_node.width = default_width; + source_node.height = default_height; + create_texture(source_node); + nodes_["source"] = source_node; + + Node sink_node = {}; + sink_node.type = NodeType::U8X4_NORM; + sink_node.width = default_width; + sink_node.height = default_height; + create_texture(sink_node); + nodes_["sink"] = sink_node; +} + +NodeRegistry::~NodeRegistry() { + for (auto& [name, node] : nodes_) { + if (node.view) { + wgpuTextureViewRelease(node.view); + } + for (auto& mip_view : node.mip_views) { + wgpuTextureViewRelease(mip_view); + } + if (node.texture) { + wgpuTextureRelease(node.texture); + } + } +} + +void NodeRegistry::declare_node(const std::string& name, NodeType type, + int width, int height) { + FATAL_CHECK(nodes_.find(name) == nodes_.end(), + "Node already declared: %s\n", name.c_str()); + + if (width <= 0) + width = default_width_; + if (height <= 0) + height = default_height_; + + Node node; + node.type = type; + node.width = width; + node.height = height; + create_texture(node); + + nodes_[name] = node; +} + +void NodeRegistry::declare_aliased_node(const std::string& name, + const std::string& alias_of) { + FATAL_CHECK(nodes_.find(alias_of) != nodes_.end(), + "Alias target does not exist: %s\n", alias_of.c_str()); + FATAL_CHECK(aliases_.find(name) == aliases_.end(), "Alias already exists: %s\n", + name.c_str()); + + aliases_[name] = alias_of; +} + +WGPUTextureView NodeRegistry::get_view(const std::string& name) { + // Check aliases first + auto alias_it = aliases_.find(name); + if (alias_it != aliases_.end()) { + return get_view(alias_it->second); + } + + auto it = nodes_.find(name); + FATAL_CHECK(it != nodes_.end(), "Node not found: %s\n", name.c_str()); + return it->second.view; +} + +std::vector +NodeRegistry::get_output_views(const std::vector& names) { + std::vector views; + views.reserve(names.size()); + for (const auto& name : names) { + views.push_back(get_view(name)); + } + return views; +} + +void NodeRegistry::resize(int width, int height) { + default_width_ = width; + default_height_ = height; + + for (auto& [name, node] : nodes_) { + // Release old texture + if (node.view) { + wgpuTextureViewRelease(node.view); + } + for (auto& mip_view : node.mip_views) { + wgpuTextureViewRelease(mip_view); + } + if (node.texture) { + wgpuTextureRelease(node.texture); + } + + // Recreate with new dimensions + node.width = width; + node.height = height; + create_texture(node); + } +} + +bool NodeRegistry::has_node(const std::string& name) const { + return nodes_.find(name) != nodes_.end() || + aliases_.find(name) != aliases_.end(); +} + +void NodeRegistry::set_external_view(const std::string& name, + WGPUTextureView view) { + // Register external view (texture not owned by registry) + Node node = {}; + node.view = view; + node.texture = nullptr; // Not owned + nodes_[name] = node; +} + +void NodeRegistry::create_texture(Node& node) { + WGPUTextureFormat format; + WGPUTextureUsage usage; + + switch (node.type) { + case NodeType::U8X4_NORM: + format = WGPUTextureFormat_RGBA8Unorm; + usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; + break; + case NodeType::F32X4: + format = WGPUTextureFormat_RGBA32Float; + usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; + break; + case NodeType::F16X8: + format = WGPUTextureFormat_RGBA16Float; // WebGPU doesn't have 8-channel, use RGBA16 + usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; + break; + case NodeType::DEPTH24: + format = WGPUTextureFormat_Depth24Plus; + usage = WGPUTextureUsage_RenderAttachment; + break; + case NodeType::COMPUTE_F32: + format = WGPUTextureFormat_RGBA32Float; + usage = WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding; + break; + } + + WGPUTextureDescriptor desc = {}; + desc.usage = usage; + desc.dimension = WGPUTextureDimension_2D; + desc.size = {static_cast(node.width), + static_cast(node.height), 1}; + desc.format = format; + desc.mipLevelCount = 1; + desc.sampleCount = 1; + + node.texture = wgpuDeviceCreateTexture(device_, &desc); + FATAL_CHECK(node.texture != nullptr, "Failed to create texture\n"); + + WGPUTextureViewDescriptor view_desc = {}; + view_desc.format = format; + view_desc.dimension = WGPUTextureViewDimension_2D; + view_desc.baseMipLevel = 0; + view_desc.mipLevelCount = 1; + view_desc.baseArrayLayer = 0; + view_desc.arrayLayerCount = 1; + view_desc.aspect = (node.type == NodeType::DEPTH24) + ? WGPUTextureAspect_DepthOnly + : WGPUTextureAspect_All; + + node.view = wgpuTextureCreateView(node.texture, &view_desc); + FATAL_CHECK(node.view != nullptr, "Failed to create texture view\n"); +} + +// Sequence implementation + +Sequence::Sequence(const GpuContext& ctx, int width, int height) + : ctx_(ctx), width_(width), height_(height), + nodes_(ctx.device, width, height) { + uniforms_buffer_.init(ctx.device); +} + +void Sequence::preprocess(float seq_time, float beat_time, float beat_phase, + float audio_intensity) { + params_.resolution = {static_cast(width_), static_cast(height_)}; + params_.aspect_ratio = + static_cast(width_) / static_cast(height_); + params_.time = seq_time; + params_.beat_time = beat_time; + params_.beat_phase = beat_phase; + params_.audio_intensity = audio_intensity; + params_._pad = 0.0f; + + uniforms_buffer_.update(ctx_.queue, params_); +} + +void Sequence::postprocess(WGPUCommandEncoder encoder) { + (void)encoder; + // Default: No-op (last effect writes to sink directly) +} + +void Sequence::render_effects(WGPUCommandEncoder encoder) { + // Execute DAG in topological order (pre-sorted by compiler) + for (const auto& dag_node : effect_dag_) { + dag_node.effect->render(encoder, params_, nodes_); + } +} + +void Sequence::resize(int width, int height) { + width_ = width; + height_ = height; + nodes_.resize(width, height); + + // Notify effects + for (auto& dag_node : effect_dag_) { + dag_node.effect->resize(width, height); + } +} + +void Sequence::init_effect_nodes() { + for (auto& dag_node : effect_dag_) { + dag_node.effect->declare_nodes(nodes_); + } +} diff --git a/src/gpu/sequence.h b/src/gpu/sequence.h new file mode 100644 index 0000000..a33dedb --- /dev/null +++ b/src/gpu/sequence.h @@ -0,0 +1,131 @@ +// Sequence: Explicit node system with DAG effect routing +// DAG-based effect routing with ping-pong optimization + +#ifndef SEQUENCE_H +#define SEQUENCE_H +#pragma once + +#include "gpu/gpu.h" +#include "gpu/uniform_helper.h" +#include "util/mini_math.h" +#include +#include +#include +#include + +class Effect; + +enum class NodeType { + U8X4_NORM, // RGBAu8 normalized (0-1) - default Source/Sink + F32X4, // RGBA float32 + F16X8, // 8-channel float16 + DEPTH24, // Depth buffer + COMPUTE_F32, // Compute buffer +}; + +struct Node { + NodeType type; + int width; + int height; + WGPUTexture texture; + WGPUTextureView view; + std::vector mip_views; // For multi-target render +}; + +struct UniformsSequenceParams { + vec2 resolution; + float aspect_ratio; + float time; // Per-sequence relative time + float beat_time; // Musical beats + float beat_phase; // Fractional beat 0.0-1.0 + float audio_intensity; + float _pad; +}; +static_assert(sizeof(UniformsSequenceParams) == 32, + "UniformsSequenceParams must be 32 bytes for WGSL alignment"); + +class NodeRegistry { + public: + NodeRegistry(WGPUDevice device, int default_width, int default_height); + ~NodeRegistry(); + + // Declare new node with explicit type/dimensions + void declare_node(const std::string& name, NodeType type, int width, + int height); + + // Declare aliased node (ping-pong optimization) + void declare_aliased_node(const std::string& name, + const std::string& alias_of); + + // Retrieve views + WGPUTextureView get_view(const std::string& name); + std::vector + get_output_views(const std::vector& names); + + // Resize all nodes + void resize(int width, int height); + + // Check if node exists + bool has_node(const std::string& name) const; + + // Register external view (for source/sink managed externally) + void set_external_view(const std::string& name, WGPUTextureView view); + + private: + WGPUDevice device_; + int default_width_; + int default_height_; + std::map nodes_; + std::map aliases_; // name -> backing node name + + void create_texture(Node& node); +}; + +struct EffectDAGNode { + std::shared_ptr effect; + std::vector input_nodes; + std::vector output_nodes; + int execution_order; // Topologically sorted +}; + +class Sequence { + public: + Sequence(const GpuContext& ctx, int width, int height); + virtual ~Sequence() = default; + + // Virtual methods (most sequences use defaults) + virtual void preprocess(float seq_time, float beat_time, float beat_phase, + float audio_intensity); + virtual void postprocess(WGPUCommandEncoder encoder); + virtual void render_effects(WGPUCommandEncoder encoder); + + void resize(int width, int height); + + // Initialize effect nodes (call at end of subclass constructor) + void init_effect_nodes(); + + // Set surface texture view for rendering (sink node) + void set_sink_view(WGPUTextureView view) { + nodes_.set_external_view("sink", view); + } + + // Set source texture view (input framebuffer) + void set_source_view(WGPUTextureView view) { + nodes_.set_external_view("source", view); + } + + // Test accessor + const std::vector& get_effect_dag() const { + return effect_dag_; + } + + protected: + const GpuContext& ctx_; + int width_; + int height_; + NodeRegistry nodes_; + std::vector effect_dag_; + UniformsSequenceParams params_; + UniformBuffer uniforms_buffer_; +}; +#endif // SEQUENCE_H diff --git a/src/gpu/sequence_v2.cc b/src/gpu/sequence_v2.cc deleted file mode 100644 index 3912849..0000000 --- a/src/gpu/sequence_v2.cc +++ /dev/null @@ -1,235 +0,0 @@ -// Sequence v2 implementation - -#include "gpu/sequence_v2.h" -#include "gpu/effect_v2.h" -#include "util/fatal_error.h" -#include - -// NodeRegistry implementation - -NodeRegistry::NodeRegistry(WGPUDevice device, int default_width, - int default_height) - : device_(device), default_width_(default_width), - default_height_(default_height) { - // Create source/sink nodes with actual textures - Node source_node = {}; - source_node.type = NodeType::U8X4_NORM; - source_node.width = default_width; - source_node.height = default_height; - create_texture(source_node); - nodes_["source"] = source_node; - - Node sink_node = {}; - sink_node.type = NodeType::U8X4_NORM; - sink_node.width = default_width; - sink_node.height = default_height; - create_texture(sink_node); - nodes_["sink"] = sink_node; -} - -NodeRegistry::~NodeRegistry() { - for (auto& [name, node] : nodes_) { - if (node.view) { - wgpuTextureViewRelease(node.view); - } - for (auto& mip_view : node.mip_views) { - wgpuTextureViewRelease(mip_view); - } - if (node.texture) { - wgpuTextureRelease(node.texture); - } - } -} - -void NodeRegistry::declare_node(const std::string& name, NodeType type, - int width, int height) { - FATAL_CHECK(nodes_.find(name) == nodes_.end(), - "Node already declared: %s\n", name.c_str()); - - if (width <= 0) - width = default_width_; - if (height <= 0) - height = default_height_; - - Node node; - node.type = type; - node.width = width; - node.height = height; - create_texture(node); - - nodes_[name] = node; -} - -void NodeRegistry::declare_aliased_node(const std::string& name, - const std::string& alias_of) { - FATAL_CHECK(nodes_.find(alias_of) != nodes_.end(), - "Alias target does not exist: %s\n", alias_of.c_str()); - FATAL_CHECK(aliases_.find(name) == aliases_.end(), "Alias already exists: %s\n", - name.c_str()); - - aliases_[name] = alias_of; -} - -WGPUTextureView NodeRegistry::get_view(const std::string& name) { - // Check aliases first - auto alias_it = aliases_.find(name); - if (alias_it != aliases_.end()) { - return get_view(alias_it->second); - } - - auto it = nodes_.find(name); - FATAL_CHECK(it != nodes_.end(), "Node not found: %s\n", name.c_str()); - return it->second.view; -} - -std::vector -NodeRegistry::get_output_views(const std::vector& names) { - std::vector views; - views.reserve(names.size()); - for (const auto& name : names) { - views.push_back(get_view(name)); - } - return views; -} - -void NodeRegistry::resize(int width, int height) { - default_width_ = width; - default_height_ = height; - - for (auto& [name, node] : nodes_) { - // Release old texture - if (node.view) { - wgpuTextureViewRelease(node.view); - } - for (auto& mip_view : node.mip_views) { - wgpuTextureViewRelease(mip_view); - } - if (node.texture) { - wgpuTextureRelease(node.texture); - } - - // Recreate with new dimensions - node.width = width; - node.height = height; - create_texture(node); - } -} - -bool NodeRegistry::has_node(const std::string& name) const { - return nodes_.find(name) != nodes_.end() || - aliases_.find(name) != aliases_.end(); -} - -void NodeRegistry::set_external_view(const std::string& name, - WGPUTextureView view) { - // Register external view (texture not owned by registry) - Node node = {}; - node.view = view; - node.texture = nullptr; // Not owned - nodes_[name] = node; -} - -void NodeRegistry::create_texture(Node& node) { - WGPUTextureFormat format; - WGPUTextureUsage usage; - - switch (node.type) { - case NodeType::U8X4_NORM: - format = WGPUTextureFormat_RGBA8Unorm; - usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; - break; - case NodeType::F32X4: - format = WGPUTextureFormat_RGBA32Float; - usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; - break; - case NodeType::F16X8: - format = WGPUTextureFormat_RGBA16Float; // WebGPU doesn't have 8-channel, use RGBA16 - usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding; - break; - case NodeType::DEPTH24: - format = WGPUTextureFormat_Depth24Plus; - usage = WGPUTextureUsage_RenderAttachment; - break; - case NodeType::COMPUTE_F32: - format = WGPUTextureFormat_RGBA32Float; - usage = WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding; - break; - } - - WGPUTextureDescriptor desc = {}; - desc.usage = usage; - desc.dimension = WGPUTextureDimension_2D; - desc.size = {static_cast(node.width), - static_cast(node.height), 1}; - desc.format = format; - desc.mipLevelCount = 1; - desc.sampleCount = 1; - - node.texture = wgpuDeviceCreateTexture(device_, &desc); - FATAL_CHECK(node.texture != nullptr, "Failed to create texture\n"); - - WGPUTextureViewDescriptor view_desc = {}; - view_desc.format = format; - view_desc.dimension = WGPUTextureViewDimension_2D; - view_desc.baseMipLevel = 0; - view_desc.mipLevelCount = 1; - view_desc.baseArrayLayer = 0; - view_desc.arrayLayerCount = 1; - view_desc.aspect = (node.type == NodeType::DEPTH24) - ? WGPUTextureAspect_DepthOnly - : WGPUTextureAspect_All; - - node.view = wgpuTextureCreateView(node.texture, &view_desc); - FATAL_CHECK(node.view != nullptr, "Failed to create texture view\n"); -} - -// SequenceV2 implementation - -SequenceV2::SequenceV2(const GpuContext& ctx, int width, int height) - : ctx_(ctx), width_(width), height_(height), - nodes_(ctx.device, width, height) { - uniforms_buffer_.init(ctx.device); -} - -void SequenceV2::preprocess(float seq_time, float beat_time, float beat_phase, - float audio_intensity) { - params_.resolution = {static_cast(width_), static_cast(height_)}; - params_.aspect_ratio = - static_cast(width_) / static_cast(height_); - params_.time = seq_time; - params_.beat_time = beat_time; - params_.beat_phase = beat_phase; - params_.audio_intensity = audio_intensity; - params_._pad = 0.0f; - - uniforms_buffer_.update(ctx_.queue, params_); -} - -void SequenceV2::postprocess(WGPUCommandEncoder encoder) { - (void)encoder; - // Default: No-op (last effect writes to sink directly) -} - -void SequenceV2::render_effects(WGPUCommandEncoder encoder) { - // Execute DAG in topological order (pre-sorted by compiler) - for (const auto& dag_node : effect_dag_) { - dag_node.effect->render(encoder, params_, nodes_); - } -} - -void SequenceV2::resize(int width, int height) { - width_ = width; - height_ = height; - nodes_.resize(width, height); - - // Notify effects - for (auto& dag_node : effect_dag_) { - dag_node.effect->resize(width, height); - } -} - -void SequenceV2::init_effect_nodes() { - for (auto& dag_node : effect_dag_) { - dag_node.effect->declare_nodes(nodes_); - } -} diff --git a/src/gpu/sequence_v2.h b/src/gpu/sequence_v2.h deleted file mode 100644 index 2197a82..0000000 --- a/src/gpu/sequence_v2.h +++ /dev/null @@ -1,128 +0,0 @@ -// Sequence v2: Explicit node system with DAG effect routing -// Replaces implicit framebuffer ping-pong with compile-time optimized nodes - -#pragma once - -#include "gpu/gpu.h" -#include "gpu/uniform_helper.h" -#include "util/mini_math.h" -#include -#include -#include -#include - -class EffectV2; - -enum class NodeType { - U8X4_NORM, // RGBAu8 normalized (0-1) - default Source/Sink - F32X4, // RGBA float32 - F16X8, // 8-channel float16 - DEPTH24, // Depth buffer - COMPUTE_F32, // Compute buffer -}; - -struct Node { - NodeType type; - int width; - int height; - WGPUTexture texture; - WGPUTextureView view; - std::vector mip_views; // For multi-target render -}; - -struct UniformsSequenceParams { - vec2 resolution; - float aspect_ratio; - float time; // Per-sequence relative time - float beat_time; // Musical beats - float beat_phase; // Fractional beat 0.0-1.0 - float audio_intensity; - float _pad; -}; -static_assert(sizeof(UniformsSequenceParams) == 32, - "UniformsSequenceParams must be 32 bytes for WGSL alignment"); - -class NodeRegistry { - public: - NodeRegistry(WGPUDevice device, int default_width, int default_height); - ~NodeRegistry(); - - // Declare new node with explicit type/dimensions - void declare_node(const std::string& name, NodeType type, int width, - int height); - - // Declare aliased node (ping-pong optimization) - void declare_aliased_node(const std::string& name, - const std::string& alias_of); - - // Retrieve views - WGPUTextureView get_view(const std::string& name); - std::vector - get_output_views(const std::vector& names); - - // Resize all nodes - void resize(int width, int height); - - // Check if node exists - bool has_node(const std::string& name) const; - - // Register external view (for source/sink managed externally) - void set_external_view(const std::string& name, WGPUTextureView view); - - private: - WGPUDevice device_; - int default_width_; - int default_height_; - std::map nodes_; - std::map aliases_; // name -> backing node name - - void create_texture(Node& node); -}; - -struct EffectDAGNode { - std::shared_ptr effect; - std::vector input_nodes; - std::vector output_nodes; - int execution_order; // Topologically sorted -}; - -class SequenceV2 { - public: - SequenceV2(const GpuContext& ctx, int width, int height); - virtual ~SequenceV2() = default; - - // Virtual methods (most sequences use defaults) - virtual void preprocess(float seq_time, float beat_time, float beat_phase, - float audio_intensity); - virtual void postprocess(WGPUCommandEncoder encoder); - virtual void render_effects(WGPUCommandEncoder encoder); - - void resize(int width, int height); - - // Initialize effect nodes (call at end of subclass constructor) - void init_effect_nodes(); - - // Set surface texture view for rendering (sink node) - void set_sink_view(WGPUTextureView view) { - nodes_.set_external_view("sink", view); - } - - // Set source texture view (input framebuffer) - void set_source_view(WGPUTextureView view) { - nodes_.set_external_view("source", view); - } - - // Test accessor - const std::vector& get_effect_dag() const { - return effect_dag_; - } - - protected: - const GpuContext& ctx_; - int width_; - int height_; - NodeRegistry nodes_; - std::vector effect_dag_; - UniformsSequenceParams params_; - UniformBuffer uniforms_buffer_; -}; diff --git a/src/tests/gpu/test_demo_effects.cc b/src/tests/gpu/test_demo_effects.cc index 02aee78..7bba831 100644 --- a/src/tests/gpu/test_demo_effects.cc +++ b/src/tests/gpu/test_demo_effects.cc @@ -14,7 +14,7 @@ #include // Helper: Test v2 effect construction -static int test_effect_v2(const char* name, std::shared_ptr effect) { +static int test_effect(const char* name, std::shared_ptr effect) { fprintf(stdout, " Testing %s...\n", name); if (!effect) { @@ -27,7 +27,7 @@ static int test_effect_v2(const char* name, std::shared_ptr effect) { } // Test all available v2 effects -static void test_v2_effects() { +static void test_effects() { fprintf(stdout, "Testing V2 effects...\n"); WebGPUTestFixture fixture; @@ -36,40 +36,40 @@ static void test_v2_effects() { return; } - std::vector>> effects = { - {"PassthroughEffectV2", - std::make_shared( + std::vector>> effects = { + {"PassthroughEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"GaussianBlurEffectV2", - std::make_shared( + {"GaussianBlurEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"PlaceholderEffectV2", - std::make_shared( + {"PlaceholderEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"HeptagonEffectV2", - std::make_shared( + {"HeptagonEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"ParticlesEffectV2", - std::make_shared( + {"ParticlesEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"RotatingCubeEffectV2", - std::make_shared( + {"RotatingCubeEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, - {"Hybrid3DEffectV2", - std::make_shared( + {"Hybrid3DEffect", + std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"})}, }; int passed = 0; for (const auto& [name, effect] : effects) { - passed += test_effect_v2(name, effect); + passed += test_effect(name, effect); } fprintf(stdout, " ✓ %d/%zu V2 effects tested\n", passed, effects.size()); @@ -81,7 +81,7 @@ int main() { extern void InitShaderComposer(); InitShaderComposer(); - test_v2_effects(); + test_effects(); fprintf(stdout, "=== All Tests Passed ===\n"); return 0; diff --git a/src/tests/gpu/test_effect_base.cc b/src/tests/gpu/test_effect_base.cc index ddccad4..8b0e6b2 100644 --- a/src/tests/gpu/test_effect_base.cc +++ b/src/tests/gpu/test_effect_base.cc @@ -1,13 +1,13 @@ // This file is part of the 64k demo project. -// It tests the EffectV2/SequenceV2 lifecycle using headless rendering. +// It tests the Effect/Sequence lifecycle using headless rendering. // Verifies effect initialization and basic rendering. #include "../common/effect_test_helpers.h" #include "../common/offscreen_render_target.h" #include "../common/webgpu_test_fixture.h" -#include "effects/passthrough_effect_v2.h" -#include "gpu/effect_v2.h" -#include "gpu/sequence_v2.h" +#include "effects/passthrough_effect.h" +#include "gpu/effect.h" +#include "gpu/sequence.h" #include #include #include @@ -80,19 +80,19 @@ static void test_effect_construction() { return; } - // Create PassthroughEffectV2 (simple effect) - auto effect = std::make_shared( + // Create PassthroughEffect (simple effect) + auto effect = std::make_shared( fixture.ctx(), std::vector{"source"}, std::vector{"sink"}); assert(effect != nullptr && "Effect should be constructed"); - fprintf(stdout, " ✓ PassthroughEffectV2 constructed\n"); + fprintf(stdout, " ✓ PassthroughEffect constructed\n"); } // Test 4: Effect added to sequence DAG static void test_effect_in_sequence() { - fprintf(stdout, "Testing effect in SequenceV2 DAG...\n"); + fprintf(stdout, "Testing effect in Sequence DAG...\n"); WebGPUTestFixture fixture; if (!fixture.init()) { @@ -101,10 +101,10 @@ static void test_effect_in_sequence() { } // Create minimal sequence with one effect - class TestSequence : public SequenceV2 { + class TestSequence : public Sequence { public: - TestSequence(const GpuContext& ctx, int w, int h) : SequenceV2(ctx, w, h) { - auto effect = std::make_shared( + TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) { + auto effect = std::make_shared( ctx, std::vector{"source"}, std::vector{"sink"}); @@ -133,10 +133,10 @@ static void test_sequence_render() { OffscreenRenderTarget target(fixture.instance(), fixture.device(), 256, 256); - class TestSequence : public SequenceV2 { + class TestSequence : public Sequence { public: - TestSequence(const GpuContext& ctx, int w, int h) : SequenceV2(ctx, w, h) { - auto effect = std::make_shared( + TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) { + auto effect = std::make_shared( ctx, std::vector{"source"}, std::vector{"sink"}); @@ -176,15 +176,15 @@ static void test_sequence_time_params() { return; } - class TestSequence : public SequenceV2 { + class TestSequence : public Sequence { public: - TestSequence(const GpuContext& ctx, int w, int h) : SequenceV2(ctx, w, h) { + TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) { init_effect_nodes(); } void preprocess(float seq_time, float beat_time, float beat_phase, float audio_intensity) override { - SequenceV2::preprocess(seq_time, beat_time, beat_phase, audio_intensity); + Sequence::preprocess(seq_time, beat_time, beat_phase, audio_intensity); last_time = seq_time; } diff --git a/src/tests/gpu/test_sequence.cc b/src/tests/gpu/test_sequence.cc new file mode 100644 index 0000000..337381a --- /dev/null +++ b/src/tests/gpu/test_sequence.cc @@ -0,0 +1,184 @@ +// Test file for Sequence v2 system +// Phase 1: Foundation tests (NodeRegistry, Sequence base class) + +#include "gpu/sequence.h" +#include "gpu/effect.h" +#include "tests/common/webgpu_test_fixture.h" +#include +#include + +// Simple test effect for DAG execution +class TestEffect : public Effect { + public: + TestEffect(const GpuContext& ctx, const std::vector& inputs, + const std::vector& outputs) + : Effect(ctx, inputs, outputs), render_called_(false) { + } + + void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, + NodeRegistry& nodes) override { + (void)encoder; + (void)params; + (void)nodes; + render_called_ = true; + } + + bool was_render_called() const { + return render_called_; + } + + private: + bool render_called_; +}; + +// Test: NodeRegistry basic allocation +void test_node_registry_basic() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test_node_registry_basic (no GPU)\n"); + return; + } + + NodeRegistry registry(fixture.ctx().device, 1280, 720); + + // Declare node + registry.declare_node("test_node", NodeType::U8X4_NORM, 1280, 720); + + // Verify node exists + assert(registry.has_node("test_node")); + + // Get view (should not crash) + WGPUTextureView view = registry.get_view("test_node"); + assert(view != nullptr); + + printf("PASS: NodeRegistry basic allocation\n"); +} + +// Test: NodeRegistry aliased nodes (ping-pong optimization) +void test_node_registry_aliased() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test_node_registry_aliased (no GPU)\n"); + return; + } + + NodeRegistry registry(fixture.ctx().device, 1280, 720); + + // Declare backing node + registry.declare_node("frame_a", NodeType::U8X4_NORM, 1280, 720); + + // Declare aliased node + registry.declare_aliased_node("frame_b", "frame_a"); + + // Both should resolve to same view + WGPUTextureView view_a = registry.get_view("frame_a"); + WGPUTextureView view_b = registry.get_view("frame_b"); + assert(view_a == view_b); + + printf("PASS: NodeRegistry aliased nodes\n"); +} + +// Test: NodeRegistry multi-output views +void test_node_registry_multi_output() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test_node_registry_multi_output (no GPU)\n"); + return; + } + + NodeRegistry registry(fixture.ctx().device, 1280, 720); + + // Declare multiple nodes + registry.declare_node("output1", NodeType::U8X4_NORM, 1280, 720); + registry.declare_node("output2", NodeType::F32X4, 1280, 720); + + // Get multiple views + std::vector names = {"output1", "output2"}; + std::vector views = registry.get_output_views(names); + + assert(views.size() == 2); + assert(views[0] != nullptr); + assert(views[1] != nullptr); + + printf("PASS: NodeRegistry multi-output views\n"); +} + +// Test: Sequence default preprocess +void test_sequence_v2_preprocess() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test_sequence_v2_preprocess (no GPU)\n"); + return; + } + + Sequence seq(fixture.ctx(), 1280, 720); + + // Call preprocess with test values + seq.preprocess(1.0f, 4.0f, 0.5f, 0.8f); + + // No crash = success (params updated internally) + printf("PASS: Sequence preprocess\n"); +} + +// Test: Sequence DAG execution +void test_sequence_v2_dag_execution() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test_sequence_v2_dag_execution (no GPU)\n"); + return; + } + + // Create sequence + class TestSequence : public Sequence { + public: + TestSequence(const GpuContext& ctx) + : Sequence(ctx, 1280, 720), + effect1_(std::make_shared(ctx, std::vector{"source"}, + std::vector{"temp"})), + effect2_(std::make_shared(ctx, std::vector{"temp"}, + std::vector{"sink"})) { + // Build DAG (2 effects in sequence) + effect_dag_.push_back( + {effect1_, {"source"}, {"temp"}, 0}); + effect_dag_.push_back( + {effect2_, {"temp"}, {"sink"}, 1}); + } + + std::shared_ptr effect1_; + std::shared_ptr effect2_; + }; + + TestSequence seq(fixture.ctx()); + + // Create command encoder + WGPUCommandEncoderDescriptor enc_desc = {}; + WGPUCommandEncoder encoder = + wgpuDeviceCreateCommandEncoder(fixture.ctx().device, &enc_desc); + + // Execute DAG + seq.render_effects(encoder); + + // Verify both effects called + assert(seq.effect1_->was_render_called()); + assert(seq.effect2_->was_render_called()); + + // Cleanup + WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(encoder, nullptr); + wgpuCommandBufferRelease(cmd); + wgpuCommandEncoderRelease(encoder); + + printf("PASS: Sequence DAG execution\n"); +} + +int main() { + printf("Running Sequence v2 tests...\n"); + + test_node_registry_basic(); + test_node_registry_aliased(); + test_node_registry_multi_output(); + test_sequence_v2_preprocess(); + test_sequence_v2_dag_execution(); + + printf("All Sequence v2 tests passed!\n"); + return 0; +} diff --git a/src/tests/gpu/test_sequence_e2e.cc b/src/tests/gpu/test_sequence_e2e.cc new file mode 100644 index 0000000..91a8da2 --- /dev/null +++ b/src/tests/gpu/test_sequence_e2e.cc @@ -0,0 +1,112 @@ +// End-to-end test for Sequence v2 system +// Tests compiler output instantiation and execution + +#include "gpu/sequence.h" +#include "gpu/effect.h" +#include "effects/gaussian_blur_effect.h" +#include "effects/heptagon_effect.h" +#include "effects/passthrough_effect.h" +#include "gpu/shaders.h" +#include "tests/common/webgpu_test_fixture.h" +#include +#include + +// Manually transcribed generated sequence (simulates compiler output) +// Simple 2-effect chain to validate DAG execution +class SimpleTestSequence : public Sequence { + public: + SimpleTestSequence(const GpuContext& ctx, int width, int height) + : Sequence(ctx, width, height) { + // Node declarations (source/sink already created by NodeRegistry) + nodes_.declare_node("temp", NodeType::U8X4_NORM, width_, height_); + + // Effect DAG construction (2 effects: source->temp->sink) + effect_dag_.push_back({ + .effect = std::make_shared(ctx, + std::vector{"source"}, + std::vector{"temp"}), + .input_nodes = {"source"}, + .output_nodes = {"temp"}, + .execution_order = 0 + }); + effect_dag_.push_back({ + .effect = std::make_shared(ctx, + std::vector{"temp"}, + std::vector{"sink"}), + .input_nodes = {"temp"}, + .output_nodes = {"sink"}, + .execution_order = 1 + }); + } +}; + +// Test: Instantiate and run v2 sequence +void test_sequence_v2_instantiation() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test (no GPU)\n"); + return; + } + + // Initialize shader composer with snippets + InitShaderComposer(); + + // Create sequence + SimpleTestSequence seq(fixture.ctx(), 1280, 720); + + // Preprocess + seq.preprocess(0.0f, 0.0f, 0.0f, 0.0f); + + // Create command encoder + WGPUCommandEncoderDescriptor enc_desc = {}; + WGPUCommandEncoder encoder = + wgpuDeviceCreateCommandEncoder(fixture.ctx().device, &enc_desc); + + // Execute DAG (should not crash) + seq.render_effects(encoder); + + // Cleanup + WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(encoder, nullptr); + wgpuCommandBufferRelease(cmd); + wgpuCommandEncoderRelease(encoder); + + printf("PASS: Sequence v2 instantiation and execution\n"); +} + +// Test: Verify DAG execution order +void test_dag_execution_order() { + WebGPUTestFixture fixture; + if (!fixture.init()) { + fprintf(stderr, "Skipping test (no GPU)\n"); + return; + } + + // Initialize shader composer with snippets + InitShaderComposer(); + + SimpleTestSequence seq(fixture.ctx(), 1280, 720); + + // Verify effects are in correct order + const auto& dag = seq.get_effect_dag(); + assert(dag.size() == 2); + assert(dag[0].execution_order == 0); + assert(dag[1].execution_order == 1); + + // Verify node routing + assert(dag[0].input_nodes[0] == "source"); + assert(dag[0].output_nodes[0] == "temp"); + assert(dag[1].input_nodes[0] == "temp"); + assert(dag[1].output_nodes[0] == "sink"); + + printf("PASS: DAG execution order validated\n"); +} + +int main() { + printf("Running Sequence v2 end-to-end tests...\n"); + + test_sequence_v2_instantiation(); + test_dag_execution_order(); + + printf("All Sequence v2 e2e tests passed!\n"); + return 0; +} diff --git a/src/tests/gpu/test_sequence_v2.cc b/src/tests/gpu/test_sequence_v2.cc deleted file mode 100644 index 54b544e..0000000 --- a/src/tests/gpu/test_sequence_v2.cc +++ /dev/null @@ -1,184 +0,0 @@ -// Test file for Sequence v2 system -// Phase 1: Foundation tests (NodeRegistry, SequenceV2 base class) - -#include "gpu/sequence_v2.h" -#include "gpu/effect_v2.h" -#include "tests/common/webgpu_test_fixture.h" -#include -#include - -// Simple test effect for DAG execution -class TestEffectV2 : public EffectV2 { - public: - TestEffectV2(const GpuContext& ctx, const std::vector& inputs, - const std::vector& outputs) - : EffectV2(ctx, inputs, outputs), render_called_(false) { - } - - void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params, - NodeRegistry& nodes) override { - (void)encoder; - (void)params; - (void)nodes; - render_called_ = true; - } - - bool was_render_called() const { - return render_called_; - } - - private: - bool render_called_; -}; - -// Test: NodeRegistry basic allocation -void test_node_registry_basic() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test_node_registry_basic (no GPU)\n"); - return; - } - - NodeRegistry registry(fixture.ctx().device, 1280, 720); - - // Declare node - registry.declare_node("test_node", NodeType::U8X4_NORM, 1280, 720); - - // Verify node exists - assert(registry.has_node("test_node")); - - // Get view (should not crash) - WGPUTextureView view = registry.get_view("test_node"); - assert(view != nullptr); - - printf("PASS: NodeRegistry basic allocation\n"); -} - -// Test: NodeRegistry aliased nodes (ping-pong optimization) -void test_node_registry_aliased() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test_node_registry_aliased (no GPU)\n"); - return; - } - - NodeRegistry registry(fixture.ctx().device, 1280, 720); - - // Declare backing node - registry.declare_node("frame_a", NodeType::U8X4_NORM, 1280, 720); - - // Declare aliased node - registry.declare_aliased_node("frame_b", "frame_a"); - - // Both should resolve to same view - WGPUTextureView view_a = registry.get_view("frame_a"); - WGPUTextureView view_b = registry.get_view("frame_b"); - assert(view_a == view_b); - - printf("PASS: NodeRegistry aliased nodes\n"); -} - -// Test: NodeRegistry multi-output views -void test_node_registry_multi_output() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test_node_registry_multi_output (no GPU)\n"); - return; - } - - NodeRegistry registry(fixture.ctx().device, 1280, 720); - - // Declare multiple nodes - registry.declare_node("output1", NodeType::U8X4_NORM, 1280, 720); - registry.declare_node("output2", NodeType::F32X4, 1280, 720); - - // Get multiple views - std::vector names = {"output1", "output2"}; - std::vector views = registry.get_output_views(names); - - assert(views.size() == 2); - assert(views[0] != nullptr); - assert(views[1] != nullptr); - - printf("PASS: NodeRegistry multi-output views\n"); -} - -// Test: SequenceV2 default preprocess -void test_sequence_v2_preprocess() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test_sequence_v2_preprocess (no GPU)\n"); - return; - } - - SequenceV2 seq(fixture.ctx(), 1280, 720); - - // Call preprocess with test values - seq.preprocess(1.0f, 4.0f, 0.5f, 0.8f); - - // No crash = success (params updated internally) - printf("PASS: SequenceV2 preprocess\n"); -} - -// Test: SequenceV2 DAG execution -void test_sequence_v2_dag_execution() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test_sequence_v2_dag_execution (no GPU)\n"); - return; - } - - // Create sequence - class TestSequence : public SequenceV2 { - public: - TestSequence(const GpuContext& ctx) - : SequenceV2(ctx, 1280, 720), - effect1_(std::make_shared(ctx, std::vector{"source"}, - std::vector{"temp"})), - effect2_(std::make_shared(ctx, std::vector{"temp"}, - std::vector{"sink"})) { - // Build DAG (2 effects in sequence) - effect_dag_.push_back( - {effect1_, {"source"}, {"temp"}, 0}); - effect_dag_.push_back( - {effect2_, {"temp"}, {"sink"}, 1}); - } - - std::shared_ptr effect1_; - std::shared_ptr effect2_; - }; - - TestSequence seq(fixture.ctx()); - - // Create command encoder - WGPUCommandEncoderDescriptor enc_desc = {}; - WGPUCommandEncoder encoder = - wgpuDeviceCreateCommandEncoder(fixture.ctx().device, &enc_desc); - - // Execute DAG - seq.render_effects(encoder); - - // Verify both effects called - assert(seq.effect1_->was_render_called()); - assert(seq.effect2_->was_render_called()); - - // Cleanup - WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(encoder, nullptr); - wgpuCommandBufferRelease(cmd); - wgpuCommandEncoderRelease(encoder); - - printf("PASS: SequenceV2 DAG execution\n"); -} - -int main() { - printf("Running Sequence v2 tests...\n"); - - test_node_registry_basic(); - test_node_registry_aliased(); - test_node_registry_multi_output(); - test_sequence_v2_preprocess(); - test_sequence_v2_dag_execution(); - - printf("All Sequence v2 tests passed!\n"); - return 0; -} diff --git a/src/tests/gpu/test_sequence_v2_e2e.cc b/src/tests/gpu/test_sequence_v2_e2e.cc deleted file mode 100644 index c015e0b..0000000 --- a/src/tests/gpu/test_sequence_v2_e2e.cc +++ /dev/null @@ -1,112 +0,0 @@ -// End-to-end test for Sequence v2 system -// Tests compiler output instantiation and execution - -#include "gpu/sequence_v2.h" -#include "gpu/effect_v2.h" -#include "effects/gaussian_blur_effect_v2.h" -#include "effects/heptagon_effect_v2.h" -#include "effects/passthrough_effect_v2.h" -#include "gpu/shaders.h" -#include "tests/common/webgpu_test_fixture.h" -#include -#include - -// Manually transcribed generated sequence (simulates compiler output) -// Simple 2-effect chain to validate DAG execution -class SimpleTestSequence : public SequenceV2 { - public: - SimpleTestSequence(const GpuContext& ctx, int width, int height) - : SequenceV2(ctx, width, height) { - // Node declarations (source/sink already created by NodeRegistry) - nodes_.declare_node("temp", NodeType::U8X4_NORM, width_, height_); - - // Effect DAG construction (2 effects: source->temp->sink) - effect_dag_.push_back({ - .effect = std::make_shared(ctx, - std::vector{"source"}, - std::vector{"temp"}), - .input_nodes = {"source"}, - .output_nodes = {"temp"}, - .execution_order = 0 - }); - effect_dag_.push_back({ - .effect = std::make_shared(ctx, - std::vector{"temp"}, - std::vector{"sink"}), - .input_nodes = {"temp"}, - .output_nodes = {"sink"}, - .execution_order = 1 - }); - } -}; - -// Test: Instantiate and run v2 sequence -void test_sequence_v2_instantiation() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test (no GPU)\n"); - return; - } - - // Initialize shader composer with snippets - InitShaderComposer(); - - // Create sequence - SimpleTestSequence seq(fixture.ctx(), 1280, 720); - - // Preprocess - seq.preprocess(0.0f, 0.0f, 0.0f, 0.0f); - - // Create command encoder - WGPUCommandEncoderDescriptor enc_desc = {}; - WGPUCommandEncoder encoder = - wgpuDeviceCreateCommandEncoder(fixture.ctx().device, &enc_desc); - - // Execute DAG (should not crash) - seq.render_effects(encoder); - - // Cleanup - WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(encoder, nullptr); - wgpuCommandBufferRelease(cmd); - wgpuCommandEncoderRelease(encoder); - - printf("PASS: Sequence v2 instantiation and execution\n"); -} - -// Test: Verify DAG execution order -void test_dag_execution_order() { - WebGPUTestFixture fixture; - if (!fixture.init()) { - fprintf(stderr, "Skipping test (no GPU)\n"); - return; - } - - // Initialize shader composer with snippets - InitShaderComposer(); - - SimpleTestSequence seq(fixture.ctx(), 1280, 720); - - // Verify effects are in correct order - const auto& dag = seq.get_effect_dag(); - assert(dag.size() == 2); - assert(dag[0].execution_order == 0); - assert(dag[1].execution_order == 1); - - // Verify node routing - assert(dag[0].input_nodes[0] == "source"); - assert(dag[0].output_nodes[0] == "temp"); - assert(dag[1].input_nodes[0] == "temp"); - assert(dag[1].output_nodes[0] == "sink"); - - printf("PASS: DAG execution order validated\n"); -} - -int main() { - printf("Running Sequence v2 end-to-end tests...\n"); - - test_sequence_v2_instantiation(); - test_dag_execution_order(); - - printf("All Sequence v2 e2e tests passed!\n"); - return 0; -} -- cgit v1.2.3