diff options
Diffstat (limited to 'workspaces/main/shaders/cnn/cnn_conv3x3.wgsl')
| -rw-r--r-- | workspaces/main/shaders/cnn/cnn_conv3x3.wgsl | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl index 48bb392..f7d11b1 100644 --- a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl +++ b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl @@ -1,33 +1,26 @@ // 3x3 convolution (vec4-optimized) -// Source layers: 7→4 channels (RGBD output) -// Assumes 'tex' (the input) is *not* normalized to [-1,1], but is [0,1] -// UV coordinates remain in [0,1] and are normalized internally -// weights: array<vec4<f32>, 72> (9 pos × 4 ch × 2 vec4) -fn cnn_conv3x3_7to4_src( +// Inner layers: 7→4 channels (vec4-optimized) +// Assumes 'tex' is already normalized to [-1,1] +fn cnn_conv3x3_7to4( tex: texture_2d<f32>, samp: sampler, uv: vec2<f32>, resolution: vec2<f32>, + gray: f32, weights: array<vec4<f32>, 72> ) -> vec4<f32> { let step = 1.0 / resolution; - - // Compute grayscale from original (converted in [-1,1]) - let original = (textureSample(tex, samp, uv) - 0.5) * 2.0; - 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; - let in1 = vec4<f32>(uv_norm, gray, 1.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 = (textureSample(tex, samp, uv + offset) - .5) * 2.0; + let rgbd = textureSample(tex, samp, uv + offset); + let in1 = vec4<f32>(uv_norm, gray, 1.0); sum.r += dot(weights[pos+0], rgbd) + dot(weights[pos+1], in1); sum.g += dot(weights[pos+2], rgbd) + dot(weights[pos+3], in1); @@ -40,31 +33,29 @@ fn cnn_conv3x3_7to4_src( return sum; } -// Inner layers: 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<vec4<f32>, 72> (9 pos × 4 ch × 2 vec4) -fn cnn_conv3x3_7to4( +// Source layer: 7→4 channels (vec4-optimized) +// Normalizes [0,1] input to [-1,1] internally +fn cnn_conv3x3_7to4_src( tex: texture_2d<f32>, samp: sampler, uv: vec2<f32>, resolution: vec2<f32>, - gray: f32, weights: array<vec4<f32>, 72> ) -> vec4<f32> { let step = 1.0 / resolution; - // Normalize UV to [-1,1] + let original = (textureSample(tex, samp, uv) - 0.5) * 2.0; + let gray = dot(original.rgb, vec3<f32>(0.2126, 0.7152, 0.0722)); let uv_norm = (uv - 0.5) * 2.0; + let in1 = vec4<f32>(uv_norm, gray, 1.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 = textureSample(tex, samp, uv + offset); - let in1 = vec4<f32>(uv_norm, gray, 1.0); + let rgbd = (textureSample(tex, samp, uv + offset) - 0.5) * 2.0; sum.r += dot(weights[pos+0], rgbd) + dot(weights[pos+1], in1); sum.g += dot(weights[pos+2], rgbd) + dot(weights[pos+3], in1); @@ -79,8 +70,7 @@ fn cnn_conv3x3_7to4( // Final layer: 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<vec4<f32>, 18> (9 pos × 2 vec4) +// Returns raw sum (activation applied at call site) fn cnn_conv3x3_7to1( tex: texture_2d<f32>, samp: sampler, @@ -90,14 +80,12 @@ fn cnn_conv3x3_7to1( weights: array<vec4<f32>, 18> ) -> f32 { let step = 1.0 / resolution; - - // Normalize UV to [-1,1] let uv_norm = (uv - 0.5) * 2.0; let in1 = vec4<f32>(uv_norm, gray, 1.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; |
