summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
diff options
context:
space:
mode:
Diffstat (limited to 'workspaces/main/shaders/cnn/cnn_conv5x5.wgsl')
-rw-r--r--workspaces/main/shaders/cnn/cnn_conv5x5.wgsl75
1 files changed, 26 insertions, 49 deletions
diff --git a/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl b/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
index 4f0a5f3..119930f 100644
--- a/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
+++ b/workspaces/main/shaders/cnn/cnn_conv5x5.wgsl
@@ -1,14 +1,14 @@
-// 5×5 variant for 7→4 channels (RGBD output)
+// 5×5 variant for 7→4 channels (vec4-optimized)
// Assumes 'tex' is already normalized to [-1,1]
// UV coordinates remain in [0,1] and are normalized internally
-// weights: array<array<f32, 8>, 100> (25 positions × 4 channels, each with 7 weights + bias)
+// weights: array<vec4<f32>, 200> (25 pos × 4 ch × 2 vec4)
fn cnn_conv5x5_7to4(
tex: texture_2d<f32>,
samp: sampler,
uv: vec2<f32>,
resolution: vec2<f32>,
gray: f32,
- weights: array<array<f32, 8>, 100>
+ weights: array<vec4<f32>, 200>
) -> vec4<f32> {
let step = 1.0 / resolution;
let uv_norm = (uv - 0.5) * 2.0;
@@ -19,39 +19,31 @@ fn cnn_conv5x5_7to4(
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 = textureSample(tex, samp, uv + offset); // Already in [-1,1]
+ let rgbd = textureSample(tex, samp, uv + offset);
+ let in1 = vec4<f32>(uv_norm, gray, 1.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++;
+ sum.r += dot(weights[pos+0], rgbd) + dot(weights[pos+1], in1);
+ sum.g += dot(weights[pos+2], rgbd) + dot(weights[pos+3], in1);
+ sum.b += dot(weights[pos+4], rgbd) + dot(weights[pos+5], in1);
+ sum.a += dot(weights[pos+6], rgbd) + dot(weights[pos+7], in1);
+ pos += 8;
}
}
return sum;
}
-// 5×5 variant for 7→1 channel (scalar output)
+// 5×5 variant for 7→1 channel (vec4-optimized)
// Assumes 'tex' is already normalized to [-1,1]
// UV coordinates remain in [0,1] and are normalized internally
-// weights: array<array<f32, 8>, 25> (25 positions, each with 7 weights + bias)
+// weights: array<vec4<f32>, 50> (25 pos × 2 vec4)
fn cnn_conv5x5_7to1(
tex: texture_2d<f32>,
samp: sampler,
uv: vec2<f32>,
resolution: vec2<f32>,
gray: f32,
- weights: array<array<f32, 8>, 25>
+ weights: array<vec4<f32>, 50>
) -> f32 {
let step = 1.0 / resolution;
let uv_norm = (uv - 0.5) * 2.0;
@@ -62,32 +54,25 @@ fn cnn_conv5x5_7to1(
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 = textureSample(tex, samp, uv + offset); // Already 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
+ let rgbd = textureSample(tex, samp, uv + offset);
+ let in1 = vec4<f32>(uv_norm, gray, 1.0);
- pos++;
+ sum += dot(weights[pos], rgbd) + dot(weights[pos+1], in1);
+ pos += 2;
}
}
- return clamp(sum, 0.0, 1.0); // Match PyTorch clamp
+ return clamp(sum, 0.0, 1.0);
}
-// Source layer: 7→4 channels (RGBD output)
+// Source layer: 7→4 channels (vec4-optimized)
// Normalizes [0,1] input to [-1,1] internally
fn cnn_conv5x5_7to4_src(
tex: texture_2d<f32>,
samp: sampler,
uv: vec2<f32>,
resolution: vec2<f32>,
- weights: array<array<f32, 8>, 100>
+ weights: array<vec4<f32>, 200>
) -> vec4<f32> {
let step = 1.0 / resolution;
@@ -102,21 +87,13 @@ fn cnn_conv5x5_7to4_src(
for (var dx = -2; dx <= 2; dx++) {
let offset = vec2<f32>(f32(dx), f32(dy)) * step;
let rgbd = (textureSample(tex, samp, uv + offset) - 0.5) * 2.0;
+ let in1 = vec4<f32>(uv_norm, gray, 1.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++;
+ sum.r += dot(weights[pos+0], rgbd) + dot(weights[pos+1], in1);
+ sum.g += dot(weights[pos+2], rgbd) + dot(weights[pos+3], in1);
+ sum.b += dot(weights[pos+4], rgbd) + dot(weights[pos+5], in1);
+ sum.a += dot(weights[pos+6], rgbd) + dot(weights[pos+7], in1);
+ pos += 8;
}
}