From 285d99bd49c37fe5c9727a9c0fe45e3519eb1338 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Mon, 23 Nov 2026 20:24:27 -0800 Subject: [PATCH] BK3: what the biased meshes were (footprint and the reference's adaptive sampling) 46 meshes were counted biased by the mean log error on smoothed maps. By each mesh's total light, unsmoothed and with zeros counted, 8 of 198 are over 1/3 stop. Four are the trace's: it starts every ray at the texel's centre where Cycles jitters over the texel (traced 8x8 points a texel, the same texels, they reach 0.96-1.07 of Cycles). Four are the reference's: Cycles' adaptive sampling stops dark pockets early, so it bakes them up to 2x too dark (with it off, 1.01-1.07). Corrects BK3's text, which said all 46 were a trace error. The test run can now trace fine points a texel (RAEA_BAKE_FINE), pick meshes (RAEA_BAKE_ONLY) and keeps zero texels in its dump. Not @done. --- docs/bake.feature | 24 +++++++++++++++++++----- systems/lightbake/src/own/tests.rs | 28 +++++++++++++++++++++------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/docs/bake.feature b/docs/bake.feature index 3eafd61a..daad0ecb 100644 --- a/docs/bake.feature +++ b/docs/bake.feature @@ -226,11 +226,25 @@ Feature: Light baked by raea: bounce light into lightmaps on raea's own ray trac | trace / Cycles, real materials, no clamp | 0.389 | -0.336 | 0.857 | 157 | 140 | | trace / the atelier's own bake (clamp 3) | 0.384 | -0.319 | 0.889 | 155 | 135 | And what they say: on matched materials the trace's total light is within 0.6% of Cycles' and the floor, deck, - walls and door are within 0.02 stops at the median. 46 meshes are biased where Cycles against itself has 2, - most of them small, thin or stacked: an error in the trace or where its rays start, not yet found. 14% of the - light is the metals' and speculars' bounces, so GGX and F82 are needed (to be swapped for cycles-port's when - that is on main). Time: the trace 17-25 s, 10 s at 256 samples and 119 s at 4096, peak 1.2 GB, setup 70-200 s - in a debug build; Cycles' diffuse-only bake 5m33s, 2.7 GB, the atelier's own 5m36s, 3.4 GB for its whole script + walls and door are within 0.02 stops at the median. 14% of the light is the metals' and speculars' bounces, so + GGX and F82 are needed (to be swapped for cycles-port's when that is on main). The 46 "biased" meshes are not an + error of the trace's, except 4 of them: that count is by mean log error on smoothed maps and it drops texels + where one side is 0. By each mesh's total light, zeros counted and no smoothing, 8 of 198 are over 1/3 stop (Cycles + against itself, smoothed: 1, and 95% of meshes within 0.05). Time: the trace 17-25 s, 10 s at 256 samples and + 119 s at 4096, peak 1.2 GB, setup 70-200 s in a debug build; Cycles' diffuse-only bake 5m33s, 2.7 GB, the + atelier's own 5m36s, 3.4 GB for its whole script + And the 8, by the trace's light over Cycles': 4 are the footprint. The trace starts every ray at the texel's + centre, and Cycles jitters its samples over the texel; on 30 mm texels a sheet's 6 mm ledge is all or nothing. + Traced 8x8 points a texel and averaged, over the same texels: bench_stock 0.333 -> 0.960, bench_mat 1.333 -> + 1.006, dt_ledge 1.291 -> 0.997, dt_rule 0.765 -> 1.068; bench_strips only 0.616 -> 0.679. To be built: jittered + starts in the kernel, kept on the triangle, with the samples that start inside other geometry left out of + the mean (a partly buried texel is half seen) + And the other 4 are the reference's: Cycles' adaptive sampling (on by default, threshold 0.01; lightbake.py doesn't turn + it off) stops a dark pocket's pixel before it has seen the light, so a cup or a shelf's back bakes too dark. With + it off (1024 samples, the same meshes, raw floats): mug_coffee 1.94 -> 1.012, books_5 1.279 -> 1.021, meet_tea + 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 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/tests.rs b/systems/lightbake/src/own/tests.rs index 6e49bf6e..55eafb11 100644 --- a/systems/lightbake/src/own/tests.rs +++ b/systems/lightbake/src/own/tests.rs @@ -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") -/// _DUMP (a folder: each map's luminances, raea's and Cycles', and the texel's place, as f32 threes) +/// _ONLY (mesh names), _FINE (points a side of each texel), _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] @@ -73,12 +73,17 @@ fn against_cycles() { let v: Vec = s.split(',').map(|x| x.parse().unwrap()).collect(); Sun { dir: [v[0], v[1], v[2]], irradiance: [v[3], v[4], v[5]], half_angle: 0.00459 } }); + // each texel traced as fine x fine points of its footprint, then averaged (what Cycles' jittered samples see) + let fine = number("RAEA_BAKE_FINE", 1.0) as u32; let mut maps = vec![]; for l in listed["meshes"].as_array().unwrap() { let name = l["mesh"].as_str().unwrap(); + if std::env::var("RAEA_BAKE_ONLY").is_ok_and(|only| !only.split(',').any(|o| o == name)) { + continue; + } 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, side, offset))); + maps.push((name.to_string(), mesh, side, l["lightmap_exposure"].as_f64().unwrap() as f32 / 683.0, texels(&m, mesh, side * fine, offset))); } let origin: Vec<[f32; 3]> = maps.iter().flat_map(|x| x.4.iter().map(|a| a.origin)).collect(); let normal: Vec<[f32; 3]> = maps.iter().flat_map(|x| x.4.iter().map(|a| a.normal)).collect(); @@ -97,12 +102,20 @@ fn against_cycles() { println!("{run}: traced {} samples, {} bounces in {seconds:.1} s", s.samples, s.bounces); let passes = number("RAEA_BAKE_SMOOTH", 2.0) as usize; let (mut first, mut rows) = (0, vec![]); - let (mut abs, mut signed, mut n) = (0.0f64, 0.0f64, 0usize); + let (mut abs, mut signed, mut n, mut zeros) = (0.0f64, 0.0f64, 0usize, 0usize); for (name, mesh, side, scale, at) in &maps { let side = *side as usize; let mut mine = vec![None; side * side]; - for (k, a) in at.iter().enumerate().filter(|(k, _)| !hid[first + k]) { - mine[a.y as usize * side + a.x as usize] = Some(light[first + k]); + let (mut got, mut open) = (vec![([0.0f32; 3], 0u32); side * side], vec![false; side * side]); + for (k, a) in at.iter().enumerate() { + let i = (a.y / fine) as usize * side + (a.x / fine) as usize; + // a point inside other geometry is dark, and counts: a texel half buried is half lit + let v = if hid[first + k] { [0.0; 3] } else { light[first + k] }; + open[i] |= !hid[first + k]; + got[i] = ([got[i].0[0] + v[0], got[i].0[1] + v[1], got[i].0[2] + v[2]], got[i].1 + 1); + } + for i in (0..side * side).filter(|&i| open[i]) { + mine[i] = Some(got[i].0.map(|c| c / got[i].1 as f32)); } first += at.len(); smooth(side, &mut mine, passes); @@ -115,10 +128,11 @@ fn against_cycles() { let q = theirs.get_pixel((i % side) as u32, (i / side) as u32).0; let (Some(v), false) = (v, q.contains(&u16::MAX)) else { continue }; let (x, y) = (lum(*v), lum(q.map(|c| c as f32 / 65535.0 * scale))); + pairs.extend([x, y, i as f32]); if x <= 0.0 || y <= 0.0 { + zeros += 1; continue; } - pairs.extend([x, y, i as f32]); let st = (x / y).log2() as f64; (a, b, k) = (a + st.abs(), b + st, k + 1); } @@ -130,7 +144,7 @@ fn against_cycles() { rows.push(serde_json::json!({ "mesh": name, "side": side, "texels": k, "mean_stops": a / k.max(1) as f64, "bias_stops": b / k.max(1) as f64, "textured": s0.texture.is_some(), "albedo": s0.albedo })); } let over = rows.iter().filter(|r| r["texels"].as_u64().unwrap() > 0 && r["mean_stops"].as_f64().unwrap() > 1.0 / 3.0).count(); - println!("{n} texels compared: mean {:.3} stops, bias {:+.3}; {over} of {} meshes over 1/3 stop", abs / n as f64, signed / n as f64, rows.len()); + println!("{n} texels compared ({zeros} left out, one side exactly 0): mean {:.3} stops, bias {:+.3}; {over} of {} meshes over 1/3 stop", abs / n as f64, signed / n as f64, rows.len()); if let Ok(out) = std::env::var("RAEA_BAKE_OUT") { std::fs::write(format!("{out}-{run}.json"), serde_json::json!({ "samples": s.samples, "bounces": s.bounces, "trace_seconds": seconds, "mean_stops": abs / n as f64, "bias_stops": signed / n as f64, "meshes": rows }).to_string()).unwrap(); }