diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index a145ccb2..917f5838 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -112,33 +112,43 @@ impl Mesh { /// Edges where the UV layout splits: two triangles meet there in 3D but not in UV space, per material in `0..materials.len().max(1)` pub fn uv_seam_edges(&self) -> Vec> { - let bits = |position: Vec3| position.to_array().map(f32::to_bits); - (0..self.materials.len().max(1)) - .map(|material| { - // Each 3D edge (by position) with the vertex pairs of the triangles that use it, the lower position first - let mut sides: std::collections::HashMap> = std::collections::HashMap::new(); - for triangle in self.triangles_of(material) { - let Some(corners) = self.indices.get(triangle * 3..triangle * 3 + 3) else { continue }; + // Signed zero denotes the same position on either side of an imported seam + let bits = |position: Vec3| position.to_array().map(|coordinate| if coordinate == 0.0 { 0 } else { coordinate.to_bits() }); + let mut seams = vec![Vec::new(); self.materials.len().max(1)]; + // Match across materials before grouping the lines for texture-set visibility + let mut sides: std::collections::HashMap> = std::collections::HashMap::new(); + for material in 0..seams.len() { + for triangle in self.triangles_of(material) { + let Some(corners) = self.indices.get(triangle * 3..triangle * 3 + 3) else { continue }; - for (from, to) in [(corners[0], corners[1]), (corners[1], corners[2]), (corners[2], corners[0])] { - let (Some(from_position), Some(to_position)) = (self.positions.get(from as usize), self.positions.get(to as usize)) else { continue }; - let (from_bits, to_bits) = (bits(*from_position), bits(*to_position)); - let side = if from_bits <= to_bits { ((from_bits, to_bits), (from, to)) } else { ((to_bits, from_bits), (to, from)) }; - sides.entry(side.0).or_default().push(side.1); + for (from, to) in [(corners[0], corners[1]), (corners[1], corners[2]), (corners[2], corners[0])] { + let (Some(from_position), Some(to_position)) = (self.positions.get(from as usize), self.positions.get(to as usize)) else { continue }; + + let (from_bits, to_bits) = (bits(*from_position), bits(*to_position)); + let (edge, pair) = if from_bits <= to_bits { ((from_bits, to_bits), (from, to)) } else { ((to_bits, from_bits), (to, from)) }; + sides.entry(edge).or_default().push((pair.0, pair.1, material)); + } + } + } + + let uv = |index: u32| self.uvs.get(index as usize).copied(); + for pairs in sides.into_values() { + let Some(&(first_from, first_to, _)) = pairs.first() else { continue }; + + if pairs.iter().any(|&(from, to, _)| (uv(from), uv(to)) != (uv(first_from), uv(first_to))) { + for (from, to, material) in pairs { + if let Some(edges) = seams.get_mut(material) { + edges.push((from.min(to), from.max(to))); } } + } + } + for edges in &mut seams { + edges.sort_unstable(); + edges.dedup(); + } - let uv = |index: u32| self.uvs.get(index as usize).copied(); - let mut seams: Vec<(u32, u32)> = sides - .into_values() - .filter(|pairs| pairs.iter().any(|&(from, to)| (uv(from), uv(to)) != (uv(pairs[0].0), uv(pairs[0].1)))) - .flat_map(|pairs| pairs.into_iter().map(|(from, to)| (from.min(to), from.max(to)))) - .collect(); - seams.sort_unstable(); - seams.dedup(); - seams - }) - .collect() + seams } /// Material of triangle `t`. @@ -487,6 +497,35 @@ mod tests { assert_eq!(hard.uv_seam_edges(), vec![Vec::<(u32, u32)>::new()]); } + #[test] + fn uv_seams_join_positions_with_opposite_signed_zero() { + let mut mesh = quad(); + mesh.positions.push(Vec3::new(-0.0, 0.0, 0.0)); + mesh.normals.push(mesh.normals[0]); + mesh.tangents.push(mesh.tangents[0]); + mesh.uvs.push(Vec2::new(0.9, 0.9)); + mesh.indices[3] = 4; + assert_eq!(mesh.positions[0], mesh.positions[4]); + assert_eq!(mesh.uv_seam_edges(), vec![vec![(0, 2), (2, 4)]]); + } + + #[test] + fn uv_seams_between_materials_belong_to_both_materials() { + let mut mesh = quad(); + mesh.positions.push(mesh.positions[0]); + mesh.normals.push(mesh.normals[0]); + mesh.tangents.push(mesh.tangents[0]); + mesh.uvs.push(Vec2::new(0.9, 0.9)); + mesh.indices[3] = 4; + mesh.materials.push("second".into()); + mesh.submeshes[0].count = 3; + mesh.submeshes.push(SubMesh { name: "second".into(), material: 1, start: 3, count: 3 }); + + assert_eq!(mesh.uv_seam_edges(), vec![vec![(0, 2)], vec![(2, 4)]]); + mesh.uvs[4] = mesh.uvs[0]; + assert_eq!(mesh.uv_seam_edges(), vec![Vec::<(u32, u32)>::new(); 2], "a material boundary alone is not a UV seam"); + } + #[test] fn validate_catches_bad_indices() { let mut m = quad(); diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index a2da0223..2bd2a301 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -1239,13 +1239,28 @@ impl Renderer { self.content_rev += 1; } - /// Upload the high poly bake mode draws when `id` changed; `None` removes it - pub fn set_bake_high(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, mesh: Option<&Mesh>, id: u64) { + /// Upload the high poly when `id` changes; remove it on `None` or when its buffers exceed the device limit + pub fn set_bake_high(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, mesh: Option<&Mesh>, id: u64) -> Result<(), String> { if self.bake_high.as_ref().map(|high| high.id) == mesh.map(|_| id) { - return; + return Ok(()); } - self.bake_high = mesh.map(|mesh| HighPolyGpu { + self.bake_high = None; + self.content_rev += 1; + let Some(mesh) = mesh else { return Ok(()) }; + + // Imported high polys can outgrow a viewport buffer even when the CPU baker accepts them + let limit = device.limits().max_buffer_size; + let buffers = [ + (mesh.positions.len(), std::mem::size_of::()), + (mesh.indices.len(), std::mem::size_of::()), + (mesh.positions.len(), std::mem::size_of::()), + ]; + if mesh.indices.len() > u32::MAX as usize || buffers.iter().any(|&(count, stride)| count.checked_mul(stride).is_none_or(|bytes| bytes as u64 > limit)) { + return Err(format!("High-poly visualization exceeds the GPU buffer limit ({limit} bytes). Use a smaller preview mesh or hide the high poly.")); + } + + self.bake_high = Some(HighPolyGpu { id, vertices: upload_buffer(device, queue, "tc_bake_high_vb", bytemuck::cast_slice(&subdivide::vertices(mesh)), wgpu::BufferUsages::VERTEX), vertex_count: mesh.positions.len(), @@ -1253,7 +1268,7 @@ impl Renderer { index_count: mesh.indices.len() as u32, missed: upload_buffer(device, queue, "tc_bake_high_missed", bytemuck::cast_slice(&vec![0f32; mesh.positions.len()]), wgpu::BufferUsages::VERTEX), }); - self.content_rev += 1; + Ok(()) } /// Which vertices of high poly `id` the bake's rays miss (1) or reach (0); ignored for another mesh or vertex count @@ -2119,14 +2134,15 @@ impl Renderer { }; pass.set_bind_group(2, refraction_source, &[]); let geometry = self.geometry(mesh, frame); - let (highlight, highlight_set) = highlight.unzip(); let bake = frame.shade.bake.filter(|_| uv_set.is_none()); let low_poly_shown = bake.is_none_or(|bake| bake.low_poly); let low_poly_set = bake.and_then(|bake| bake.only_set).or(uv_set); + let (highlight, highlight_set) = highlight.filter(|(_, set)| low_poly_shown && low_poly_set.is_none_or(|only| only == *set)).unzip(); + // The wireframe and the highlight draw the mesh's own edges (displaced in the vertex shader too) let passes = [ (Some(mesh_pipeline).filter(|_| low_poly_shown), &geometry.vb, &geometry.ib, &geometry.draws, low_poly_set, 1), - (wire, &mesh.geo.vb, &mesh.lines, &mesh.line_draws, uv_set, 1), + (wire.filter(|_| low_poly_shown), &mesh.geo.vb, &mesh.lines, &mesh.line_draws, low_poly_set, 1), (highlight, &mesh.geo.vb, &mesh.lines, &mesh.line_draws, highlight_set, HIGHLIGHT_SHIFTS), ]; for (pipeline, vertices, indices, ranges, only_set, instances) in passes { diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index d148a268..84a6e0e2 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -52,6 +52,7 @@ fn setup() -> Option<(Headless, Frame)> { eprintln!("skipping: no GPU adapter"); return None; }; + eprintln!("headless adapter: {:?}", h.device.adapter_info()); let mesh = primitives::material_ball(); h.renderer.set_mesh(&h.device, &h.queue, &mesh, MESH_ID); h.renderer.set_texture_sets(&h.device, &h.queue, &[((256, 256), 0), ((256, 256), 1)], 2); @@ -242,7 +243,7 @@ fn bake_visualization_draws_the_high_poly_and_the_cage() { for position in &mut high.positions { *position += ball_center; } - h.renderer.set_bake_high(&h.device, &h.queue, Some(&high), HIGH_POLY_ID); + h.renderer.set_bake_high(&h.device, &h.queue, Some(&high), HIGH_POLY_ID).unwrap(); let corners: Vec = high.indices.iter().map(|index| (high.positions[*index as usize] - ball_center) * CAGE_SCALE + ball_center).collect(); h.renderer.set_bake_cage(&h.device, &h.queue, 0, Some((&corners, &CAGE_EDGES)), CAGE_TOPOLOGY); let bake = BakeVisualization { high_poly: false, cage: false, uv_seams: false, low_poly: true, only_set: None }; @@ -291,6 +292,55 @@ fn bake_visualization_draws_the_high_poly_and_the_cage() { assert_ne!(caged.px, bigger.px, "the cage grew"); } +#[test] +fn oversized_bake_high_poly_leaves_the_view_renderable() { + let Some((mut headless, frame)) = setup() else { return }; + + let high = primitives::sphere(0.5, 16, 8); + headless.renderer.set_bake_high(&headless.device, &headless.queue, Some(&high), 1).unwrap(); + let limit = headless.device.limits().max_buffer_size; + // The viewport vertex contains position, normal, tangent and UV: twelve f32 values + let vertex_count = limit as usize / (12 * std::mem::size_of::()) + 1; + let oversized = texturecraft_geom::Mesh { positions: vec![Vec3::ZERO; vertex_count], ..Default::default() }; + let error = headless.renderer.set_bake_high(&headless.device, &headless.queue, Some(&oversized), 2).unwrap_err(); + assert!(error.contains("GPU buffer limit"), "{error}"); + + headless.renderer.set_bake_high_missed(&headless.queue, 2, &[]); + let bake = BakeVisualization { high_poly: true, cage: false, uv_seams: false, low_poly: false, only_set: None }; + let cleared = render(&mut headless, &shaded(&frame, |shade| shade.bake = Some(bake)), "bake_oversized_cleared"); + let background = render(&mut headless, &Frame { visible: vec![false, false], ..frame.clone() }, "bake_oversized_background"); + assert_same(&cleared, &background, "an oversized replacement must not leave the previous high poly visible"); + headless.renderer.set_bake_high(&headless.device, &headless.queue, Some(&high), 3).unwrap(); + let recovered = render(&mut headless, &shaded(&frame, |shade| shade.bake = Some(bake)), "bake_oversized_recovered"); + assert_ne!(recovered.px, background.px, "a later valid high poly still draws"); +} + +#[test] +fn bake_low_poly_visibility_also_filters_its_wireframe() { + let Some((mut headless, frame)) = setup() else { return }; + + let bake = BakeVisualization { high_poly: false, cage: false, uv_seams: false, low_poly: false, only_set: None }; + let hidden = shaded(&frame, |shade| { + shade.bake = Some(bake); + shade.wireframe_3d = true; + shade.highlight = Some(texturecraft_render::SetHighlight { set: 0, color: [1.0, 0.5, 0.0] }); + }); + let background = render(&mut headless, &Frame { visible: vec![false, false], ..frame.clone() }, "bake_wire_background"); + let hidden_wire = render(&mut headless, &hidden, "bake_wire_hidden"); + assert_same(&hidden_wire, &background, "hiding the project mesh also hides its wireframe and highlight"); + + for texture_set in [0, 1] { + let selected = shaded(&hidden, |shade| { + shade.bake = Some(BakeVisualization { low_poly: true, only_set: Some(texture_set), ..bake }); + shade.highlight = Some(texturecraft_render::SetHighlight { set: 1 - texture_set, color: [1.0, 0.5, 0.0] }); + }); + let expected = Frame { visible: vec![texture_set == 0, texture_set == 1], ..selected.clone() }; + let actual = render(&mut headless, &selected, &format!("bake_wire_selected_{texture_set}")); + let expected = render(&mut headless, &expected, &format!("bake_wire_expected_{texture_set}")); + assert_same(&actual, &expected, "the wireframe and highlight respect the selected texture set"); + } +} + #[test] fn ortho_background_matches_perspective() { let Some((mut h, frame)) = setup() else { return }; diff --git a/crates/ui-egui/src/bake_scene/reach_link.rs b/crates/ui-egui/src/bake_scene/reach_link.rs index cdf0ca58..185eaf3e 100644 --- a/crates/ui-egui/src/bake_scene/reach_link.rs +++ b/crates/ui-egui/src/bake_scene/reach_link.rs @@ -51,6 +51,11 @@ impl ReachLink { /// Start a new reach when the high poly (sent under `high_id`) or a set's inputs changed, and color the high poly when the reach or the distances changed pub(super) fn sync(&mut self, session: &Session, high_id: u64, only_set: Option, out: &mut SceneSync) { + // Coalesce paint edits while a worker runs; after polling it, the next job captures the latest inputs + if self.job.is_some() { + return; + } + let set_count = session.doc.project.texture_sets.len(); let keys: Vec> = session .doc @@ -101,3 +106,52 @@ impl ReachLink { self.shown = Some(shown); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::{Arc, mpsc}; + use std::time::Duration; + use texturecraft_engine::bake::BakePaintMap; + use texturecraft_engine::core::BakePaintTarget; + use texturecraft_engine::geom::primitives; + + #[test] + fn input_changes_wait_for_the_running_reach_then_use_the_latest_inputs() { + let mut session = Session::new_project("Reach", primitives::plane(1.0, 1), "plane", None, 128).unwrap(); + session.add_high_poly_bytes("high.obj", b"v 0 0.1 0\nv 0.1 0.1 0\nv 0 0.1 0.1\nf 1 2 3\n".to_vec()).unwrap(); + let (sender, receiver) = mpsc::channel(); + let mut link = ReachLink { job: Some(receiver), ..Default::default() }; + + // Hold the original job open while paint and mesh inputs change + for amount in [0.25, 0.5, 0.75] { + session.doc.bake_paint.insert((0, BakePaintTarget::Cage), Arc::new(BakePaintMap::constant(amount))); + let mut out = SceneSync::default(); + link.sync(&session, 2, None, &mut out); + assert!(out.bake_high_missed.is_none()); + } + assert!(sender.send(Vec::new()).is_ok(), "input changes must keep the running job's receiver"); + link.poll(); + + // The finished job is stale: a new job must capture the latest settings before sending colors + session.doc.project.texture_sets[0].bake.high_poly.use_low_as_high = true; + let mut out = SceneSync::default(); + link.sync(&session, 2, None, &mut out); + assert!(link.busy()); + assert!(out.bake_high_missed.is_none()); + + for _ in 0..2000 { + link.poll(); + if !link.busy() { + break; + } + std::thread::sleep(Duration::from_millis(5)); + } + assert!(!link.busy(), "the latest reach must finish"); + link.sync(&session, 2, None, &mut out); + let (high_id, missed) = out.bake_high_missed.unwrap(); + assert_eq!(high_id, 2); + assert_eq!(missed, vec![0.0; session.bake_meshes.merged_high().unwrap().mesh.positions.len()]); + assert!(link.reach[0].is_none(), "the latest setting bakes without a high poly"); + } +} diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 6bce1312..acdac551 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -351,8 +351,14 @@ impl CallbackTrait for ViewCallback { for (texture_set, size, bytes) in &sync.aux { gpu.renderer.upload_aux(device, queue, *texture_set, *size, bytes); } - if let Some((high, id)) = &sync.bake_high { - gpu.renderer.set_bake_high(device, queue, high.as_deref(), *id); + if let Some((high, id)) = &sync.bake_high + && let Err(error) = gpu.renderer.set_bake_high(device, queue, high.as_deref(), *id) + { + let mut reports = gpu.reports.lock().unwrap_or_else(PoisonError::into_inner); + if reports.errors.len() < MAX_GPU_ERRORS && !reports.errors.contains(&error) { + reports.errors.push(error); + } + gpu.ctx.request_repaint(); } if let Some((id, missed)) = &sync.bake_high_missed { gpu.renderer.set_bake_high_missed(queue, *id, missed); diff --git a/crates/ui-egui/tests/ui_snapshots.rs b/crates/ui-egui/tests/ui_snapshots.rs index 4b40d7e9..11e62a48 100644 --- a/crates/ui-egui/tests/ui_snapshots.rs +++ b/crates/ui-egui/tests/ui_snapshots.rs @@ -77,7 +77,7 @@ fn window_snapshots() { save(&mut harness, &output_dir, "bake-settings"); harness.state_mut().session.add_high_poly_bytes("High Poly.obj", b"v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n".to_vec()).unwrap(); save(&mut harness, &output_dir, "bake-settings-high-poly"); - harness.get_by_label("Cage").scroll_to_me(); + harness.get_by_role_and_label(egui::Role::ComboBox, "Cage Type").scroll_to_me(); save(&mut harness, &output_dir, "bake-settings-cage-skew"); harness.get_by_label("Edge Contrast").scroll_to_me(); save(&mut harness, &output_dir, "bake-settings-skew");