summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
blob: 06ca73a4283c5fee8ee699aa85539fd9326a6151 (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
// 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;
}