diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-06 02:14:30 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-06 02:14:30 +0100 |
| commit | 75bdd2f8c17169422d175ed2d9dfceb9acb0fe77 (patch) | |
| tree | 9a08a9cc1527c36d10cad71b34ab8ffa64c9cef0 /assets/final/shaders/render/scene_query_bvh.wgsl | |
| parent | ae4b03ef6f5ef07dcc80affd6877d17fceee7d29 (diff) | |
refactor(gpu): Implement compile-time BVH toggle via shader composition
Completed Task #18-B optimization and refactoring.
- Replaced runtime branching in shader with compile-time snippet substitution in ShaderComposer.
- Added 'scene_query_bvh.wgsl' and 'scene_query_linear.wgsl' as distinct snippets.
- Refactored Renderer3D to manage two separate pipelines (with and without BVH).
- Updated ShaderComposer to support snippet substitution during composition.
- Verified both paths with test_3d_render (default and --no-bvh).
- Removed temporary shader hacks and cleaned up renderer_3d.wgsl.
Diffstat (limited to 'assets/final/shaders/render/scene_query_bvh.wgsl')
| -rw-r--r-- | assets/final/shaders/render/scene_query_bvh.wgsl | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/assets/final/shaders/render/scene_query_bvh.wgsl b/assets/final/shaders/render/scene_query_bvh.wgsl new file mode 100644 index 0000000..c7dfdf4 --- /dev/null +++ b/assets/final/shaders/render/scene_query_bvh.wgsl @@ -0,0 +1,55 @@ +#include "math/sdf_shapes" +#include "math/sdf_utils" + +struct BVHNode { + min: vec3<f32>, + left_idx: i32, + max: vec3<f32>, + obj_idx_or_right: i32, +}; + +@group(0) @binding(2) var<storage, read> bvh_nodes: array<BVHNode>; + +fn get_dist(p: vec3<f32>, obj_type: f32) -> f32 { + if (obj_type == 1.0) { return length(p) - 1.0; } // Unit Sphere + if (obj_type == 2.0) { return sdBox(p, vec3<f32>(1.0)); } // Unit Box + if (obj_type == 3.0) { return sdTorus(p, vec2<f32>(1.0, 0.4)); } // Unit Torus + if (obj_type == 4.0) { return sdPlane(p, vec3<f32>(0.0, 1.0, 0.0), 0.0); } + return 100.0; +} + +fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 { + var d = 1000.0; + var stack: array<i32, 32>; + var stack_ptr = 0; + + if (arrayLength(&bvh_nodes) > 0u) { + stack[stack_ptr] = 0; + stack_ptr++; + } + + while (stack_ptr > 0) { + stack_ptr--; + let node_idx = stack[stack_ptr]; + let node = bvh_nodes[node_idx]; + + if (aabb_sdf(p, node.min, node.max) < d) { + if (node.left_idx < 0) { // Leaf + let obj_idx = u32(node.obj_idx_or_right); + if (obj_idx == skip_idx) { continue; } + let obj = object_data.objects[obj_idx]; + let q = (obj.inv_model * vec4<f32>(p, 1.0)).xyz; + let s = min(length(obj.model[0].xyz), min(length(obj.model[1].xyz), length(obj.model[2].xyz))); + d = min(d, get_dist(q, obj.params.x) * s); + } else { // Internal + if (stack_ptr < 31) { + stack[stack_ptr] = node.left_idx; + stack_ptr++; + stack[stack_ptr] = node.obj_idx_or_right; + stack_ptr++; + } + } + } + } + return d; +} |
