Skip to content

Commit

Permalink
bevy_pbr2: Update and correct comments to 256 lights
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Oct 8, 2021
1 parent 4421f34 commit 93e4725
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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????!!!!!
Expand Down
8 changes: 4 additions & 4 deletions pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
},
Expand All @@ -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),
Expand Down
19 changes: 16 additions & 3 deletions pipelined/bevy_pbr2/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ fn fetch_directional_shadow(light_id: u32, frag_position: vec4<f32>, 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<f32> {
let rgb = clamp(
abs(
Expand Down Expand Up @@ -686,8 +690,9 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {

// 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) {
Expand All @@ -699,10 +704,18 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
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<f32>(
(1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * cluster_color,
output_color.a
);
}

// tone_mapping (done later in the pipeline)
Expand Down

0 comments on commit 93e4725

Please sign in to comment.