From 93e4725c65e3ed997308e0e5d4a58d9266079378 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Fri, 8 Oct 2021 20:04:38 +0200 Subject: [PATCH] bevy_pbr2: Update and correct comments to 256 lights --- pipelined/bevy_pbr2/src/render/light.rs | 8 ++++---- pipelined/bevy_pbr2/src/render/mod.rs | 8 ++++---- pipelined/bevy_pbr2/src/render/pbr.wgsl | 19 ++++++++++++++++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pipelined/bevy_pbr2/src/render/light.rs b/pipelined/bevy_pbr2/src/render/light.rs index bda9eaedf2bfe..065bb98bef376 100644 --- a/pipelined/bevy_pbr2/src/render/light.rs +++ b/pipelined/bevy_pbr2/src/render/light.rs @@ -1363,12 +1363,12 @@ const CLUSTER_COUNT_MASK: u32 = (1 << 8) - 1; const POINT_LIGHT_INDEX_MASK: u32 = (1 << 8) - 1; // NOTE: With uniform buffer max binding size as 16384 bytes -// that means we can fit say 128 point lights in one uniform -// buffer, which means the count can be at most 128 so it -// needs 7 bits, use 8 for convenience. +// that means we can fit say 256 point lights in one uniform +// buffer, which means the count can be at most 256 so it +// needs 8 bits, use 8 for convenience. // The array of indices can also use u8 and that means the // offset in to the array of indices needs to be able to address -// 16384 values. lod2(16384) = 21 bits. +// 16384 values. log2(16384) = 14 bits. // This means we can pack the offset into the upper 24 bits of a u32 // and the count into the lower 8 bits. // FIXME: Probably there are endianness concerns here????!!!!! diff --git a/pipelined/bevy_pbr2/src/render/mod.rs b/pipelined/bevy_pbr2/src/render/mod.rs index 105bc69d9645a..8db644e8c7553 100644 --- a/pipelined/bevy_pbr2/src/render/mod.rs +++ b/pipelined/bevy_pbr2/src/render/mod.rs @@ -222,7 +222,7 @@ impl FromWorld for PbrShaders { ty: BufferBindingType::Uniform, has_dynamic_offset: false, // NOTE: Static size for uniform buffers. GpuPointLight has a padded - // size of 128 bytes, so 16384 / 128 = 128 point lights max + // size of 64 bytes, so 16384 / 64 = 256 point lights max min_binding_size: BufferSize::new(16384), }, count: None, @@ -234,7 +234,7 @@ impl FromWorld for PbrShaders { ty: BindingType::Buffer { ty: BufferBindingType::Uniform, has_dynamic_offset: false, - // NOTE: With 128 point lights max, indices need 7 bits. Use u8 for + // NOTE: With 256 point lights max, indices need 8 bits. Use u8 for // convenience. min_binding_size: BufferSize::new(16384), }, @@ -247,8 +247,8 @@ impl FromWorld for PbrShaders { ty: BindingType::Buffer { ty: BufferBindingType::Uniform, has_dynamic_offset: false, - // NOTE: The offset needs to address 16384 indices, which needs 21 bits. - // The count can be at most all 128 lights so 7 bits. + // NOTE: The offset needs to address 16384 indices, which needs 14 bits. + // The count can be at most all 256 lights so 8 bits. // Pack the offset into the upper 24 bits and the count into the // lower 8 bits for convenience. min_binding_size: BufferSize::new(16384), diff --git a/pipelined/bevy_pbr2/src/render/pbr.wgsl b/pipelined/bevy_pbr2/src/render/pbr.wgsl index 8451e3b851a24..c560ba22f2f9f 100644 --- a/pipelined/bevy_pbr2/src/render/pbr.wgsl +++ b/pipelined/bevy_pbr2/src/render/pbr.wgsl @@ -530,6 +530,10 @@ fn fetch_directional_shadow(light_id: u32, frag_position: vec4, surface_nor return textureSampleCompareLevel(directional_shadow_textures, directional_shadow_textures_sampler, light_local, i32(light_id), depth); } +fn random1D(s: f32) -> f32 { + return fract(sin(s * 12.9898) * 43758.5453123); +} + fn hsv2rgb(hue: f32, saturation: f32, value: f32) -> vec3 { let rgb = clamp( abs( @@ -686,8 +690,9 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4 { // Cluster allocation debug (using 'over' alpha blending) let cluster_debug_mode = 0; - let cluster_overlay_alpha = 0.05; + let cluster_overlay_alpha = 0.4; if (cluster_debug_mode == 1) { + // Visualize the z slices var z_slice: u32 = view_z_to_z_slice(view_z); // A hack to make the colors alternate a bit more if ((z_slice & 1u) == 1u) { @@ -699,10 +704,18 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4 { output_color.a ); } elseif (cluster_debug_mode == 2) { + // Visualize the number of lights per cluster output_color.r = (1.0 - cluster_overlay_alpha) * output_color.r - + cluster_overlay_alpha * smoothStep(0.0, 16.0, f32(offset_and_count.count)); + + cluster_overlay_alpha * smoothStep(0.0, 32.0, f32(offset_and_count.count)); output_color.g = (1.0 - cluster_overlay_alpha) * output_color.g - + cluster_overlay_alpha * (1.0 - smoothStep(0.0, 16.0, f32(offset_and_count.count))); + + cluster_overlay_alpha * (1.0 - smoothStep(0.0, 32.0, f32(offset_and_count.count))); + } elseif (cluster_debug_mode == 3) { + // Visualize the cluster + let cluster_color = hsv2rgb(random1D(f32(cluster_index)), 1.0, 0.5); + output_color = vec4( + (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * cluster_color, + output_color.a + ); } // tone_mapping (done later in the pipeline)