summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-10 21:11:05 +0100
committerskal <pascal.massimino@gmail.com>2026-02-10 21:11:05 +0100
commit7a05f4d33b611ba1e9b6c68e0d0bd67d6ea011ee (patch)
treea88109bee56197ffca8d7aacd07a878fae502d11 /workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
parent2fbfc406abe5a42f45face9b07a91ec64c0d4f78 (diff)
refactor: Optimize CNN grayscale computation
Compute gray once per fragment using dot() instead of per-layer. Pass gray as f32 parameter to conv functions instead of vec4 original. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'workspaces/main/shaders/cnn/cnn_conv3x3.wgsl')
-rw-r--r--workspaces/main/shaders/cnn/cnn_conv3x3.wgsl16
1 files changed, 5 insertions, 11 deletions
diff --git a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
index 96ddf5b..79b0350 100644
--- a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
+++ b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl
@@ -15,7 +15,7 @@ fn cnn_conv3x3_7to4_src(
// Compute grayscale from original (converted in [-1,1])
let original = (textureSample(tex, samp, uv) - 0.5) * 2.0;
- let gray = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
+ let gray = dot(original.rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
// Normalize UV to [-1,1]
let uv_norm = (uv - 0.5) * 2.0;
@@ -52,7 +52,7 @@ fn cnn_conv3x3_7to4_src(
}
// Inner layers: 7→4 channels (RGBD output)
-// Assumes 'tex' and 'original' are already normalized to [-1,1]
+// Assumes 'tex' is already normalized to [-1,1]
// UV coordinates remain in [0,1] and are normalized internally
// weights: array<array<f32, 8>, 36> (9 positions × 4 channels, each with 7 weights + bias)
fn cnn_conv3x3_7to4(
@@ -60,14 +60,11 @@ fn cnn_conv3x3_7to4(
samp: sampler,
uv: vec2<f32>,
resolution: vec2<f32>,
- original: vec4<f32>,
+ gray: f32,
weights: array<array<f32, 8>, 36>
) -> vec4<f32> {
let step = 1.0 / resolution;
- // Compute grayscale from original (already in [-1,1])
- let gray = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
-
// Normalize UV to [-1,1]
let uv_norm = (uv - 0.5) * 2.0;
@@ -103,7 +100,7 @@ fn cnn_conv3x3_7to4(
}
// Final layer: 7→1 channel (scalar output)
-// Assumes 'tex' and 'original' are already normalized to [-1,1]
+// Assumes 'tex' is already normalized to [-1,1]
// UV coordinates remain in [0,1] and are normalized internally
// weights: array<array<f32, 8>, 9> (9 positions, each with 7 weights + bias)
fn cnn_conv3x3_7to1(
@@ -111,14 +108,11 @@ fn cnn_conv3x3_7to1(
samp: sampler,
uv: vec2<f32>,
resolution: vec2<f32>,
- original: vec4<f32>,
+ gray: f32,
weights: array<array<f32, 8>, 9>
) -> f32 {
let step = 1.0 / resolution;
- // Compute grayscale from original (already in [-1,1])
- let gray = 0.2126*original.r + 0.7152*original.g + 0.0722*original.b;
-
// Normalize UV to [-1,1]
let uv_norm = (uv - 0.5) * 2.0;