From 47397444b30b0f461b1633297a68300179586fda Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 10 Feb 2026 08:01:25 +0100 Subject: feat: Add CNN post-processing effect with modular WGSL architecture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements multi-layer convolutional neural network shader for stylized post-processing of 3D rendered scenes: **Core Components:** - CNNEffect: C++ effect class with single-layer rendering (expandable to multi-pass) - Modular WGSL snippets: cnn_activation, cnn_conv3x3/5x5/7x7, cnn_weights_generated - Placeholder identity-like weights for initial testing (to be replaced by trained weights) **Architecture:** - Flexible kernel sizes (3×3, 5×5, 7×7) via separate snippet files - ShaderComposer integration (#include resolution) - Residual connections (input + processed output) - Supports parallel convolutions (design ready, single conv implemented) **Size Impact:** - ~3-4 KB shader code (snippets + main shader) - ~2-4 KB weights (depends on network architecture when trained) - Total: ~5-8 KB (acceptable for 64k demo) **Testing:** - CNNEffect added to test_demo_effects.cc - 36/36 tests passing (100%) **Next Steps:** - Training script (scripts/train_cnn.py) to generate real weights - Multi-layer rendering with ping-pong textures - Weight quantization for size optimization handoff(Claude): CNN effect foundation complete, ready for training integration --- src/gpu/demo_effects.h | 2 ++ src/gpu/effects/cnn_effect.cc | 36 ++++++++++++++++++++++++++++++++++++ src/gpu/effects/cnn_effect.h | 29 +++++++++++++++++++++++++++++ src/gpu/effects/shaders.cc | 11 +++++++++++ src/gpu/effects/shaders.h | 1 + src/tests/gpu/test_demo_effects.cc | 2 ++ 6 files changed, 81 insertions(+) create mode 100644 src/gpu/effects/cnn_effect.cc create mode 100644 src/gpu/effects/cnn_effect.h (limited to 'src') diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index ff7e017..6e6cb73 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -184,6 +184,8 @@ class DistortEffect : public PostProcessEffect { // (included above) FlashEffect now defined in gpu/effects/flash_effect.h // (included above) +class CNNEffect; + // Auto-generated functions void LoadTimeline(MainSequence& main_seq, const GpuContext& ctx); float GetDemoDuration(); // Returns demo end time in seconds, or -1 if not diff --git a/src/gpu/effects/cnn_effect.cc b/src/gpu/effects/cnn_effect.cc new file mode 100644 index 0000000..25db0c2 --- /dev/null +++ b/src/gpu/effects/cnn_effect.cc @@ -0,0 +1,36 @@ +// CNN post-processing effect implementation +// Neural network-based stylization with modular WGSL + +#include "gpu/effects/cnn_effect.h" +#include "gpu/effects/post_process_helper.h" +#include "gpu/effects/shaders.h" + +CNNEffect::CNNEffect(const GpuContext& ctx, int num_layers) + : PostProcessEffect(ctx), num_layers_(num_layers), input_view_(nullptr), + bind_group_(nullptr) { + pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, + cnn_layer_shader_wgsl); +} + +void CNNEffect::init(MainSequence* demo) { + PostProcessEffect::init(demo); + params_buffer_.init(ctx_.device); + + CNNLayerParams params = {0, 1, {0.0f, 0.0f}}; + params_buffer_.update(ctx_.queue, params); +} + +void CNNEffect::render(WGPURenderPassEncoder pass, float time, float beat, + float intensity, float aspect_ratio) { + if (!bind_group_) return; + + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); +} + +void CNNEffect::update_bind_group(WGPUTextureView input_view) { + input_view_ = input_view; + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, + input_view_, uniforms_.get(), params_buffer_.get()); +} diff --git a/src/gpu/effects/cnn_effect.h b/src/gpu/effects/cnn_effect.h new file mode 100644 index 0000000..9cc4935 --- /dev/null +++ b/src/gpu/effects/cnn_effect.h @@ -0,0 +1,29 @@ +// CNN post-processing effect header +// Multi-layer neural network stylization + +#pragma once +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +struct CNNLayerParams { + int layer_index; + int use_residual; + float _pad[2]; +}; +static_assert(sizeof(CNNLayerParams) == 16); + +class CNNEffect : public PostProcessEffect { + public: + explicit CNNEffect(const GpuContext& ctx, int num_layers = 1); + + void init(MainSequence* demo) override; + void render(WGPURenderPassEncoder pass, float time, float beat, + float intensity, float aspect_ratio) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: + int num_layers_; + WGPUTextureView input_view_; + UniformBuffer params_buffer_; + WGPUBindGroup bind_group_; +}; diff --git a/src/gpu/effects/shaders.cc b/src/gpu/effects/shaders.cc index 625c5b6..6559bf5 100644 --- a/src/gpu/effects/shaders.cc +++ b/src/gpu/effects/shaders.cc @@ -51,6 +51,13 @@ void InitShaderComposer() { register_if_exists("ray_box", AssetId::ASSET_SHADER_RAY_BOX); register_if_exists("ray_triangle", AssetId::ASSET_SHADER_RAY_TRIANGLE); + + register_if_exists("cnn_activation", AssetId::ASSET_SHADER_CNN_ACTIVATION); + register_if_exists("cnn_conv3x3", AssetId::ASSET_SHADER_CNN_CONV3X3); + register_if_exists("cnn_conv5x5", AssetId::ASSET_SHADER_CNN_CONV5X5); + register_if_exists("cnn_conv7x7", AssetId::ASSET_SHADER_CNN_CONV7X7); + register_if_exists("cnn_weights_generated", + AssetId::ASSET_SHADER_CNN_WEIGHTS); } // Helper to get asset string or empty string @@ -99,6 +106,10 @@ const char* chroma_aberration_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_CHROMA_ABERRATION); +const char* cnn_layer_shader_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_CNN_LAYER); + const char* gen_noise_compute_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_NOISE); diff --git a/src/gpu/effects/shaders.h b/src/gpu/effects/shaders.h index 68b8834..7acc2a6 100644 --- a/src/gpu/effects/shaders.h +++ b/src/gpu/effects/shaders.h @@ -18,6 +18,7 @@ extern const char* solarize_shader_wgsl; extern const char* distort_shader_wgsl; extern const char* chroma_aberration_shader_wgsl; extern const char* vignette_shader_wgsl; +extern const char* cnn_layer_shader_wgsl; extern const char* gen_noise_compute_wgsl; extern const char* gen_perlin_compute_wgsl; extern const char* gen_grid_compute_wgsl; diff --git a/src/tests/gpu/test_demo_effects.cc b/src/tests/gpu/test_demo_effects.cc index 8a7d8af..9281413 100644 --- a/src/tests/gpu/test_demo_effects.cc +++ b/src/tests/gpu/test_demo_effects.cc @@ -13,6 +13,7 @@ #include "../common/effect_test_helpers.h" #include "gpu/demo_effects.h" #include "gpu/effect.h" +#include "gpu/effects/cnn_effect.h" #include "../common/webgpu_test_fixture.h" #include #include @@ -88,6 +89,7 @@ static void test_post_process_effects() { {"ThemeModulationEffect", std::make_shared(fixture.ctx())}, {"VignetteEffect", std::make_shared(fixture.ctx())}, + {"CNNEffect", std::make_shared(fixture.ctx(), 1)}, }; int passed = 0; -- cgit v1.2.3