use crcbl_hal::{Format, ImageUsage};
use crcbl_shaders::probe_gather::{PUNCTUAL_RSM_SIDE, RSM_SIDE};
use crate::shadow;
use crate::transient::TransientImageDesc;
crcbl_console::convar! {
pub static r_probe_bounce: bool = true;
}
pub(crate) fn enabled() -> bool {
r_probe_bounce.get_bool()
}
#[must_use]
pub fn extent() -> (u32, u32) {
(RSM_SIDE, RSM_SIDE)
}
#[must_use]
pub fn punctual_extent() -> (u32, u32) {
(
PUNCTUAL_RSM_SIDE * shadow::ATLAS_COLUMNS,
PUNCTUAL_RSM_SIDE * shadow::ATLAS_ROWS,
)
}
#[must_use]
pub fn punctual_tile(tile: usize) -> shadow::TileRect {
let (x, y) = shadow::tile_origin(shadow::light_tile(tile));
shadow::TileRect {
x: x / shadow::TILE * PUNCTUAL_RSM_SIDE,
y: y / shadow::TILE * PUNCTUAL_RSM_SIDE,
side: PUNCTUAL_RSM_SIDE,
}
}
#[must_use]
pub fn texel_area(reach: f32) -> f32 {
#[expect(
clippy::cast_precision_loss,
reason = "the map is a few dozen texels a side"
)]
let side = 2.0 * reach / RSM_SIDE as f32;
side * side
}
#[must_use]
pub fn albedo_target(extent: (u32, u32)) -> TransientImageDesc {
TransientImageDesc::new(
extent,
Format::Rgba16Float,
ImageUsage::COLOR_ATTACHMENT.union(ImageUsage::SAMPLED),
)
}
#[must_use]
pub fn normal_target(extent: (u32, u32)) -> TransientImageDesc {
TransientImageDesc::new(
extent,
Format::Rgba8Unorm,
ImageUsage::COLOR_ATTACHMENT.union(ImageUsage::SAMPLED),
)
}
#[must_use]
pub fn world_target(extent: (u32, u32)) -> TransientImageDesc {
TransientImageDesc::new(
extent,
Format::Rgba32Float,
ImageUsage::COLOR_ATTACHMENT.union(ImageUsage::SAMPLED),
)
}
#[must_use]
pub fn depth_target(extent: (u32, u32)) -> TransientImageDesc {
TransientImageDesc::new(
extent,
Format::D32Float,
ImageUsage::DEPTH_STENCIL_ATTACHMENT,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_texel_covers_the_cascade_divided_by_the_map_s_side() {
#[expect(
clippy::cast_precision_loss,
reason = "the map is a few dozen texels a side"
)]
let side = RSM_SIDE as f32;
assert_eq!(texel_area(0.5 * side), 1.0);
assert_eq!(texel_area(side), 4.0);
}
#[test]
fn every_attachment_covers_its_own_map() {
let sun = extent();
assert_eq!(sun.0, sun.1, "the sun's map is square");
for extent in [sun, punctual_extent()] {
for desc in [
albedo_target(extent),
normal_target(extent),
world_target(extent),
depth_target(extent),
] {
assert_eq!(desc.extent, extent);
assert_eq!(desc.samples, 1);
assert_eq!(desc.mip_levels, 1);
}
}
}
#[test]
fn the_targets_carry_the_formats_the_shader_writes() {
for extent in [extent(), punctual_extent()] {
assert_eq!(albedo_target(extent).format, Format::Rgba16Float);
assert_eq!(normal_target(extent).format, Format::Rgba8Unorm);
assert_eq!(world_target(extent).format, Format::Rgba32Float);
assert_eq!(depth_target(extent).format, Format::D32Float);
assert!(
!depth_target(extent).usage.contains(ImageUsage::SAMPLED),
"nothing reads a map's depth, so it must not claim to be sampled"
);
}
}
#[test]
fn every_light_tile_is_its_own_rectangle_inside_the_punctual_map() {
let (width, height) = punctual_extent();
let mut seen: Vec<shadow::TileRect> = Vec::new();
for tile in 0..shadow::LIGHT_TILES {
let rect = punctual_tile(tile);
assert_eq!(rect.side, PUNCTUAL_RSM_SIDE);
assert!(
rect.x + rect.side <= width && rect.y + rect.side <= height,
"light tile {tile} at {rect:?} leaves a {width}x{height} map"
);
assert!(
!seen.contains(&rect),
"light tile {tile} lands on {rect:?}, which another tile already holds"
);
seen.push(rect);
}
}
#[test]
fn a_spot_s_map_is_a_perspective_of_its_own_outer_angle() {
use crate::light::SpotLight;
use glam::{Vec3, Vec4};
let spot = SpotLight {
position: Vec3::new(1.0, 2.0, -0.5),
radius: 9.0,
color: Vec3::ONE,
direction: Vec3::new(0.2, -1.0, 0.3),
inner_angle: 0.2,
outer_angle: 0.55,
fill: false,
};
let axis = spot.direction.normalize();
let across = axis.cross(Vec3::X).normalize();
let edge = spot.position + axis + across * spot.outer_angle.tan();
let clip: Vec4 = shadow::spot_matrix(&spot) * edge.extend(1.0);
let ndc = (clip.x / clip.w).hypot(clip.y / clip.w);
assert!(
(ndc - 1.0).abs() < 1e-4,
"a point on the cone's outer edge lands {ndc:.5} of the way across the \
clip volume — the map's half field of view is not the outer half-angle, \
and `producer_tangent` derives the wrong solid angle from it"
);
}
}