From 75bdd2f8c17169422d175ed2d9dfceb9acb0fe77 Mon Sep 17 00:00:00 2001 From: skal Date: Fri, 6 Feb 2026 02:14:30 +0100 Subject: 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. --- src/3d/renderer.h | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'src/3d/renderer.h') diff --git a/src/3d/renderer.h b/src/3d/renderer.h index b468423..db86b72 100644 --- a/src/3d/renderer.h +++ b/src/3d/renderer.h @@ -19,7 +19,7 @@ struct GlobalUniforms { mat4 view_proj; mat4 inv_view_proj; // Added for skybox/raymarching vec4 camera_pos_time; // xyz = camera_pos, w = time - vec4 params; // x = num_objects, y = use_bvh, zw = padding + vec4 params; // x = num_objects, yzw = padding vec2 resolution; vec2 padding; }; @@ -41,25 +41,15 @@ class Renderer3D { static void SetDebugEnabled(bool enabled) { s_debug_enabled_ = enabled; } - static void SetBvhEnabled(bool enabled) { - s_bvh_enabled_ = enabled; - } #endif // Renders the scene to the given texture view (Convenience: creates a pass) - void render(const Scene& scene, const Camera& camera, float time, - WGPUTextureView target_view, WGPUTextureView depth_view_opt = nullptr); // Records draw commands to an existing pass. - // Assumes the pass has a compatible pipeline (or we set it here). - - // Note: Caller must ensure depth/color attachments are set up correctly in - // the pass. - void draw(WGPURenderPassEncoder pass, const Scene& scene, const Camera& camera, float time); @@ -71,8 +61,12 @@ class Renderer3D { // Resize handler (if needed for internal buffers) void resize(int width, int height); + // Set whether to use BVH acceleration + void SetBvhEnabled(bool enabled) { bvh_enabled_ = enabled; } + private: void create_pipeline(); + WGPURenderPipeline create_pipeline_impl(bool use_bvh); void create_skybox_pipeline(); void create_default_resources(); void update_uniforms(const Scene& scene, const Camera& camera, float time); @@ -81,7 +75,8 @@ class Renderer3D { WGPUQueue queue_ = nullptr; WGPUTextureFormat format_ = WGPUTextureFormat_Undefined; - WGPURenderPipeline pipeline_ = nullptr; + WGPURenderPipeline pipeline_ = nullptr; // BVH enabled + WGPURenderPipeline pipeline_no_bvh_ = nullptr; // BVH disabled WGPUBindGroup bind_group_ = nullptr; WGPURenderPipeline skybox_pipeline_ = nullptr; WGPUBindGroup skybox_bind_group_ = nullptr; @@ -90,6 +85,7 @@ class Renderer3D { WGPUBuffer bvh_storage_buffer_ = nullptr; BVH cpu_bvh_; // Keep a CPU-side copy for building/uploading + bool bvh_enabled_ = true; WGPUTextureView noise_texture_view_ = nullptr; WGPUTextureView sky_texture_view_ = nullptr; @@ -107,6 +103,5 @@ class Renderer3D { #if !defined(STRIP_ALL) VisualDebug visual_debug_; static bool s_debug_enabled_; - static bool s_bvh_enabled_; #endif -}; +}; \ No newline at end of file -- cgit v1.2.3