summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-10 17:37:01 +0100
committerskal <pascal.massimino@gmail.com>2026-02-10 17:37:01 +0100
commitf3c7ef8cd612f5ac908f39310c4c11566879313f (patch)
tree1e66127a855f30282c852731c0dd88ae6c7039bc /workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
parent0aa35e895d70f4535b7fac0f5df318888a6847dc (diff)
fix: Support variable kernel sizes in CNN layer generation
Training script was hardcoded to generate cnn_conv3x3_* calls regardless of actual kernel size, causing shader validation errors when layer 1 used 5×5 kernel (100 weights) but called 3×3 function (expected 36). Changes: - train_cnn.py: Generate correct conv function based on kernel_sizes[i] - cnn_conv5x5.wgsl: Add cnn_conv5x5_7to4 and cnn_conv5x5_7to1 variants - Regenerate cnn_layer.wgsl with correct function calls for [3,5,3] - Document kernel size→function mapping in HOWTO.md Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'workspaces/main/shaders/cnn/cnn_conv5x5.wgsl')
-rw-r--r--workspaces/main/shaders/cnn/cnn_conv5x5.wgsl86
1 files changed, 86 insertions, 0 deletions
diff --git a/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl b/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
index bd9abfa..15eaf96 100644
--- a/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
+++ b/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
@@ -51,3 +51,89 @@ fn cnn_conv5x5_with_coord(
return sum;
}
+
+// 5×5 variant for 7→4 channels (RGBD output)
+// weights: array<array<f32, 8>, 100> (25 positions × 4 channels, each with 7 weights + bias)
+fn cnn_conv5x5_7to4(
+ tex: texture_2d<f32>,
+ samp: sampler,
+ uv: vec2<f32>,
+ resolution: vec2<f32>,
+ original: vec4<f32>,
+ weights: array<array<f32, 8>, 100>
+) -> vec4<f32> {
+ let step = 1.0 / resolution;
+
+ let gray_01 = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
+ let gray = (gray_01 - 0.5) * 2.0;
+ let uv_norm = (uv - 0.5) * 2.0;
+
+ var sum = vec4<f32>(0.0);
+ var pos = 0;
+
+ for (var dy = -2; dy <= 2; dy++) {
+ for (var dx = -2; dx <= 2; dx++) {
+ let offset = vec2<f32>(f32(dx), f32(dy)) * step;
+ let rgbd_01 = textureSample(tex, samp, uv + offset);
+ let rgbd = (rgbd_01 - 0.5) * 2.0;
+
+ let inputs = array<f32, 7>(
+ rgbd.r, rgbd.g, rgbd.b, rgbd.a,
+ uv_norm.x, uv_norm.y, gray
+ );
+
+ for (var out_c = 0; out_c < 4; out_c++) {
+ let idx = pos * 4 + out_c;
+ var channel_sum = weights[idx][7];
+ 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;
+}
+
+// 5×5 variant for 7→1 channel (scalar output)
+// weights: array<array<f32, 8>, 25> (25 positions, each with 7 weights + bias)
+fn cnn_conv5x5_7to1(
+ tex: texture_2d<f32>,
+ samp: sampler,
+ uv: vec2<f32>,
+ resolution: vec2<f32>,
+ original: vec4<f32>,
+ weights: array<array<f32, 8>, 25>
+) -> f32 {
+ let step = 1.0 / resolution;
+
+ let gray_01 = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
+ let gray = (gray_01 - 0.5) * 2.0;
+ let uv_norm = (uv - 0.5) * 2.0;
+
+ var sum = 0.0;
+ var pos = 0;
+
+ for (var dy = -2; dy <= 2; dy++) {
+ for (var dx = -2; dx <= 2; dx++) {
+ let offset = vec2<f32>(f32(dx), f32(dy)) * step;
+ let rgbd_01 = textureSample(tex, samp, uv + offset);
+ let rgbd = (rgbd_01 - 0.5) * 2.0;
+
+ 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;
+}