Skip to main content

roxlap_scene/
chunks.rs

1//! Sparse chunk storage helpers.
2//!
3//! A grid's [`Grid::chunks`] map holds populated chunks keyed by
4//! their `(chx, chy, chz)` index. A missing entry is an implicit
5//! all-air chunk; this module provides the constructor for fresh
6//! all-air chunks plus the `chunk` / `chunk_mut` / `ensure_chunk`
7//! lookup API.
8//!
9//! [`Grid::chunks`]: crate::Grid::chunks
10
11use glam::IVec3;
12use roxlap_formats::edit::{set_spans, Vspan};
13use roxlap_formats::vxl::Vxl;
14
15use crate::{Grid, CHUNK_SIZE_XY, CHUNK_SIZE_Z};
16
17/// Bytes of edit-pool headroom reserved per chunk on creation.
18/// 256 bytes/column × 128² columns ≈ 4 MiB; a generous budget for
19/// runtime edits within a single chunk before [`voxalloc`] starts
20/// returning out-of-space. Tunable later if memory becomes an
21/// issue.
22///
23/// [`voxalloc`]: roxlap_formats::vxl::Vxl::voxalloc
24const CHUNK_EDIT_HEADROOM_PER_COLUMN: usize = 256;
25
26/// Construct a fresh all-air [`Vxl`] sized for one chunk
27/// (`vsid = CHUNK_SIZE_XY`).
28///
29/// Strategy mirrors `roxlap_cavegen::pack_dense_grid_to_vxl`: seed
30/// each column with one solid voxel at z=0 + implicit-solid below
31/// (the voxlap "loadnul" shape), then carve the entire z range to
32/// air via [`set_spans`]. Finishes with [`Vxl::reserve_edit_capacity`]
33/// so subsequent runtime edits don't need a separate upgrade pass.
34///
35/// This is the canonical empty-chunk constructor — every code
36/// path that materialises a sparse chunk goes through it (see
37/// [`Grid::ensure_chunk`]).
38pub(crate) fn empty_chunk_vxl() -> Vxl {
39    let vsid = CHUNK_SIZE_XY;
40    let n_cols = (vsid as usize) * (vsid as usize);
41
42    // 1. Seed: every column = 4-byte slab header + 1 colour. Colour
43    //    is irrelevant — the whole column gets carved below.
44    let mut data: Vec<u8> = Vec::with_capacity(n_cols * 8);
45    let mut column_offset: Vec<u32> = Vec::with_capacity(n_cols + 1);
46    for _ in 0..n_cols {
47        column_offset.push(u32::try_from(data.len()).expect("offset fits in u32"));
48        data.extend_from_slice(&[0, 0, 0, 0]); // header
49        data.extend_from_slice(&[0, 0, 0, 0]); // 1 placeholder colour
50    }
51    column_offset.push(u32::try_from(data.len()).expect("offset fits in u32"));
52
53    let mut vxl = Vxl {
54        vsid,
55        // Per-grid placement lives on `GridTransform`; the per-chunk
56        // Vxl's intrinsic camera fields are unused at this layer.
57        ipo: [0.0; 3],
58        ist: [1.0, 0.0, 0.0],
59        ihe: [0.0, 0.0, 1.0],
60        ifo: [0.0, 1.0, 0.0],
61        data: data.into_boxed_slice(),
62        column_offset: column_offset.into_boxed_slice(),
63        mip_base_offsets: Box::new([0, n_cols + 1]),
64        vbit: Box::new([]),
65        vbiti: 0,
66    };
67    vxl.reserve_edit_capacity(n_cols * CHUNK_EDIT_HEADROOM_PER_COLUMN);
68
69    // 2. Carve [0, 255] in every column to make it all-air.
70    //    `Vspan.z1` is inclusive per voxlap's vspans convention.
71    let mut spans: Vec<Vspan> = Vec::with_capacity(n_cols);
72    for y in 0..vsid {
73        for x in 0..vsid {
74            spans.push(Vspan {
75                x,
76                y,
77                z0: 0,
78                z1: u8::MAX,
79            });
80        }
81    }
82    set_spans(&mut vxl, &spans, None);
83
84    vxl
85}
86
87/// True if voxel `(x, y, z)` is solid within one chunk's [`Vxl`] —
88/// i.e. covered by a solid run in column `(x, y)`. `(x, y)` are
89/// `< CHUNK_SIZE_XY`, `z < CHUNK_SIZE_Z`.
90///
91/// PF.6 — walks the slab chain in place with an early-out at `z`,
92/// mirroring `expandrle`'s run derivation ONE RUN AT A TIME instead of
93/// heap-allocating a 516-int buffer and decoding the whole column per
94/// query (this sits on every CPU shadow-ray step and every
95/// `Scene::raycast` step). Run k spans `[top_k, bot_k)` where `top_0 =
96/// slab[1]`, each following non-degenerate slab header closes the
97/// previous run at `slab[v+3]` and opens the next at `slab[v+1]`, and
98/// the final run extends to the column bottom — exactly the list
99/// `expandrle` would emit.
100#[allow(clippy::cast_possible_wrap)]
101pub(crate) fn vxl_voxel_solid(vxl: &Vxl, x: u32, y: u32, z: u32) -> bool {
102    let idx = (y * vxl.vsid + x) as usize;
103    let slab = vxl.column_data(idx);
104    let z = z as i32;
105    let mut top = i32::from(slab[1]);
106    let mut v = 0usize;
107    while slab[v] != 0 {
108        v += usize::from(slab[v]) * 4;
109        if slab[v + 3] >= slab[v + 1] {
110            // Degenerate slab (no air gap above): merges into the
111            // current run — same skip `expandrle` takes.
112            continue;
113        }
114        let bot = i32::from(slab[v + 3]);
115        if z < bot {
116            // z is above this run's bottom: solid iff inside the run
117            // (below `top` would be the preceding air gap).
118            return z >= top;
119        }
120        top = i32::from(slab[v + 1]);
121    }
122    // Last run extends to the column bottom (bedrock).
123    z >= top
124}
125
126/// PF.6 — chunk-cached solid sampler for DDA marches. Consecutive steps
127/// almost always stay inside one chunk; caching the last `chunks`
128/// HashMap probe turns the per-step cost into one compare + the in-place
129/// slab walk. `chunk_at` exposes presence so callers can skip absent
130/// (all-air) chunks wholesale.
131pub(crate) struct SolidSampler<'a> {
132    grid: &'a Grid,
133    cached_idx: IVec3,
134    cached: Option<&'a Vxl>,
135    primed: bool,
136}
137
138impl<'a> SolidSampler<'a> {
139    /// The chunk holding grid-local voxel-space chunk index `chunk_idx`,
140    /// through the one-entry cache.
141    pub(crate) fn chunk_at(&mut self, chunk_idx: IVec3) -> Option<&'a Vxl> {
142        if !self.primed || chunk_idx != self.cached_idx {
143            self.cached = self.grid.chunk(chunk_idx);
144            self.cached_idx = chunk_idx;
145            self.primed = true;
146        }
147        self.cached
148    }
149}
150
151/// What [`Grid::bake`] / [`Grid::bake_bbox`] write into the per-voxel
152/// brightness byte (QE-B6 — replaces the voxlap magic-`u32` lightmode).
153#[derive(Debug, Clone, Copy, PartialEq)]
154pub enum BakeMode {
155    /// Directional estnorm shading (voxlap lightmode 1) — the classic
156    /// standalone look for hosts that don't run a dynamic light rig.
157    Directional,
158    /// Ambient occlusion (lightmode 3): crevices and inner corners
159    /// darken. The right bake *under* a runtime `LightRig`, which
160    /// reads the byte as its ambient/AO fill. Carries its tuning
161    /// parameters — unlike the deprecated `bake_lightmode_bbox`,
162    /// [`Grid::bake_bbox`] honours them.
163    AmbientOcclusion(roxlap_core::AoParams),
164    /// EV.3 — voxlap's point-light bake (lightmode 2): a **dim**
165    /// directional base (about a quarter of
166    /// [`Directional`](Self::Directional)'s) plus a cube-law
167    /// Lambertian pool around every light in
168    /// [`Grid::bake_lights`] — glowing crystals, torches, lava.
169    /// [`Grid::bake_bbox`] (the carve-relight primitive) picks the
170    /// grid's lights up automatically, so incremental edits keep
171    /// their glow pools. The dark base is the point: light pools
172    /// read against gloom.
173    PointLights,
174}
175
176impl BakeMode {
177    /// The voxlap wire value the bake internals consume.
178    fn lightmode(self) -> u32 {
179        match self {
180            Self::Directional => 1,
181            Self::AmbientOcclusion(_) => 3,
182            Self::PointLights => 2,
183        }
184    }
185
186    /// The AO parameters (defaults for the non-AO modes, where the
187    /// bake ignores them).
188    fn ao(self) -> roxlap_core::AoParams {
189        match self {
190            Self::Directional | Self::PointLights => roxlap_core::AoParams::default(),
191            Self::AmbientOcclusion(ao) => ao,
192        }
193    }
194}
195
196/// EV.3 — one baked point light (voxlap's `lightsrc[]`), consumed by
197/// [`BakeMode::PointLights`] from [`Grid::bake_lights`]. Baked, not
198/// dynamic: its Lambertian pool is written into the per-voxel
199/// brightness bytes by [`Grid::bake`] / [`Grid::bake_bbox`] and costs
200/// nothing at render time (both backends just read the byte). For a
201/// flickering/moving light use the runtime `LightRig` instead.
202#[derive(Debug, Clone, Copy, PartialEq)]
203pub struct BakeLight {
204    /// Grid-local voxel-space position (the same frame as
205    /// [`Grid::set_voxel`]).
206    pub pos: glam::Vec3,
207    /// Hard cutoff radius in voxels — the cube-law contribution fades
208    /// to exactly zero here.
209    pub radius: f32,
210    /// Voxlap brightness scale: the byte gain at distance `d` is
211    /// roughly `strength · cosθ / d²` (cube-law falloff × Lambert), on
212    /// the 0–255 brightness-byte scale whose neutral is 128. A wall 5
213    /// voxels away therefore gains about `strength / 25` — `2000` is a
214    /// solid reading-torch, `8000` floods a small cavern.
215    pub strength: f32,
216}
217
218impl BakeLight {
219    /// This light rebased into `chunk_idx`'s chunk-local frame as the
220    /// bake-internal [`roxlap_core::LightSrc`], or `None` when its
221    /// influence sphere misses the chunk's voxel box entirely.
222    #[allow(clippy::cast_possible_wrap, clippy::cast_precision_loss)]
223    fn chunk_local(&self, chunk_idx: IVec3) -> Option<roxlap_core::LightSrc> {
224        let base = glam::Vec3::new(
225            (chunk_idx.x * CHUNK_SIZE_XY as i32) as f32,
226            (chunk_idx.y * CHUNK_SIZE_XY as i32) as f32,
227            (chunk_idx.z * CHUNK_SIZE_Z as i32) as f32,
228        );
229        let local = self.pos - base;
230        // Sphere-vs-chunk-AABB cull: nearest box point to the light.
231        let ext = glam::Vec3::new(
232            CHUNK_SIZE_XY as f32,
233            CHUNK_SIZE_XY as f32,
234            CHUNK_SIZE_Z as f32,
235        );
236        let nearest = local.clamp(glam::Vec3::ZERO, ext);
237        if (nearest - local).length_squared() > self.radius * self.radius {
238            return None;
239        }
240        Some(roxlap_core::LightSrc {
241            pos: [local.x, local.y, local.z],
242            r2: self.radius * self.radius,
243            sc: self.strength,
244        })
245    }
246}
247
248impl Grid {
249    /// True if the grid-local integer voxel `voxel` is solid (inside a
250    /// solid run of its chunk). An implicit-air or absent chunk reads
251    /// as `false`. `voxel` is a grid-local voxel coordinate
252    /// (pre-transform) — get one from a world point via
253    /// [`crate::world_to_grid_local`] + [`crate::voxel_global`]. Useful
254    /// for picking, collision, and world queries.
255    #[must_use]
256    pub fn voxel_solid(&self, voxel: IVec3) -> bool {
257        let (chunk_idx, in_chunk) = crate::voxel_split(voxel);
258        match self.chunk(chunk_idx) {
259            Some(vxl) => vxl_voxel_solid(vxl, in_chunk.x, in_chunk.y, in_chunk.z),
260            None => false,
261        }
262    }
263
264    /// AU.0 — the chunk-local half of [`Self::voxel_solid`], for
265    /// external DDA marches with strong chunk locality: split the
266    /// voxel with [`crate::voxel_split`], borrow the chunk once via
267    /// [`Self::chunk`], and test cells against the borrow — one
268    /// HashMap probe per chunk instead of per voxel. `in_chunk` is
269    /// the chunk-local coordinate `voxel_split` returned.
270    #[must_use]
271    pub fn chunk_voxel_solid(vxl: &Vxl, in_chunk: glam::UVec3) -> bool {
272        vxl_voxel_solid(vxl, in_chunk.x, in_chunk.y, in_chunk.z)
273    }
274
275    /// PF.6 — a chunk-cached [`SolidSampler`] over this grid, for DDA
276    /// marches that issue many [`Self::voxel_solid`]-style queries with
277    /// strong chunk locality (shadow rays, `Scene::raycast`).
278    pub(crate) fn solid_sampler(&self) -> SolidSampler<'_> {
279        SolidSampler {
280            grid: self,
281            cached_idx: IVec3::ZERO,
282            cached: None,
283            primed: false,
284        }
285    }
286
287    /// Packed BGRA colour of the textured voxel at grid-local `voxel`,
288    /// or `None` for air / untextured cells. Thin wrapper over
289    /// [`roxlap_formats::vxl::Vxl::voxel_color`] after the chunk split —
290    /// the colour-inspection companion to [`Self::voxel_solid`]. Use it
291    /// to read back what a pick / raycast hit looks like.
292    #[must_use]
293    pub fn voxel_color(&self, voxel: IVec3) -> Option<roxlap_formats::color::VoxColor> {
294        let (chunk_idx, in_chunk) = crate::voxel_split(voxel);
295        self.chunk(chunk_idx)?
296            .voxel_color(in_chunk.x, in_chunk.y, in_chunk.z)
297    }
298
299    /// Borrow the chunk at `chunk_idx` if it has been materialised.
300    /// `None` means the chunk is implicitly all-air.
301    #[must_use]
302    pub fn chunk(&self, chunk_idx: IVec3) -> Option<&Vxl> {
303        self.chunks.get(&chunk_idx)
304    }
305
306    /// Bake per-voxel lighting (voxlap `updatevxl`/`estnorm` shading)
307    /// into every materialised chunk's brightness bytes, in place.
308    /// Both the CPU rasteriser and the GPU marcher read these
309    /// pre-baked brightness bytes, so call this once after building a
310    /// grid and again over edited chunks after a carve (then bump
311    /// their versions so the GPU re-uploads — edits already do, via
312    /// [`Grid::set_voxel`] &c.). For small runtime edits prefer
313    /// [`Self::bake_bbox`] — it re-bakes only the touched columns.
314    ///
315    /// Each chunk is baked neighbour-aware in all three axes: estnorm's
316    /// (and AO's) ±2-voxel padding that crosses a chunk face reads the
317    /// actual neighbour chunk (when populated) — XY neighbours and, for
318    /// stacked grids, the chunks above/below on `chz±1` — so brightness
319    /// / occlusion is continuous across every seam. Under
320    /// [`BakeMode::PointLights`] the grid's [`Grid::bake_lights`] are
321    /// applied (EV.3); the other modes ignore them. No-op for an empty
322    /// grid.
323    pub fn bake(&mut self, mode: BakeMode) {
324        self.bake_u32(mode.lightmode(), mode.ao());
325    }
326
327    /// QE-B6 — magic `u32` lightmode; use [`Self::bake`] with a typed
328    /// [`BakeMode`] instead (`1` → `BakeMode::Directional`, `3` →
329    /// `BakeMode::AmbientOcclusion(AoParams::default())`).
330    #[deprecated(since = "0.23.0", note = "use `bake(BakeMode::..)`")]
331    pub fn bake_lightmode(&mut self, lightmode: u32) {
332        self.bake_u32(lightmode, roxlap_core::AoParams::default());
333    }
334
335    /// QE-B6 — use [`Self::bake`] with
336    /// `BakeMode::AmbientOcclusion(ao)` instead.
337    #[deprecated(since = "0.23.0", note = "use `bake(BakeMode::AmbientOcclusion(ao))`")]
338    pub fn bake_lightmode_with_ao(&mut self, lightmode: u32, ao: roxlap_core::AoParams) {
339        self.bake_u32(lightmode, ao);
340    }
341
342    fn bake_u32(&mut self, lightmode: u32, ao: roxlap_core::AoParams) {
343        if lightmode == 0 {
344            return;
345        }
346        #[allow(clippy::cast_possible_wrap)]
347        let cs_xy = CHUNK_SIZE_XY as i32;
348        #[allow(clippy::cast_possible_wrap)]
349        let cs_z = CHUNK_SIZE_Z as i32;
350        let chunk_idxs: Vec<IVec3> = self.chunks.keys().copied().collect();
351        // PF.11 — wave-parallel cache phase: each chunk's estnorm cache
352        // reads the grid immutably and is independent of the others, so a
353        // wave of `current_num_threads` caches builds in parallel; the
354        // apply phase (itself row-parallel inside
355        // `apply_lighting_with_cache`) then runs per chunk. Waves bound
356        // the resident cache memory. Byte-identical to the serial bake —
357        // caches are pure functions of the (unmodified-during-the-wave)
358        // grid, and each apply writes only its own chunk.
359        use rayon::prelude::*;
360        let wave = rayon::current_num_threads().max(1);
361        for batch in chunk_idxs.chunks(wave) {
362            let caches: Vec<(IVec3, roxlap_core::EstNormCache)> = batch
363                .par_iter()
364                .map(|&chunk_idx| {
365                    (
366                        chunk_idx,
367                        self.build_chunk_estnorm_cache(chunk_idx, 0, 0, cs_xy, cs_xy),
368                    )
369                })
370                .collect();
371            for (chunk_idx, cache) in caches {
372                // EV.3 — lightmode 2 pulls the grid's baked point
373                // lights, rebased chunk-local + sphere-culled; the
374                // other modes ignore lights, so skip the translation.
375                let lights = self.chunk_bake_lights(chunk_idx, lightmode);
376                let target = self.chunks.get_mut(&chunk_idx).expect("populated chunk");
377                roxlap_core::apply_lighting_with_cache(
378                    &mut target.data,
379                    &target.column_offset,
380                    CHUNK_SIZE_XY,
381                    0,
382                    0,
383                    0,
384                    cs_xy,
385                    cs_xy,
386                    cs_z,
387                    &cache,
388                    lightmode,
389                    &lights,
390                    ao,
391                );
392            }
393        }
394    }
395
396    /// PF.11 — re-bake lighting over just the grid-local voxel bbox
397    /// `[lo, hi]` (inclusive), neighbour-aware across chunk seams in all
398    /// three axes: the write region is padded by `±ESTNORMRAD` internally
399    /// (an edit changes the estnorm of nearby voxels too — pass only the
400    /// geometric edit extent, mirroring `update_lighting`'s convention),
401    /// and any chunk the padded region touches gets its strip re-baked.
402    /// Touched chunks get their versions bumped so the GPU re-uploads.
403    ///
404    /// This is the runtime-edit primitive the full-grid
405    /// [`Self::bake_lightmode`] is far too heavy for: a bullet-hole
406    /// rebake touches a few hundred columns instead of a whole chunk
407    /// (the cave demo measured ~0.04 ms vs 4–7 ms). Mip regeneration is
408    /// NOT performed — near-field renders read mip 0; callers streaming
409    /// distant edited chunks should remip as they already do for edits.
410    #[allow(clippy::cast_possible_wrap)]
411    pub fn bake_bbox(&mut self, lo: IVec3, hi: IVec3, mode: BakeMode) {
412        self.bake_bbox_u32(lo, hi, mode.lightmode(), mode.ao());
413    }
414
415    /// QE-B6 — magic `u32` lightmode, and this variant silently baked
416    /// AO with default params. Use [`Self::bake_bbox`] with a typed
417    /// [`BakeMode`] (which honours `AmbientOcclusion`'s params).
418    #[deprecated(since = "0.23.0", note = "use `bake_bbox(lo, hi, BakeMode::..)`")]
419    pub fn bake_lightmode_bbox(&mut self, lo: IVec3, hi: IVec3, lightmode: u32) {
420        self.bake_bbox_u32(lo, hi, lightmode, roxlap_core::AoParams::default());
421    }
422
423    #[allow(clippy::cast_possible_wrap)]
424    fn bake_bbox_u32(&mut self, lo: IVec3, hi: IVec3, lightmode: u32, ao: roxlap_core::AoParams) {
425        if lightmode == 0 {
426            return;
427        }
428        let cs_xy = CHUNK_SIZE_XY as i32;
429        let cs_z = CHUNK_SIZE_Z as i32;
430        let pad = roxlap_core::ESTNORMRAD;
431        // Padded half-open apply region in grid-local voxel coords.
432        let a_lo = IVec3::new(lo.x - pad, lo.y - pad, lo.z - pad);
433        let a_hi = IVec3::new(hi.x + pad + 1, hi.y + pad + 1, hi.z + pad + 1);
434        if a_lo.x >= a_hi.x || a_lo.y >= a_hi.y || a_lo.z >= a_hi.z {
435            return;
436        }
437        // Chunk range the padded region touches (inclusive).
438        let c_lo = IVec3::new(
439            a_lo.x.div_euclid(cs_xy),
440            a_lo.y.div_euclid(cs_xy),
441            a_lo.z.div_euclid(cs_z),
442        );
443        let c_hi = IVec3::new(
444            (a_hi.x - 1).div_euclid(cs_xy),
445            (a_hi.y - 1).div_euclid(cs_xy),
446            (a_hi.z - 1).div_euclid(cs_z),
447        );
448        for chz in c_lo.z..=c_hi.z {
449            for chy in c_lo.y..=c_hi.y {
450                for chx in c_lo.x..=c_hi.x {
451                    let chunk_idx = IVec3::new(chx, chy, chz);
452                    if !self.chunks.contains_key(&chunk_idx) {
453                        continue;
454                    }
455                    // Clip the padded region to this chunk, chunk-local.
456                    let base = IVec3::new(chx * cs_xy, chy * cs_xy, chz * cs_z);
457                    let lx0 = (a_lo.x - base.x).max(0);
458                    let ly0 = (a_lo.y - base.y).max(0);
459                    let lz0 = (a_lo.z - base.z).max(0);
460                    let lx1 = (a_hi.x - base.x).min(cs_xy);
461                    let ly1 = (a_hi.y - base.y).min(cs_xy);
462                    let lz1 = (a_hi.z - base.z).min(cs_z);
463                    if lx0 >= lx1 || ly0 >= ly1 || lz0 >= lz1 {
464                        continue;
465                    }
466                    let cache = self.build_chunk_estnorm_cache(chunk_idx, lx0, ly0, lx1, ly1);
467                    // EV.3 — carve relights keep their glow pools: the
468                    // grid's baked lights flow into the bbox re-bake too.
469                    let lights = self.chunk_bake_lights(chunk_idx, lightmode);
470                    let target = self.chunks.get_mut(&chunk_idx).expect("populated chunk");
471                    roxlap_core::apply_lighting_with_cache(
472                        &mut target.data,
473                        &target.column_offset,
474                        CHUNK_SIZE_XY,
475                        lx0,
476                        ly0,
477                        lz0,
478                        lx1,
479                        ly1,
480                        lz1,
481                        &cache,
482                        lightmode,
483                        &lights,
484                        ao,
485                    );
486                    // PF.12 — brightness bytes changed within the clipped
487                    // region only.
488                    self.bump_chunk_version_bbox(
489                        chunk_idx,
490                        IVec3::new(lx0, ly0, lz0),
491                        IVec3::new(lx1 - 1, ly1 - 1, lz1 - 1),
492                    );
493                }
494            }
495        }
496    }
497
498    /// EV.3 — the grid's [`BakeLight`]s that reach `chunk_idx`, rebased
499    /// chunk-local for the bake internals. Empty (no allocation beyond
500    /// the `Vec` header) for the non-point-light modes and for chunks
501    /// outside every light's influence sphere.
502    fn chunk_bake_lights(&self, chunk_idx: IVec3, lightmode: u32) -> Vec<roxlap_core::LightSrc> {
503        if lightmode != 2 || self.bake_lights.is_empty() {
504            return Vec::new();
505        }
506        self.bake_lights
507            .iter()
508            .filter_map(|l| l.chunk_local(chunk_idx))
509            .collect()
510    }
511
512    /// PF.11 — build the neighbour-aware estnorm cache for a chunk-local
513    /// region of `chunk_idx` from an immutable grid borrow: the reader
514    /// resolves a chunk-local `(px, py)` (which may extend `±ESTNORMRAD`
515    /// outside the region / the target chunk) into the neighbour chunk
516    /// owning that column, and `chz_delta` walks the stacked neighbour in
517    /// z (continuous AO + estnorm across every seam). Padding over an
518    /// unpopulated neighbour returns `None` (= treated as air).
519    #[allow(clippy::cast_possible_wrap)]
520    fn build_chunk_estnorm_cache(
521        &self,
522        chunk_idx: IVec3,
523        x0: i32,
524        y0: i32,
525        x1: i32,
526        y1: i32,
527    ) -> roxlap_core::EstNormCache {
528        let cs_xy = CHUNK_SIZE_XY as i32;
529        let reader = |px: i32, py: i32, chz_delta: i32| -> Option<&[u8]> {
530            let nb_chx = chunk_idx.x + px.div_euclid(cs_xy);
531            let nb_chy = chunk_idx.y + py.div_euclid(cs_xy);
532            let in_x = px.rem_euclid(cs_xy);
533            let in_y = py.rem_euclid(cs_xy);
534            let chunk = self.chunk(IVec3::new(nb_chx, nb_chy, chunk_idx.z + chz_delta))?;
535            let col_idx = (in_y as u32) * CHUNK_SIZE_XY + (in_x as u32);
536            let off = chunk.column_offset[col_idx as usize] as usize;
537            Some(&chunk.data[off..])
538        };
539        roxlap_core::EstNormCache::build_with_reader_z(reader, x0, y0, x1, y1)
540    }
541
542    /// Mutably borrow a materialised chunk. Returns `None` for
543    /// implicit-air chunks; use [`Grid::ensure_chunk`] when you
544    /// need a `&mut Vxl` for an edit that may write voxels.
545    pub fn chunk_mut(&mut self, chunk_idx: IVec3) -> Option<&mut Vxl> {
546        self.chunks.get_mut(&chunk_idx)
547    }
548
549    /// Borrow `chunk_idx`'s [`Vxl`], creating an empty all-air
550    /// chunk first if it doesn't exist yet. The returned `&mut`
551    /// is valid for editing via [`roxlap_formats::edit`] — the new
552    /// chunk has [`Vxl::reserve_edit_capacity`] already applied.
553    pub fn ensure_chunk(&mut self, chunk_idx: IVec3) -> &mut Vxl {
554        // PF.13 (H9) — materialising a chunk mutates the chunk set.
555        if !self.chunks.contains_key(&chunk_idx) {
556            self.note_chunk_set_changed();
557        }
558        self.chunks.entry(chunk_idx).or_insert_with(empty_chunk_vxl)
559    }
560
561    /// Number of materialised chunks. Implicit-air chunks don't
562    /// count.
563    #[must_use]
564    pub fn chunk_count(&self) -> usize {
565        self.chunks.len()
566    }
567
568    /// S4B.2.c.3: build a per-chunk [`roxlap_core::GridView`] table
569    /// over this grid's XY chunk footprint at `chz = 0`.
570    ///
571    /// Returns `None` if no chz=0 chunk is populated (the entire
572    /// grid would render as implicit air anyway).
573    ///
574    /// Iterates `chunks` once to find the chx/chy bounding box,
575    /// then a second time to fill the row-major
576    /// `Vec<Option<GridView<'_>>>`. Empty XY slots (implicit-air
577    /// chunks inside the box) get `None`.
578    ///
579    /// Pair with [`roxlap_core::ChunkGrid`] + [`roxlap_core::
580    /// GridView::from_chunk_grid`] to drive the Approach B render
581    /// path:
582    ///
583    /// ```ignore
584    /// let backing = grid.chunk_xy_backing().unwrap();
585    /// let cg = roxlap_core::ChunkGrid {
586    ///     chunks: &backing.chunks,
587    ///     origin_chunk_xy: backing.origin_chunk_xy,
588    ///     chunks_x: backing.chunks_x,
589    ///     chunks_y: backing.chunks_y,
590    /// };
591    /// let view = roxlap_core::GridView::from_chunk_grid(
592    ///     &cg, crate::CHUNK_SIZE_XY,
593    /// );
594    /// ```
595    ///
596    /// Only chz=0 chunks contribute (multi-z handoff lands in
597    /// S4B.3); higher-chz chunks in [`Self::chunks`] are ignored
598    /// here.
599    #[must_use]
600    pub fn chunk_xy_backing(&self) -> Option<ChunkXyBacking<'_>> {
601        let mut min_x = i32::MAX;
602        let mut min_y = i32::MAX;
603        let mut max_x = i32::MIN;
604        let mut max_y = i32::MIN;
605        let mut any = false;
606        for chunk_idx in self.chunks.keys() {
607            if chunk_idx.z != 0 {
608                continue;
609            }
610            min_x = min_x.min(chunk_idx.x);
611            min_y = min_y.min(chunk_idx.y);
612            max_x = max_x.max(chunk_idx.x);
613            max_y = max_y.max(chunk_idx.y);
614            any = true;
615        }
616        if !any {
617            return None;
618        }
619        #[allow(clippy::cast_sign_loss)]
620        let chunks_x = (max_x - min_x + 1) as u32;
621        #[allow(clippy::cast_sign_loss)]
622        let chunks_y = (max_y - min_y + 1) as u32;
623        let mut table: Vec<Option<roxlap_core::GridView<'_>>> =
624            vec![None; (chunks_x * chunks_y) as usize];
625        for (chunk_idx, vxl) in &self.chunks {
626            if chunk_idx.z != 0 {
627                continue;
628            }
629            let dx = chunk_idx.x - min_x;
630            let dy = chunk_idx.y - min_y;
631            #[allow(clippy::cast_sign_loss)]
632            let i = (dy as u32 * chunks_x + dx as u32) as usize;
633            table[i] = Some(roxlap_core::GridView::from_single_vxl(vxl));
634        }
635        Some(ChunkXyBacking {
636            chunks: table,
637            origin_chunk_xy: [min_x, min_y],
638            origin_chunk_z: 0,
639            chunks_x,
640            chunks_y,
641            chunks_z: 1,
642        })
643    }
644
645    /// S4B.6.a: 3D-aware version of [`Self::chunk_xy_backing`].
646    /// Enumerates ALL chunks across the chx/chy/chz bounding box
647    /// (not just `chz=0`) so a stacked grid can be rendered once
648    /// S4B.6.c switches the rasterizer to a chunk-z-aware column
649    /// walker.
650    ///
651    /// Iterates `chunks` once for the XYZ bbox, then a second time
652    /// to fill the row-major-per-z `Vec<Option<GridView<'_>>>`.
653    /// Index layout: `[(dz * chunks_y + dy) * chunks_x + dx]` —
654    /// matches [`roxlap_core::ChunkGrid`]'s indexing exactly.
655    ///
656    /// Returns `None` for empty grids.
657    #[must_use]
658    pub fn chunk_xyz_backing(&self) -> Option<ChunkXyBacking<'_>> {
659        let mut min_x = i32::MAX;
660        let mut min_y = i32::MAX;
661        let mut min_z = i32::MAX;
662        let mut max_x = i32::MIN;
663        let mut max_y = i32::MIN;
664        let mut max_z = i32::MIN;
665        let mut any = false;
666        for chunk_idx in self.chunks.keys() {
667            min_x = min_x.min(chunk_idx.x);
668            min_y = min_y.min(chunk_idx.y);
669            min_z = min_z.min(chunk_idx.z);
670            max_x = max_x.max(chunk_idx.x);
671            max_y = max_y.max(chunk_idx.y);
672            max_z = max_z.max(chunk_idx.z);
673            any = true;
674        }
675        if !any {
676            return None;
677        }
678        #[allow(clippy::cast_sign_loss)]
679        let chunks_x = (max_x - min_x + 1) as u32;
680        #[allow(clippy::cast_sign_loss)]
681        let chunks_y = (max_y - min_y + 1) as u32;
682        #[allow(clippy::cast_sign_loss)]
683        let chunks_z = (max_z - min_z + 1) as u32;
684        let mut table: Vec<Option<roxlap_core::GridView<'_>>> =
685            vec![None; (chunks_x * chunks_y * chunks_z) as usize];
686        for (chunk_idx, vxl) in &self.chunks {
687            let dx = chunk_idx.x - min_x;
688            let dy = chunk_idx.y - min_y;
689            let dz = chunk_idx.z - min_z;
690            #[allow(clippy::cast_sign_loss)]
691            let (dx, dy, dz) = (dx as u32, dy as u32, dz as u32);
692            let i = ((dz * chunks_y + dy) * chunks_x + dx) as usize;
693            table[i] = Some(roxlap_core::GridView::from_single_vxl(vxl));
694        }
695        Some(ChunkXyBacking {
696            chunks: table,
697            origin_chunk_xy: [min_x, min_y],
698            origin_chunk_z: min_z,
699            chunks_x,
700            chunks_y,
701            chunks_z,
702        })
703    }
704}
705
706/// S4B.2.c.3: chx/chy chunk table built from a [`Grid`].
707///
708/// Owns the `Vec<Option<GridView>>` so [`roxlap_core::ChunkGrid`]
709/// (which borrows the table) can live alongside the GridView
710/// constructed from it. Used by the Approach B render path —
711/// see [`Grid::chunk_xy_backing`].
712///
713/// S4B.6.a: gained `chunks_z` + `origin_chunk_z` for tall-world
714/// support. Pre-S4B.6.a `chunk_xy_backing` always populates these
715/// as `chunks_z=1 origin_chunk_z=0` (= chz=0 only); S4B.6.c will
716/// switch the render path to `chunk_xyz_backing` once the
717/// rasterizer is stack-aware.
718pub struct ChunkXyBacking<'a> {
719    /// Per-chunk views over the chx/chy/chz extent.
720    /// Length `chunks_x * chunks_y * chunks_z`; index layout
721    /// `[(dz * chunks_y + dy) * chunks_x + dx]`. `None` for
722    /// implicit-air chunks inside the bbox.
723    pub chunks: Vec<Option<roxlap_core::GridView<'a>>>,
724    /// XY index of the chunk at `chunks[0]` — the minimum chx/chy
725    /// among populated chunks at `chz = origin_chunk_z`.
726    pub origin_chunk_xy: [i32; 2],
727    /// Z index of the chunk at `chunks[0]`.
728    pub origin_chunk_z: i32,
729    /// Number of chunks along the X axis. Row stride.
730    pub chunks_x: u32,
731    /// Number of chunks along the Y axis.
732    pub chunks_y: u32,
733    /// Number of chunks along the Z axis. `1` from
734    /// [`Grid::chunk_xy_backing`]; `>1` only via the S4B.6.a
735    /// [`Grid::chunk_xyz_backing`].
736    pub chunks_z: u32,
737}
738
739#[cfg(test)]
740pub(crate) mod tests {
741    use super::*;
742    use crate::{GridTransform, CHUNK_SIZE_Z};
743    use roxlap_formats::color::VoxColor;
744
745    /// Decode `column`'s slab bytes and return `true` iff `z` is
746    /// covered by any solid run. Mirrors voxlap's column-walk
747    /// semantics — the b2 buffer is `[top0, bot0, top1, bot1, ...,
748    /// MAXZDIM_sentinel]`, with each `[top, bot)` pair denoting a
749    /// solid range.
750    pub(crate) fn voxel_is_solid(vxl: &Vxl, x: u32, y: u32, z: u32) -> bool {
751        super::vxl_voxel_solid(vxl, x, y, z)
752    }
753
754    #[test]
755    fn voxel_solid_reflects_set_voxel() {
756        // Grid::voxel_solid (the public picking query) reads back an
757        // edit: the set voxel is solid, its neighbour is air, and an
758        // unmaterialised chunk reads as air.
759        let mut g = Grid::new(GridTransform::identity());
760        g.set_voxel(IVec3::new(5, 6, 7), Some(VoxColor(0x80_aa_bb_cc)));
761        assert!(g.voxel_solid(IVec3::new(5, 6, 7)), "set voxel is solid");
762        assert!(!g.voxel_solid(IVec3::new(5, 6, 8)), "neighbour is air");
763        assert!(
764            !g.voxel_solid(IVec3::new(900, 900, 7)),
765            "absent chunk reads as air",
766        );
767    }
768
769    #[test]
770    fn voxel_solid_handles_negative_coords() {
771        // Negative grid-local voxels decompose via div_euclid (addr
772        // semantics); the query must follow the same split.
773        let mut g = Grid::new(GridTransform::identity());
774        g.set_voxel(IVec3::new(-1, -1, 10), Some(VoxColor(0x80_11_22_33)));
775        assert!(g.voxel_solid(IVec3::new(-1, -1, 10)));
776        assert!(!g.voxel_solid(IVec3::new(-1, -1, 11)));
777    }
778
779    #[test]
780    fn empty_chunk_has_correct_vsid() {
781        let vxl = empty_chunk_vxl();
782        assert_eq!(vxl.vsid, CHUNK_SIZE_XY);
783    }
784
785    #[test]
786    fn empty_chunk_is_all_air() {
787        let vxl = empty_chunk_vxl();
788        // Sample a few representative voxels — full coverage is in
789        // `empty_chunk_no_voxel_solid_anywhere` below.
790        for &(x, y, z) in &[
791            (0u32, 0u32, 0u32),
792            (0, 0, 100),
793            (0, 0, 200),
794            (CHUNK_SIZE_XY - 1, CHUNK_SIZE_XY - 1, 0),
795            (64, 64, 128),
796        ] {
797            assert!(
798                !voxel_is_solid(&vxl, x, y, z),
799                "voxel ({x}, {y}, {z}) should be air"
800            );
801        }
802    }
803
804    #[test]
805    fn empty_chunk_air_above_bedrock_on_grid_sample() {
806        // Stride 16 across the chunk catches structural breakage
807        // (a corner column wrong, a z-band wrong, etc.) without the
808        // 4M-query cost of a brute-force scan in debug mode.
809        // Voxlap's slab format keeps z=255 solid as the "below the
810        // world" sentinel; the renderer's `treat_z_max_as_air` flag
811        // handles displaying it as transparent. See
812        // `project_below_bedrock_all_sky.md` for the S1.X fix.
813        let vxl = empty_chunk_vxl();
814        let bedrock_z = CHUNK_SIZE_Z - 1;
815        for y in (0..CHUNK_SIZE_XY).step_by(16) {
816            for x in (0..CHUNK_SIZE_XY).step_by(16) {
817                for z in (0..bedrock_z).step_by(16) {
818                    assert!(
819                        !voxel_is_solid(&vxl, x, y, z),
820                        "voxel ({x}, {y}, {z}) leaked solid in empty chunk"
821                    );
822                }
823                // bedrock z is solid (placeholder).
824                assert!(voxel_is_solid(&vxl, x, y, bedrock_z));
825            }
826        }
827    }
828
829    #[test]
830    fn empty_chunk_keeps_bedrock_placeholder() {
831        // Voxlap's invariant: every column carries an implicit
832        // solid voxel at z = MAXZDIM-1 = 255 even after a full
833        // carve. The renderer reads this as the bedrock placeholder.
834        let vxl = empty_chunk_vxl();
835        assert!(voxel_is_solid(&vxl, 0, 0, CHUNK_SIZE_Z - 1));
836        assert!(voxel_is_solid(&vxl, 64, 64, CHUNK_SIZE_Z - 1));
837    }
838
839    #[test]
840    fn ensure_chunk_creates_when_missing() {
841        let mut g = Grid::new(GridTransform::identity());
842        assert_eq!(g.chunk_count(), 0);
843        assert!(g.chunk(IVec3::ZERO).is_none());
844        let _ = g.ensure_chunk(IVec3::ZERO);
845        assert_eq!(g.chunk_count(), 1);
846        assert!(g.chunk(IVec3::ZERO).is_some());
847    }
848
849    #[test]
850    fn ensure_chunk_returns_existing() {
851        // Calling ensure_chunk a second time on the same index
852        // doesn't replace the chunk. Verify by writing through the
853        // first call and reading through the second.
854        let mut g = Grid::new(GridTransform::identity());
855        let chunk = IVec3::new(2, -1, 0);
856        g.ensure_chunk(chunk);
857        // Voxel local (5, 6, 7) inside chunk (2, -1, 0) is
858        // grid-local global (2*128 + 5, -1*128 + 6, 0*256 + 7) =
859        // (261, -122, 7).
860        g.set_voxel(IVec3::new(261, -122, 7), Some(VoxColor(0x80_aa_bb_cc)));
861        let vxl = g.ensure_chunk(chunk);
862        assert!(voxel_is_solid(vxl, 5, 6, 7));
863        assert_eq!(g.chunk_count(), 1);
864    }
865
866    #[test]
867    fn chunk_mut_returns_none_for_missing() {
868        let mut g = Grid::new(GridTransform::identity());
869        assert!(g.chunk_mut(IVec3::ZERO).is_none());
870    }
871
872    /// S4B.6.a: legacy `chunk_xy_backing` ignores chz!=0 chunks and
873    /// always returns `chunks_z=1 origin_chunk_z=0`. Sanity-check
874    /// the new field defaults so pre-S4B.6 render path stays
875    /// byte-identical.
876    #[test]
877    fn chunk_xy_backing_returns_chunks_z_one() {
878        let mut g = Grid::new(GridTransform::identity());
879        g.ensure_chunk(IVec3::new(0, 0, 0));
880        g.ensure_chunk(IVec3::new(1, 0, 0));
881        // Add a chunk at chz=1 — chunk_xy_backing should ignore it.
882        g.ensure_chunk(IVec3::new(0, 0, 1));
883        let backing = g.chunk_xy_backing().expect("two chz=0 chunks present");
884        assert_eq!(backing.chunks_z, 1);
885        assert_eq!(backing.origin_chunk_z, 0);
886        assert_eq!(backing.chunks_x, 2);
887        assert_eq!(backing.chunks_y, 1);
888        assert_eq!(backing.chunks.len(), 2);
889    }
890
891    /// S4B.6.a: new `chunk_xyz_backing` enumerates ALL chunks
892    /// including chz!=0. Indexing layout must match
893    /// `roxlap_core::ChunkGrid`: row-major per z slab.
894    #[test]
895    fn chunk_xyz_backing_with_stacked_chunks_enumerates_all_z() {
896        let mut g = Grid::new(GridTransform::identity());
897        g.ensure_chunk(IVec3::new(0, 0, 0));
898        g.ensure_chunk(IVec3::new(1, 0, 0));
899        g.ensure_chunk(IVec3::new(0, 0, 1));
900        // Leave (1, 0, 1) implicit-air.
901        let backing = g.chunk_xyz_backing().expect("at least one chunk");
902        assert_eq!(backing.chunks_x, 2);
903        assert_eq!(backing.chunks_y, 1);
904        assert_eq!(backing.chunks_z, 2);
905        assert_eq!(backing.origin_chunk_xy, [0, 0]);
906        assert_eq!(backing.origin_chunk_z, 0);
907        assert_eq!(backing.chunks.len(), 4); // dims [2, 1, 2]
908                                             // Index layout: [(dz * chunks_y + dy) * chunks_x + dx]
909                                             // (0, 0, 0) → dx=0, dy=0, dz=0 → 0
910                                             // (1, 0, 0) → dx=1, dy=0, dz=0 → 1
911                                             // (0, 0, 1) → dx=0, dy=0, dz=1 → 2
912                                             // (1, 0, 1) → dx=1, dy=0, dz=1 → 3 (implicit-air → None)
913        assert!(backing.chunks[0].is_some(), "(0, 0, 0) present");
914        assert!(backing.chunks[1].is_some(), "(1, 0, 0) present");
915        assert!(backing.chunks[2].is_some(), "(0, 0, 1) present");
916        assert!(backing.chunks[3].is_none(), "(1, 0, 1) implicit-air");
917    }
918
919    /// S4B.6.a: chunk_xyz_backing with negative chz origin —
920    /// origin_chunk_z = min_z must reflect the actual minimum chz.
921    #[test]
922    fn chunk_xyz_backing_with_negative_origin_chunk_z() {
923        let mut g = Grid::new(GridTransform::identity());
924        g.ensure_chunk(IVec3::new(0, 0, -2));
925        g.ensure_chunk(IVec3::new(0, 0, 0));
926        let backing = g.chunk_xyz_backing().expect("at least one chunk");
927        assert_eq!(backing.chunks_z, 3); // chz range [-2, 0]
928        assert_eq!(backing.origin_chunk_z, -2);
929        assert!(backing.chunks[0].is_some(), "chz=-2 → dz=0");
930        assert!(backing.chunks[1].is_none(), "chz=-1 → dz=1 implicit-air");
931        assert!(backing.chunks[2].is_some(), "chz=0 → dz=2");
932    }
933
934    /// PF.11 — `bake_lightmode_bbox` around an edit must reproduce, byte
935    /// for byte, what a full-grid re-bake would write: the padded write
936    /// region covers every voxel whose estnorm the edit could change
937    /// (±ESTNORMRAD), including strips in neighbour chunks across seams.
938    #[test]
939    fn bbox_rebake_matches_full_rebake() {
940        // Terrain spanning a 2×2 chunk seam, with relief so estnorm is
941        // non-trivial around the edit.
942        let build = || {
943            let mut g = Grid::new(GridTransform::identity());
944            g.set_rect(
945                IVec3::new(0, 0, 160),
946                IVec3::new(255, 255, 255),
947                Some(VoxColor(0x80_66_77_88)),
948            );
949            for i in 0..10 {
950                let (x, y) = (23 * i % 240 + 8, 37 * i % 240 + 8);
951                g.set_sphere(IVec3::new(x, y, 165), 6, None);
952            }
953            g.bake(BakeMode::Directional);
954            g
955        };
956        let mut full = build();
957        let mut bbox = build();
958
959        // Identical edit in both, straddling the chunk (0,0)/(1,0) seam
960        // so the neighbour-strip rebake is exercised.
961        let (lo, hi) = (IVec3::new(120, 60, 158), IVec3::new(136, 76, 200));
962        full.set_rect(lo, hi, None);
963        bbox.set_rect(lo, hi, None);
964
965        // Ground truth: full re-bake. Candidate: bbox-only re-bake.
966        full.bake(BakeMode::Directional);
967        bbox.bake_bbox(lo, hi, BakeMode::Directional);
968
969        for (idx, a) in &full.chunks {
970            let b = bbox.chunks.get(idx).expect("same chunk set");
971            assert_eq!(
972                a.data, b.data,
973                "chunk {idx:?} bytes diverge between full and bbox rebake",
974            );
975        }
976    }
977
978    /// EV.3 — `BakeMode::PointLights` writes a Lambertian pool around a
979    /// [`BakeLight`]: floor voxels under the light brighten, voxels
980    /// beyond its radius stay at the dim lightmode-2 base, and that
981    /// base is darker than the `Directional` bake.
982    #[test]
983    fn point_light_bake_brightens_near_light() {
984        let floor = |g: &mut Grid| {
985            g.set_rect(
986                IVec3::new(0, 0, 200),
987                IVec3::new(127, 127, 255),
988                Some(VoxColor(0x80_66_77_88)),
989            );
990        };
991        let byte_at = |g: &Grid, x: u32, y: u32| {
992            (g.chunk(IVec3::ZERO)
993                .expect("chunk")
994                .voxel_color(x, y, 200)
995                .expect("floor voxel")
996                .0
997                >> 24)
998                & 0xff
999        };
1000
1001        let mut g = Grid::new(GridTransform::identity());
1002        floor(&mut g);
1003        // Crystal light hovering 8 voxels above the floor centre.
1004        g.bake_lights.push(BakeLight {
1005            pos: glam::Vec3::new(64.0, 64.0, 192.0),
1006            radius: 40.0,
1007            strength: 4000.0,
1008        });
1009        g.bake(BakeMode::PointLights);
1010        let near = byte_at(&g, 64, 64);
1011        let far = byte_at(&g, 4, 4); // ~85 voxels away > radius 40 ⇒ base only
1012        assert!(
1013            near > far + 20,
1014            "light pool must brighten the floor under it (near={near} far={far})"
1015        );
1016
1017        // The lightmode-2 base is deliberately dim vs the Directional bake.
1018        let mut dir = Grid::new(GridTransform::identity());
1019        floor(&mut dir);
1020        dir.bake(BakeMode::Directional);
1021        let dir_far = byte_at(&dir, 4, 4);
1022        assert!(
1023            far < dir_far,
1024            "PointLights base ({far}) must be dimmer than Directional ({dir_far})"
1025        );
1026    }
1027
1028    /// EV.3 — a bbox re-bake under `PointLights` must reproduce the
1029    /// full re-bake byte-for-byte, glow pools included (the carve
1030    /// relight path keeps crystal lighting intact).
1031    #[test]
1032    fn point_light_bbox_rebake_matches_full_rebake() {
1033        let build = || {
1034            let mut g = Grid::new(GridTransform::identity());
1035            g.set_rect(
1036                IVec3::new(0, 0, 160),
1037                IVec3::new(255, 255, 255),
1038                Some(VoxColor(0x80_66_77_88)),
1039            );
1040            g.bake_lights.push(BakeLight {
1041                pos: glam::Vec3::new(126.0, 70.0, 152.0),
1042                radius: 48.0,
1043                strength: 4000.0,
1044            });
1045            g.bake(BakeMode::PointLights);
1046            g
1047        };
1048        let mut full = build();
1049        let mut bbox = build();
1050
1051        // Carve inside the light's pool, straddling the chunk seam.
1052        let (lo, hi) = (IVec3::new(120, 60, 158), IVec3::new(136, 76, 200));
1053        full.set_rect(lo, hi, None);
1054        bbox.set_rect(lo, hi, None);
1055
1056        full.bake(BakeMode::PointLights);
1057        bbox.bake_bbox(lo, hi, BakeMode::PointLights);
1058
1059        for (idx, a) in &full.chunks {
1060            let b = bbox.chunks.get(idx).expect("same chunk set");
1061            assert_eq!(
1062                a.data, b.data,
1063                "chunk {idx:?} bytes diverge between full and bbox point-light rebake",
1064            );
1065        }
1066    }
1067
1068    /// PF.12 — `remip_bbox` must be byte-identical to a full
1069    /// `generate_mips` when the bbox covers the edits: data bytes,
1070    /// column offsets, and the mip table all match — across repeated
1071    /// incremental rounds, corner-of-chunk edits, and voxalloc-scattered
1072    /// columns.
1073    #[test]
1074    fn remip_bbox_matches_generate_mips() {
1075        use roxlap_formats::edit::{set_sphere_with_colfunc, SpanOp};
1076
1077        // Realistic chunk: terrain + caves, then a full initial ladder.
1078        let mut g = Grid::new(GridTransform::identity());
1079        g.set_rect(
1080            IVec3::new(0, 0, 150),
1081            IVec3::new(127, 127, 255),
1082            Some(VoxColor(0x80_66_77_88)),
1083        );
1084        for i in 0..8 {
1085            let (x, y) = (29 * i % 100 + 12, 41 * i % 100 + 12);
1086            g.set_sphere(IVec3::new(x, y, 158), 5, None);
1087        }
1088        let base = g.chunks.get_mut(&IVec3::ZERO).expect("chunk");
1089        base.generate_mips(6);
1090
1091        let mut full = base.clone();
1092        let mut inc = base.clone();
1093
1094        // Round 1: a carve straddling brick/column-group boundaries plus
1095        // a corner edit (exercises clamping at the chunk edge).
1096        let edits: [(IVec3, u32); 3] = [
1097            (IVec3::new(63, 64, 170), 7),
1098            (IVec3::new(0, 0, 155), 4),
1099            (IVec3::new(126, 100, 165), 5),
1100        ];
1101        for round in 0..2 {
1102            let mut lo = IVec3::splat(i32::MAX);
1103            let mut hi = IVec3::splat(i32::MIN);
1104            for (k, &(c, r)) in edits.iter().enumerate() {
1105                if k % 2 != round % 2 {
1106                    continue; // vary the edit set per round
1107                }
1108                #[allow(clippy::cast_possible_wrap)]
1109                let ri = r as i32;
1110                for v in [&mut full, &mut inc] {
1111                    set_sphere_with_colfunc(v, c.into(), r, SpanOp::Carve, |_, _, _| {
1112                        VoxColor(0x80_31_41_59)
1113                    });
1114                }
1115                lo = lo.min(c - IVec3::splat(ri));
1116                hi = hi.max(c + IVec3::splat(ri));
1117            }
1118            full.generate_mips(6);
1119            inc.remip_bbox(lo.x, lo.y, hi.x, hi.y, 6);
1120
1121            assert_eq!(
1122                full.mip_base_offsets, inc.mip_base_offsets,
1123                "round {round}: mip tables diverge",
1124            );
1125            assert_eq!(
1126                full.column_offset, inc.column_offset,
1127                "round {round}: column offsets diverge",
1128            );
1129            assert_eq!(full.data, inc.data, "round {round}: data bytes diverge");
1130        }
1131    }
1132
1133    /// PF.6 — the in-place slab walk in `vxl_voxel_solid` must agree
1134    /// with the `expandrle` run list (the pre-PF.6 reference decoder)
1135    /// for every z, on columns with multiple slabs (caves), single
1136    /// slabs, and untouched bedrock.
1137    #[test]
1138    fn vxl_voxel_solid_matches_expandrle_reference() {
1139        use roxlap_formats::edit::expandrle;
1140
1141        let mut g = Grid::new(GridTransform::identity());
1142        // Carve two separate air pockets into one column (multi-slab)
1143        // plus a sphere for varied neighbours.
1144        g.set_rect(IVec3::new(3, 4, 40), IVec3::new(4, 5, 60), None);
1145        g.set_rect(IVec3::new(3, 4, 100), IVec3::new(4, 5, 140), None);
1146        g.set_voxel(IVec3::new(3, 4, 120), Some(VoxColor(0x80_11_22_33))); // island inside the pocket
1147        g.set_sphere(IVec3::new(8, 8, 80), 6, None);
1148        let vxl = g.chunk(IVec3::ZERO).expect("chunk materialised");
1149
1150        let maxzdim = CHUNK_SIZE_Z as i32;
1151        for (x, y) in [(3u32, 4u32), (8, 8), (0, 0), (8, 2), (12, 8)] {
1152            // Reference: full expandrle decode → run-list scan.
1153            let column = vxl.column_data((y * vxl.vsid + x) as usize);
1154            let mut b2 = vec![maxzdim; 2 * (CHUNK_SIZE_Z as usize) + 4];
1155            expandrle(column, &mut b2);
1156            for z in 0..CHUNK_SIZE_Z {
1157                let zi = z as i32;
1158                let mut reference = false;
1159                let mut i = 0;
1160                while b2[i] < maxzdim {
1161                    if zi >= b2[i] && zi < b2[i + 1] {
1162                        reference = true;
1163                        break;
1164                    }
1165                    i += 2;
1166                }
1167                assert_eq!(
1168                    vxl_voxel_solid(vxl, x, y, z),
1169                    reference,
1170                    "column ({x}, {y}) z={z} disagrees with expandrle",
1171                );
1172            }
1173        }
1174    }
1175}