From 3153b55135788c8a7691929f913a2c9b96a44154 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 10 Feb 2026 19:24:17 +0100 Subject: udpate CNN shader code. --- workspaces/main/shaders/cnn/cnn_conv3x3.wgsl | 67 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) (limited to 'workspaces/main/shaders/cnn/cnn_conv3x3.wgsl') diff --git a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl index b895504..ebb87b5 100644 --- a/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl +++ b/workspaces/main/shaders/cnn/cnn_conv3x3.wgsl @@ -1,55 +1,54 @@ // 3x3 convolution with weight indexing -// Samples 9 pixels, applies mat4 weights per sample -fn cnn_conv3x3( +// 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, 36> (9 positions × 4 channels, each with 7 weights + bias) +fn cnn_conv3x3_7to4_src( tex: texture_2d, samp: sampler, uv: vec2, resolution: vec2, - weights: array, 9>, - bias: vec4 + weights: array, 36> ) -> vec4 { 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(dx), f32(dy)) * step; - let sample = textureSample(tex, samp, uv + offset); - sum += weights[idx] * sample; - idx++; - } - } - return sum; -} + // 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; -fn cnn_conv3x3_with_coord( - tex: texture_2d, - samp: sampler, - uv: vec2, - resolution: vec2, - rgba_weights: array, 9>, - coord_weights: mat2x4, - bias: vec4 -) -> vec4 { - let step = 1.0 / resolution; - var sum = bias; + // Normalize UV to [-1,1] + let uv_norm = (uv - 0.5) * 2.0; - sum += coord_weights * uv; + var sum = vec4(0.0); - var idx = 0; + var pos = 0; for (var dy = -1; dy <= 1; dy++) { for (var dx = -1; dx <= 1; dx++) { let offset = vec2(f32(dx), f32(dy)) * step; - let rgba = textureSample(tex, samp, uv + offset); - sum += rgba_weights[idx] * rgba; - idx++; + let rgbd = (textureSample(tex, samp, uv + offset) - .5) * 2.0; // convert to [-1,1] + + // 7-channel input: [R,G,B,D, uv.x, uv.y, gray] all in [-1,1] + let inputs = array( + rgbd.r, rgbd.g, rgbd.b, rgbd.a, + uv_norm.x, uv_norm.y, gray + ); + + // Accumulate for each output channel (RGBD) + for (var out_c = 0; out_c < 4; out_c++) { + let idx = pos * 4 + out_c; + var channel_sum = weights[idx][7]; // Bias (8th element) + 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; + return sum; // Output in [-1,1] range } // Inner layers: 7→4 channels (RGBD output) -- cgit v1.2.3