From 83e13fb6c083d0dc5969869ed508f2789d71c6e6 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:43:30 -0500 Subject: [PATCH 1/4] fix(geom): Mesh::validate refuses non-finite normals and absurd UVs validate checked positions were finite but not normals or UVs. A UV like 1e30 saturates the integer texel maths in ImageRes::sample, the colour bake and the path tracer, and x0 + 1 then overflows (a panic in debug builds, a wrapped lookup in release). UVs must now be finite and within +-1e6 (UDIM layouts stop at 10 columns); normals must be finite. Co-Authored-By: Claude Sonnet 5.5 --- crates/geom/src/mesh.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index 95d66b4e..1d92dc8b 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -47,6 +47,9 @@ pub struct Mesh { /// Hard cap on mesh size accepted from files (triangles). pub const MAX_TRIANGLES: usize = 20_000_000; +/// Largest UV coordinate a mesh may carry (UDIM tiles stop at 10 columns; this is far beyond any +/// real layout). Texel lookups turn UVs into integers, which a UV like 1e30 would overflow. +pub const MAX_UV: f32 = 1.0e6; impl Mesh { pub fn triangle_count(&self) -> usize { @@ -145,6 +148,12 @@ impl Mesh { if self.positions.iter().any(|p| !p.is_finite()) { return Err(GeomError::Invalid("mesh has non-finite positions".into())); } + if self.normals.iter().any(|n| !n.is_finite()) { + return Err(GeomError::Invalid("mesh has non-finite normals".into())); + } + if self.uvs.iter().any(|uv| !uv.is_finite() || uv.x.abs() > MAX_UV || uv.y.abs() > MAX_UV) { + return Err(GeomError::Invalid(format!("mesh has non-finite UVs or UVs beyond ±{MAX_UV}"))); + } for s in &self.submeshes { if s.start as usize + s.count as usize > self.indices.len() || s.material >= self.materials.len().max(1) { return Err(GeomError::Invalid(format!("submesh {} is out of range", s.name))); @@ -443,6 +452,26 @@ mod tests { assert!(m.validate().is_err()); } + #[test] + fn validate_refuses_non_finite_normals_and_absurd_uvs() { + assert!(quad().validate().is_ok()); + for bad in [f32::NAN, f32::INFINITY, -f32::INFINITY, 1.0e30, -2.0e6] { + let mut m = quad(); + m.uvs[1].x = bad; + assert!(m.validate().is_err(), "uv {bad}"); + let mut m = quad(); + m.uvs[2].y = bad; + assert!(m.validate().is_err(), "uv {bad}"); + } + let mut m = quad(); + m.normals[0].z = f32::NAN; + assert!(m.validate().is_err()); + // Large but real UDIM-style layouts stay valid. + let mut m = quad(); + m.uvs[0] = Vec2::new(1009.5, 0.5); + assert!(m.validate().is_ok()); + } + #[test] fn append_offsets_indices_and_materials() { let mut a = quad(); From da53dc8a8c37ca21d8b3c5fe2c1c4efc8558c37a Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Fri, 6 Nov 2026 07:29:11 -0500 Subject: [PATCH 2/4] fix(#253): texel lookups at huge finite UVs no longer overflow MAX_UV (1e6) does not protect the 32-bit lookup in the path tracer (1e6 * 4096 saturates i32, then +dx overflows), and sample()/base_color() add 1 to a saturated i64. UVs also come from places other than mesh attributes. Lookups now saturate/64-bit and refuse non-finite coordinates after scaling. Co-Authored-By: Claude Sonnet 5.5 --- crates/bake/src/map/source.rs | 27 +++++++++++++++++++++++---- crates/compose/src/store.rs | 28 +++++++++++++++++++++++++++- crates/render/src/trace.rs | 29 +++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/crates/bake/src/map/source.rs b/crates/bake/src/map/source.rs index 0e1f9164..27b50ffe 100644 --- a/crates/bake/src/map/source.rs +++ b/crates/bake/src/map/source.rs @@ -117,10 +117,15 @@ fn base_color(material: &BaseColor, uv: Vec2) -> Vec4 { Vec4::new(srgb_to_linear(color(0)), srgb_to_linear(color(1)), srgb_to_linear(color(2)), color(3)) }; let (fx, fy) = (uv.x * width as f32 - 0.5, uv.y * height as f32 - 0.5); - let (x0, y0) = (fx.floor(), fy.floor()); - let (tx, ty) = (fx - x0, fy - y0); - let (x, y) = (x0 as i64, y0 as i64); - texel(x, y).lerp(texel(x + 1, y), tx).lerp(texel(x, y + 1).lerp(texel(x + 1, y + 1), tx), ty) + if fx.is_finite() && fy.is_finite() { + let (x0, y0) = (fx.floor(), fy.floor()); + let (tx, ty) = (fx - x0, fy - y0); + let (x, y) = (x0 as i64, y0 as i64); + let (x1, y1) = (x.saturating_add(1), y.saturating_add(1)); + texel(x, y).lerp(texel(x1, y), tx).lerp(texel(x, y1).lerp(texel(x1, y1), tx), ty) + } else { + Vec4::ONE + } }; let color = sampled_color * material.factor; Vec4::new(linear_to_srgb(color.x), linear_to_srgb(color.y), linear_to_srgb(color.z), color.w) @@ -133,3 +138,17 @@ fn match_key(name: &str, suffix: &str, keys: &mut HashMap) -> u32 { let next = keys.len() as u32; *keys.entry(base).or_insert(next) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn base_color_sampling_survives_huge_finite_uvs() { + let material = BaseColor { factor: Vec4::ONE, width: 2, height: 2, texels: vec![[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [255; 4]] }; + for uv in [Vec2::new(1.0e30, 0.5), Vec2::new(0.5, -1.0e30), Vec2::splat(f32::MAX), Vec2::splat(1.0e6)] { + let c = base_color(&material, uv); + assert!(c.is_finite(), "{uv}: {c}"); + } + } +} diff --git a/crates/compose/src/store.rs b/crates/compose/src/store.rs index 1370c0c0..1dc801b5 100644 --- a/crates/compose/src/store.rs +++ b/crates/compose/src/store.rs @@ -85,9 +85,17 @@ impl ImageRes { } let fx = u * self.width as f32 - 0.5; let fy = v * self.height as f32 - 0.5; + if !fx.is_finite() || !fy.is_finite() { + return [0.0; 4]; + } let (x0, y0) = (fx.floor() as i64, fy.floor() as i64); let (tx, ty) = (fx - fx.floor(), fy - fy.floor()); - let (a, b, c, d) = (self.texel(x0, y0), self.texel(x0 + 1, y0), self.texel(x0, y0 + 1), self.texel(x0 + 1, y0 + 1)); + let (a, b, c, d) = ( + self.texel(x0, y0), + self.texel(x0.saturating_add(1), y0), + self.texel(x0, y0.saturating_add(1)), + self.texel(x0.saturating_add(1), y0.saturating_add(1)), + ); let mut out = [0.0; 4]; for k in 0..4 { let top = a[k] + (b[k] - a[k]) * tx; @@ -97,3 +105,21 @@ impl ImageRes { out } } + +#[cfg(test)] +mod tests { + use super::*; + + fn checker() -> ImageRes { + ImageRes { width: 2, height: 2, rgba: vec![[0.0; 4], [1.0; 4], [1.0; 4], [0.0; 4]], srgb: false } + } + + #[test] + fn sampling_at_huge_finite_coordinates_does_not_overflow() { + let img = checker(); + for (u, v) in [(1.0e30, 0.5), (0.5, -1.0e30), (f32::MAX, f32::MAX), (1.0e6, -1.0e6)] { + let s = img.sample(u, v); + assert!(s.iter().all(|c| c.is_finite() && (0.0..=1.0).contains(c)), "({u}, {v}) -> {s:?}"); + } + } +} diff --git a/crates/render/src/trace.rs b/crates/render/src/trace.rs index a823a031..39b51d1d 100644 --- a/crates/render/src/trace.rs +++ b/crates/render/src/trace.rs @@ -537,10 +537,7 @@ fn surface(scene: &TraceScene, tri: u32, u: f32, v: f32, o: Vec3, d: Vec3, t: f3 let set = scene.tri_set.get(tri as usize).and_then(|s| scene.sets.get(*s)); // The composite texel at offset (dx, dy) from the hit. - let texel = |s: &TraceSet, dx: i32, dy: i32| { - let (w, h) = (s.composite.resolution.max(1) as i32, s.composite.height.max(1) as i32); - (((uv.x * w as f32).floor() as i32 + dx).rem_euclid(w) as u32, ((uv.y * h as f32).floor() as i32 + dy).rem_euclid(h) as u32) - }; + let texel = |s: &TraceSet, dx: i32, dy: i32| wrapped_texel(uv, (s.composite.resolution, s.composite.height), dx, dy); let ch = |c: Channel, dx: i32, dy: i32| -> [f32; 3] { let Some(s) = set else { return c.default_value() }; let (x, y) = texel(s, dx, dy); @@ -1042,8 +1039,32 @@ fn trace(scene: &TraceScene, mut o: Vec3, mut d: Vec3, settings: &TraceSettings, Some(Sample { radiance: if l.is_finite() { l } else { Vec3::ZERO }, albedo, normal, depth }) } +/// The texel of a `size` image at `uv` (wrapping), offset by `(dx, dy)`. 64-bit so a far-off UV +/// times a wide image can't overflow the offset (in 32 bits a UV of 1e6 on an image 4096 wide saturated, and the offset then overflowed). +fn wrapped_texel(uv: Vec2, size: (u32, u32), dx: i32, dy: i32) -> (u32, u32) { + let (w, h) = (i64::from(size.0.max(1)), i64::from(size.1.max(1))); + let at = |t: f32, n: i64, d: i32| ((t * n as f32).floor() as i64).saturating_add(i64::from(d)).rem_euclid(n) as u32; + (at(uv.x, w, dx), at(uv.y, h, dy)) +} + #[cfg(test)] mod tests { + #[test] + fn texel_lookup_survives_far_off_uvs() { + use super::wrapped_texel; + use texturecraft_geom::glam::Vec2; + // Inside the image, and offsets wrap around the edges. + assert_eq!(wrapped_texel(Vec2::new(0.25, 0.75), (4, 4), 0, 0), (1, 3)); + assert_eq!(wrapped_texel(Vec2::new(0.0, 0.99), (4, 4), -1, 1), (3, 0)); + // 1e6 (the largest UV a mesh may carry) times 8192 texels is past i32::MAX; 1e30 saturates. + for uv in [1.0e6, -1.0e6, 1.0e30, f32::MAX] { + for (dx, dy) in [(1, 1), (-1, -1), (0, 0)] { + let (x, y) = wrapped_texel(Vec2::new(uv, uv), (8192, 8192), dx, dy); + assert!(x < 8192 && y < 8192, "{uv}: ({x}, {y})"); + } + } + } + use super::*; use texturecraft_geom::primitives; From 88e0eb1ba5417f931bcde44efa08978bc9f234ff Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:40:16 -0500 Subject: [PATCH 3/4] fix(#253): clamp the height sample position so x0 + 1 can't overflow A huge finite UV saturates the cast to isize (32-bit on wasm32), and x0 + 1 then overflowed (a panic in debug). Past one texel outside, every lookup clamps to the same edge texel, so clamping the position first changes no result. Co-Authored-By: Claude Sonnet 5.5 --- crates/render/src/displace.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/render/src/displace.rs b/crates/render/src/displace.rs index f2e2b19a..d40c9b73 100644 --- a/crates/render/src/displace.rs +++ b/crates/render/src/displace.rs @@ -117,6 +117,10 @@ impl Heights { if !(fx.is_finite() && fy.is_finite()) { return 0.0; } + // Past one texel outside, every lookup clamps to the same edge texel, so clamping the + // position first changes no result but keeps `x0 + 1` far from overflowing: a huge finite + // UV saturates the `as isize` cast, and isize is 32 bits on wasm32. + let (fx, fy) = (fx.clamp(-1.0, self.w as f32), fy.clamp(-1.0, self.h as f32)); let (x0, y0) = (fx.floor(), fy.floor()); let (tx, ty) = (fx - x0, fy - y0); let (x0, y0) = (x0 as isize, y0 as isize); @@ -144,6 +148,21 @@ mod tests { TraceSet { material: 0, composite, height_strength: strength } } + #[test] + fn sampling_at_huge_or_far_uvs_clamps_to_the_edge_without_overflow() { + let heights = Heights { w: 2, h: 2, data: vec![1.0, 2.0, 3.0, 4.0] }; + // x0 + 1 used to overflow for these (the cast to isize saturates at isize::MAX). + for far in [1.0e30_f32, -1.0e30, f32::MAX, f32::MIN, 1.0e10] { + assert!(heights.sample(Vec2::new(far, far)).is_finite()); + } + assert_eq!(heights.sample(Vec2::new(1.0e30, 1.0e30)), 4.0); + assert_eq!(heights.sample(Vec2::new(-1.0e30, -1.0e30)), 1.0); + assert_eq!(heights.sample(Vec2::new(1.0e30, -1.0e30)), 2.0); + assert_eq!(heights.sample(Vec2::new(f32::NAN, 0.5)), 0.0); + // In range, bilinear is unchanged: the centre of the 2 x 2 is the mean. + assert!((heights.sample(Vec2::new(0.5, 0.5)) - 2.5).abs() < 1e-6); + } + #[test] fn uniform_height_lifts_the_dense_plane_along_its_normal() { let plane = primitives::plane(2.0, 1); From a5ba5eb719646208cfc270dc0963076e9e807ed2 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Fri, 6 Nov 2026 22:24:10 -0500 Subject: [PATCH 4/4] docs(#253): doc comments for the touched functions and the new tests Co-Authored-By: Claude Sonnet 5.5 --- crates/bake/src/map/source.rs | 3 +++ crates/compose/src/store.rs | 2 ++ crates/geom/src/mesh.rs | 1 + crates/render/src/displace.rs | 1 + crates/render/src/trace.rs | 3 +++ 5 files changed, 10 insertions(+) diff --git a/crates/bake/src/map/source.rs b/crates/bake/src/map/source.rs index 27b50ffe..7f2e7e32 100644 --- a/crates/bake/src/map/source.rs +++ b/crates/bake/src/map/source.rs @@ -106,6 +106,8 @@ impl<'a> Source<'a> { } } +/// The material's base colour at `uv` (bilinear, wrapping), sRGB RGBA times its factor; white +/// where there is no texture or the UV is not finite. fn base_color(material: &BaseColor, uv: Vec2) -> Vec4 { let sampled_color = if material.texels.is_empty() || !uv.is_finite() { Vec4::ONE @@ -143,6 +145,7 @@ fn match_key(name: &str, suffix: &str, keys: &mut HashMap) -> u32 { mod tests { use super::*; + /// Sampling at huge but finite UVs returns a finite colour instead of overflowing. #[test] fn base_color_sampling_survives_huge_finite_uvs() { let material = BaseColor { factor: Vec4::ONE, width: 2, height: 2, texels: vec![[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [255; 4]] }; diff --git a/crates/compose/src/store.rs b/crates/compose/src/store.rs index 1dc801b5..d8461f50 100644 --- a/crates/compose/src/store.rs +++ b/crates/compose/src/store.rs @@ -110,10 +110,12 @@ impl ImageRes { mod tests { use super::*; + /// A 2 x 2 black and white checker image for the sampling test. fn checker() -> ImageRes { ImageRes { width: 2, height: 2, rgba: vec![[0.0; 4], [1.0; 4], [1.0; 4], [0.0; 4]], srgb: false } } + /// Sampling at huge finite coordinates stays finite and within 0..=1 instead of overflowing. #[test] fn sampling_at_huge_finite_coordinates_does_not_overflow() { let img = checker(); diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index 1d92dc8b..64c8b1e5 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -452,6 +452,7 @@ mod tests { assert!(m.validate().is_err()); } + /// Validation rejects non-finite normals and non-finite or beyond-limit UVs, but accepts UDIM-style UVs. #[test] fn validate_refuses_non_finite_normals_and_absurd_uvs() { assert!(quad().validate().is_ok()); diff --git a/crates/render/src/displace.rs b/crates/render/src/displace.rs index d40c9b73..abd71692 100644 --- a/crates/render/src/displace.rs +++ b/crates/render/src/displace.rs @@ -148,6 +148,7 @@ mod tests { TraceSet { material: 0, composite, height_strength: strength } } + /// Far-off UVs clamp to the nearest edge texel without overflow; NaN gives 0 and in-range sampling is unchanged. #[test] fn sampling_at_huge_or_far_uvs_clamps_to_the_edge_without_overflow() { let heights = Heights { w: 2, h: 2, data: vec![1.0, 2.0, 3.0, 4.0] }; diff --git a/crates/render/src/trace.rs b/crates/render/src/trace.rs index 39b51d1d..1ea49e22 100644 --- a/crates/render/src/trace.rs +++ b/crates/render/src/trace.rs @@ -514,6 +514,8 @@ fn rnm(base: Vec3, detail: Vec3) -> Vec3 { (t * t.dot(u) - u * t.z).normalize_or(Vec3::Z) } +/// Builds the shading surface at a ray hit: interpolated geometry, normal and tangent frame, and the +/// set's channel values (base colour, roughness, metallic, coat and so on) at the hit's UV. fn surface(scene: &TraceScene, tri: u32, u: f32, v: f32, o: Vec3, d: Vec3, t: f32) -> Option { let m = &scene.mesh; let i = tri as usize * 3; @@ -1049,6 +1051,7 @@ fn wrapped_texel(uv: Vec2, size: (u32, u32), dx: i32, dy: i32) -> (u32, u32) { #[cfg(test)] mod tests { + /// `wrapped_texel` wraps offsets at the edges and stays inside the image for far-off UVs. #[test] fn texel_lookup_survives_far_off_uvs() { use super::wrapped_texel;