use glam::{Mat4, Vec3};
pub(crate) const MAX_PROBES: usize = 256;
pub(crate) const LEVELS: u32 = 7;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SpecularProbeBox {
pub min: Vec3,
pub max: Vec3,
}
#[derive(Clone, Debug)]
pub struct SpecularProbeRadiance {
pub face_size: u32,
pub texels: SpecularProbeTexels,
}
#[derive(Clone, Debug)]
pub enum SpecularProbeTexels {
Rgba16Float(Vec<u16>),
Bc6hUfloat(Vec<u8>),
}
impl SpecularProbeTexels {
pub(crate) fn bytes(&self) -> &[u8] {
match self {
Self::Rgba16Float(texels) => bytemuck::cast_slice(texels),
Self::Bc6hUfloat(blocks) => blocks,
}
}
pub(crate) fn validate_radiance(&self) -> Result<(), ProbeError> {
if let Self::Rgba16Float(values) = self {
for (component, &value) in values.iter().enumerate() {
if value & 0x7c00 == 0x7c00 || (value & 0x8000 != 0 && value & 0x7fff != 0) {
return Err(ProbeError::InvalidRadiance { component });
}
}
}
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct BakedSpecularProbe {
pub center: Vec3,
pub world_to_local: Mat4,
pub influence: SpecularProbeBox,
pub blend: Vec3,
pub proxy: Option<SpecularProbeBox>,
pub radiance: SpecularProbeRadiance,
}
#[derive(Debug)]
pub enum ProbeError {
InvalidProbe(&'static str),
InvalidDimensions,
InvalidPayloadLength { expected: usize, actual: usize },
InvalidRadiance { component: usize },
MixedRadiance,
CompressionUnsupported,
TooManyProbes,
Readback(String),
}
impl std::fmt::Display for ProbeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidProbe(reason) => write!(f, "invalid specular probe: {reason}"),
Self::InvalidDimensions => write!(
f,
"specular probe faces must be a power of two from 64 within device limits"
),
Self::InvalidPayloadLength { expected, actual } => write!(
f,
"specular probe radiance requires {expected} bytes, received {actual}"
),
Self::InvalidRadiance { component } => write!(
f,
"specular probe component {component} is negative or non-finite"
),
Self::MixedRadiance => write!(
f,
"a collection's probes share one face size and texel encoding"
),
Self::CompressionUnsupported => write!(
f,
"BC6H specular probes need wgpu::Features::TEXTURE_COMPRESSION_BC"
),
Self::TooManyProbes => write!(
f,
"a collection holds at most {MAX_PROBES} probes within the device's array layers"
),
Self::Readback(reason) => {
write!(f, "static specular capture readback failed: {reason}")
}
}
}
}
impl std::error::Error for ProbeError {}
fn validate_box(b: SpecularProbeBox, what: &'static str) -> Result<(), ProbeError> {
if !b.min.is_finite() || !b.max.is_finite() || !b.min.cmplt(b.max).all() {
return Err(ProbeError::InvalidProbe(what));
}
Ok(())
}
impl BakedSpecularProbe {
pub(crate) fn validate_placement(&self) -> Result<(), ProbeError> {
let m = self.world_to_local;
if !self.center.is_finite() || !m.is_finite() {
return Err(ProbeError::InvalidProbe("coordinates must be finite"));
}
let axes = [
m.x_axis.truncate(),
m.y_axis.truncate(),
m.z_axis.truncate(),
];
let affine = Vec3::new(m.x_axis.w, m.y_axis.w, m.z_axis.w);
if affine.abs().max_element() > 1e-5
|| (m.w_axis.w - 1.).abs() > 1e-5
|| axes
.iter()
.any(|axis| (axis.length_squared() - 1.).abs() > 1e-4)
|| axes[0].dot(axes[1]).abs() > 1e-4
|| axes[0].dot(axes[2]).abs() > 1e-4
|| axes[1].dot(axes[2]).abs() > 1e-4
|| (m.determinant() - 1.).abs() > 1e-4
{
return Err(ProbeError::InvalidProbe(
"transform must be a rigid rotation and translation",
));
}
validate_box(self.influence, "influence must have positive extent")?;
if !self.blend.is_finite() || self.blend.min_element() < 0. {
return Err(ProbeError::InvalidProbe(
"blend distances must be finite and nonnegative",
));
}
if let Some(proxy) = self.proxy {
validate_box(proxy, "proxy must have positive extent")?;
let local = m.transform_point3(self.center);
if !local.cmpge(proxy.min).all() || !local.cmple(proxy.max).all() {
return Err(ProbeError::InvalidProbe(
"capture center must lie inside the proxy",
));
}
}
Ok(())
}
}