Skip to content

Commit

Permalink
Debug view mode for variance visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Oct 28, 2023
1 parent a275637 commit 3de2a81
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
1 change: 0 additions & 1 deletion blade-render/code/debug-param.inc.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ struct DebugParams {
pad: u32,
mouse_pos: vec2<u32>,
};
var<uniform> debug: DebugParams;
1 change: 1 addition & 0 deletions blade-render/code/fill-gbuf.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct HitEntry {
var<storage, read> hit_entries: array<HitEntry>;

var<uniform> camera: CameraParams;
var<uniform> debug: DebugParams;
var acc_struct: acceleration_structure;

var out_depth: texture_storage_2d<r32float, write>;
Expand Down
26 changes: 15 additions & 11 deletions blade-render/code/post-proc.wgsl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +13,7 @@ var t_albedo: texture_2d<f32>;
var light_diffuse: texture_2d<f32>;
var t_debug: texture_2d<f32>;
var<uniform> tone_map_params: ToneMapParams;
var<uniform> debug_params: DebugParams;

struct VertexOutput {
@builtin(position) clip_pos: vec4<f32>,
Expand All @@ -29,20 +31,22 @@ fn blit_vs(@builtin(vertex_index) vi: u32) -> VertexOutput {
@fragment
fn blit_fs(vo: VertexOutput) -> @location(0) vec4<f32> {
let tc = vec2<i32>(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<f32>(l_ldr, 1.0);
} else {
return color;
return vec4<f32>(color, 1.0);
}
} else if (debug_params.view_mode == DebugMode_Variance) {
return vec4<f32>(illumunation.w);
} else {
return textureLoad(t_debug, tc, 0);
}
}
1 change: 1 addition & 0 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct MainParams {
var<uniform> camera: CameraParams;
var<uniform> prev_camera: CameraParams;
var<uniform> parameters: MainParams;
var<uniform> debug: DebugParams;
var acc_struct: acceleration_structure;
var env_map: texture_2d<f32>;
var sampler_linear: sampler;
Expand Down
1 change: 0 additions & 1 deletion blade-render/src/asset_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl AssetHub {
sh_baker.register_enum::<crate::render::DebugMode>();
sh_baker.register_bitflags::<crate::render::DebugDrawFlags>();
sh_baker.register_bitflags::<crate::render::DebugTextureFlags>();
sh_baker.register_enum::<crate::render::PostProcMode>();
let shaders = AssetManager::new(target, choir, sh_baker);

Self {
Expand Down
42 changes: 21 additions & 21 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum DebugMode {
Depth = 1,
Normal = 2,
HitConsistency = 3,
Variance = 4,
}

impl Default for DebugMode {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -1410,24 +1408,26 @@ 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 {
t_albedo: cur.albedo_view,
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);
Expand Down
7 changes: 1 addition & 6 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down

0 comments on commit 3de2a81

Please sign in to comment.