From 3de2a81a4aae15d13de152ad957de6629b76debe Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 28 Oct 2023 15:00:36 -0700 Subject: [PATCH] Debug view mode for variance visualization --- blade-render/code/debug-param.inc.wgsl | 1 - blade-render/code/fill-gbuf.wgsl | 1 + blade-render/code/post-proc.wgsl | 26 +++++++++------- blade-render/code/ray-trace.wgsl | 1 + blade-render/src/asset_hub.rs | 1 - blade-render/src/render/mod.rs | 42 +++++++++++++------------- examples/scene/main.rs | 7 +---- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/blade-render/code/debug-param.inc.wgsl b/blade-render/code/debug-param.inc.wgsl index 7df8154c..544c6af4 100644 --- a/blade-render/code/debug-param.inc.wgsl +++ b/blade-render/code/debug-param.inc.wgsl @@ -9,4 +9,3 @@ struct DebugParams { pad: u32, mouse_pos: vec2, }; -var debug: DebugParams; diff --git a/blade-render/code/fill-gbuf.wgsl b/blade-render/code/fill-gbuf.wgsl index 9d43c382..1dde3ce9 100644 --- a/blade-render/code/fill-gbuf.wgsl +++ b/blade-render/code/fill-gbuf.wgsl @@ -38,6 +38,7 @@ struct HitEntry { var hit_entries: array; var camera: CameraParams; +var debug: DebugParams; var acc_struct: acceleration_structure; var out_depth: texture_storage_2d; diff --git a/blade-render/code/post-proc.wgsl b/blade-render/code/post-proc.wgsl index c0eea300..ac6edd2f 100644 --- a/blade-render/code/post-proc.wgsl +++ b/blade-render/code/post-proc.wgsl @@ -1,7 +1,8 @@ -#use PostProcMode +#include "debug.inc.wgsl" +#include "debug-param.inc.wgsl" struct ToneMapParams { - mode: u32, + enabled: u32, average_lum: f32, key_value: f32, // minimum value of the pixels mapped to white brightness @@ -12,6 +13,7 @@ var t_albedo: texture_2d; var light_diffuse: texture_2d; var t_debug: texture_2d; var tone_map_params: ToneMapParams; +var debug_params: DebugParams; struct VertexOutput { @builtin(position) clip_pos: vec4, @@ -29,20 +31,22 @@ fn blit_vs(@builtin(vertex_index) vi: u32) -> VertexOutput { @fragment fn blit_fs(vo: VertexOutput) -> @location(0) vec4 { let tc = vec2(i32(vo.clip_pos.x), i32(vo.input_size.y) - i32(vo.clip_pos.y) - 1); - if (tone_map_params.mode == PostProcMode_Debug) { - return textureLoad(t_debug, tc, 0); - } else { - let albedo = textureLoad(t_albedo, tc, 0); - let radiance = textureLoad(light_diffuse, tc, 0); - let color = albedo * radiance; - if (tone_map_params.mode == PostProcMode_Tonemap) { + let illumunation = textureLoad(light_diffuse, tc, 0); + if (debug_params.view_mode == DebugMode_Final) { + let albedo = textureLoad(t_albedo, tc, 0).xyz; + let color = albedo.xyz * illumunation.xyz; + if (tone_map_params.enabled != 0u) { // Following https://blog.en.uwa4d.com/2022/07/19/physically-based-renderingg-hdr-tone-mapping/ - let l_adjusted = tone_map_params.key_value / tone_map_params.average_lum * color.xyz; + let l_adjusted = tone_map_params.key_value / tone_map_params.average_lum * color; let l_white = tone_map_params.white_level; let l_ldr = l_adjusted * (1.0 + l_adjusted / (l_white*l_white)) / (1.0 + l_adjusted); return vec4(l_ldr, 1.0); } else { - return color; + return vec4(color, 1.0); } + } else if (debug_params.view_mode == DebugMode_Variance) { + return vec4(illumunation.w); + } else { + return textureLoad(t_debug, tc, 0); } } diff --git a/blade-render/code/ray-trace.wgsl b/blade-render/code/ray-trace.wgsl index 0d244adb..8d52bfc0 100644 --- a/blade-render/code/ray-trace.wgsl +++ b/blade-render/code/ray-trace.wgsl @@ -32,6 +32,7 @@ struct MainParams { var camera: CameraParams; var prev_camera: CameraParams; var parameters: MainParams; +var debug: DebugParams; var acc_struct: acceleration_structure; var env_map: texture_2d; var sampler_linear: sampler; diff --git a/blade-render/src/asset_hub.rs b/blade-render/src/asset_hub.rs index 60904e19..aa297e52 100644 --- a/blade-render/src/asset_hub.rs +++ b/blade-render/src/asset_hub.rs @@ -37,7 +37,6 @@ impl AssetHub { sh_baker.register_enum::(); sh_baker.register_bitflags::(); sh_baker.register_bitflags::(); - sh_baker.register_enum::(); let shaders = AssetManager::new(target, choir, sh_baker); Self { diff --git a/blade-render/src/render/mod.rs b/blade-render/src/render/mod.rs index 11059cc4..6b8d14fd 100644 --- a/blade-render/src/render/mod.rs +++ b/blade-render/src/render/mod.rs @@ -29,6 +29,7 @@ pub enum DebugMode { Depth = 1, Normal = 2, HitConsistency = 3, + Variance = 4, } impl Default for DebugMode { @@ -37,14 +38,6 @@ impl Default for DebugMode { } } -#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, blade_macros::AsPrimitive, strum::EnumIter)] -#[repr(u32)] -pub enum PostProcMode { - Tonemap = 0, - Reconstruct = 1, - Debug = 2, -} - bitflags::bitflags! { #[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq, PartialOrd)] pub struct DebugDrawFlags: u32 { @@ -480,7 +473,7 @@ struct AtrousData { #[repr(C)] #[derive(Clone, Copy, Default, bytemuck::Zeroable, bytemuck::Pod)] struct ToneMapParams { - mode: u32, + enabled: u32, average_lum: f32, key_value: f32, white_level: f32, @@ -492,6 +485,7 @@ struct PostProcData { light_diffuse: blade_graphics::TextureView, t_debug: blade_graphics::TextureView, tone_map_params: ToneMapParams, + debug_params: DebugParams, } #[derive(blade_macros::ShaderData)] @@ -1234,6 +1228,19 @@ impl Renderer { self.post_proc_input = self.frame_data[0].light_diffuse_view; } + fn make_debug_params(&self, config: &DebugConfig) -> DebugParams { + DebugParams { + view_mode: config.view_mode as u32, + draw_flags: config.draw_flags.bits(), + texture_flags: config.texture_flags.bits(), + unused: 0, + mouse_pos: match config.mouse_pos { + Some(p) => [p[0], self.screen_size.height as i32 - p[1]], + None => [-1; 2], + }, + } + } + fn make_camera_params(&self, camera: &super::Camera) -> CameraParams { let fov_x = 2.0 * ((camera.fov_y * 0.5).tan() * self.screen_size.width as f32 @@ -1258,16 +1265,7 @@ impl Renderer { ray_config: RayConfig, ) { assert!(!self.is_tlas_dirty); - let debug = DebugParams { - view_mode: debug_config.view_mode as u32, - draw_flags: debug_config.draw_flags.bits(), - texture_flags: debug_config.texture_flags.bits(), - unused: 0, - mouse_pos: match debug_config.mouse_pos { - Some(p) => [p[0], self.screen_size.height as i32 - p[1]], - None => [-1; 2], - }, - }; + let debug = self.make_debug_params(&debug_config); let cur = self.frame_data.first().unwrap(); let prev = self.frame_data.last().unwrap(); @@ -1410,12 +1408,13 @@ impl Renderer { pub fn post_proc( &self, pass: &mut blade_graphics::RenderCommandEncoder, - mode: PostProcMode, + debug_config: DebugConfig, debug_blits: &[DebugBlit], ) { let pp = &self.scene.post_processing; let cur = self.frame_data.first().unwrap(); if let mut pc = pass.with(&self.post_proc_pipeline) { + let debug_params = self.make_debug_params(&debug_config); pc.bind( 0, &PostProcData { @@ -1423,11 +1422,12 @@ impl Renderer { light_diffuse: self.post_proc_input, t_debug: self.debug_view, tone_map_params: ToneMapParams { - mode: mode as u32, + enabled: 1, average_lum: pp.average_luminocity, key_value: pp.exposure_key_value, white_level: pp.white_level, }, + debug_params, }, ); pc.draw(0, 3, 0, 1); diff --git a/examples/scene/main.rs b/examples/scene/main.rs index 55e11717..9a574660 100644 --- a/examples/scene/main.rs +++ b/examples/scene/main.rs @@ -349,11 +349,6 @@ impl Example { scale_factor, }; if self.pending_scene.is_none() { - let pp_mode = if self.debug.view_mode == blade_render::DebugMode::default() { - blade_render::PostProcMode::Tonemap - } else { - blade_render::PostProcMode::Debug - }; let mut debug_blit_array = [blade_render::DebugBlit::default()]; let debug_blits = match self.debug_blit { Some(ref blit) => { @@ -362,7 +357,7 @@ impl Example { } None => &[], }; - self.renderer.post_proc(&mut pass, pp_mode, debug_blits); + self.renderer.post_proc(&mut pass, self.debug, debug_blits); } self.gui_painter .paint(&mut pass, gui_primitives, &screen_desc, &self.context);