sgl_3d/content/
baked_specular_probe.rs1use glam::{Mat4, Vec3};
8
9pub(crate) const MAX_PROBES: usize = 256;
18
19pub(crate) const LEVELS: u32 = 7;
21
22#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct SpecularProbeBox {
25 pub min: Vec3,
26 pub max: Vec3,
27}
28
29#[derive(Clone, Debug)]
34pub struct SpecularProbeRadiance {
35 pub face_size: u32,
36 pub texels: SpecularProbeTexels,
37}
38
39#[derive(Clone, Debug)]
41pub enum SpecularProbeTexels {
42 Rgba16Float(Vec<u16>),
44 Bc6hUfloat(Vec<u8>),
49}
50
51impl SpecularProbeTexels {
52 pub(crate) fn bytes(&self) -> &[u8] {
53 match self {
54 Self::Rgba16Float(texels) => bytemuck::cast_slice(texels),
55 Self::Bc6hUfloat(blocks) => blocks,
56 }
57 }
58
59 pub(crate) fn validate_radiance(&self) -> Result<(), ProbeError> {
62 if let Self::Rgba16Float(values) = self {
63 for (component, &value) in values.iter().enumerate() {
64 if value & 0x7c00 == 0x7c00 || (value & 0x8000 != 0 && value & 0x7fff != 0) {
66 return Err(ProbeError::InvalidRadiance { component });
67 }
68 }
69 }
70 Ok(())
71 }
72}
73
74#[derive(Clone, Debug)]
76pub struct BakedSpecularProbe {
77 pub center: Vec3,
79 pub world_to_local: Mat4,
81 pub influence: SpecularProbeBox,
86 pub blend: Vec3,
87 pub proxy: Option<SpecularProbeBox>,
91 pub radiance: SpecularProbeRadiance,
92}
93
94#[derive(Debug)]
96pub enum ProbeError {
97 InvalidProbe(&'static str),
98 InvalidDimensions,
99 InvalidPayloadLength { expected: usize, actual: usize },
100 InvalidRadiance { component: usize },
101 MixedRadiance,
102 CompressionUnsupported,
103 TooManyProbes,
104 Readback(String),
105}
106
107impl std::fmt::Display for ProbeError {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 match self {
110 Self::InvalidProbe(reason) => write!(f, "invalid specular probe: {reason}"),
111 Self::InvalidDimensions => write!(
112 f,
113 "specular probe faces must be a power of two from 64 within device limits"
114 ),
115 Self::InvalidPayloadLength { expected, actual } => write!(
116 f,
117 "specular probe radiance requires {expected} bytes, received {actual}"
118 ),
119 Self::InvalidRadiance { component } => write!(
120 f,
121 "specular probe component {component} is negative or non-finite"
122 ),
123 Self::MixedRadiance => write!(
124 f,
125 "a collection's probes share one face size and texel encoding"
126 ),
127 Self::CompressionUnsupported => write!(
128 f,
129 "BC6H specular probes need wgpu::Features::TEXTURE_COMPRESSION_BC"
130 ),
131 Self::TooManyProbes => write!(
132 f,
133 "a collection holds at most {MAX_PROBES} probes within the device's array layers"
134 ),
135 Self::Readback(reason) => {
136 write!(f, "static specular capture readback failed: {reason}")
137 }
138 }
139 }
140}
141impl std::error::Error for ProbeError {}
142
143fn validate_box(b: SpecularProbeBox, what: &'static str) -> Result<(), ProbeError> {
144 if !b.min.is_finite() || !(b.max - b.min).is_finite() || !b.min.cmplt(b.max).all() {
145 return Err(ProbeError::InvalidProbe(what));
146 }
147 Ok(())
148}
149
150impl BakedSpecularProbe {
151 pub(crate) fn validate_placement(&self) -> Result<(), ProbeError> {
153 let m = self.world_to_local;
154 if !self.center.is_finite() || !m.is_finite() {
155 return Err(ProbeError::InvalidProbe("coordinates must be finite"));
156 }
157 let axes = [
158 m.x_axis.truncate(),
159 m.y_axis.truncate(),
160 m.z_axis.truncate(),
161 ];
162 let affine = Vec3::new(m.x_axis.w, m.y_axis.w, m.z_axis.w);
163 if affine.abs().max_element() > 1e-5
164 || (m.w_axis.w - 1.).abs() > 1e-5
165 || axes
166 .iter()
167 .any(|axis| (axis.length_squared() - 1.).abs() > 1e-4)
168 || axes[0].dot(axes[1]).abs() > 1e-4
169 || axes[0].dot(axes[2]).abs() > 1e-4
170 || axes[1].dot(axes[2]).abs() > 1e-4
171 || (m.determinant() - 1.).abs() > 1e-4
172 {
173 return Err(ProbeError::InvalidProbe(
174 "transform must be a rigid rotation and translation",
175 ));
176 }
177 validate_box(
178 self.influence,
179 "influence must have positive, finite extent",
180 )?;
181 if !self.blend.is_finite() || self.blend.min_element() < 0. {
182 return Err(ProbeError::InvalidProbe(
183 "blend distances must be finite and nonnegative",
184 ));
185 }
186 if let Some(proxy) = self.proxy {
187 validate_box(proxy, "proxy must have positive, finite extent")?;
188 let local = m.transform_point3(self.center);
189 if !local.cmpge(proxy.min).all() || !local.cmple(proxy.max).all() {
190 return Err(ProbeError::InvalidProbe(
191 "capture center must lie inside the proxy",
192 ));
193 }
194 }
195 Ok(())
196 }
197}