From ced248b0a8973db6d11b79e8290e2f5bb17ffcaa Mon Sep 17 00:00:00 2001 From: skal Date: Fri, 13 Feb 2026 16:48:02 +0100 Subject: CNN v2: Add mip-level support to runtime effect Binary format v2 includes mip_level in header (20 bytes, was 16). Effect reads mip_level and passes to static features shader via uniform. Shader samples from correct mip texture based on mip_level. Changes: - export_cnn_v2_weights.py: Header v2 with mip_level field - cnn_v2_effect.h: Add StaticFeatureParams, mip_level member, params buffer - cnn_v2_effect.cc: Read mip_level from weights, create/bind params buffer, update per-frame - cnn_v2_static.wgsl: Accept params uniform, sample from selected mip level Binary format v2: - Header: 20 bytes (magic, version=2, num_layers, total_weights, mip_level) - Backward compatible: v1 weights load with mip_level=0 Co-Authored-By: Claude Sonnet 4.5 --- workspaces/main/shaders/cnn_v2/cnn_v2_static.wgsl | 25 ++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'workspaces/main') diff --git a/workspaces/main/shaders/cnn_v2/cnn_v2_static.wgsl b/workspaces/main/shaders/cnn_v2/cnn_v2_static.wgsl index 7a9e6de..f71fad2 100644 --- a/workspaces/main/shaders/cnn_v2/cnn_v2_static.wgsl +++ b/workspaces/main/shaders/cnn_v2/cnn_v2_static.wgsl @@ -1,13 +1,19 @@ // CNN v2 Static Features Compute Shader // Generates 8D parametric features: [p0, p1, p2, p3, uv.x, uv.y, sin10_x, bias] -// p0-p3: Parametric features (currently RGBD from mip0, could be mip1/2, gradients, etc.) +// p0-p3: Parametric features from specified mip level (0=mip0, 1=mip1, 2=mip2, 3=mip3) // Note: Input image RGBD (mip0) fed separately to Layer 0 +struct StaticFeatureParams { + mip_level: u32, + padding: vec3, +} + @group(0) @binding(0) var input_tex: texture_2d; @group(0) @binding(1) var input_tex_mip1: texture_2d; @group(0) @binding(2) var input_tex_mip2: texture_2d; @group(0) @binding(3) var depth_tex: texture_2d; @group(0) @binding(4) var output_tex: texture_storage_2d; +@group(0) @binding(5) var params: StaticFeatureParams; @compute @workgroup_size(8, 8) fn main(@builtin(global_invocation_id) id: vec3) { @@ -18,10 +24,19 @@ fn main(@builtin(global_invocation_id) id: vec3) { return; } - // Parametric features (p0-p3) - // TODO: Experiment with mip1 grayscale, Sobel gradients, etc. - // For now, use RGBD from mip 0 (same as input, but could differ) - let rgba = textureLoad(input_tex, coord, 0); + // Parametric features (p0-p3) - sample from specified mip level + var rgba: vec4; + if (params.mip_level == 0u) { + rgba = textureLoad(input_tex, coord, 0); + } else if (params.mip_level == 1u) { + rgba = textureLoad(input_tex_mip1, coord, 0); + } else if (params.mip_level == 2u) { + rgba = textureLoad(input_tex_mip2, coord, 0); + } else { + // Mip 3 or higher: use mip 2 as fallback + rgba = textureLoad(input_tex_mip2, coord, 0); + } + let p0 = rgba.r; let p1 = rgba.g; let p2 = rgba.b; -- cgit v1.2.3