summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
blob: df58b4d2a25197a6dc53adca72bf0c1cd9285762 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 3x3 convolution with weight indexing
// Samples 9 pixels, applies mat4 weights per sample

fn cnn_conv3x3(
  tex: texture_2d<f32>,
  samp: sampler,
  uv: vec2<f32>,
  resolution: vec2<f32>,
  weights: array<mat4x4<f32>, 9>,
  bias: vec4<f32>
) -> vec4<f32> {
  let step = 1.0 / resolution;
  var sum = bias;
  var idx = 0;

  for (var dy = -1; dy <= 1; dy++) {
    for (var dx = -1; dx <= 1; dx++) {
      let offset = vec2<f32>(f32(dx), f32(dy)) * step;
      let sample = textureSample(tex, samp, uv + offset);
      sum += weights[idx] * sample;
      idx++;
    }
  }

  return sum;
}

fn cnn_conv3x3_with_coord(
  tex: texture_2d<f32>,
  samp: sampler,
  uv: vec2<f32>,
  resolution: vec2<f32>,
  rgba_weights: array<mat4x4<f32>, 9>,
  coord_weights: mat2x4<f32>,
  bias: vec4<f32>
) -> vec4<f32> {
  let step = 1.0 / resolution;
  var sum = bias;

  sum += coord_weights * uv;

  var idx = 0;
  for (var dy = -1; dy <= 1; dy++) {
    for (var dx = -1; dx <= 1; dx++) {
      let offset = vec2<f32>(f32(dx), f32(dy)) * step;
      let rgba = textureSample(tex, samp, uv + offset);
      sum += rgba_weights[idx] * rgba;
      idx++;
    }
  }

  return sum;
}

// Inner layers: 7→4 channels (RGBD output)
// weights: array<array<f32, 8>, 36> (9 positions × 4 channels, each with 7 weights + bias)
fn cnn_conv3x3_7to4(
  tex: texture_2d<f32>,
  samp: sampler,
  uv: vec2<f32>,
  resolution: vec2<f32>,
  original: vec4<f32>,
  weights: array<array<f32, 8>, 36>
) -> vec4<f32> {
  let step = 1.0 / resolution;

  // Compute grayscale from original and normalize to [-1,1]
  let gray_01 = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
  let gray = (gray_01 - 0.5) * 2.0;

  // Normalize UV to [-1,1]
  let uv_norm = (uv - 0.5) * 2.0;

  var sum = vec4<f32>(0.0);

  var pos = 0;
  for (var dy = -1; dy <= 1; dy++) {
    for (var dx = -1; dx <= 1; dx++) {
      let offset = vec2<f32>(f32(dx), f32(dy)) * step;
      let rgbd_01 = textureSample(tex, samp, uv + offset);

      // Normalize RGBD to [-1,1]
      let rgbd = (rgbd_01 - 0.5) * 2.0;

      // 7-channel input: [R,G,B,D, uv.x, uv.y, gray] all in [-1,1]
      let inputs = array<f32, 7>(
        rgbd.r, rgbd.g, rgbd.b, rgbd.a,
        uv_norm.x, uv_norm.y, gray
      );

      // Accumulate for each output channel (RGBD)
      for (var out_c = 0; out_c < 4; out_c++) {
        let idx = pos * 4 + out_c;
        var channel_sum = weights[idx][7];  // Bias (8th element)
        for (var in_c = 0; in_c < 7; in_c++) {
          channel_sum += weights[idx][in_c] * inputs[in_c];
        }
        sum[out_c] += channel_sum;
      }

      pos++;
    }
  }

  return sum;  // Output in [-1,1] range
}

// Final layer: 7→1 channel (scalar output)
// weights: array<array<f32, 8>, 9> (9 positions, each with 7 weights + bias)
fn cnn_conv3x3_7to1(
  tex: texture_2d<f32>,
  samp: sampler,
  uv: vec2<f32>,
  resolution: vec2<f32>,
  original: vec4<f32>,
  weights: array<array<f32, 8>, 9>
) -> f32 {
  let step = 1.0 / resolution;

  // Normalize grayscale to [-1,1]
  let gray_01 = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
  let gray = (gray_01 - 0.5) * 2.0;

  // Normalize UV to [-1,1]
  let uv_norm = (uv - 0.5) * 2.0;

  var sum = 0.0;

  var pos = 0;
  for (var dy = -1; dy <= 1; dy++) {
    for (var dx = -1; dx <= 1; dx++) {
      let offset = vec2<f32>(f32(dx), f32(dy)) * step;
      let rgbd_01 = textureSample(tex, samp, uv + offset);

      // Normalize RGBD to [-1,1]
      let rgbd = (rgbd_01 - 0.5) * 2.0;

      // 7-channel input all in [-1,1]
      sum += weights[pos][0] * rgbd.r;
      sum += weights[pos][1] * rgbd.g;
      sum += weights[pos][2] * rgbd.b;
      sum += weights[pos][3] * rgbd.a;
      sum += weights[pos][4] * uv_norm.x;
      sum += weights[pos][5] * uv_norm.y;
      sum += weights[pos][6] * gray;
      sum += weights[pos][7];  // Bias

      pos++;
    }
  }

  return sum;  // Output in [-1,1], needs denormalization
}