diff --git a/docs/bake.feature b/docs/bake.feature index 6adcdcfb..46b2035d 100644 --- a/docs/bake.feature +++ b/docs/bake.feature @@ -248,14 +248,21 @@ Feature: Light baked by raea: bounce light into lightmaps on raea's own ray trac 1.216 -> 1.030, tower_base 1.183 -> 1.040, books_0 1.199 -> 1.072. Neither the sun's subtraction nor the PNGs' 16-bit clipping (0.92-1.05 of the raw floats) nor the sheets' gaps (a fixture of sheets 0.05-5 mm apart: both black at 0.2 mm) explain it. BK0's own references were baked the same way - And against the honest reference, measured 11-23 with jittered starts: Cycles' diffuse-only atelier with adaptive - sampling off, unsmoothed, unclamped, 512 samples (12m49s against 3m55s with it on, the same scene), raw floats, by - each mesh's total light, zeros counted: 3 of 198 meshes over 1/3 stop (bench_strips 0.581, meet_sheet_0 1.292, - lamp_shade_in 1.272) and 21 over 1/10, total light 0.997. Cycles' own second seed (smoothed) is 1 of 198. The - trace 1024 samples, 4 bounces, 17.9 s, peak 1.0 GB. Known and by design: a partly buried texel is averaged over its - open part, where Cycles averages all of it, so bench_stock (1.247), dt_ledge (1.185) and bench_mat (1.171) sit - above it; averaged over the whole footprint they were 0.960, 0.997 and 1.006. Open: bench_strips, and the setup, - 252.9 s in a debug build (70-200 before the starts): the 16-ray probe of each texel's corners + And against the honest reference, measured 11-23 with jittered starts, a release build: Cycles' diffuse-only atelier + with adaptive sampling off, unsmoothed, unclamped, 512 samples, raw floats (12m49s against 3m55s with it on, the + same scene), by each mesh's total light, zeros counted; the trace 1024 samples, 4 bounces. (Cycles' own second + seed, smoothed, is over 1/3 stop on 1 of 198.) The atelier's own bakes were made with adaptive sampling on + | a texel's samples start from | over 1/3 stop | over 1/10 | total light | + | the open part of it (what the bake does) | 3 of 198 | 21 | 0.997 | + | all of it, buried points too (as Cycles averages) | 1 of 198 | 10 | 0.993 | + They differ where a texel is partly buried: Cycles' average counts the dark inside, so it bakes such a texel darker + than what can be seen of it; the bake keeps the open part. The one left over 1/3 in both is bench_strips (0.578, + thin strips stacked, 9 mm), not explained; the two others in the open column are meet_sheet_0 (1.292) and + lamp_shade_in (1.272). Setup in release: the atelier's own .glb 12.8 s to read and lay out, 2 992 000 texels on + its own UVs, and 16.7 s to trace 1024 samples, peak 1.5 GB. On the atlas bake of BK2 (64 texels a metre): the table + set 0.85 s with its texels, 0.84 s to trace 512 samples (1.5 M texels), and the atelier 19.9 s and 8.5 s (4.3 M texels). + Other sessions were rendering. The bar is 60 s on the table set, all in: not yet timed with reading, + cleaning (BK4) and writing in it. The 16-ray probe on the CPU is fast enough there, and 20 of the atelier's 28 s And the trace's own indirect clamp is gone: at 3 it kept 0.869 of Cycles' light at the same clamp where unclamped it keeps 0.994 (Cycles' own clamp keeps 0.963), so it was not Cycles' clamp. A bake is unclamped And a ray straight up gave a non-finite value on Metal (a panorama's longitude, atan2(0, 0)), one texel of the diff --git a/systems/lightbake/src/own.rs b/systems/lightbake/src/own.rs index 573671c3..85d04b86 100644 --- a/systems/lightbake/src/own.rs +++ b/systems/lightbake/src/own.rs @@ -121,7 +121,7 @@ fn texture(m: &mut Model, image: &gltf::Image, blob: &[u8], images: &mut HashMap /// A mesh's texels on its second UVs at `side` by `side`, each once (the first triangle over its centre); `offset` as /// Texels' own. -pub fn texels(m: &Model, mesh: &Mesh, world: &TriMesh, side: u32, offset: f32) -> Vec { +pub fn texels(m: &Model, mesh: &Mesh, world: &TriMesh, side: u32, offset: f32, whole: bool) -> Vec { let mut taken = vec![false; (side * side) as usize]; let mut out = vec![]; for tri in &m.triangles[mesh.triangles.clone()] { @@ -161,7 +161,7 @@ pub fn texels(m: &Model, mesh: &Mesh, world: &TriMesh, side: u32, offset: f32) - } } out.into_par_iter().enumerate().map(|(k, (a, n, pos))| { - let (origin, open) = starts(world, pos, Vec3::from(a.origin), n, a.sides.map(Vec3::from), offset, k as u32); + let (origin, open) = starts(world, pos, Vec3::from(a.origin), n, a.sides.map(Vec3::from), offset, k as u32, whole); At { origin, open, ..a } }).collect() } diff --git a/systems/lightbake/src/own/tests.rs b/systems/lightbake/src/own/tests.rs index e83369a5..0eef4d40 100644 --- a/systems/lightbake/src/own/tests.rs +++ b/systems/lightbake/src/own/tests.rs @@ -26,7 +26,7 @@ fn texels_lie_where_the_second_uvs_put_them() { ..Model::default() }; let world = parry3d::shape::TriMesh::new(m.positions.iter().map(|p| Vec3::from(*p)).collect(), m.triangles.clone()).unwrap(); - let t = texels(&m, &m.meshes[0], &world, 16, 1e-4); + let t = texels(&m, &m.meshes[0], &world, 16, 1e-4, false); assert_eq!(t.len(), 8 * 8, "a quarter of 16 x 16, each once"); for a in &t { let want = [(a.x as f32 + 0.5) / 8.0 * 2.0, 1.0 - (a.y as f32 + 0.5 - 8.0) / 8.0]; @@ -56,7 +56,7 @@ fn smooth(side: usize, map: &mut Vec>, passes: usize) { /// .blend held, and each map against Cycles' (compare.rs's map_stops: the mean distance in stops and the signed one, /// over the texels both cover, Cycles' clipped ones out). RAEA_BAKE_DIR holds set.glb, set.lightmaps.json and /// lightmaps/, RAEA_BAKE_SKY the panorama; _RUNS ("512x4": samples x bounces, each in turn), _STRENGTH, _ROTATION, _SMOOTH, _SUN ("x,y,z,r,g,b") -/// _ONLY (mesh names), _DUMP (a folder: each map's luminances, raea's and Cycles', and the texel's place, as f32 threes) +/// _ONLY (mesh names), _WHOLE (a texel's buried part counts, as in Cycles), _DUMP (a folder: each map's luminances, raea's and Cycles', and the texel's place, as f32 threes) /// and _OUT (each run's table, OUT-RUN.json). #[test] #[ignore] @@ -82,7 +82,7 @@ fn against_cycles() { } let mesh = m.meshes.iter().find(|x| x.name == name).unwrap_or_else(|| panic!("no mesh {name}")); let side = l["size"].as_u64().unwrap() as u32; - maps.push((name.to_string(), mesh, side, l["lightmap_exposure"].as_f64().unwrap() as f32 / 683.0, texels(&m, mesh, &world, side, offset))); + maps.push((name.to_string(), mesh, side, l["lightmap_exposure"].as_f64().unwrap() as f32 / 683.0, texels(&m, mesh, &world, side, offset, std::env::var("RAEA_BAKE_WHOLE").is_ok()))); } let origin: Vec<[f32; 3]> = maps.iter().flat_map(|x| x.4.iter().map(|a| a.origin)).collect(); let sides: Vec<[[f32; 3]; 2]> = maps.iter().flat_map(|x| x.4.iter().map(|a| a.sides)).collect(); diff --git a/systems/lightbake/src/texels.rs b/systems/lightbake/src/texels.rs index bc42e616..848843cc 100644 --- a/systems/lightbake/src/texels.rs +++ b/systems/lightbake/src/texels.rs @@ -63,7 +63,7 @@ pub fn texels(glb: &[u8], req: &Value) -> Result { }).collect(); let (origin, open) = layout.texels.par_iter().zip(&sides).enumerate().map(|(i, (t, s))| { let tri = mesh.indices()[t.tri as usize].map(|v| mesh.vertices()[v as usize]); - starts(&mesh, tri, Vec3::from(t.p), Vec3::from(t.n), s.map(Vec3::from), offset, i as u32) + starts(&mesh, tri, Vec3::from(t.p), Vec3::from(t.n), s.map(Vec3::from), offset, i as u32, false) }).unzip(); Ok(Texels { layout, origin, sides, open, offset, mesh }) } @@ -85,12 +85,16 @@ pub(crate) fn inside(mesh: &TriMesh, o: Vec3, n: Vec3, seed: u32) -> bool { /// Where a texel's samples start: its point off the surface, and which of the 4 x 4 points of its footprint are open /// (on its triangle, clear of its edges, not inside other geometry). A texel half buried keeps only its open half: -/// what it's averaged over is what can be seen. A corner and the centre that agree stand for the rest. -pub(crate) fn starts(mesh: &TriMesh, tri: [Vec3; 3], p: Vec3, n: Vec3, [u, v]: [Vec3; 2], offset: f32, seed: u32) -> ([f32; 3], u32) { +/// what it's averaged over is what can be seen (`whole` takes the buried part too, as Cycles does: for comparing). A +/// corner and the centre that agree stand for the rest. +pub(crate) fn starts(mesh: &TriMesh, tri: [Vec3; 3], p: Vec3, n: Vec3, [u, v]: [Vec3; 2], offset: f32, seed: u32, whole: bool) -> ([f32; 3], u32) { let gap = 4.0 * offset; let centre = off_edges(tri, p, gap, 0.5 * u.length().max(v.length())) + n * offset; let at = |c: u32| p + u * ((c % 4) as f32 / 4.0 - 0.375) + v * ((c / 4) as f32 / 4.0 - 0.375); let cells: Vec = (0..16).filter(|&c| off_edges(tri, at(c), gap, f32::MAX).distance(at(c)) < 0.5 * offset).collect(); + if whole && !cells.is_empty() { + return ((p + n * offset).to_array(), cells.iter().fold(0, |m, c| m | 1 << c)); + } let hid = inside(mesh, centre, n, seed); let buried = |c: u32| inside(mesh, at(c) + n * offset, n, seed ^ (c + 1).wrapping_mul(0x9e37_79b9)); if cells.is_empty() { diff --git a/systems/lightbake/src/trace_tests.rs b/systems/lightbake/src/trace_tests.rs index bd8a5e6b..d5974b87 100644 --- a/systems/lightbake/src/trace_tests.rs +++ b/systems/lightbake/src/trace_tests.rs @@ -98,7 +98,7 @@ fn half_covered(on_it: &[crate::texels::tests::Tri], samples_at_centre: bool) -> let floor = [[-40.0, 0.0, -40.0], [-40.0, 0.0, 80.0], [80.0, 0.0, -40.0]]; let mesh = parry3d::shape::TriMesh::new(std::iter::once(floor).chain(on_it.iter().copied()).flatten().map(Vec3::from).collect(), (0..on_it.len() as u32 + 1).map(|k| [3 * k, 3 * k + 1, 3 * k + 2]).collect()).unwrap(); let sides = [[0.4, 0.0, 0.0], [0.0, 0.0, 0.4]]; - let (origin, open) = starts(&mesh, floor.map(Vec3::from), Vec3::new(0.1, 0.0, 0.0), Vec3::Y, sides.map(Vec3::from), 1e-4, 7); + let (origin, open) = starts(&mesh, floor.map(Vec3::from), Vec3::new(0.1, 0.0, 0.0), Vec3::Y, sides.map(Vec3::from), 1e-4, 7, false); let (p, i, s) = world(&mesh, &vec![Surface::default(); mesh.indices().len()]); let st = Starts { origin: &[origin], normal: &[[0.0, 1.0, 0.0]], sides: &[sides], open: &[if samples_at_centre { CENTRE } else { open }], offset: 1e-4 }; let l = trace(&World { positions: &p, triangles: &i, surfaces: &s, uvs: &[], textures: &[] }, &st, &Settings { samples: 4096, ..Settings::default() }, &mut |_, _| {}).unwrap();