summaryrefslogtreecommitdiff
path: root/src/gpu/effects/cnn_v2_effect.h
blob: 4389e4f56d28eb9ce5fcb01cc1ac0335b27fed9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// CNN v2 Effect - Parametric Static Features
// Multi-pass post-processing with 7D feature input

#pragma once
#include "gpu/effect.h"
#include <vector>

struct CNNv2EffectParams {
  float blend_amount = 1.0f;
};

class CNNv2Effect : public PostProcessEffect {
public:
  explicit CNNv2Effect(const GpuContext& ctx);
  explicit CNNv2Effect(const GpuContext& ctx, const CNNv2EffectParams& params);
  ~CNNv2Effect();

  void init(MainSequence* demo) override;
  void resize(int width, int height) override;
  void compute(WGPUCommandEncoder encoder,
               const CommonPostProcessUniforms& uniforms) override;
  void render(WGPURenderPassEncoder pass,
              const CommonPostProcessUniforms& uniforms) override;
  void update_bind_group(WGPUTextureView input_view) override;

  void set_beat_modulation(bool enabled, float scale = 1.0f) {
    beat_modulated_ = enabled;
    beat_scale_ = scale;
  }

private:
  struct LayerInfo {
    uint32_t kernel_size;
    uint32_t in_channels;
    uint32_t out_channels;
    uint32_t weight_offset;
    uint32_t weight_count;
  };

  struct LayerParams {
    uint32_t kernel_size;
    uint32_t in_channels;
    uint32_t out_channels;
    uint32_t weight_offset;
    uint32_t is_output_layer;
    float blend_amount;
  };

  void create_textures();
  void create_pipelines();
  void load_weights();
  void cleanup();

  // Static features compute
  WGPUComputePipeline static_pipeline_;
  WGPUBindGroup static_bind_group_;
  WGPUTexture static_features_tex_;
  WGPUTextureView static_features_view_;

  // CNN layers (storage buffer architecture)
  WGPUComputePipeline layer_pipeline_;      // Single pipeline for all layers
  WGPUBuffer weights_buffer_;               // Storage buffer for weights
  std::vector<WGPUBuffer> layer_params_buffers_;  // Uniform buffers (one per layer)
  std::vector<LayerInfo> layer_info_;       // Layer metadata
  std::vector<WGPUBindGroup> layer_bind_groups_;  // Per-layer bind groups
  std::vector<WGPUTexture> layer_textures_;       // Ping-pong buffers
  std::vector<WGPUTextureView> layer_views_;

  // Input mips
  WGPUTexture input_mip_tex_;
  WGPUTextureView input_mip_view_[3];
  WGPUTextureView current_input_view_;

  float blend_amount_ = 1.0f;
  bool beat_modulated_ = false;
  float beat_scale_ = 1.0f;
  bool initialized_;
};