Skip to main content

roxlap_core/
dda.rs

1//! Per-pixel 3D-DDA + brickmap CPU renderer (Substage DDA).
2//!
3//! This is the clean-room replacement for the voxlap-derived
4//! column-coherent opticast pipeline (`opticast` + `grouscan` +
5//! `scan_loops`). Every pixel casts one independent ray, so none of
6//! the column/row-coherence stitching artifacts of the 2.5D voxlap
7//! renderer can occur (silhouette notch, floor hairlines, axis-aligned
8//! mip beams, cross-chunk virtual-column complexity). See
9//! `PORTING-DDA.md` for the full stage plan.
10//!
11//! **Stage status — DDA.6 (per-grid distance mip) + DDA.7 (tile
12//! parallelism).** Each pixel casts one ray over the grid's full voxel
13//! box ([`GridView::voxel_bounds`], spanning every chunk in XY **and**
14//! Z) via a 3D-DDA (Amanatides–Woo). A uniform render mip (chosen per
15//! grid by LOD distance, clamped by [`effective_mip`] to a level every
16//! chunk has built) coarsens the cell size to `2^mip` mip-0 voxels and
17//! samples mip-`mip` data — the ray stays in mip-0 units so depth and
18//! fog are exact. `BrickMaps` (one occupancy map per populated chunk,
19//! at the render mip) are built once per frame and shared immutably; a
20//! `Sampler` resolves each cell to its chunk
21//! ([`GridView::chunk_at_xyz`]) and brick-gates the
22//! [`GridView::surface_color_mip`] slab walk, caching the current chunk
23//! so air costs an O(1) bit test. [`render_dda_parallel`] splits the
24//! frame into disjoint rayon bands — bit-identical to sequential since
25//! pixels are independent. Hits are shaded by baked brightness
26//! (`shade`) + [`DdaEnv::side_shades`] face tint, fogged toward
27//! [`DdaEnv::fog_color`] (`apply_fog`); misses sample the
28//! [`DdaEnv::sky`] panorama (`sample_sky`) or keep the solid pre-fill.
29//!
30//! Buffer conventions match the rest of the engine so this backend is
31//! colour is packed `0x80RRGGBB`; depth is perpendicular distance from
32//! the camera with **smaller = closer** (so the scene compositor's
33//! min-z merge works directly on the z-buffer this writes).
34
35use std::collections::HashMap;
36
37use rayon::prelude::*;
38
39use crate::camera_math::{self, CameraState};
40use crate::grid_view::GridView;
41use crate::opticast::OpticastSettings;
42use crate::raster_target::RasterTarget;
43use crate::sky::Sky;
44use crate::Camera;
45use roxlap_formats::material::{material_for_color, Material, MaterialTable};
46use roxlap_formats::Rgb;
47
48/// Per-frame environment for DDA shading (Substage DDA.5): a textured
49/// sky panorama, distance fog, and per-face side shading.
50///
51/// [`DdaEnv::default`] disables all three — flat baked-brightness hits
52/// and a caller-pre-filled solid sky — so the brickmap/dense equivalence
53/// tests run against an unchanged pipeline.
54#[derive(Clone, Copy)]
55pub struct DdaEnv<'a> {
56    /// Textured sky sampled per-ray-direction on a miss. `None` leaves
57    /// the destination untouched (caller's solid sky pre-fill shows).
58    pub sky: Option<&'a Sky>,
59    /// Fog target colour (`0x__RRGGBB`); hits blend toward it with
60    /// distance. Typically the sky colour so terrain fades into the sky.
61    pub fog_color: u32,
62    /// Depth at which fog is fully opaque. `<= 0` disables fog.
63    pub fog_max_dist: f32,
64    /// Per-face brightness reduction `[x-, x+, y-, y+, z-, z+]`, applied
65    /// to the hit face (voxlap `setsideshades`). All-zero = off.
66    pub side_shades: [i8; 6],
67    /// TV: global voxel-material palette (id → opacity + blend mode). `None`
68    /// keeps terrain fully opaque (the first-hit path, bit-identical).
69    pub materials: Option<&'a MaterialTable>,
70    /// TV: terrain colour→material map (`(rgb, material_id)`). A hit voxel's
71    /// colour is looked up here for its material. **Empty** (the default) ⇒
72    /// every voxel is opaque, so the march returns the first hit unchanged.
73    pub terrain_materials: &'a [(Rgb, u8)],
74    /// CPU.1 — dynamic lighting (stage DL on the CPU): sun + point lights +
75    /// stylized cel/ramp, evaluated flat per voxel. Disabled by default ⇒ the
76    /// hit uses the baked-byte `shade` path, byte-identical to pre-DL. Lights
77    /// here are already in the grid's **local** frame (the scene renderer
78    /// transforms them per grid). Shadows: see [`Self::world_shadow`].
79    pub lights: CpuLights<'a>,
80    /// XS.1 — when set, shadow rays test the **whole scene** (all grids +
81    /// sprites) via this world-space occluder + the current grid's
82    /// local→world transform, instead of the single-grid `SamplerShadow`.
83    /// `None` ⇒ single-grid shadows (the direct `render_dda` path / tests).
84    pub world_shadow: Option<WorldShadowCtx<'a>>,
85}
86
87/// CPU.1 — one point light in a grid's local frame for the CPU renderer.
88#[derive(Clone, Copy)]
89pub struct CpuPointLight {
90    /// Grid-local position (world/voxel units).
91    pub pos: [f32; 3],
92    /// Linear RGB, 0..1.
93    pub color: [f32; 3],
94    /// Scalar multiplier on `color` — the light's contribution is
95    /// `albedo · color · intensity · N·L · falloff` per channel.
96    /// `1.0` is nominal; values above it over-drive (the sum clamps at
97    /// pack time).
98    pub intensity: f32,
99    /// Hard cutoff distance (world/voxel units).
100    pub radius: f32,
101    /// CPU.2 — whether this light casts a hard shadow (a shadow ray
102    /// marches to the light through the grid's voxels). Mirrors the
103    /// GPU's per-light `casts_shadow`; the renderer applies the same
104    /// caster cap before building the CPU rig.
105    pub casts_shadow: bool,
106    /// SL — spot (cone) axis: grid-local unit direction the light shines
107    /// **along**. Ignored for a pure point light (see [`Self::cos_outer`]).
108    pub spot_dir: [f32; 3],
109    /// SL — cosine of the inner cone half-angle (full brightness within it).
110    pub cos_inner: f32,
111    /// SL — cosine of the outer cone half-angle (zero past it; soft
112    /// `smoothstep` between the two). `-1.0` (a 180° cone) ⇒ a pure point
113    /// light: the cone mask is skipped entirely and the light is omnidirectional.
114    pub cos_outer: f32,
115}
116
117/// CPU.1 — the per-frame dynamic-light environment for one grid (grid-local).
118/// Mirror of the GPU `shade_lit` inputs. `enabled == false` (the default)
119/// keeps the baked-byte path. CPU.2 adds hard voxel shadows (sun + flagged
120/// point lights) via a per-(voxel,face) shadow march; `shadow_strength == 0`
121/// (the [`Default`]) leaves the lighting diffuse-only.
122#[derive(Clone, Copy, Default)]
123pub struct CpuLights<'a> {
124    /// Whether dynamic lighting is active this frame (else the baked path).
125    pub enabled: bool,
126    /// Whether the sun is present.
127    pub sun: bool,
128    /// Grid-local unit direction **to** the sun.
129    pub sun_dir: [f32; 3],
130    /// Sun colour, linear RGB 0..1.
131    pub sun_color: [f32; 3],
132    /// Scalar multiplier on `sun_color` (the sun term is
133    /// `albedo · sun_color · sun_intensity · key`). `1.0` is nominal.
134    pub sun_intensity: f32,
135    /// CPU.2 — whether the sun casts a hard shadow.
136    pub sun_casts_shadow: bool,
137    /// Grid-local point lights.
138    pub points: &'a [CpuPointLight],
139    /// Ambient multiplier on the baked byte (smooth mode's fill).
140    pub ambient: [f32; 3],
141    /// Cel band count: 0 = smooth, ≥1 = quantize + gradient-map (stylized).
142    pub bands: u32,
143    /// Stylized ramp's cool unlit-end tint (used when `bands > 0`).
144    pub shadow_tint: [f32; 3],
145    /// CPU.2 — fraction of a caster's light removed where a shadow ray is
146    /// occluded (`0` ⇒ shadows off, `1` ⇒ full black). A shadowed sample
147    /// keeps `1 - shadow_strength` of that caster.
148    pub shadow_strength: f32,
149    /// CPU.2 — shadow-ray origin bias along the surface normal, voxel
150    /// units (kills self-shadow acne). ~1.5 is a good default.
151    pub shadow_bias: f32,
152    /// CPU.2 — sun shadow-ray length cap, voxel units (point-light rays
153    /// stop at the light instead).
154    pub shadow_max_dist: f32,
155}
156
157impl Default for DdaEnv<'_> {
158    fn default() -> Self {
159        Self {
160            sky: None,
161            fog_color: 0,
162            fog_max_dist: 0.0,
163            side_shades: [0; 6],
164            materials: None,
165            terrain_materials: &[],
166            lights: CpuLights::default(),
167            world_shadow: None,
168        }
169    }
170}
171
172/// Per-pixel output target for the DDA renderer.
173///
174/// Abstracts "where does a ray hit go" so the traversal core stays
175/// free of framebuffer mechanics. The production impl is
176/// [`RasterSink`] (raw fb/zb pointers); tests use a recording sink.
177/// Only *hits* are reported — misses (sky) leave the destination
178/// untouched, matching the caller-pre-fills-sky convention.
179pub trait PixelSink {
180    /// Record a ray hit at framebuffer index `idx` (`py * pitch + px`)
181    /// with packed ARGB `color` and perpendicular `dist` (smaller =
182    /// closer).
183    fn put(&mut self, idx: usize, color: u32, dist: f32);
184}
185
186/// [`PixelSink`] over a borrowed `(framebuffer, zbuffer)` pair.
187///
188/// Wraps a [`RasterTarget`] so the DDA path writes through the same
189/// raw-pointer mechanism the scalar rasterizer uses — which keeps the
190/// door open for the same strip/tile-disjoint parallel writes in
191/// DDA.7.
192pub struct RasterSink<'a> {
193    target: RasterTarget<'a>,
194    len: usize,
195}
196
197impl<'a> RasterSink<'a> {
198    /// Build a sink from exclusive framebuffer + zbuffer borrows.
199    /// Both slices must have the same length (the pixel count).
200    #[must_use]
201    pub fn new(framebuffer: &'a mut [u32], zbuffer: &'a mut [f32]) -> Self {
202        debug_assert_eq!(framebuffer.len(), zbuffer.len());
203        let len = framebuffer.len();
204        Self {
205            target: RasterTarget::new(framebuffer, zbuffer),
206            len,
207        }
208    }
209}
210
211impl PixelSink for RasterSink<'_> {
212    fn put(&mut self, idx: usize, color: u32, dist: f32) {
213        if idx < self.len {
214            // SAFETY: bounds checked above; single-threaded writer in
215            // DDA.0 so the disjoint-write invariant holds trivially.
216            unsafe {
217                self.target.write_color(idx, color);
218                self.target.write_depth(idx, dist);
219            }
220        }
221    }
222}
223
224/// A resolved ray hit: surface colour + perpendicular distance.
225#[derive(Debug, Clone, Copy)]
226struct Hit {
227    color: u32,
228    dist: f32,
229}
230
231/// Test-only per-thread traversal counters for the perf bench.
232#[cfg(test)]
233pub(crate) mod prof {
234    use std::cell::Cell;
235    thread_local! {
236        pub static CELLS: Cell<u64> = const { Cell::new(0) };
237        pub static BRICKS: Cell<u64> = const { Cell::new(0) };
238        pub static SURF: Cell<u64> = const { Cell::new(0) };
239    }
240    pub fn reset() {
241        CELLS.with(|x| x.set(0));
242        BRICKS.with(|x| x.set(0));
243        SURF.with(|x| x.set(0));
244    }
245    pub fn read() -> (u64, u64, u64) {
246        (
247            CELLS.with(Cell::get),
248            BRICKS.with(Cell::get),
249            SURF.with(Cell::get),
250        )
251    }
252}
253
254/// Apply the voxel's baked directional brightness (Substage DDA.5).
255///
256/// Voxlap (and the GPU marcher, `grid_dda.wgsl`) store per-voxel
257/// brightness in the colour's high byte on a `0..128` scale — `0x80`
258/// is full brightness — written by `Grid::bake_lightmode` (estnorm
259/// directional shading). The shaded channel is `c · a / 128`, so the
260/// DDA matches the GPU look; an unbaked / full-bright voxel (`a =
261/// 0x80`) passes through unchanged. Output alpha is normalised to
262/// `0x80` (the standard "lit" flag; the present blit ignores it).
263///
264/// The renderer only *reads* the baked byte — it computes no normals
265/// itself, so per-impact relight is free (re-bake the chunk and the
266/// byte updates). The estnorm bake that produces the byte is the
267/// voxlap-derived piece slated for a clean-room rewrite in DDA.10.
268///
269/// `bright_sub` is the per-face `side_shades` reduction (DDA.5): voxlap
270/// subtracts it from the brightness byte before the multiply, so a
271/// shaded face is uniformly darker. `0` = no side shading.
272#[inline]
273pub(crate) fn shade(color: u32, bright_sub: u32) -> u32 {
274    let a = ((color >> 24) & 0xff).saturating_sub(bright_sub);
275    let ch = |shift: u32| -> u32 { ((((color >> shift) & 0xff) * a) >> 7).min(255) };
276    0x8000_0000 | (ch(16) << 16) | (ch(8) << 8) | ch(0)
277}
278
279// CPU.1 — cel quantization: snap a 0..1 factor to `bands + 1` levels.
280#[inline]
281fn cel_band(x: f32, bands: u32) -> f32 {
282    let b = bands as f32;
283    ((x * b).round() / b).clamp(0.0, 1.0)
284}
285
286// CPU.1 — point-light distance falloff (mirror of the GPU's): smooth
287// quadratic from 1 at the light to 0 at `radius`, hard-cut beyond.
288#[inline]
289fn point_falloff(d: f32, radius: f32) -> f32 {
290    let x = (1.0 - d / radius).clamp(0.0, 1.0);
291    x * x
292}
293
294// SL — Hermite `smoothstep` (mirror of WGSL's), with a defined hard-edge case:
295// when `edge0 == edge1` WGSL is undefined, so we step at the shared threshold.
296#[inline]
297fn smoothstep_scalar(edge0: f32, edge1: f32, x: f32) -> f32 {
298    if edge1 <= edge0 {
299        return if x < edge0 { 0.0 } else { 1.0 };
300    }
301    let t = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
302    t * t * (3.0 - 2.0 * t)
303}
304
305// SL — spot (cone) angular mask (mirror of the shaders' `spot_cone`). `ldir` is
306// the unit direction from the surface TO the light; `axis` the cone axis (the
307// way the light shines). Returns 1.0 for a pure point light (`cos_outer <=
308// -0.999`, the 180° degenerate); else a soft `smoothstep` from 0 at the outer
309// half-angle to 1 at the inner (hard step when the two coincide).
310#[inline]
311fn spot_cone(ldir: [f32; 3], axis: [f32; 3], cos_inner: f32, cos_outer: f32) -> f32 {
312    if cos_outer <= -0.999 {
313        return 1.0;
314    }
315    let cd = -dot3(ldir, axis);
316    smoothstep_scalar(cos_outer, cos_inner, cd)
317}
318
319// CPU.1 — face normal (grid-local) from the crossed axis + step: points back
320// toward the incoming ray. `axis == 3` (entry voxel, no face) falls back to up
321// (-z, voxlap z-down).
322#[inline]
323fn face_normal_cpu(axis: usize, step: [i32; 3]) -> [f32; 3] {
324    let mut n = [0.0f32; 3];
325    if axis < 3 {
326        n[axis] = -(step[axis] as f32);
327    } else {
328        n[2] = -1.0;
329    }
330    n
331}
332
333#[inline]
334fn dot3(a: [f32; 3], b: [f32; 3]) -> f32 {
335    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
336}
337
338/// CPU.2 — a hard-shadow occlusion test for the dynamic-lighting shade.
339/// `occluded(origin, dir, max_t)` returns `true` if a solid voxel blocks
340/// the segment from `origin` (grid-local, already biased off the surface)
341/// in unit direction `dir` within `max_t` voxel units. Terrain hits pass
342/// a [`SamplerShadow`] (marches the current grid only) or a
343/// [`WorldShadow`] (cross-grid + sprites, XS.1/XS.2); sprites that don't
344/// cast/receive shadows pass `None`.
345pub(crate) trait ShadowTester {
346    fn occluded(&mut self, origin: [f32; 3], dir: [f32; 3], max_t: f32) -> bool;
347}
348
349/// XS.1 — a **world-space** occlusion oracle over the whole scene (all grids,
350/// and sprites in XS.2). Implemented in `roxlap-scene` (it needs the grid /
351/// sprite stores); the CPU DDA reaches it through [`DdaEnv::world_shadow`] so
352/// shadow rays cross grid + object boundaries instead of stopping at the
353/// current grid. `occluded_world(origin, dir, max_t)` is in **world** voxel
354/// units: `true` iff any solid voxel anywhere blocks the segment.
355///
356/// `Sync` because [`DdaEnv`] (which borrows it) is shared across the
357/// rayon strip workers in [`render_dda_parallel`]; the occluder is a
358/// read-only borrow of the scene, so this holds.
359pub trait WorldOccluder: Sync {
360    /// `true` iff any solid voxel in the scene blocks the segment from
361    /// world-space `origin` in unit direction `dir` within `max_t`
362    /// world voxel units.
363    fn occluded_world(&self, origin: [f32; 3], dir: [f32; 3], max_t: f32) -> bool;
364}
365
366/// XS.1 — per-grid context for a cross-scene shadow query: the scene-wide
367/// [`WorldOccluder`] plus the **current grid's** local→world transform, so a
368/// grid-local shadow ray (the frame `shade_dynamic` works in) can be lifted
369/// to world space before the scene-wide test. `cols[i]` is the world-space
370/// image of grid-local axis `i` (the grid rotation's columns); `origin` is the
371/// grid's world origin.
372#[derive(Clone, Copy)]
373pub struct WorldShadowCtx<'a> {
374    /// The scene-wide occlusion oracle every lifted shadow ray is
375    /// tested against.
376    pub occluder: &'a dyn WorldOccluder,
377    /// The current grid's world-space origin — the translation part of
378    /// its local→world transform (added to rotated positions;
379    /// directions skip it).
380    pub origin: [f32; 3],
381    /// Columns of the grid's local→world rotation: `cols[i]` is the
382    /// world-space image of grid-local axis `i`. Assumed orthonormal
383    /// so grid-local ray lengths equal world lengths.
384    pub cols: [[f32; 3]; 3],
385}
386
387impl<'a> WorldShadowCtx<'a> {
388    /// Identity transform — for shading already in world space (sprites): the
389    /// grid-local ray IS the world ray.
390    #[must_use]
391    pub fn identity(occluder: &'a dyn WorldOccluder) -> Self {
392        Self {
393            occluder,
394            origin: [0.0; 3],
395            cols: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
396        }
397    }
398}
399
400/// XS.2 — a [`WorldOccluder`] that ORs two others (e.g. the grid occluder +
401/// the sprite occluder), so a single shadow query covers both. `true` if
402/// either blocks the ray.
403pub struct CompositeOccluder<'a> {
404    /// First occluder — queried first (short-circuits `b` on a hit).
405    pub a: &'a dyn WorldOccluder,
406    /// Second occluder — queried only when `a` reports no hit.
407    pub b: &'a dyn WorldOccluder,
408}
409
410impl WorldOccluder for CompositeOccluder<'_> {
411    fn occluded_world(&self, origin: [f32; 3], dir: [f32; 3], max_t: f32) -> bool {
412        self.a.occluded_world(origin, dir, max_t) || self.b.occluded_world(origin, dir, max_t)
413    }
414}
415
416/// XS.1 — [`ShadowTester`] that lifts a grid-local shadow ray to world space
417/// (via [`WorldShadowCtx`]) and queries the scene-wide [`WorldOccluder`], so
418/// occlusion crosses grid + sprite boundaries. Sprites (already world-space)
419/// use an identity [`WorldShadowCtx`] (see [`WorldShadowCtx::identity`]).
420pub(crate) struct WorldShadow<'a> {
421    pub ctx: WorldShadowCtx<'a>,
422}
423
424impl ShadowTester for WorldShadow<'_> {
425    fn occluded(&mut self, origin: [f32; 3], dir: [f32; 3], max_t: f32) -> bool {
426        let c = &self.ctx.cols;
427        // world = grid_origin + R · local (R columns = `cols`); dir rotates only.
428        let wo = [
429            self.ctx.origin[0] + c[0][0] * origin[0] + c[1][0] * origin[1] + c[2][0] * origin[2],
430            self.ctx.origin[1] + c[0][1] * origin[0] + c[1][1] * origin[1] + c[2][1] * origin[2],
431            self.ctx.origin[2] + c[0][2] * origin[0] + c[1][2] * origin[1] + c[2][2] * origin[2],
432        ];
433        let wd = [
434            c[0][0] * dir[0] + c[1][0] * dir[1] + c[2][0] * dir[2],
435            c[0][1] * dir[0] + c[1][1] * dir[1] + c[2][1] * dir[2],
436            c[0][2] * dir[0] + c[1][2] * dir[1] + c[2][2] * dir[2],
437        ];
438        self.ctx.occluder.occluded_world(wo, wd, max_t)
439    }
440}
441
442/// CPU.1 — dynamic-lighting shade for a terrain voxel (the CPU mirror of the
443/// GPU `shade_lit`): raw albedo × (ambient/AO + sun + point lights), evaluated
444/// **flat per voxel** (at the voxel centre, so a whole face reads one tone —
445/// the retro look). `bands > 0` quantizes (cel) and gradient-maps the sun key
446/// from `shadow_tint` (cool) to the sun colour (warm). **No shadows.** Returns
447/// a packed `0x80RRGGBB` colour (same convention as [`shade`]).
448fn shade_lit_cpu(
449    color: u32,
450    bright_sub: u32,
451    axis: usize,
452    step: [i32; 3],
453    cellc: [i32; 3],
454    cell_size: f32,
455    l: &CpuLights<'_>,
456    shadow: Option<&mut dyn ShadowTester>,
457) -> u32 {
458    let a_b = ((color >> 24) & 0xff).saturating_sub(bright_sub);
459    let ao = a_b as f32 / 128.0;
460    let albedo = [
461        ((color >> 16) & 0xff) as f32 / 255.0,
462        ((color >> 8) & 0xff) as f32 / 255.0,
463        (color & 0xff) as f32 / 255.0,
464    ];
465    let n = face_normal_cpu(axis, step);
466    // Voxel centre (grid-local) — flat per-voxel sample point.
467    let center = [
468        (cellc[0] as f32 + 0.5) * cell_size,
469        (cellc[1] as f32 + 0.5) * cell_size,
470        (cellc[2] as f32 + 0.5) * cell_size,
471    ];
472    shade_dynamic(albedo, ao, n, center, l, shadow)
473}
474
475/// CPU.1/DL.7 — the shared dynamic-lighting core (terrain + sprites): raw
476/// `albedo` × (ambient/AO + sun + point lights), sampled **flat per voxel**
477/// at `sample` with surface normal `n`. `bands > 0` quantizes (cel) and
478/// gradient-maps the sun key from `shadow_tint` (cool) to the sun colour
479/// (warm). **No shadows** (GPU-only). Returns a packed `0x80RRGGBB` colour.
480pub(crate) fn shade_dynamic(
481    albedo: [f32; 3],
482    ao: f32,
483    n: [f32; 3],
484    sample: [f32; 3],
485    l: &CpuLights<'_>,
486    shadow: Option<&mut dyn ShadowTester>,
487) -> u32 {
488    let styled = l.bands > 0;
489    // CPU.2 — shadow ray origin: bias off the surface along the normal to
490    // avoid self-shadow acne (shared by every caster). Light kept in
491    // shadow = `1 - shadow_strength` (1.0 ⇒ shadows effectively off).
492    let mut shadow = shadow;
493    let shadow_origin = [
494        sample[0] + n[0] * l.shadow_bias,
495        sample[1] + n[1] * l.shadow_bias,
496        sample[2] + n[2] * l.shadow_bias,
497    ];
498    let in_shadow = 1.0 - l.shadow_strength;
499
500    // Sun key (0..1): N·L × shadow factor.
501    let sun_key = if l.sun {
502        let ndl = dot3(n, l.sun_dir).max(0.0);
503        if ndl > 0.0 && l.sun_casts_shadow {
504            let occ = shadow
505                .as_deref_mut()
506                .is_some_and(|s| s.occluded(shadow_origin, l.sun_dir, l.shadow_max_dist));
507            if occ {
508                ndl * in_shadow
509            } else {
510                ndl
511            }
512        } else {
513            ndl
514        }
515    } else {
516        0.0
517    };
518
519    // Base term: ambient + sun. Smooth = additive; stylized = gradient map.
520    let mut lit = if styled {
521        let key = cel_band(sun_key, l.bands);
522        let m = |i: usize| {
523            let warm = l.sun_color[i] * l.sun_intensity;
524            (l.shadow_tint[i] + (warm - l.shadow_tint[i]) * key) * ao
525        };
526        [albedo[0] * m(0), albedo[1] * m(1), albedo[2] * m(2)]
527    } else {
528        let base = |i: usize| {
529            albedo[i] * l.ambient[i] * ao + albedo[i] * l.sun_color[i] * l.sun_intensity * sun_key
530        };
531        [base(0), base(1), base(2)]
532    };
533
534    // Point lights (flat per voxel). CPU.2 — a flagged caster's shadow ray
535    // marches to the light; an occluded sample keeps `in_shadow` of it.
536    for p in l.points {
537        let d3 = [
538            p.pos[0] - sample[0],
539            p.pos[1] - sample[1],
540            p.pos[2] - sample[2],
541        ];
542        // PF.7 (C4) — squared-distance reject first: the sqrt only runs
543        // for lights actually within radius (same thresholds squared).
544        let d2 = d3[0] * d3[0] + d3[1] * d3[1] + d3[2] * d3[2];
545        if d2 < p.radius * p.radius && d2 > 1e-8 {
546            let dist = d2.sqrt();
547            let inv = 1.0 / dist;
548            let ldir = [d3[0] * inv, d3[1] * inv, d3[2] * inv];
549            let ndl = dot3(n, ldir).max(0.0);
550            // SL — spot cone mask (1.0 for a pure point light). Computed
551            // before the shadow march so an off-cone spot skips it entirely.
552            let cone = spot_cone(ldir, p.spot_dir, p.cos_inner, p.cos_outer);
553            if ndl > 0.0 && cone > 0.0 {
554                // Shadow ray marches from the surface to the light (`dist`).
555                let sh = if p.casts_shadow
556                    && shadow
557                        .as_deref_mut()
558                        .is_some_and(|s| s.occluded(shadow_origin, ldir, dist))
559                {
560                    in_shadow
561                } else {
562                    1.0
563                };
564                let mut f = ndl * point_falloff(dist, p.radius) * cone * sh;
565                if styled {
566                    f = cel_band(f, l.bands);
567                }
568                for i in 0..3 {
569                    lit[i] += albedo[i] * p.color[i] * p.intensity * f;
570                }
571            }
572        }
573    }
574
575    let pack = |v: f32| -> u32 { (v.clamp(0.0, 1.0) * 255.0) as u32 };
576    0x8000_0000 | (pack(lit[0]) << 16) | (pack(lit[1]) << 8) | pack(lit[2])
577}
578
579/// Blend `color` toward `env.fog_color` by perpendicular `depth`
580/// (linear, fully fogged at `env.fog_max_dist`). No-op when fog is
581/// disabled (`fog_max_dist <= 0`).
582#[inline]
583fn apply_fog(color: u32, depth: f32, env: &DdaEnv<'_>) -> u32 {
584    if env.fog_max_dist <= 0.0 {
585        return color;
586    }
587    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
588    let f = ((depth / env.fog_max_dist).clamp(0.0, 1.0) * 256.0) as u32; // 0..256
589    let g = 256 - f;
590    let fog = env.fog_color;
591    let mix = |shift: u32| -> u32 {
592        let src = (color >> shift) & 0xff;
593        let dst = (fog >> shift) & 0xff;
594        ((src * g + dst * f) >> 8).min(255)
595    };
596    0x8000_0000 | (mix(16) << 16) | (mix(8) << 8) | mix(0)
597}
598
599/// Composite premultiplied `accum` (+ remaining `trans`) over a packed
600/// background colour → packed `0x80RRGGBB`.
601#[inline]
602fn composite_over(accum: [f32; 3], trans: f32, bg: u32) -> u32 {
603    let b = rgb_to_f32(bg);
604    f32_to_rgb([
605        accum[0] + trans * b[0],
606        accum[1] + trans * b[1],
607        accum[2] + trans * b[2],
608    ])
609}
610
611/// Finalize a translucent terrain ray that exited the grid (sky). Returns
612/// `None` when nothing was accumulated (the opaque first-hit path — the
613/// caller's sky handling stands, bit-identical), else the accumulated
614/// layers composited over the sky at `dist`.
615#[inline]
616fn finalize_exit(
617    touched: bool,
618    accum: [f32; 3],
619    trans: f32,
620    env: &DdaEnv<'_>,
621    dir: [f32; 3],
622    dist: f32,
623) -> Option<Hit> {
624    if !touched {
625        return None;
626    }
627    let bg = match env.sky {
628        Some(s) => sample_sky(s, dir),
629        None => 0x8000_0000 | (env.fog_color & 0x00ff_ffff),
630    };
631    Some(Hit {
632        color: composite_over(accum, trans, bg),
633        dist,
634    })
635}
636
637/// Unpack `0x__RRGGBB` to `0..1` float channels (RGB; the high byte is
638/// dropped — it has already been folded into the colour by `shade`/`fog`).
639#[inline]
640#[allow(clippy::cast_precision_loss)]
641fn rgb_to_f32(c: u32) -> [f32; 3] {
642    [
643        ((c >> 16) & 0xff) as f32 / 255.0,
644        ((c >> 8) & 0xff) as f32 / 255.0,
645        (c & 0xff) as f32 / 255.0,
646    ]
647}
648
649/// Repack `0..1` float channels (clamped) into `0x80RRGGBB`.
650#[inline]
651#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
652fn f32_to_rgb(c: [f32; 3]) -> u32 {
653    let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u32;
654    0x8000_0000 | (q(c[0]) << 16) | (q(c[1]) << 8) | q(c[2])
655}
656
657/// Sample the sky panorama in ray direction `dir` (need not be
658/// normalised), returning a packed `0x80RRGGBB` colour.
659///
660/// Clean-room equirectangular mapping (not voxlap's `lng`/`lat` asm
661/// search): the texture's x axis is elevation (`asin` of the vertical
662/// component), the y axis is azimuth (`atan2` around the vertical). A
663/// `ysiz == 1` panorama (e.g. [`Sky::blue_gradient`]) is a pure
664/// horizon→zenith gradient.
665#[allow(
666    clippy::cast_possible_truncation,
667    clippy::cast_sign_loss,
668    clippy::cast_precision_loss
669)]
670fn sample_sky(sky: &Sky, dir: [f32; 3]) -> u32 {
671    let len = (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]).sqrt();
672    if len < 1e-9 {
673        return 0x8000_0000;
674    }
675    let d = [dir[0] / len, dir[1] / len, dir[2] / len];
676    let xsiz_full = sky.lat.len().max(1) as i32; // original column count
677    let pi = std::f32::consts::PI;
678    // Elevation → x, matching the GPU `sky_color` (scene_dda.wgsl): z is
679    // down, so `acos(-z)` is 0 at the zenith (looking up) and π at the nadir
680    // (looking down); `/π` puts the zenith at x=0 and the nadir at x=xsiz.
681    let elev01 = (-d[2]).clamp(-1.0, 1.0).acos() / pi; // 0 (up) .. 1 (down)
682    let x = (elev01 * xsiz_full as f32) as i32;
683    let x = x.clamp(0, xsiz_full - 1);
684    // Azimuth → y (wrapped).
685    let y = if sky.ysiz <= 1 {
686        0
687    } else {
688        let az = d[1].atan2(d[0]); // -pi..pi
689        let yf = ((az / (pi * 2.0)) + 0.5) * sky.ysiz as f32;
690        (yf as i32).rem_euclid(sky.ysiz)
691    };
692    let idx = (y * xsiz_full + x) as usize;
693    let px = sky.pixels.get(idx).copied().unwrap_or(0) as u32;
694    0x8000_0000 | (px & 0x00ff_ffff)
695}
696
697/// Fill the panorama [`Sky`] into every **background** pixel — one whose
698/// z-buffer entry is still `+∞` (no grid/terrain hit). The per-grid DDA only
699/// samples the sky inside each grid's screen rect (and only its sky-owning
700/// grid); pixels outside any grid — most of a sprite/effect-only view, or the
701/// margins around a small world grid — would otherwise keep the caller's flat
702/// clear colour. This paints the real panorama there while leaving terrain
703/// (finite z) and composited translucent pixels untouched. The z-buffer is
704/// not modified. `cam`/`settings` are the same per-frame projection the
705/// renderer used.
706#[allow(clippy::cast_possible_truncation)]
707pub fn render_sky_fill(
708    fb: &mut [u32],
709    zb: &[f32],
710    pitch_pixels: usize,
711    width: u32,
712    height: u32,
713    cam: &CameraState,
714    settings: &OpticastSettings,
715    sky: &Sky,
716) {
717    // PF.7 (C6) — rayon rows: this was a full-frame single-threaded pass
718    // with an `acos` + `atan2` per background pixel (an entire serial
719    // frame on sky-dominant views). Rows are disjoint (`by_ref` chunks of
720    // the framebuffer); `zb` is read-only. Bit-identical.
721    fb.par_chunks_mut(pitch_pixels)
722        .take(height as usize)
723        .enumerate()
724        .for_each(|(py, frow)| {
725            let row = py * pitch_pixels;
726            #[allow(clippy::cast_possible_truncation)]
727            let py = py as u32;
728            for px in 0..width {
729                let idx = row + px as usize;
730                if zb[idx].is_finite() {
731                    continue; // a grid/terrain hit owns this pixel
732                }
733                let (_origin, dir) = pixel_ray(cam, settings, px, py);
734                frow[px as usize] = sample_sky(sky, dir);
735            }
736        });
737}
738
739/// World-space ray for screen pixel `(px, py)` under opticast's
740/// pinhole: origin is the camera position, direction is
741/// `(px - hx)·right + (py - hy)·down + hz·forward`.
742///
743/// This is the exact ray `camera_math::derive` bakes into its corner
744/// vectors (`corn[0]` is `pixel (0, 0)`'s direction), so the DDA
745/// renderer samples the same rays the voxlap path's frustum is built
746/// from. The direction is **not** normalised — callers that need a
747/// unit ray (and a true Euclidean distance) normalise themselves;
748/// DDA.1 will track perpendicular distance via the forward-projection
749/// instead, matching the engine's z-buffer convention.
750#[must_use]
751pub fn pixel_ray(
752    cs: &CameraState,
753    settings: &OpticastSettings,
754    px: u32,
755    py: u32,
756) -> ([f32; 3], [f32; 3]) {
757    // u32 → f32 is exact for any realistic screen coordinate.
758    #[allow(clippy::cast_precision_loss)]
759    let sx = px as f32 - settings.hx;
760    #[allow(clippy::cast_precision_loss)]
761    let sy = py as f32 - settings.hy;
762    let dir = [
763        sx * cs.right[0] + sy * cs.down[0] + settings.hz * cs.forward[0],
764        sx * cs.right[1] + sy * cs.down[1] + settings.hz * cs.forward[1],
765        sx * cs.right[2] + sy * cs.down[2] + settings.hz * cs.forward[2],
766    ];
767    (cs.pos, dir)
768}
769
770/// Ray ↔ axis-aligned box `[lo, hi]` slab test. Returns the
771/// `(t_enter, t_exit)` parameter interval along `dir` (already clamped
772/// so `t_enter >= 0`, i.e. a camera inside the box starts at `t = 0`),
773/// or `None` if the ray misses the box. `dir` need not be normalised —
774/// `t` is in units of `|dir|`.
775pub(crate) fn intersect_aabb(
776    o: [f32; 3],
777    dir: [f32; 3],
778    lo: [f32; 3],
779    hi: [f32; 3],
780) -> Option<(f32, f32)> {
781    let mut t0 = 0.0f32;
782    let mut t1 = f32::INFINITY;
783    for a in 0..3 {
784        if dir[a].abs() < 1e-9 {
785            // Ray parallel to this slab — must already be inside it.
786            if o[a] < lo[a] || o[a] > hi[a] {
787                return None;
788            }
789        } else {
790            let inv = 1.0 / dir[a];
791            let mut ta = (lo[a] - o[a]) * inv;
792            let mut tb = (hi[a] - o[a]) * inv;
793            if ta > tb {
794                core::mem::swap(&mut ta, &mut tb);
795            }
796            t0 = t0.max(ta);
797            t1 = t1.min(tb);
798            if t0 > t1 {
799                return None;
800            }
801        }
802    }
803    Some((t0, t1))
804}
805
806/// Brick edge length in voxels — one occupancy bit per `BRICK³` block.
807const BRICK: i32 = 8;
808
809/// Per-chunk brick occupancy map for two-level DDA empty-space skip
810/// (Substage DDA.3).
811///
812/// One bit per `BRICK³` block of the active chunk, set iff any voxel in
813/// the block is solid. The ray steps the coarse brick grid (8× longer
814/// strides) and only descends into a per-voxel walk inside occupied
815/// bricks, so a ray through open air crosses ~`length / 8` empty bricks
816/// instead of `length` air voxels — each of which would otherwise walk
817/// the column slab chain via `surface_color`.
818///
819/// Built per frame from a [`GridView`] in [`render_dda`]. A persistent
820/// per-chunk cache with edit-driven invalidation (locked decision #2 in
821/// `PORTING-DDA.md`) is a later perf refinement.
822#[derive(Debug)]
823pub(crate) struct BrickMap {
824    /// Brick counts along x / y / z (one entry per `BRICK³` cells).
825    nb: [i32; 3],
826    /// Brick occupancy bitset; brick `(bx, by, bz)` is bit
827    /// `(bz * nb[1] + by) * nb[0] + bx`.
828    bits: Vec<u64>,
829    /// Super-brick counts (one entry per `BRICK³` *bricks* = `SUPER³`
830    /// cells), `ceil(nb / BRICK)`.
831    ns: [i32; 3],
832    /// Super-brick occupancy (DDA.7 perf): a coarse level so a ray
833    /// through open air above the terrain skips `SUPER` cells per outer
834    /// step instead of `BRICK`. A super-brick is set iff any child brick
835    /// is set.
836    super_bits: Vec<u64>,
837}
838
839/// Super-brick edge in cells (`BRICK` bricks per axis).
840const SUPER: i32 = BRICK * BRICK;
841
842impl BrickMap {
843    /// Scan every mip-`mip` column of `grid`, building brick + super-
844    /// brick occupancy. `mip` must be `< grid.mip_count()`.
845    #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
846    fn build(grid: &GridView<'_>, mip: u32) -> Self {
847        let vsid_m = (grid.vsid >> mip).max(1) as i32;
848        let z_m = (crate::grid_view::CHUNK_SIZE_Z >> mip).max(1) as i32;
849        let nb = [
850            (vsid_m + BRICK - 1) / BRICK,
851            (vsid_m + BRICK - 1) / BRICK,
852            (z_m + BRICK - 1) / BRICK,
853        ];
854        let ns = [
855            (nb[0] + BRICK - 1) / BRICK,
856            (nb[1] + BRICK - 1) / BRICK,
857            (nb[2] + BRICK - 1) / BRICK,
858        ];
859        let count = (nb[0] * nb[1] * nb[2]) as usize;
860        let scount = (ns[0] * ns[1] * ns[2]) as usize;
861        let mut bits = vec![0u64; count.div_ceil(64)];
862        let mut super_bits = vec![0u64; scount.div_ceil(64)];
863        for y in 0..vsid_m {
864            for x in 0..vsid_m {
865                let (bx, by) = (x / BRICK, y / BRICK);
866                grid.for_each_run_mip(x as u32, y as u32, mip, |top, bot| {
867                    for bz in (top / BRICK)..=((bot - 1) / BRICK) {
868                        let idx = ((bz * nb[1] + by) * nb[0] + bx) as usize;
869                        bits[idx / 64] |= 1u64 << (idx % 64);
870                        let sidx =
871                            (((bz / BRICK) * ns[1] + by / BRICK) * ns[0] + bx / BRICK) as usize;
872                        super_bits[sidx / 64] |= 1u64 << (sidx % 64);
873                    }
874                });
875            }
876        }
877        Self {
878            nb,
879            bits,
880            ns,
881            super_bits,
882        }
883    }
884
885    /// Whether brick `b` is in range and holds any solid voxel.
886    #[inline]
887    #[allow(clippy::cast_sign_loss)]
888    fn occupied(&self, b: [i32; 3]) -> bool {
889        if b[0] < 0
890            || b[0] >= self.nb[0]
891            || b[1] < 0
892            || b[1] >= self.nb[1]
893            || b[2] < 0
894            || b[2] >= self.nb[2]
895        {
896            return false;
897        }
898        let idx = ((b[2] * self.nb[1] + b[1]) * self.nb[0] + b[0]) as usize;
899        (self.bits[idx / 64] >> (idx % 64)) & 1 != 0
900    }
901
902    /// Whether super-brick `s` is in range and holds any solid voxel.
903    #[inline]
904    #[allow(clippy::cast_sign_loss)]
905    fn occupied_super(&self, s: [i32; 3]) -> bool {
906        if s[0] < 0
907            || s[0] >= self.ns[0]
908            || s[1] < 0
909            || s[1] >= self.ns[1]
910            || s[2] < 0
911            || s[2] >= self.ns[2]
912        {
913            return false;
914        }
915        let idx = ((s[2] * self.ns[1] + s[1]) * self.ns[0] + s[0]) as usize;
916        (self.super_bits[idx / 64] >> (idx % 64)) & 1 != 0
917    }
918}
919
920/// Per-axis 3D-DDA stepping state for a cell size of `cell` voxels.
921/// `t_max[a]` is the ray parameter at which the next `a`-boundary is
922/// crossed; `t_delta[a]` is the parameter increment per cell. An
923/// axis-parallel component gets `t_max = t_delta = +inf` so it's never
924/// chosen as the stepping axis.
925pub(crate) fn dda_setup(
926    origin: [f32; 3],
927    dir: [f32; 3],
928    cell: [i32; 3],
929    cell_size: f32,
930) -> ([i32; 3], [f32; 3], [f32; 3]) {
931    let mut step = [0i32; 3];
932    let mut t_max = [f32::INFINITY; 3];
933    let mut t_delta = [f32::INFINITY; 3];
934    for a in 0..3 {
935        if dir[a] > 1e-9 {
936            step[a] = 1;
937            #[allow(clippy::cast_precision_loss)]
938            let boundary = (cell[a] + 1) as f32 * cell_size;
939            t_max[a] = (boundary - origin[a]) / dir[a];
940            t_delta[a] = cell_size / dir[a];
941        } else if dir[a] < -1e-9 {
942            step[a] = -1;
943            #[allow(clippy::cast_precision_loss)]
944            let boundary = cell[a] as f32 * cell_size;
945            t_max[a] = (boundary - origin[a]) / dir[a];
946            t_delta[a] = -cell_size / dir[a];
947        }
948    }
949    (step, t_max, t_delta)
950}
951
952/// Index of the axis with the smallest `t_max` (the next boundary the
953/// ray crosses).
954#[inline]
955pub(crate) fn min_axis(t_max: [f32; 3]) -> usize {
956    if t_max[0] <= t_max[1] && t_max[0] <= t_max[2] {
957        0
958    } else if t_max[1] <= t_max[2] {
959        1
960    } else {
961        2
962    }
963}
964
965/// Persistent, cross-frame brick occupancy cache (Substage DDA.7
966/// perf). Keyed by `(chunk x, y, z, mip)` with the chunk's edit
967/// `version`; an entry is reused until its chunk's version changes, so a
968/// static / streamed-once world pays **zero** brick-build cost after the
969/// first frame (the per-frame rebuild was the dominant DDA cost).
970///
971/// Owned by the caller across frames (the scene's `Grid`), populated
972/// single-threaded via [`Self::ensure`], then borrowed immutably by the
973/// parallel render bands.
974#[derive(Debug, Default)]
975pub struct BrickCache {
976    maps: HashMap<(i32, i32, i32, u32), (u64, BrickMap)>,
977}
978
979impl BrickCache {
980    /// An empty cache (same as [`Default`]). Entries appear via
981    /// [`ensure`](Self::ensure) as chunks are first rendered.
982    #[must_use]
983    pub fn new() -> Self {
984        Self::default()
985    }
986
987    /// Ensure a current mip-`mip` brick map exists for `chunk` (built
988    /// from `view`); rebuilds only when the cached `version` differs.
989    pub fn ensure(&mut self, chunk: [i32; 3], mip: u32, version: u64, view: &GridView<'_>) {
990        let key = (chunk[0], chunk[1], chunk[2], mip);
991        let stale = self.maps.get(&key).map_or(true, |(v, _)| *v != version);
992        if stale {
993            self.maps.insert(key, (version, BrickMap::build(view, mip)));
994        }
995    }
996
997    #[inline]
998    fn get(&self, chunk: [i32; 3], mip: u32) -> Option<&BrickMap> {
999        self.maps
1000            .get(&(chunk[0], chunk[1], chunk[2], mip))
1001            .map(|(_, m)| m)
1002    }
1003
1004    /// Drop cached entries whose chunk fails `keep` — bounds memory as
1005    /// streaming evicts chunks. Called once per frame by the scene.
1006    pub fn retain_chunks(&mut self, keep: impl Fn([i32; 3]) -> bool) {
1007        self.maps.retain(|k, _| keep([k.0, k.1, k.2]));
1008    }
1009
1010    /// PF.9 — occupancy of the `BRICK`³ block containing chunk-local
1011    /// mip-`mip` cell `cell` of `chunk`. `None` when no map is cached for
1012    /// that (chunk, mip) — the caller must fall back to dense stepping.
1013    /// `Some(false)` guarantees the whole 8³ block holds no solid voxel,
1014    /// so an external shadow march may skip it wholesale.
1015    #[must_use]
1016    pub fn brick_occupied_at(&self, chunk: [i32; 3], mip: u32, cell: [i32; 3]) -> Option<bool> {
1017        self.get(chunk, mip)
1018            .map(|m| m.occupied([cell[0] >> 3, cell[1] >> 3, cell[2] >> 3]))
1019    }
1020
1021    /// PF.9 — like [`Self::brick_occupied_at`] for the `SUPER`³ (64³ at
1022    /// mip 0) super-brick level.
1023    #[must_use]
1024    pub fn super_occupied_at(&self, chunk: [i32; 3], mip: u32, cell: [i32; 3]) -> Option<bool> {
1025        self.get(chunk, mip)
1026            .map(|m| m.occupied_super([cell[0] >> 6, cell[1] >> 6, cell[2] >> 6]))
1027    }
1028}
1029
1030/// Build a throwaway [`BrickCache`] covering every populated chunk of
1031/// `grid` at the effective mip — for the sequential [`render_dda`] /
1032/// tests, where no persistent cache is threaded in. Returns
1033/// `(cache, effective_mip)`.
1034#[allow(clippy::cast_possible_wrap)]
1035fn local_cache(grid: &GridView<'_>, requested_mip: u32) -> (BrickCache, u32) {
1036    let mip = effective_mip(grid, requested_mip);
1037    let mut cache = BrickCache::new();
1038    if let Some(cg) = grid.chunk_grid {
1039        for dz in 0..cg.chunks_z as i32 {
1040            for dy in 0..cg.chunks_y as i32 {
1041                for dx in 0..cg.chunks_x as i32 {
1042                    let slot = ((dz * cg.chunks_y as i32 + dy) * cg.chunks_x as i32 + dx) as usize;
1043                    if let Some(Some(view)) = cg.chunks.get(slot) {
1044                        let ch = [
1045                            cg.origin_chunk_xy[0] + dx,
1046                            cg.origin_chunk_xy[1] + dy,
1047                            cg.origin_chunk_z + dz,
1048                        ];
1049                        cache.ensure(ch, mip, 0, view);
1050                    }
1051                }
1052            }
1053        }
1054    } else {
1055        cache.ensure([0, 0, 0], mip, 0, grid);
1056    }
1057    (cache, mip)
1058}
1059
1060/// Clamp a requested render mip to one every populated chunk actually
1061/// has built — so the uniform-mip traversal never under-samples a chunk
1062/// that lacks the requested level (which would punch holes). `0` short-
1063/// circuits (always available).
1064#[must_use]
1065pub fn effective_mip(grid: &GridView<'_>, requested: u32) -> u32 {
1066    if requested == 0 {
1067        return 0;
1068    }
1069    let mut m = requested;
1070    if let Some(cg) = grid.chunk_grid {
1071        for c in cg.chunks.iter().flatten() {
1072            m = m.min(c.mip_count().saturating_sub(1));
1073        }
1074    } else {
1075        m = m.min(grid.mip_count().saturating_sub(1));
1076    }
1077    m
1078}
1079
1080/// Cross-chunk voxel sampler (Substage DDA.4 / DDA.7).
1081///
1082/// Resolves a grid-local voxel coordinate to the chunk that owns it
1083/// (via [`GridView::chunk_at_xyz`]) and answers the DDA's per-voxel hit
1084/// query — brick-gated [`GridView::surface_color`]. It borrows the
1085/// shared immutable [`BrickMaps`] and caches the **current chunk**
1086/// (`cur_*`: view + brick-map reference): a ray usually stays in one
1087/// chunk for many voxels, so the per-voxel cost is a single index
1088/// compare + an O(1) brick bit test — no hashing, no mutation. Holding
1089/// only shared borrows, a `Sampler` is cheap to spin up per render band.
1090///
1091/// Single-chunk grids are the degenerate case: every voxel maps to
1092/// chunk `[0, 0, 0]` (= the view itself).
1093struct Sampler<'a> {
1094    grid: GridView<'a>,
1095    bricks: &'a BrickCache,
1096    /// Effective render mip (DDA.6). Traversal cells are mip-`mip`
1097    /// cells; sampling reads mip-`mip` data.
1098    mip: u32,
1099    /// Chunk size in mip-`mip` cells is a power of two; store it as
1100    /// `log2` (shift) + `size - 1` (mask) so [`Self::locate`] splits a
1101    /// cell into `(chunk, in-chunk)` with a shift + an `&` per axis
1102    /// instead of a signed `div_euclid` — the dominant per-cell cost.
1103    /// Arithmetic `>>` floors toward -∞ (= `div_euclid` for a positive
1104    /// power-of-two divisor) and `& mask` gives the non-negative
1105    /// remainder (= `rem_euclid`) even for negative cells (two's
1106    /// complement), so results are identical to the division form.
1107    xy_shift: u32,
1108    xy_mask: i32,
1109    z_shift: u32,
1110    z_mask: i32,
1111    cur_ch: [i32; 3],
1112    cur_view: Option<GridView<'a>>,
1113    cur_brick: Option<&'a BrickMap>,
1114    has_cur: bool,
1115}
1116
1117impl<'a> Sampler<'a> {
1118    fn new(grid: GridView<'a>, bricks: &'a BrickCache, mip: u32) -> Self {
1119        let cs_xy = (grid.chunk_size_xy >> mip).max(1);
1120        let cs_z = (crate::grid_view::CHUNK_SIZE_Z >> mip).max(1);
1121        debug_assert!(
1122            cs_xy.is_power_of_two() && cs_z.is_power_of_two(),
1123            "chunk dims must be powers of two for the shift/mask split"
1124        );
1125        #[allow(clippy::cast_possible_wrap)]
1126        Self {
1127            grid,
1128            bricks,
1129            mip,
1130            xy_shift: cs_xy.trailing_zeros(),
1131            xy_mask: cs_xy as i32 - 1,
1132            z_shift: cs_z.trailing_zeros(),
1133            z_mask: cs_z as i32 - 1,
1134            cur_ch: [0; 3],
1135            cur_view: None,
1136            cur_brick: None,
1137            has_cur: false,
1138        }
1139    }
1140
1141    /// Refresh the current-chunk cache (view + brick map) for `ch`.
1142    fn select_chunk(&mut self, ch: [i32; 3]) {
1143        if self.has_cur && self.cur_ch == ch {
1144            return;
1145        }
1146        self.cur_view = self.grid.chunk_at_xyz(ch);
1147        self.cur_brick = self.bricks.get(ch, self.mip);
1148        self.cur_ch = ch;
1149        self.has_cur = true;
1150    }
1151
1152    /// Split a grid-local **mip-`mip` cell** index into `(chunk index,
1153    /// in-chunk mip-cell)` via shift + mask (see field docs). Chunk
1154    /// indices are mip-independent; only the per-chunk resolution
1155    /// shrinks with mip.
1156    #[allow(clippy::cast_sign_loss)]
1157    fn locate(&self, c: [i32; 3]) -> ([i32; 3], [u32; 3]) {
1158        let ch = [
1159            c[0] >> self.xy_shift,
1160            c[1] >> self.xy_shift,
1161            c[2] >> self.z_shift,
1162        ];
1163        let loc = [
1164            (c[0] & self.xy_mask) as u32,
1165            (c[1] & self.xy_mask) as u32,
1166            (c[2] & self.z_mask) as u32,
1167        ];
1168        (ch, loc)
1169    }
1170
1171    /// Hit colour for grid-local mip-cell `c`, or `None` for air / empty
1172    /// chunk / uncoloured bedrock. Brick-gated so air inside a populated
1173    /// chunk costs only a bit test, not a slab walk.
1174    #[allow(clippy::cast_possible_wrap)]
1175    fn hit(&mut self, c: [i32; 3]) -> Option<u32> {
1176        #[cfg(test)]
1177        prof::SURF.with(|x| x.set(x.get() + 1));
1178        let (ch, loc) = self.locate(c);
1179        self.select_chunk(ch);
1180        let occupied = self.cur_brick.is_some_and(|bm| {
1181            bm.occupied([
1182                loc[0] as i32 / BRICK,
1183                loc[1] as i32 / BRICK,
1184                loc[2] as i32 / BRICK,
1185            ])
1186        });
1187        if !occupied {
1188            return None;
1189        }
1190        self.cur_view?
1191            .surface_color_mip(loc[0], loc[1], loc[2], self.mip)
1192            .map(|c| c.0)
1193    }
1194
1195    /// Chunk size in mip-cells along XY / Z (always a power of two).
1196    #[inline]
1197    fn cells_per_chunk_xy(&self) -> i32 {
1198        1 << self.xy_shift
1199    }
1200    #[inline]
1201    fn cells_per_chunk_z(&self) -> i32 {
1202        1 << self.z_shift
1203    }
1204
1205    /// Whether the brick at brick-index `brick` (in `BRICK`-mip-cell
1206    /// units) holds any solid voxel. Used by the outer brick-DDA to skip
1207    /// empty space `BRICK` cells at a time. Assumes bricks nest within
1208    /// chunks (caller gates on [`Self::cells_per_chunk_xy`]`>= BRICK`).
1209    #[allow(clippy::cast_sign_loss)]
1210    fn brick_occupied(&mut self, brick: [i32; 3]) -> bool {
1211        // First mip-cell of the brick (BRICK = 8 → `<< 3`).
1212        let c0 = [brick[0] << 3, brick[1] << 3, brick[2] << 3];
1213        let ch = [
1214            c0[0] >> self.xy_shift,
1215            c0[1] >> self.xy_shift,
1216            c0[2] >> self.z_shift,
1217        ];
1218        self.select_chunk(ch);
1219        self.cur_brick.is_some_and(|bm| {
1220            bm.occupied([
1221                (c0[0] & self.xy_mask) >> 3,
1222                (c0[1] & self.xy_mask) >> 3,
1223                (c0[2] & self.z_mask) >> 3,
1224            ])
1225        })
1226    }
1227
1228    /// Whether the super-brick at super-index `s` (in `SUPER`-mip-cell
1229    /// units) holds any solid voxel. Outer-most empty-space skip (steps
1230    /// `SUPER` cells). Assumes super-bricks nest in chunks (caller gates
1231    /// on `cells_per_chunk >= SUPER`).
1232    #[allow(clippy::cast_sign_loss)]
1233    fn super_occupied(&mut self, s: [i32; 3]) -> bool {
1234        // First mip-cell of the super-brick (SUPER = 64 → `<< 6`).
1235        let c0 = [s[0] << 6, s[1] << 6, s[2] << 6];
1236        let ch = [
1237            c0[0] >> self.xy_shift,
1238            c0[1] >> self.xy_shift,
1239            c0[2] >> self.z_shift,
1240        ];
1241        self.select_chunk(ch);
1242        self.cur_brick.is_some_and(|bm| {
1243            bm.occupied_super([
1244                (c0[0] & self.xy_mask) >> 6,
1245                (c0[1] & self.xy_mask) >> 6,
1246                (c0[2] & self.z_mask) >> 6,
1247            ])
1248        })
1249    }
1250}
1251
1252/// CPU.2 — safety cap on a shadow ray's voxel steps (the `shadow_max_dist`
1253/// / light-distance bound is the real limit; this only backstops a
1254/// degenerate ray). Mirrors the GPU `shadow_max_steps`.
1255const SHADOW_MAX_STEPS: u32 = 1024;
1256
1257/// CPU.2 — [`ShadowTester`] backed by the render [`Sampler`]: a hard-shadow
1258/// occlusion march over the grid's mip-`mip` voxels. The march reuses the
1259/// same `sampler.hit()` occupancy the primary ray uses (so a shadow ray is
1260/// blocked by the same surfaces the camera sees) and the same `[lo_c, hi_c)`
1261/// voxel-box bounds, stepping a standard 3D-DDA until it hits a solid cell
1262/// (occluded), leaves the box / exceeds `max_t` (lit), or hits the step cap.
1263struct SamplerShadow<'s, 'a> {
1264    sampler: &'s mut Sampler<'a>,
1265    cell_size: f32,
1266    lo_c: [i32; 3],
1267    hi_c: [i32; 3],
1268}
1269
1270impl ShadowTester for SamplerShadow<'_, '_> {
1271    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
1272    fn occluded(&mut self, origin: [f32; 3], dir: [f32; 3], max_t: f32) -> bool {
1273        let cs = self.cell_size;
1274        // PF.9 (C3) — the shadow march gets the primary ray's empty-space
1275        // skip: fast-forward across empty super-bricks / bricks (skipping
1276        // an EMPTY box can never hide an occluder). The step budget is
1277        // consumed in Manhattan cell distance — exactly what the dense
1278        // walk would have spent crossing the same span — so the
1279        // `SHADOW_MAX_STEPS` truncation fires at the identical point and
1280        // hit/no-hit stays bit-compatible with the pre-skip march.
1281        let has_super =
1282            self.sampler.cells_per_chunk_xy() >= SUPER && self.sampler.cells_per_chunk_z() >= SUPER;
1283        let has_brick =
1284            self.sampler.cells_per_chunk_xy() >= BRICK && self.sampler.cells_per_chunk_z() >= BRICK;
1285        let mut cellc = [
1286            (origin[0] / cs).floor() as i32,
1287            (origin[1] / cs).floor() as i32,
1288            (origin[2] / cs).floor() as i32,
1289        ];
1290        let (step, mut t_max, t_delta) = dda_setup(origin, dir, cellc, cs);
1291        let inv = [
1292            if step[0] != 0 { 1.0 / dir[0] } else { 0.0 },
1293            if step[1] != 0 { 1.0 / dir[1] } else { 0.0 },
1294            if step[2] != 0 { 1.0 / dir[2] } else { 0.0 },
1295        ];
1296        let mut t_curr = 0.0f32;
1297        let mut used = 0u32;
1298        while used < SHADOW_MAX_STEPS {
1299            if cellc[0] < self.lo_c[0]
1300                || cellc[0] >= self.hi_c[0]
1301                || cellc[1] < self.lo_c[1]
1302                || cellc[1] >= self.hi_c[1]
1303                || cellc[2] < self.lo_c[2]
1304                || cellc[2] >= self.hi_c[2]
1305            {
1306                return false; // left the voxel box → no occluder ahead
1307            }
1308            if t_curr > max_t {
1309                return false; // past the cap / the light → unshadowed
1310            }
1311            // Empty-space skip (mirrors `cell_walk_skip`'s landing logic:
1312            // the exit axis is pinned to the integer boundary cell so the
1313            // next box's entry cell is always visited densely).
1314            let skip_shift = if has_super
1315                && !self
1316                    .sampler
1317                    .super_occupied([cellc[0] >> 6, cellc[1] >> 6, cellc[2] >> 6])
1318            {
1319                Some(6u32)
1320            } else if has_brick
1321                && !self
1322                    .sampler
1323                    .brick_occupied([cellc[0] >> 3, cellc[1] >> 3, cellc[2] >> 3])
1324            {
1325                Some(3u32)
1326            } else {
1327                None
1328            };
1329            if let Some(sh) = skip_shift {
1330                let mut best_t = f32::INFINITY;
1331                let mut best_axis = 3usize;
1332                let mut plane = [0i32; 3];
1333                for a in 0..3 {
1334                    if step[a] == 0 {
1335                        continue;
1336                    }
1337                    let idx = cellc[a] >> sh;
1338                    plane[a] = if step[a] > 0 {
1339                        (idx + 1) << sh
1340                    } else {
1341                        idx << sh
1342                    };
1343                    let tb = (plane[a] as f32 * cs - origin[a]) * inv[a];
1344                    if tb < best_t {
1345                        best_t = tb;
1346                        best_axis = a;
1347                    }
1348                }
1349                if best_axis == 3 {
1350                    return false;
1351                }
1352                let pb = [
1353                    origin[0] + dir[0] * (best_t + 1e-4),
1354                    origin[1] + dir[1] * (best_t + 1e-4),
1355                    origin[2] + dir[2] * (best_t + 1e-4),
1356                ];
1357                let mut nc = [
1358                    (pb[0] / cs).floor() as i32,
1359                    (pb[1] / cs).floor() as i32,
1360                    (pb[2] / cs).floor() as i32,
1361                ];
1362                nc[best_axis] = if step[best_axis] > 0 {
1363                    plane[best_axis]
1364                } else {
1365                    plane[best_axis] - 1
1366                };
1367                // Budget: the dense walk would have spent one step per
1368                // cell (Manhattan distance). If it runs out inside the
1369                // empty box the dense walk would have returned `false`
1370                // there — nothing solid to find inside it.
1371                let crossed =
1372                    cellc[0].abs_diff(nc[0]) + cellc[1].abs_diff(nc[1]) + cellc[2].abs_diff(nc[2]);
1373                if used.saturating_add(crossed) >= SHADOW_MAX_STEPS {
1374                    return false;
1375                }
1376                used += crossed;
1377                cellc = nc;
1378                for a in 0..3 {
1379                    if step[a] > 0 {
1380                        t_max[a] = ((cellc[a] + 1) as f32 * cs - origin[a]) * inv[a];
1381                    } else if step[a] < 0 {
1382                        t_max[a] = (cellc[a] as f32 * cs - origin[a]) * inv[a];
1383                    }
1384                }
1385                t_curr = best_t.max(t_curr);
1386                continue;
1387            }
1388            if self.sampler.hit(cellc).is_some() {
1389                return true; // a surface blocks the ray
1390            }
1391            let axis = min_axis(t_max);
1392            t_curr = t_max[axis];
1393            cellc[axis] += step[axis];
1394            t_max[axis] += t_delta[axis];
1395            used += 1;
1396        }
1397        false
1398    }
1399}
1400
1401/// Walk mip-cells along the ray within `[lo_c, hi_c)` and return the
1402/// first solid hit, with leak-free empty-space skipping (DDA.7 redux).
1403///
1404/// **Why one continuous DDA, not nested level-walks.** The previous
1405/// design ran an outer brick/super DDA that *jumped* whole bricks and
1406/// only descended into occupied ones. Stepping a coarse cell at a time
1407/// lets the ray slip diagonally **past an occupied coarse cell it only
1408/// touches at a shared edge/corner** — a leak that showed as bright
1409/// sky seams across thin diagonal walls (the cave-demo report). Here a
1410/// *single* cell-granularity DDA carries the exact `(cellc, t_max)`
1411/// state for the whole ray; it only ever **fast-forwards across an
1412/// empty super-brick / brick**, where skipping cannot miss anything.
1413/// The exit axis lands on the integer box-boundary cell (no float
1414/// re-floor on the critical axis), so the entry cell of the next —
1415/// possibly occupied — box is always visited densely. Result: hits are
1416/// bit-identical to the dense per-cell reference, with the empty-space
1417/// speed-up retained.
1418///
1419/// `cell_size` is the mip-cell edge in mip-0 voxels (`1 << mip`);
1420/// `fwd_dot = dir·forward` → perpendicular depth.
1421#[allow(
1422    clippy::too_many_arguments,
1423    clippy::cast_possible_truncation,
1424    clippy::cast_sign_loss,
1425    clippy::cast_precision_loss
1426)]
1427fn cell_walk_skip(
1428    origin: [f32; 3],
1429    dir: [f32; 3],
1430    fwd_dot: f32,
1431    sampler: &mut Sampler<'_>,
1432    lo_c: [i32; 3],
1433    hi_c: [i32; 3],
1434    cell_size: f32,
1435    t_enter: f32,
1436    t_exit: f32,
1437    max_dist: f32,
1438    env: &DdaEnv<'_>,
1439) -> Option<Hit> {
1440    let has_super = sampler.cells_per_chunk_xy() >= SUPER && sampler.cells_per_chunk_z() >= SUPER;
1441    let has_brick = sampler.cells_per_chunk_xy() >= BRICK && sampler.cells_per_chunk_z() >= BRICK;
1442
1443    let start = t_enter + 1e-4;
1444    let p = [
1445        origin[0] + dir[0] * start,
1446        origin[1] + dir[1] * start,
1447        origin[2] + dir[2] * start,
1448    ];
1449    let mut cellc = [
1450        ((p[0] / cell_size).floor() as i32).clamp(lo_c[0], hi_c[0] - 1),
1451        ((p[1] / cell_size).floor() as i32).clamp(lo_c[1], hi_c[1] - 1),
1452        ((p[2] / cell_size).floor() as i32).clamp(lo_c[2], hi_c[2] - 1),
1453    ];
1454    let (step, mut t_max, t_delta) = dda_setup(origin, dir, cellc, cell_size);
1455    // Reciprocal direction → the per-skip box-boundary t and the t_max
1456    // refresh use multiplies instead of divisions (the dominant skip
1457    // cost). `0.0` where `step == 0` (that axis' t_max stays +∞).
1458    let inv = [
1459        if step[0] != 0 { 1.0 / dir[0] } else { 0.0 },
1460        if step[1] != 0 { 1.0 / dir[1] } else { 0.0 },
1461        if step[2] != 0 { 1.0 / dir[2] } else { 0.0 },
1462    ];
1463    let mut t_curr = t_enter;
1464    let mut last_axis = 3usize;
1465    // World ray length per ray-parameter unit; divided by `cell_size` it turns
1466    // a cell's `t` span into its path length in voxel units (Volumetric weight).
1467    let dir_len = (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]).sqrt();
1468    // PF.7 (C4) — hoisted out of the hit block: "does anything cast a
1469    // shadow?" is ray-invariant, and the O(lights) `any()` scan ran per
1470    // hit (per translucent layer on glass/water rays).
1471    let shadow_casts = env.lights.enabled
1472        && env.lights.shadow_strength > 0.0
1473        && (env.lights.sun_casts_shadow || env.lights.points.iter().any(|p| p.casts_shadow));
1474
1475    // TV: front-to-back translucent accumulation. While no translucent voxel
1476    // is hit (`touched` stays false) every return is unchanged — the opaque
1477    // world renders bit-identically. `prev_*` drive per-span compositing (one
1478    // alpha layer per contiguous solid run or material change).
1479    let mut accum = [0.0f32; 3];
1480    let mut trans = 1.0f32;
1481    let mut touched = false;
1482    let mut prev_solid = false;
1483    let mut prev_mat = 0u8;
1484
1485    // Each iteration either advances ≥1 cell (dense) or ≥1 box (skip),
1486    // so the total cell span bounds the loop.
1487    let span = (hi_c[0] - lo_c[0]) + (hi_c[1] - lo_c[1]) + (hi_c[2] - lo_c[2]);
1488    let max_steps = span.max(0) as usize + 16;
1489    for _ in 0..max_steps {
1490        if cellc[0] < lo_c[0]
1491            || cellc[0] >= hi_c[0]
1492            || cellc[1] < lo_c[1]
1493            || cellc[1] >= hi_c[1]
1494            || cellc[2] < lo_c[2]
1495            || cellc[2] >= hi_c[2]
1496        {
1497            return finalize_exit(touched, accum, trans, env, dir, max_dist);
1498        }
1499        let depth = t_curr * fwd_dot;
1500        if depth > max_dist || t_curr > t_exit {
1501            return finalize_exit(touched, accum, trans, env, dir, max_dist);
1502        }
1503        // Fog is fully opaque at `fog_max_dist`: nothing beyond is
1504        // visible, so stop the ray there and return the fog colour
1505        // rather than traversing (and skip/step-counting) to the far box
1506        // wall. Both correct and the dominant perf win for foggy worlds —
1507        // it caps every ray's length at the fog distance.
1508        if env.fog_max_dist > 0.0 && depth >= env.fog_max_dist {
1509            let fog = 0x8000_0000 | (env.fog_color & 0x00ff_ffff);
1510            let color = if touched {
1511                composite_over(accum, trans, fog)
1512            } else {
1513                fog
1514            };
1515            return Some(Hit {
1516                color,
1517                dist: env.fog_max_dist,
1518            });
1519        }
1520
1521        // Empty-space skip: a whole empty super-brick, else an empty
1522        // brick. Skipping only empty boxes can never miss a surface.
1523        let skip_shift = if has_super
1524            && !sampler.super_occupied([cellc[0] >> 6, cellc[1] >> 6, cellc[2] >> 6])
1525        {
1526            Some(6u32)
1527        } else if has_brick
1528            && !sampler.brick_occupied([cellc[0] >> 3, cellc[1] >> 3, cellc[2] >> 3])
1529        {
1530            Some(3u32)
1531        } else {
1532            None
1533        };
1534        if let Some(sh) = skip_shift {
1535            #[cfg(test)]
1536            prof::BRICKS.with(|x| x.set(x.get() + 1));
1537            // Nearest box boundary along the ray (in cell units).
1538            let mut best_t = f32::INFINITY;
1539            let mut best_axis = 3usize;
1540            let mut plane = [0i32; 3];
1541            for a in 0..3 {
1542                if step[a] == 0 {
1543                    continue;
1544                }
1545                let idx = cellc[a] >> sh;
1546                plane[a] = if step[a] > 0 {
1547                    (idx + 1) << sh
1548                } else {
1549                    idx << sh
1550                };
1551                let tb = (plane[a] as f32 * cell_size - origin[a]) * inv[a];
1552                if tb < best_t {
1553                    best_t = tb;
1554                    best_axis = a;
1555                }
1556            }
1557            if best_axis == 3 {
1558                return finalize_exit(touched, accum, trans, env, dir, max_dist);
1559            }
1560            // Land just across the boundary; pin the exit axis to the
1561            // integer boundary cell so float error can't skip the next
1562            // box's entry cell. Other axes haven't crossed their box
1563            // boundary (best_t is the min), so the point's floor is safe.
1564            let pb = [
1565                origin[0] + dir[0] * (best_t + 1e-4),
1566                origin[1] + dir[1] * (best_t + 1e-4),
1567                origin[2] + dir[2] * (best_t + 1e-4),
1568            ];
1569            let mut nc = [
1570                (pb[0] / cell_size).floor() as i32,
1571                (pb[1] / cell_size).floor() as i32,
1572                (pb[2] / cell_size).floor() as i32,
1573            ];
1574            nc[best_axis] = if step[best_axis] > 0 {
1575                plane[best_axis]
1576            } else {
1577                plane[best_axis] - 1
1578            };
1579            // The skip crossed a box boundary; if that takes the ray out
1580            // of the grid box it has exited (sky) — return rather than
1581            // clamping back in-bounds, which would spin at the edge.
1582            if nc[0] < lo_c[0]
1583                || nc[0] >= hi_c[0]
1584                || nc[1] < lo_c[1]
1585                || nc[1] >= hi_c[1]
1586                || nc[2] < lo_c[2]
1587                || nc[2] >= hi_c[2]
1588            {
1589                return finalize_exit(touched, accum, trans, env, dir, max_dist);
1590            }
1591            cellc = nc;
1592            // Refresh t_max for the new cell (dir unchanged → t_delta and
1593            // step constant; axes with step==0 keep their +∞).
1594            for a in 0..3 {
1595                if step[a] > 0 {
1596                    t_max[a] = ((cellc[a] + 1) as f32 * cell_size - origin[a]) * inv[a];
1597                } else if step[a] < 0 {
1598                    t_max[a] = (cellc[a] as f32 * cell_size - origin[a]) * inv[a];
1599                }
1600            }
1601            t_curr = best_t.max(t_curr);
1602            last_axis = best_axis;
1603            prev_solid = false; // skipped empty space → next hit starts a run
1604            continue;
1605        }
1606
1607        // Occupied brick: dense per-cell surface test.
1608        #[cfg(test)]
1609        prof::CELLS.with(|x| x.set(x.get() + 1));
1610        if let Some(color) = sampler.hit(cellc) {
1611            let bright_sub = side_shade_sub(env, last_axis, step);
1612            // CPU.1 — dynamic lighting (flat per voxel) when a rig is active;
1613            // else the baked-byte `shade` path (byte-identical). CPU.2 — a
1614            // sun/point shadow march reuses this same `sampler` (occupancy +
1615            // box bounds); only built when a caster is actually flagged so
1616            // the no-shadow rig stays march-free.
1617            let shaded = if env.lights.enabled {
1618                let casts = shadow_casts;
1619                // Pick the shadow oracle: the scene-wide one (cross-grid +
1620                // sprites, XS.1) when present, else the single-grid Sampler;
1621                // `None` when no caster is flagged, so the rig stays
1622                // march-free. The two testers live in branch-local slots so
1623                // exactly one is borrowed for the `shade_lit_cpu` call.
1624                let mut world_sh;
1625                let mut sampler_sh;
1626                let tester: Option<&mut dyn ShadowTester> = if !casts {
1627                    None
1628                } else if let Some(ctx) = env.world_shadow {
1629                    world_sh = WorldShadow { ctx };
1630                    Some(&mut world_sh)
1631                } else {
1632                    sampler_sh = SamplerShadow {
1633                        sampler: &mut *sampler,
1634                        cell_size,
1635                        lo_c,
1636                        hi_c,
1637                    };
1638                    Some(&mut sampler_sh)
1639                };
1640                shade_lit_cpu(
1641                    color,
1642                    bright_sub,
1643                    last_axis,
1644                    step,
1645                    cellc,
1646                    cell_size,
1647                    &env.lights,
1648                    tester,
1649                )
1650            } else {
1651                shade(color, bright_sub)
1652            };
1653            let lit = apply_fog(shaded, depth.max(0.0), env);
1654            // PF.7 (C4) — one colour→material scan per hit: resolve the id
1655            // and the material together (the id was previously re-scanned
1656            // for the translucent path below).
1657            let (m, mat_id) = match env.materials {
1658                Some(table) if !env.terrain_materials.is_empty() => {
1659                    let id = material_for_color(env.terrain_materials, color);
1660                    (table.get(id), id)
1661                }
1662                _ => (Material::OPAQUE, 0),
1663            };
1664            if m.is_opaque() {
1665                // Opaque surface: the background. Return the first hit verbatim
1666                // when nothing translucent preceded it (bit-identical), else
1667                // composite the accumulated layers over it.
1668                let color = if touched {
1669                    composite_over(accum, trans, lit)
1670                } else {
1671                    lit
1672                };
1673                return Some(Hit {
1674                    color,
1675                    dist: depth.max(0.0),
1676                });
1677            }
1678            let a = f32::from(m.alpha) / 255.0;
1679            if matches!(m.mode, roxlap_formats::material::BlendMode::Volumetric) {
1680                // Per-cell Beer–Lambert: opacity weighted by the ray's path
1681                // length through this voxel (so a filled volume thickens
1682                // smoothly with depth, a sliver contributes ≈0). Occludes.
1683                let t_exit = t_max[min_axis(t_max)];
1684                let seg_len = (t_exit - t_curr).max(0.0) * dir_len / cell_size;
1685                let eff_a = 1.0 - (1.0 - a).powf(seg_len);
1686                let c = rgb_to_f32(lit);
1687                accum[0] += trans * eff_a * c[0];
1688                accum[1] += trans * eff_a * c[1];
1689                accum[2] += trans * eff_a * c[2];
1690                trans *= 1.0 - eff_a;
1691                touched = true;
1692                prev_mat = mat_id;
1693                if trans < 1.0 / 256.0 {
1694                    return Some(Hit {
1695                        color: f32_to_rgb(accum),
1696                        dist: depth.max(0.0),
1697                    });
1698                }
1699            } else if !prev_solid || mat_id != prev_mat {
1700                // AlphaBlend / Additive: one alpha layer per solid-run entry or
1701                // material change (per-span — avoids the voxel-grid striping
1702                // through a thick glass/water slab; thickness-independent).
1703                let c = rgb_to_f32(lit);
1704                accum[0] += trans * a * c[0];
1705                accum[1] += trans * a * c[1];
1706                accum[2] += trans * a * c[2];
1707                if !matches!(m.mode, roxlap_formats::material::BlendMode::Additive) {
1708                    trans *= 1.0 - a; // AlphaBlend occludes; Additive does not.
1709                }
1710                touched = true;
1711                prev_mat = mat_id;
1712                if trans < 1.0 / 256.0 {
1713                    return Some(Hit {
1714                        color: f32_to_rgb(accum),
1715                        dist: depth.max(0.0),
1716                    });
1717                }
1718            }
1719            prev_solid = true;
1720        } else {
1721            prev_solid = false;
1722        }
1723        let axis = min_axis(t_max);
1724        last_axis = axis;
1725        t_curr = t_max[axis];
1726        cellc[axis] += step[axis];
1727        t_max[axis] += t_delta[axis];
1728    }
1729    None
1730}
1731
1732/// Per-face brightness reduction for the hit face. `axis` is the axis
1733/// the ray crossed to enter the hit voxel (`3` = entry voxel, no face);
1734/// `step[axis]` gives the crossing direction. Maps to the
1735/// `[x-, x+, y-, y+, z-, z+]` `side_shades` entry of the face the ray
1736/// looks at (a `+step` crossing enters through the low / `-` face).
1737#[inline]
1738fn side_shade_sub(env: &DdaEnv<'_>, axis: usize, step: [i32; 3]) -> u32 {
1739    if axis >= 3 {
1740        return 0;
1741    }
1742    let face = axis * 2 + usize::from(step[axis] < 0);
1743    env.side_shades[face].max(0) as u32
1744}
1745
1746/// Cast one ray into the grid and return the first solid hit.
1747///
1748/// **DDA.4:** cross-chunk per-pixel 3D-DDA over the grid's full voxel
1749/// box ([`GridView::voxel_bounds`], spanning every chunk in XY **and**
1750/// Z). The [`Sampler`] resolves each stepped voxel to its chunk and
1751/// brick-gates the slab walk. Cross-chunk look-down (the case the
1752/// voxlap renderer needed the whole virtual-column stack for) falls out
1753/// of the box simply spanning `chunks_z` along Z.
1754fn cast_ray(
1755    origin: [f32; 3],
1756    dir: [f32; 3],
1757    forward: [f32; 3],
1758    sampler: &mut Sampler<'_>,
1759    settings: &OpticastSettings,
1760    env: &DdaEnv<'_>,
1761) -> Option<Hit> {
1762    let (lo_i, hi_i) = sampler.grid.voxel_bounds();
1763    #[allow(clippy::cast_precision_loss)]
1764    let lo_f = [lo_i[0] as f32, lo_i[1] as f32, lo_i[2] as f32];
1765    #[allow(clippy::cast_precision_loss)]
1766    let hi_f = [hi_i[0] as f32, hi_i[1] as f32, hi_i[2] as f32];
1767    let (t_enter, t_exit) = intersect_aabb(origin, dir, lo_f, hi_f)?;
1768    let fwd_dot = dir[0] * forward[0] + dir[1] * forward[1] + dir[2] * forward[2];
1769    #[allow(clippy::cast_precision_loss)]
1770    let max_dist = settings.max_scan_dist.max(1) as f32;
1771    let cell = 1i32 << sampler.mip;
1772    let cell_size = cell as f32;
1773    let lo_c = [
1774        lo_i[0].div_euclid(cell),
1775        lo_i[1].div_euclid(cell),
1776        lo_i[2].div_euclid(cell),
1777    ];
1778    let hi_c = [
1779        hi_i[0].div_euclid(cell),
1780        hi_i[1].div_euclid(cell),
1781        hi_i[2].div_euclid(cell),
1782    ];
1783    cell_walk_skip(
1784        origin, dir, fwd_dot, sampler, lo_c, hi_c, cell_size, t_enter, t_exit, max_dist, env,
1785    )
1786}
1787
1788/// Render one grid into `sink` with per-pixel 3D-DDA.
1789///
1790/// `camera` is the grid-local pose, `settings`
1791/// ([`OpticastSettings`]) carries the projection + viewport (including
1792/// the `y_start..y_end` strip bound), and `grid` is the per-frame
1793/// [`GridView`] borrow. `pitch_pixels` is the framebuffer
1794/// row stride in pixels (matches `ScalarRasterizer::new`'s argument).
1795///
1796/// On a miss, a textured sky ([`DdaEnv::sky`]) is sampled per ray
1797/// direction and written at `+inf` depth; with no textured sky the miss
1798/// writes nothing, so the caller's solid sky pre-fill shows (the
1799/// `render_scene_composed` path pre-fills it).
1800pub fn render_dda(
1801    camera: &Camera,
1802    settings: &OpticastSettings,
1803    grid: GridView<'_>,
1804    pitch_pixels: usize,
1805    env: &DdaEnv<'_>,
1806    mip: u32,
1807    sink: &mut impl PixelSink,
1808) {
1809    let cs = camera_math::derive(
1810        camera,
1811        settings.xres,
1812        settings.yres,
1813        settings.hx,
1814        settings.hy,
1815        settings.hz,
1816    );
1817
1818    // Sequential path builds a throwaway per-call cache (tests / single
1819    // grid). The parallel path takes a persistent cross-frame cache.
1820    let (cache, mip) = local_cache(&grid, mip);
1821    let mut sampler = Sampler::new(grid, &cache, mip);
1822
1823    for py in settings.y_start..settings.y_end {
1824        let row = py as usize * pitch_pixels;
1825        for px in settings.x_start..settings.x_end {
1826            if let Some((color, dist)) = pixel_result(&cs, settings, &mut sampler, env, px, py) {
1827                sink.put(row + px as usize, color, dist);
1828            }
1829        }
1830    }
1831}
1832
1833/// Resolve one pixel: a shaded + fogged hit colour, a sampled textured
1834/// sky on a miss, or `None` (miss with no textured sky → caller's
1835/// pre-fill stands). Shared by the sequential ([`render_dda`]) and
1836/// parallel ([`render_dda_parallel`]) drivers.
1837#[inline]
1838fn pixel_result(
1839    cs: &CameraState,
1840    settings: &OpticastSettings,
1841    sampler: &mut Sampler<'_>,
1842    env: &DdaEnv<'_>,
1843    px: u32,
1844    py: u32,
1845) -> Option<(u32, f32)> {
1846    let (origin, dir) = pixel_ray(cs, settings, px, py);
1847    if let Some(hit) = cast_ray(origin, dir, cs.forward, sampler, settings, env) {
1848        Some((hit.color, hit.dist))
1849    } else {
1850        env.sky.map(|sky| (sample_sky(sky, dir), f32::INFINITY))
1851    }
1852}
1853
1854/// Tile-parallel [`render_dda`] writing straight into `(fb, zb)`.
1855///
1856/// DDA pixels are independent, so the framebuffer splits into disjoint
1857/// horizontal bands rendered concurrently (rayon) — **bit-identical**
1858/// to the sequential render regardless of thread count, unlike voxlap's
1859/// per-strip discretisation. Each band spins up its own lightweight
1860/// `Sampler` over the shared, immutable `cache`.
1861///
1862/// `cache` must already hold current brick maps for every chunk at
1863/// `mip` (populate via [`BrickCache::ensure`]); `mip` is the effective
1864/// render mip ([`effective_mip`]). `(fb, zb)` use the standard
1865/// conventions (`0x80RRGGBB`; z = perp distance, smaller = closer); a
1866/// miss writes nothing unless [`DdaEnv::sky`] is set. `pitch_pixels` is
1867/// the row stride.
1868#[allow(clippy::cast_possible_truncation, clippy::too_many_arguments)]
1869pub fn render_dda_parallel(
1870    camera: &Camera,
1871    settings: &OpticastSettings,
1872    grid: GridView<'_>,
1873    fb: &mut [u32],
1874    zb: &mut [f32],
1875    pitch_pixels: usize,
1876    env: &DdaEnv<'_>,
1877    cache: &BrickCache,
1878    mip: u32,
1879) {
1880    debug_assert_eq!(fb.len(), zb.len());
1881    let (y0, y1) = (settings.y_start, settings.y_end);
1882    if y1 <= y0 {
1883        return;
1884    }
1885    let cs = camera_math::derive(
1886        camera,
1887        settings.xres,
1888        settings.yres,
1889        settings.hx,
1890        settings.hy,
1891        settings.hz,
1892    );
1893    let target = RasterTarget::new(fb, zb);
1894
1895    // PF.7 (C5) — small fixed bands + rayon work-stealing instead of one
1896    // equal band per thread: sky-heavy rows finish instantly while
1897    // horizon/terrain rows dominate, so an equal split left threads idle
1898    // for the tail of every frame. 8 rows amortises the per-band
1899    // `Sampler` construction while staying fine-grained enough to
1900    // balance. Bit-identical (pixels are independent; rows disjoint).
1901    let band = 8u32;
1902    let bands: Vec<(u32, u32)> = (y0..y1)
1903        .step_by(band as usize)
1904        .map(|s| (s, (s + band).min(y1)))
1905        .collect();
1906
1907    bands.par_iter().for_each(|&(by0, by1)| {
1908        let mut sampler = Sampler::new(grid, cache, mip);
1909        for py in by0..by1 {
1910            let row = py as usize * pitch_pixels;
1911            for px in settings.x_start..settings.x_end {
1912                if let Some((color, dist)) = pixel_result(&cs, settings, &mut sampler, env, px, py)
1913                {
1914                    let idx = row + px as usize;
1915                    // SAFETY: bands cover disjoint row ranges, so writes
1916                    // never alias across threads; `idx` is in-bounds for
1917                    // a `pitch * height`-sized buffer.
1918                    unsafe {
1919                        target.write_color(idx, color);
1920                        target.write_depth(idx, dist);
1921                    }
1922                }
1923            }
1924        }
1925    });
1926}
1927
1928/// Dense per-voxel reference cast for a **single-chunk** grid: walks
1929/// every voxel of `[0, vsid)² × [0, CHUNK_SIZE_Z)` calling
1930/// [`GridView::surface_color`] directly — no brick gate, no chunk
1931/// resolution. The equivalence oracle the brickmap + sampler
1932/// [`cast_ray`] is checked against in tests.
1933#[cfg(test)]
1934#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
1935fn cast_ray_reference(
1936    origin: [f32; 3],
1937    dir: [f32; 3],
1938    forward: [f32; 3],
1939    grid: &GridView<'_>,
1940    settings: &OpticastSettings,
1941) -> Option<Hit> {
1942    let nx = grid.vsid as f32;
1943    let nz = f32::from(u16::try_from(crate::grid_view::CHUNK_SIZE_Z).unwrap_or(256));
1944    #[allow(clippy::cast_possible_wrap)]
1945    let n_i = [
1946        grid.vsid as i32,
1947        grid.vsid as i32,
1948        crate::grid_view::CHUNK_SIZE_Z as i32,
1949    ];
1950    let (t_enter, t_exit) = intersect_aabb(origin, dir, [0.0; 3], [nx, nx, nz])?;
1951    let fwd_dot = dir[0] * forward[0] + dir[1] * forward[1] + dir[2] * forward[2];
1952    let max_dist = settings.max_scan_dist.max(1) as f32;
1953
1954    let start = t_enter + 1e-4;
1955    let p = [
1956        origin[0] + dir[0] * start,
1957        origin[1] + dir[1] * start,
1958        origin[2] + dir[2] * start,
1959    ];
1960    let mut voxel = [
1961        (p[0].floor() as i32).clamp(0, n_i[0] - 1),
1962        (p[1].floor() as i32).clamp(0, n_i[1] - 1),
1963        (p[2].floor() as i32).clamp(0, n_i[2] - 1),
1964    ];
1965    let (step, mut t_max, t_delta) = dda_setup(origin, dir, voxel, 1.0);
1966    let mut t_curr = t_enter;
1967    let max_steps = (n_i[0] + n_i[1] + n_i[2]) as usize + 8;
1968    for _ in 0..max_steps {
1969        if voxel[0] < 0
1970            || voxel[0] >= n_i[0]
1971            || voxel[1] < 0
1972            || voxel[1] >= n_i[1]
1973            || voxel[2] < 0
1974            || voxel[2] >= n_i[2]
1975        {
1976            return None;
1977        }
1978        let depth = t_curr * fwd_dot;
1979        if depth > max_dist || t_curr > t_exit {
1980            return None;
1981        }
1982        #[allow(clippy::cast_sign_loss)]
1983        if let Some(color) = grid.surface_color(voxel[0] as u32, voxel[1] as u32, voxel[2] as u32) {
1984            return Some(Hit {
1985                color: shade(color.0, 0),
1986                dist: depth.max(0.0),
1987            });
1988        }
1989        let axis = min_axis(t_max);
1990        t_curr = t_max[axis];
1991        voxel[axis] += step[axis];
1992        t_max[axis] += t_delta[axis];
1993    }
1994    None
1995}
1996
1997#[cfg(test)]
1998mod tests {
1999    use super::*;
2000    use roxlap_formats::VoxColor;
2001
2002    // CPU.1 — luminance of a packed colour's low-24-bit RGB.
2003    fn lum(p: u32) -> u32 {
2004        (p & 0xff) + ((p >> 8) & 0xff) + ((p >> 16) & 0xff)
2005    }
2006
2007    #[test]
2008    fn cel_band_quantizes_and_collapses() {
2009        // Two distinct factors round to the same band at bands=2.
2010        assert_eq!(cel_band(0.8, 2), cel_band(0.9, 2));
2011        assert!((cel_band(0.8, 2) - 1.0).abs() < 1e-6);
2012        // ...but a low factor lands on a different band.
2013        assert_ne!(cel_band(0.3, 2), cel_band(0.8, 2));
2014    }
2015
2016    #[test]
2017    fn shade_lit_cpu_sun_lights_by_facing() {
2018        // Grey voxel (brightness 0x80 = full ambient). Floor top face: hit via
2019        // a +z step (axis 2) ⇒ normal points up (-z).
2020        let color = 0x80_80_80_80;
2021        let step = [0, 0, 1];
2022        let base = CpuLights {
2023            enabled: true,
2024            sun: true,
2025            sun_color: [1.0; 3],
2026            sun_intensity: 1.0,
2027            ambient: [0.2; 3],
2028            ..CpuLights::default()
2029        };
2030        let facing = CpuLights {
2031            sun_dir: [0.0, 0.0, -1.0],
2032            ..base
2033        }; // toward sun = up
2034        let back = CpuLights {
2035            sun_dir: [0.0, 0.0, 1.0],
2036            ..base
2037        }; // sun below the face
2038        let lit = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &facing, None);
2039        let dark = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &back, None);
2040        assert!(
2041            lum(lit) > lum(dark),
2042            "sun facing the surface must brighten it: {lit:#08x} vs {dark:#08x}",
2043        );
2044    }
2045
2046    #[test]
2047    fn shade_dynamic_spot_cone_masks_off_axis() {
2048        // Surface at the origin, up-facing normal (-z, voxlap z-down); a light
2049        // 10 units "above" it (at -z). No ambient/AO ⇒ only the light shows.
2050        let albedo = [0.5, 0.5, 0.5];
2051        let n = [0.0, 0.0, -1.0];
2052        let sample = [0.0, 0.0, 0.0];
2053        let inner = 10.0f32.to_radians().cos();
2054        let outer = 15.0f32.to_radians().cos();
2055        let shade = |spot_dir: [f32; 3], cos_inner: f32, cos_outer: f32| {
2056            let pts = [CpuPointLight {
2057                pos: [0.0, 0.0, -10.0],
2058                color: [1.0; 3],
2059                intensity: 1.0,
2060                radius: 64.0,
2061                casts_shadow: false,
2062                spot_dir,
2063                cos_inner,
2064                cos_outer,
2065            }];
2066            let l = CpuLights {
2067                enabled: true,
2068                ambient: [0.0; 3],
2069                points: &pts,
2070                ..CpuLights::default()
2071            };
2072            shade_dynamic(albedo, 0.0, n, sample, &l, None)
2073        };
2074        // A pure point light (cos_outer = -1) ignores the axis entirely.
2075        let point = shade([0.0, 0.0, 1.0], -1.0, -1.0);
2076        // A spot whose axis shines straight down onto the surface (on-axis).
2077        let on_axis = shade([0.0, 0.0, 1.0], inner, outer);
2078        // Same spot aimed sideways ⇒ the surface is outside the cone.
2079        let off_axis = shade([1.0, 0.0, 0.0], inner, outer);
2080
2081        // On-axis (cd == 1) is fully inside the cone ⇒ identical to a point.
2082        assert_eq!(
2083            on_axis, point,
2084            "on-axis spot must equal the point light: {on_axis:#08x} vs {point:#08x}",
2085        );
2086        // Off-axis is masked to zero ⇒ only the (zero) ambient remains.
2087        assert!(
2088            lum(on_axis) > lum(off_axis),
2089            "off-axis spot must be darker: {on_axis:#08x} vs {off_axis:#08x}",
2090        );
2091        assert_eq!(lum(off_axis), 0, "off-cone spot contributes nothing");
2092    }
2093
2094    #[test]
2095    fn shade_lit_cpu_cel_terraces_sun() {
2096        // Two sun elevations with distinct N·L (0.8 / 0.9) collapse to one
2097        // band at bands=2 ⇒ identical stylized colour; smooth (bands=0) differs.
2098        let color = 0x80_80_80_80;
2099        let step = [0, 0, 1];
2100        let mk = |zc: f32, bands: u32| {
2101            let n = (1.0f32 - zc * zc).sqrt();
2102            CpuLights {
2103                enabled: true,
2104                sun: true,
2105                sun_dir: [n, 0.0, -zc], // ndl on the up face = zc
2106                sun_color: [1.0; 3],
2107                sun_intensity: 1.0,
2108                ambient: [0.1; 3],
2109                bands,
2110                ..CpuLights::default()
2111            }
2112        };
2113        let smooth_a = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &mk(0.8, 0), None);
2114        let smooth_b = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &mk(0.9, 0), None);
2115        assert_ne!(smooth_a, smooth_b, "smooth diffuse must vary with N·L");
2116        let cel_a = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &mk(0.8, 2), None);
2117        let cel_b = shade_lit_cpu(color, 0, 2, step, [0, 0, 0], 1.0, &mk(0.9, 2), None);
2118        assert_eq!(
2119            cel_a, cel_b,
2120            "cel banding must terrace both N·L to one level"
2121        );
2122    }
2123
2124    /// CPU.2 — the shadow application math (independent of the march): an
2125    /// occluded sun-lit sample keeps only `1 - shadow_strength` of the sun
2126    /// key, and `shadow_strength == 0` makes shadows invisible.
2127    #[test]
2128    fn shade_dynamic_sun_shadow_darkens() {
2129        struct Mock(bool);
2130        impl ShadowTester for Mock {
2131            fn occluded(&mut self, _: [f32; 3], _: [f32; 3], _: f32) -> bool {
2132                self.0
2133            }
2134        }
2135        let l = CpuLights {
2136            enabled: true,
2137            sun: true,
2138            sun_dir: [0.0, 0.0, -1.0], // up = toward the sun
2139            sun_color: [1.0; 3],
2140            sun_intensity: 1.0,
2141            sun_casts_shadow: true,
2142            ambient: [0.2; 3],
2143            shadow_strength: 0.7,
2144            shadow_bias: 1.5,
2145            shadow_max_dist: 64.0,
2146            ..CpuLights::default()
2147        };
2148        let albedo = [0.8; 3];
2149        let n = [0.0, 0.0, -1.0]; // up face, faces the sun
2150        let s = [0.5, 0.5, 0.5];
2151        let lit = shade_dynamic(albedo, 1.0, n, s, &l, Some(&mut Mock(false)));
2152        let shadowed = shade_dynamic(albedo, 1.0, n, s, &l, Some(&mut Mock(true)));
2153        assert!(
2154            lum(shadowed) < lum(lit),
2155            "an occluded sun face must darken: shadowed={shadowed:#08x} lit={lit:#08x}",
2156        );
2157        // strength 0 ⇒ no visible shadow even when occluded.
2158        let l0 = CpuLights {
2159            shadow_strength: 0.0,
2160            ..l
2161        };
2162        assert_eq!(
2163            shade_dynamic(albedo, 1.0, n, s, &l0, Some(&mut Mock(true))),
2164            shade_dynamic(albedo, 1.0, n, s, &l0, Some(&mut Mock(false))),
2165            "shadow_strength 0 ⇒ shadows invisible",
2166        );
2167    }
2168
2169    /// CPU.2 — the actual [`SamplerShadow`] march casts a sun shadow through
2170    /// the grid: a wall on a floor, lit by a grazing sun, darkens the floor
2171    /// in the wall's shadow. Total scene luminance with shadows enabled is
2172    /// strictly less than with them off (shadows only ever subtract), and
2173    /// the gap is non-trivial (a real shadow, not FP noise).
2174    #[test]
2175    fn sampler_shadow_march_casts_sun_shadow() {
2176        // Floor at z>=60; a thin wall at x==32 rising from the floor (z 30..60).
2177        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, _y, z| {
2178            if z >= 60 {
2179                Some(VoxColor(0x80_80_80_80)) // floor
2180            } else if x == 32 && (30..60).contains(&z) {
2181                Some(VoxColor(0x80_70_70_70)) // wall (distinct so it's not a dead branch)
2182            } else {
2183                None
2184            }
2185        });
2186        let grid = GridView::from_single_vxl(&vxl);
2187        // Straight-down camera over the floor (voxlap z-down: forward = +z).
2188        let cam = Camera {
2189            pos: [32.0, 32.0, 6.0],
2190            right: [1.0, 0.0, 0.0],
2191            down: [0.0, 1.0, 0.0],
2192            forward: [0.0, 0.0, 1.0],
2193        };
2194        // Sun grazing from +x and above ⇒ the wall shadows the floor at x<32.
2195        let inv = 1.0f32 / 2.0f32.sqrt();
2196        let base = CpuLights {
2197            enabled: true,
2198            sun: true,
2199            sun_dir: [inv, 0.0, -inv],
2200            sun_color: [1.0; 3],
2201            sun_intensity: 1.0,
2202            ambient: [0.25; 3],
2203            shadow_strength: 0.8,
2204            shadow_bias: 1.5,
2205            shadow_max_dist: 128.0,
2206            ..CpuLights::default()
2207        };
2208        let (w, h) = (96u32, 96u32);
2209        let lit_env = DdaEnv {
2210            lights: CpuLights {
2211                sun_casts_shadow: false,
2212                ..base
2213            },
2214            ..DdaEnv::default()
2215        };
2216        let shadow_env = DdaEnv {
2217            lights: CpuLights {
2218                sun_casts_shadow: true,
2219                ..base
2220            },
2221            ..DdaEnv::default()
2222        };
2223        let (fb_lit, _) = render_brickmap_env(grid, &cam, w, h, &lit_env);
2224        let (fb_sh, _) = render_brickmap_env(grid, &cam, w, h, &shadow_env);
2225        let sum: fn(&[u32]) -> u64 = |fb| fb.iter().map(|&p| u64::from(lum(p))).sum();
2226        let lit_sum = sum(&fb_lit);
2227        let sh_sum = sum(&fb_sh);
2228        assert!(
2229            sh_sum < lit_sum,
2230            "the wall's shadow must darken the floor: shadow_sum={sh_sum} lit_sum={lit_sum}",
2231        );
2232        // Non-trivial: at least a few % of the lit total was removed.
2233        assert!(
2234            (lit_sum - sh_sum) * 50 > lit_sum,
2235            "shadow should remove >2% of total luminance: lit={lit_sum} shadow={sh_sum}",
2236        );
2237    }
2238
2239    /// Recording sink: collects `(idx, color, dist)` puts for tests.
2240    #[derive(Default)]
2241    struct Recorder {
2242        puts: Vec<(usize, u32, f32)>,
2243    }
2244    impl PixelSink for Recorder {
2245        fn put(&mut self, idx: usize, color: u32, dist: f32) {
2246            self.puts.push((idx, color, dist));
2247        }
2248    }
2249
2250    fn oracle_camera() -> Camera {
2251        // Identity-basis camera at origin: ray math is integer-exact.
2252        Camera {
2253            pos: [0.0, 0.0, 0.0],
2254            right: [1.0, 0.0, 0.0],
2255            down: [0.0, 0.0, 1.0],
2256            forward: [0.0, 1.0, 0.0],
2257        }
2258    }
2259
2260    /// Render `grid` from `camera` into a `w × h` framebuffer and
2261    /// return the per-pixel hit mask (`true` where a ray hit a voxel).
2262    fn render_mask(grid: GridView<'_>, camera: &Camera, w: u32, h: u32) -> Vec<bool> {
2263        let n = (w as usize) * (h as usize);
2264        let mut fb = vec![0u32; n]; // sky sentinel = 0
2265        let mut zb = vec![f32::INFINITY; n];
2266        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
2267        {
2268            let mut sink = RasterSink::new(&mut fb, &mut zb);
2269            render_dda(
2270                camera,
2271                &settings,
2272                grid,
2273                w as usize,
2274                &DdaEnv::default(),
2275                0,
2276                &mut sink,
2277            );
2278        }
2279        fb.iter().map(|&c| c != 0).collect()
2280    }
2281
2282    /// A silhouette is "row-convex" if every framebuffer row's hit
2283    /// pixels form a single contiguous run (no interior gap). The
2284    /// voxlap silhouette notch is exactly such an interior gap, so this
2285    /// is the headline DDA.1 acceptance check.
2286    fn rows_have_no_holes(mask: &[bool], w: u32, h: u32) -> bool {
2287        let w = w as usize;
2288        for y in 0..h as usize {
2289            let row = &mask[y * w..(y + 1) * w];
2290            let first = row.iter().position(|&b| b);
2291            let last = row.iter().rposition(|&b| b);
2292            if let (Some(f), Some(l)) = (first, last) {
2293                if row[f..=l].iter().any(|&b| !b) {
2294                    return false;
2295                }
2296            }
2297        }
2298        true
2299    }
2300
2301    /// Same contiguity check down each column.
2302    fn cols_have_no_holes(mask: &[bool], w: u32, h: u32) -> bool {
2303        let w = w as usize;
2304        let h = h as usize;
2305        for x in 0..w {
2306            let col: Vec<bool> = (0..h).map(|y| mask[y * w + x]).collect();
2307            let first = col.iter().position(|&b| b);
2308            let last = col.iter().rposition(|&b| b);
2309            if let (Some(f), Some(l)) = (first, last) {
2310                if col[f..=l].iter().any(|&b| !b) {
2311                    return false;
2312                }
2313            }
2314        }
2315        true
2316    }
2317
2318    /// The principal-point pixel `(hx, hy)` looks straight down the
2319    /// forward axis, scaled by `hz`.
2320    #[test]
2321    fn center_pixel_ray_is_forward() {
2322        let settings = OpticastSettings::for_oracle_framebuffer(640, 480);
2323        let cs = camera_math::derive(&oracle_camera(), 640, 480, 320.0, 240.0, 320.0);
2324        // hx = hy = 320 / 240 → use the exact principal point.
2325        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
2326        let (origin, dir) = pixel_ray(&cs, &settings, settings.hx as u32, settings.hy as u32);
2327        assert_eq!(origin, [0.0, 0.0, 0.0]);
2328        // hz·forward = 320·[0,1,0].
2329        assert_eq!(
2330            dir.map(f32::to_bits),
2331            [0.0f32, 320.0, 0.0].map(f32::to_bits)
2332        );
2333    }
2334
2335    /// Pixel `(0, 0)`'s ray equals `camera_math`'s `corn[0]` — proving
2336    /// the DDA renderer samples the same rays the voxlap frustum is
2337    /// built from.
2338    #[test]
2339    fn corner_pixel_ray_matches_camera_corn0() {
2340        let settings = OpticastSettings::for_oracle_framebuffer(640, 480);
2341        let cs = camera_math::derive(&oracle_camera(), 640, 480, 320.0, 240.0, 320.0);
2342        let (_origin, dir) = pixel_ray(&cs, &settings, 0, 0);
2343        assert_eq!(dir.map(f32::to_bits), cs.corn[0].map(f32::to_bits));
2344    }
2345
2346    /// The renderer's independent slab decoder
2347    /// ([`GridView::voxel_color`]) must agree with the reference
2348    /// [`roxlap_formats::vxl::Vxl::voxel_color`] for every cell —
2349    /// including a column with an air gap, which exercises the
2350    /// ceiling-colour-list branch.
2351    #[test]
2352    fn gridview_voxel_color_matches_reference() {
2353        // Two solid runs per column separated by air → ceiling list.
2354        let vxl = roxlap_formats::vxl::Vxl::from_dense(8, |x, _, z| {
2355            let lo = (10..=12).contains(&z);
2356            let hi = (40..=42).contains(&z);
2357            (lo || hi).then_some(VoxColor(0x80_10_20_30 + x))
2358        });
2359        let grid = GridView::from_single_vxl(&vxl);
2360        for x in 0..8 {
2361            for y in 0..8 {
2362                for z in 0..64 {
2363                    assert_eq!(
2364                        grid.voxel_color(x, y, z),
2365                        vxl.voxel_color(x, y, z),
2366                        "mismatch at ({x},{y},{z})"
2367                    );
2368                }
2369            }
2370        }
2371    }
2372
2373    /// An all-air grid produces no hits (every ray misses).
2374    #[test]
2375    fn empty_grid_no_hits() {
2376        let vxl = roxlap_formats::vxl::Vxl::empty(64);
2377        let grid = GridView::from_single_vxl(&vxl);
2378        let settings = OpticastSettings::for_oracle_framebuffer(64, 48);
2379        let mut rec = Recorder::default();
2380        render_dda(
2381            &oracle_camera(),
2382            &settings,
2383            grid,
2384            64,
2385            &DdaEnv::default(),
2386            0,
2387            &mut rec,
2388        );
2389        assert!(rec.puts.is_empty(), "all-air grid must produce no hits");
2390    }
2391
2392    /// Camera above a solid floor, looking straight down: every ray
2393    /// hits, the recovered colour is the floor colour, and the centre
2394    /// pixel's depth ≈ the camera's height above the floor.
2395    #[test]
2396    fn floor_seen_from_above() {
2397        const FLOOR_Z: u32 = 40;
2398        const FLOOR_COL: VoxColor = VoxColor(0x80_30_60_90);
2399        let vxl =
2400            roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| (z >= FLOOR_Z).then_some(FLOOR_COL));
2401        let grid = GridView::from_single_vxl(&vxl);
2402
2403        // Eye above the floor (z is down), looking down (+z).
2404        let cam = Camera {
2405            pos: [16.0, 16.0, 10.0],
2406            right: [1.0, 0.0, 0.0],
2407            down: [0.0, 1.0, 0.0],
2408            forward: [0.0, 0.0, 1.0],
2409        };
2410        let settings = OpticastSettings::for_oracle_framebuffer(48, 48);
2411        let mut rec = Recorder::default();
2412        render_dda(&cam, &settings, grid, 48, &DdaEnv::default(), 0, &mut rec);
2413
2414        assert!(!rec.puts.is_empty(), "floor must be visible");
2415        // Centre pixel looks straight down → depth ≈ FLOOR_Z - eye_z.
2416        let centre = 24usize * 48 + 24;
2417        let hit = rec
2418            .puts
2419            .iter()
2420            .find(|(idx, _, _)| *idx == centre)
2421            .expect("centre ray must hit the floor");
2422        assert_eq!(hit.1 & 0x00ff_ffff, FLOOR_COL.0 & 0x00ff_ffff);
2423        let expected = (FLOOR_Z as f32) - 10.0;
2424        assert!(
2425            (hit.2 - expected).abs() < 1.5,
2426            "centre depth {} not ≈ {}",
2427            hit.2,
2428            expected
2429        );
2430    }
2431
2432    /// DDA.2: a camera looking at the horizon splits the frame into
2433    /// sky (upward rays miss → no write) and floor (downward rays hit).
2434    /// The top of the frame must be mostly sky, the bottom mostly
2435    /// floor.
2436    #[test]
2437    fn horizon_splits_sky_and_floor() {
2438        const FLOOR_Z: u32 = 40;
2439        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |_, _, z| {
2440            (z >= FLOOR_Z).then_some(VoxColor(0x80_44_66_88))
2441        });
2442        let grid = GridView::from_single_vxl(&vxl);
2443
2444        // At z=30 (above the z=40 floor), looking +y horizontally,
2445        // down = +z. Upward rays (low py) escape through the box top
2446        // (z=0) → sky; downward rays (high py) strike the floor.
2447        let cam = Camera {
2448            pos: [32.0, 4.0, 30.0],
2449            right: [-1.0, 0.0, 0.0],
2450            down: [0.0, 0.0, 1.0],
2451            forward: [0.0, 1.0, 0.0],
2452        };
2453        let (w, h) = (64u32, 64u32);
2454        let mask = render_mask(grid, &cam, w, h);
2455
2456        let count_band = |y0: usize, y1: usize| -> usize {
2457            (y0 * w as usize..y1 * w as usize)
2458                .filter(|&i| mask[i])
2459                .count()
2460        };
2461        let top = count_band(0, h as usize / 4);
2462        let bottom = count_band(3 * h as usize / 4, h as usize);
2463        assert!(mask.iter().any(|&b| b), "floor must be visible");
2464        assert!(mask.iter().any(|&b| !b), "sky must be visible");
2465        assert!(
2466            bottom > top,
2467            "bottom band ({bottom}) should hit more floor than top band ({top})"
2468        );
2469    }
2470
2471    /// Render `grid` from `camera` with the dense reference cast (no
2472    /// brickmap), returning `(colour, depth)` buffers.
2473    fn render_reference(
2474        grid: GridView<'_>,
2475        camera: &Camera,
2476        w: u32,
2477        h: u32,
2478    ) -> (Vec<u32>, Vec<f32>) {
2479        let n = (w as usize) * (h as usize);
2480        let mut fb = vec![0u32; n];
2481        let mut zb = vec![f32::INFINITY; n];
2482        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
2483        let cs = camera_math::derive(camera, w, h, settings.hx, settings.hy, settings.hz);
2484        for py in 0..h {
2485            for px in 0..w {
2486                let (o, d) = pixel_ray(&cs, &settings, px, py);
2487                if let Some(hit) = cast_ray_reference(o, d, cs.forward, &grid, &settings) {
2488                    let i = (py * w + px) as usize;
2489                    fb[i] = hit.color;
2490                    zb[i] = hit.dist;
2491                }
2492            }
2493        }
2494        (fb, zb)
2495    }
2496
2497    /// Render `grid` from `camera` via the production brickmap path.
2498    fn render_brickmap(
2499        grid: GridView<'_>,
2500        camera: &Camera,
2501        w: u32,
2502        h: u32,
2503    ) -> (Vec<u32>, Vec<f32>) {
2504        render_brickmap_env(grid, camera, w, h, &DdaEnv::default())
2505    }
2506
2507    /// As [`render_brickmap`] but with an explicit [`DdaEnv`] (fog /
2508    /// textured sky / side shades).
2509    fn render_brickmap_env(
2510        grid: GridView<'_>,
2511        camera: &Camera,
2512        w: u32,
2513        h: u32,
2514        env: &DdaEnv<'_>,
2515    ) -> (Vec<u32>, Vec<f32>) {
2516        let n = (w as usize) * (h as usize);
2517        let mut fb = vec![0u32; n];
2518        let mut zb = vec![f32::INFINITY; n];
2519        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
2520        {
2521            let mut sink = RasterSink::new(&mut fb, &mut zb);
2522            render_dda(camera, &settings, grid, w as usize, env, 0, &mut sink);
2523        }
2524        (fb, zb)
2525    }
2526
2527    /// Regression for the cave-demo "bright sky seams" report: the
2528    /// empty-space-skip walk must not leak past an occupied box the ray
2529    /// only grazes at a shared edge/corner. A 1-voxel-thick diagonal
2530    /// wall (`x+y==64`, voxels edge-connected) with air on both sides is
2531    /// the canonical case. The production skip walk must hit exactly the
2532    /// same pixels as the dense per-cell reference — zero divergence.
2533    #[test]
2534    fn no_sky_leak_through_diagonal_wall() {
2535        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
2536            ((x + y == 64) && (2..62).contains(&z)).then_some(VoxColor(0x80_40_80_60))
2537        });
2538        let grid = GridView::from_single_vxl(&vxl);
2539        let (w, h) = (160u32, 160u32);
2540        let c = [10.0, 10.0, 32.0];
2541        let poses = [
2542            Camera::from_yaw_pitch(c, 0.785, 0.0),
2543            Camera::from_yaw_pitch(c, 0.6, 0.1),
2544            Camera::from_yaw_pitch(c, 0.95, -0.1),
2545            Camera::from_yaw_pitch(c, 0.785, 0.3),
2546            Camera::from_yaw_pitch(c, 0.5, 0.0),
2547        ];
2548        for (i, cam) in poses.iter().enumerate() {
2549            let (fb_b, _) = render_brickmap(grid, cam, w, h);
2550            let (fb_r, _) = render_reference(grid, cam, w, h);
2551            let leak = (0..(w * h) as usize)
2552                .filter(|&k| (fb_b[k] != 0) != (fb_r[k] != 0))
2553                .count();
2554            assert_eq!(leak, 0, "pose {i}: {leak} px diverge from dense reference");
2555        }
2556    }
2557
2558    /// TV terrain transparency: a glass-coloured voxel slab in front of an
2559    /// opaque floor. With no terrain material map the glass is an opaque first
2560    /// hit; with the map it becomes translucent and the floor tints through.
2561    #[test]
2562    fn terrain_glass_tints_floor_behind() {
2563        let glass = VoxColor(0x80_40_C0_E0); // cyan
2564        let floor = VoxColor(0x80_C0_40_40); // red
2565        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
2566            if z == 4 {
2567                Some(glass)
2568            } else if z >= 10 {
2569                Some(floor)
2570            } else {
2571                None
2572            }
2573        });
2574        let grid = GridView::from_single_vxl(&vxl);
2575        // Camera above the grid looking straight down (+z), centred.
2576        let cam = Camera {
2577            pos: [8.0, 8.0, 0.0],
2578            right: [1.0, 0.0, 0.0],
2579            down: [0.0, 1.0, 0.0],
2580            forward: [0.0, 0.0, 1.0],
2581        };
2582        let (w, h) = (32u32, 32u32);
2583        let centre = (h / 2 * w + w / 2) as usize;
2584
2585        // Opaque: the glass voxel stops the ray (no terrain materials).
2586        let (fb_op, _) = render_brickmap(grid, &cam, w, h);
2587        assert_eq!(
2588            fb_op[centre] & 0x00ff_ffff,
2589            0x0040_C0E0,
2590            "opaque glass first-hit"
2591        );
2592
2593        // Translucent: glass colour → material 1 (alpha-blend).
2594        let mut table = MaterialTable::new();
2595        table.set(1, Material::alpha_blend(128));
2596        let env = DdaEnv {
2597            materials: Some(&table),
2598            terrain_materials: &[(glass.rgb_part(), 1)],
2599            lights: CpuLights::default(),
2600            ..DdaEnv::default()
2601        };
2602        let (fb_tr, _) = render_brickmap_env(grid, &cam, w, h, &env);
2603        assert_ne!(
2604            fb_tr[centre], fb_op[centre],
2605            "glass should composite over the floor, not stay opaque"
2606        );
2607        let r_op = (fb_op[centre] >> 16) & 0xff; // glass red ≈ 0x40
2608        let r_tr = (fb_tr[centre] >> 16) & 0xff; // + floor red bleeds in
2609        assert!(
2610            r_tr > r_op,
2611            "floor red tints through the glass (op={r_op:02x} tr={r_tr:02x})"
2612        );
2613    }
2614
2615    /// TV terrain Volumetric: a **filled** grey smoke volume over a red floor.
2616    /// Beer–Lambert opacity grows with the ray's path length, so a deeper smoke
2617    /// column shows more of its own colour (green channel rises toward the
2618    /// smoke grey) — thickness-dependent, unlike per-span AlphaBlend.
2619    #[test]
2620    fn terrain_volumetric_thickness_deepens_opacity() {
2621        let smoke = VoxColor(0x80_90_90_90); // grey
2622        let floor = VoxColor(0x80_C0_20_20); // red (low green)
2623                                             // Centre green channel for a smoke column `depth` voxels deep (filled),
2624                                             // floor at z>=12, camera looking straight down.
2625        let green_at = |depth: u32| -> u32 {
2626            let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
2627                if (4..4 + depth).contains(&z) {
2628                    Some(smoke)
2629                } else if z >= 12 {
2630                    Some(floor)
2631                } else {
2632                    None
2633                }
2634            });
2635            let grid = GridView::from_single_vxl(&vxl);
2636            let cam = Camera {
2637                pos: [8.0, 8.0, 0.0],
2638                right: [1.0, 0.0, 0.0],
2639                down: [0.0, 1.0, 0.0],
2640                forward: [0.0, 0.0, 1.0],
2641            };
2642            let (w, h) = (32u32, 32u32);
2643            let mut table = MaterialTable::new();
2644            table.set(1, Material::volumetric(80));
2645            let env = DdaEnv {
2646                materials: Some(&table),
2647                terrain_materials: &[(smoke.rgb_part(), 1)],
2648                lights: CpuLights::default(),
2649                ..DdaEnv::default()
2650            };
2651            let (fb, _) = render_brickmap_env(grid, &cam, w, h, &env);
2652            (fb[(h / 2 * w + w / 2) as usize] >> 8) & 0xff
2653        };
2654        let shallow = green_at(1);
2655        let deep = green_at(7);
2656        assert!(
2657            deep > shallow,
2658            "deeper Volumetric smoke shows more of its grey (deep g={deep:02x} > shallow g={shallow:02x})"
2659        );
2660    }
2661
2662    /// DDA.5: distance fog blends a hit toward the fog colour. A far
2663    /// floor pixel is closer to the fog colour than a near one.
2664    #[test]
2665    fn distance_fog_blends_toward_fog_color() {
2666        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |_, _, z| {
2667            (z >= 40).then_some(VoxColor(0x80_FF_FF_FF))
2668        });
2669        let grid = GridView::from_single_vxl(&vxl);
2670        let cam = Camera {
2671            pos: [32.0, 2.0, 38.0],
2672            right: [1.0, 0.0, 0.0],
2673            down: [0.0, 0.0, 1.0],
2674            forward: [0.0, 1.0, 0.0],
2675        };
2676        let env = DdaEnv {
2677            sky: None,
2678            fog_color: 0x00_00_00_00, // black fog → distance darkens
2679            fog_max_dist: 64.0,
2680            side_shades: [0; 6],
2681            materials: None,
2682            terrain_materials: &[],
2683            lights: CpuLights::default(),
2684            world_shadow: None,
2685        };
2686        let (w, h) = (64u32, 64u32);
2687        let (fog, _) = render_brickmap_env(grid, &cam, w, h, &env);
2688        let (nofog, zb) = render_brickmap(grid, &cam, w, h);
2689        let (idx, depth) = zb.iter().enumerate().filter(|(_, z)| z.is_finite()).fold(
2690            (0usize, 0.0f32),
2691            |acc, (i, &z)| {
2692                if z > acc.1 {
2693                    (i, z)
2694                } else {
2695                    acc
2696                }
2697            },
2698        );
2699        assert!(depth > 20.0, "need a deep pixel to test fog (got {depth})");
2700        let lum = |c: u32| (c & 0xff) + ((c >> 8) & 0xff) + ((c >> 16) & 0xff);
2701        assert!(
2702            lum(fog[idx]) < lum(nofog[idx]),
2703            "fogged pixel {:08x} not darker than {:08x}",
2704            fog[idx],
2705            nofog[idx]
2706        );
2707    }
2708
2709    /// DDA.5: with a textured sky, miss pixels are filled from the sky
2710    /// panorama (direction-dependent) instead of left at the pre-fill.
2711    #[test]
2712    fn textured_sky_fills_misses() {
2713        let sky = crate::sky::Sky::blue_gradient();
2714        let vxl = roxlap_formats::vxl::Vxl::empty(32); // all air → all miss
2715        let grid = GridView::from_single_vxl(&vxl);
2716        let env = DdaEnv {
2717            sky: Some(&sky),
2718            fog_color: 0,
2719            fog_max_dist: 0.0,
2720            side_shades: [0; 6],
2721            materials: None,
2722            terrain_materials: &[],
2723            lights: CpuLights::default(),
2724            world_shadow: None,
2725        };
2726        let cam = Camera::from_yaw_pitch([16.0, 16.0, 128.0], 0.3, -0.4);
2727        let (w, h) = (48u32, 48u32);
2728        let (fb, _) = render_brickmap_env(grid, &cam, w, h, &env);
2729        assert!(fb.iter().all(|&c| c >> 24 == 0x80), "all misses sky-filled");
2730        let top = fb[0];
2731        let bottom = fb[(h - 1) as usize * w as usize];
2732        assert_ne!(top, bottom, "sky gradient should vary with elevation");
2733    }
2734
2735    /// Sky elevation orientation matches the GPU `sky_color` (acos(-z)/π):
2736    /// looking **up** (−z) samples panorama column 0 (zenith), looking
2737    /// **down** (+z) samples the last column (nadir). Regression for the
2738    /// CPU up/down inversion.
2739    #[test]
2740    fn sky_elevation_zenith_at_column_zero() {
2741        let mut pixels = vec![0i32; 8];
2742        pixels[0] = 0x0011_1111; // zenith marker
2743        pixels[7] = 0x0099_9999; // nadir marker
2744        let sky = crate::sky::Sky::from_pixels(pixels, 8, 1);
2745        let up = sample_sky(&sky, [0.0, 0.0, -1.0]); // −z is up
2746        let down = sample_sky(&sky, [0.0, 0.0, 1.0]); // +z is down
2747        assert_eq!(
2748            up & 0x00ff_ffff,
2749            0x0011_1111,
2750            "looking up → column 0 (zenith)"
2751        );
2752        assert_eq!(
2753            down & 0x00ff_ffff,
2754            0x0099_9999,
2755            "looking down → last column (nadir)"
2756        );
2757    }
2758
2759    /// `render_sky_fill` paints the panorama for a **gridless** view — the
2760    /// same per-pixel sky sample the miss-ray path uses, with no grid present
2761    /// (the CPU empty-scene background, matching the GPU).
2762    #[test]
2763    fn sky_fill_paints_panorama_gridless() {
2764        let sky = crate::sky::Sky::blue_gradient();
2765        let cam = Camera::from_yaw_pitch([0.0, 0.0, 0.0], 0.3, -0.4);
2766        let (w, h) = (48u32, 48u32);
2767        let cs = crate::camera_math::derive(&cam, w, h, 24.0, 24.0, 24.0);
2768        let settings = crate::opticast::OpticastSettings::for_oracle_framebuffer(w, h);
2769        let mut fb = vec![0u32; (w * h) as usize];
2770        // All-background z-buffer (+∞) → every pixel gets the sky.
2771        let zb = vec![f32::INFINITY; (w * h) as usize];
2772        render_sky_fill(&mut fb, &zb, w as usize, w, h, &cs, &settings, &sky);
2773        assert!(
2774            fb.iter().all(|&c| c >> 24 == 0x80),
2775            "every pixel sky-filled with the brightness byte set"
2776        );
2777        let top = fb[0];
2778        let bottom = fb[(h - 1) as usize * w as usize];
2779        assert_ne!(top, bottom, "sky gradient should vary with elevation");
2780        // A finite-z (terrain) pixel is left untouched.
2781        let mut fb2 = vec![0x1234_5678u32; (w * h) as usize];
2782        let mut zb2 = vec![f32::INFINITY; (w * h) as usize];
2783        zb2[0] = 10.0; // pretend a terrain hit at pixel 0
2784        render_sky_fill(&mut fb2, &zb2, w as usize, w, h, &cs, &settings, &sky);
2785        assert_eq!(fb2[0], 0x1234_5678, "finite-z pixel is not overwritten");
2786    }
2787
2788    /// DDA.5: side shading darkens the hit face by its `side_shades`
2789    /// entry. A top-facing floor (ray crosses +z to enter) gets the
2790    /// `z-` face reduction (index 4).
2791    #[test]
2792    fn side_shades_darken_hit_face() {
2793        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
2794            (z >= 8).then_some(VoxColor(0x80_FF_FF_FF))
2795        });
2796        let grid = GridView::from_single_vxl(&vxl);
2797        let cam = Camera {
2798            pos: [8.0, 8.0, 2.0],
2799            right: [1.0, 0.0, 0.0],
2800            down: [0.0, 1.0, 0.0],
2801            forward: [0.0, 0.0, 1.0],
2802        };
2803        let centre = 16 * 32 + 16;
2804        let (plain, _) = render_brickmap(grid, &cam, 32, 32);
2805        let env = DdaEnv {
2806            sky: None,
2807            fog_color: 0,
2808            fog_max_dist: 0.0,
2809            side_shades: [0, 0, 0, 0, 0x40, 0],
2810            materials: None,
2811            terrain_materials: &[],
2812            lights: CpuLights::default(),
2813            world_shadow: None,
2814        };
2815        let (shaded, _) = render_brickmap_env(grid, &cam, 32, 32, &env);
2816        let lum = |c: u32| (c & 0xff) + ((c >> 8) & 0xff) + ((c >> 16) & 0xff);
2817        assert!(
2818            lum(shaded[centre]) < lum(plain[centre]),
2819            "side-shaded face {:08x} not darker than {:08x}",
2820            shaded[centre],
2821            plain[centre]
2822        );
2823    }
2824
2825    /// The two-level brick-skip cast closely approximates the dense
2826    /// per-voxel reference. The outer brick DDA re-seeds the inner cell
2827    /// walk at each occupied brick, so a few silhouette-boundary pixels
2828    /// jitter by one voxel (different hit cell → different colour/depth)
2829    /// — visually invisible, and the gain is ~`BRICK`× fewer air steps.
2830    /// Assert the divergence is tiny: coverage (hit/sky mask) is nearly
2831    /// identical and only a small fraction of pixels differ. (The
2832    /// thread-invariance guarantee is the separate, exact
2833    /// `parallel_matches_sequential`.)
2834    #[test]
2835    fn brickmap_approximates_dense_reference() {
2836        // Rolling heightmap + a floating block (air above and below).
2837        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
2838            let surf = 30 + ((x / 5 + y / 7) % 11);
2839            let ground = z >= surf;
2840            let block = (20..=24).contains(&z) && (10..20).contains(&x) && (40..50).contains(&y);
2841            (ground || block).then_some(VoxColor(0x80_30_50_70 + (x ^ y) % 0x40))
2842        });
2843        let grid = GridView::from_single_vxl(&vxl);
2844
2845        let (w, h) = (80u32, 80u32);
2846        let poses = [
2847            Camera::orbit(0.6, 0.5, 90.0, [32.0, 32.0, 40.0]),
2848            Camera::orbit(2.1, 0.2, 70.0, [32.0, 32.0, 35.0]),
2849            Camera::orbit(-1.0, 0.9, 120.0, [32.0, 32.0, 45.0]),
2850        ];
2851        let n = (w * h) as usize;
2852        for (i, cam) in poses.iter().enumerate() {
2853            let (fb_b, zb_b) = render_brickmap(grid, cam, w, h);
2854            let (fb_r, _zb_r) = render_reference(grid, cam, w, h);
2855            // Coverage (hit vs sky) must match almost exactly.
2856            let cov_b = fb_b.iter().filter(|&&c| c != 0).count();
2857            let cov_r = fb_r.iter().filter(|&&c| c != 0).count();
2858            assert!(cov_b > 200, "pose {i} rendered ~empty (cov {cov_b})");
2859            let cov_diff = cov_b.abs_diff(cov_r);
2860            assert!(
2861                cov_diff * 100 <= n, // < 1 % of pixels flip hit↔sky
2862                "pose {i} coverage diverged: brick {cov_b} vs dense {cov_r}"
2863            );
2864            // Colour diffs (boundary-voxel jitter) must be a small slice.
2865            let diffs = fb_b.iter().zip(&fb_r).filter(|(a, b)| a != b).count();
2866            assert!(
2867                diffs * 100 <= n * 3, // < 3 % of pixels differ
2868                "pose {i} too many pixel diffs vs dense: {diffs}/{n}"
2869            );
2870            // Depth must be sane (finite where hit), not wildly off.
2871            for k in 0..n {
2872                if fb_b[k] != 0 {
2873                    assert!(zb_b[k].is_finite(), "pose {i} px {k} non-finite depth");
2874                }
2875            }
2876        }
2877    }
2878
2879    /// DDA.5: a voxel's baked brightness byte darkens its colour. A
2880    /// half-bright voxel (`a = 0x40`) renders at roughly half RGB; a
2881    /// full-bright one (`a = 0x80`) is unchanged.
2882    #[test]
2883    fn baked_brightness_darkens_color() {
2884        // Half brightness: alpha 0x40 (64/128). White RGB → ~mid grey.
2885        let dim = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
2886            (z >= 8).then_some(VoxColor(0x40_FF_FF_FF))
2887        });
2888        let grid = GridView::from_single_vxl(&dim);
2889        let cam = Camera {
2890            pos: [8.0, 8.0, 2.0],
2891            right: [1.0, 0.0, 0.0],
2892            down: [0.0, 1.0, 0.0],
2893            forward: [0.0, 0.0, 1.0],
2894        };
2895        let (fb, _) = render_brickmap(grid, &cam, 32, 32);
2896        let centre = 16 * 32 + 16;
2897        // 0xFF * 64 >> 7 = 127 per channel; alpha normalised to 0x80.
2898        assert_eq!(fb[centre], 0x80_7F_7F_7F, "got {:08x}", fb[centre]);
2899
2900        // Full brightness passes RGB through unchanged.
2901        let full = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
2902            (z >= 8).then_some(VoxColor(0x80_FF_FF_FF))
2903        });
2904        let gridf = GridView::from_single_vxl(&full);
2905        let (fbf, _) = render_brickmap(gridf, &cam, 32, 32);
2906        assert_eq!(fbf[centre], 0x80_FF_FF_FF, "got {:08x}", fbf[centre]);
2907    }
2908
2909    /// DDA.4 headline gate: cross-chunk look-down. A camera in an
2910    /// all-air upper chunk (chz=0) looking straight down must see the
2911    /// floor in the *lower* stacked chunk (chz=1), through the chunk-Z
2912    /// boundary. This is exactly the case the voxlap renderer needed the
2913    /// whole virtual-column stack (S4B.6.j / VC) for; the DDA gets it
2914    /// for free from the outer box spanning `chunks_z`.
2915    #[test]
2916    fn cross_chunk_lookdown_sees_lower_stacked_floor() {
2917        const FLOOR_LOCAL_Z: u32 = 40;
2918        const FLOOR_COL: VoxColor = VoxColor(0x80_22_88_44);
2919        let upper = roxlap_formats::vxl::Vxl::empty(32); // all air + bedrock
2920        let lower = roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| {
2921            (z >= FLOOR_LOCAL_Z).then_some(FLOOR_COL)
2922        });
2923        let v_up = GridView::from_single_vxl(&upper);
2924        let v_lo = GridView::from_single_vxl(&lower);
2925        // Z-stack: index (dz*chunks_y+dy)*chunks_x+dx → [upper, lower].
2926        let chunks = [Some(v_up), Some(v_lo)];
2927        let cg = crate::ChunkGrid {
2928            chunks: &chunks,
2929            origin_chunk_xy: [0, 0],
2930            origin_chunk_z: 0,
2931            chunks_x: 1,
2932            chunks_y: 1,
2933            chunks_z: 2,
2934        };
2935        let grid = GridView::from_chunk_grid(&cg, 32);
2936
2937        // Camera in the upper chunk (world z=100), looking straight down.
2938        let cam = Camera {
2939            pos: [16.0, 16.0, 100.0],
2940            right: [1.0, 0.0, 0.0],
2941            down: [0.0, 1.0, 0.0],
2942            forward: [0.0, 0.0, 1.0],
2943        };
2944        let (w, h) = (48u32, 48u32);
2945        let (fb, zb) = render_brickmap(grid, &cam, w, h);
2946        let centre = 24 * 48 + 24;
2947        assert!(
2948            fb[centre] & 0x00ff_ffff == FLOOR_COL.0 & 0x00ff_ffff,
2949            "centre ray must reach the lower-chunk floor (got {:08x})",
2950            fb[centre]
2951        );
2952        // Floor world-z = 256 + 40 = 296; camera z = 100 → depth ≈ 196.
2953        let expected = 296.0 - 100.0;
2954        assert!(
2955            (zb[centre] - expected).abs() < 2.0,
2956            "look-down depth {} not ≈ {expected}",
2957            zb[centre]
2958        );
2959    }
2960
2961    /// DDA.4: a floor spanning two side-by-side chunks (chunks_x=2)
2962    /// renders continuously across the chunk-XY seam — hits on both
2963    /// sides, no gap column.
2964    #[test]
2965    fn cross_chunk_xy_floor_is_seamless() {
2966        let mk = || {
2967            roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| {
2968                (z >= 20).then_some(VoxColor(0x80_50_50_50))
2969            })
2970        };
2971        let (c0, c1) = (mk(), mk());
2972        let v0 = GridView::from_single_vxl(&c0);
2973        let v1 = GridView::from_single_vxl(&c1);
2974        let chunks = [Some(v0), Some(v1)];
2975        let cg = crate::ChunkGrid {
2976            chunks: &chunks,
2977            origin_chunk_xy: [0, 0],
2978            origin_chunk_z: 0,
2979            chunks_x: 2,
2980            chunks_y: 1,
2981            chunks_z: 1,
2982        };
2983        let grid = GridView::from_chunk_grid(&cg, 32);
2984
2985        // High above the seam (x=32), looking straight down.
2986        let cam = Camera {
2987            pos: [32.0, 16.0, 4.0],
2988            right: [1.0, 0.0, 0.0],
2989            down: [0.0, 1.0, 0.0],
2990            forward: [0.0, 0.0, 1.0],
2991        };
2992        let (w, h) = (64u32, 64u32);
2993        let mask = render_mask(grid, &cam, w, h);
2994        // Both the left chunk (screen left) and right chunk (screen
2995        // right) must show floor on the centre row.
2996        let row = (h / 2) as usize * w as usize;
2997        let left = (0..w as usize / 2).filter(|&x| mask[row + x]).count();
2998        let right = (w as usize / 2..w as usize)
2999            .filter(|&x| mask[row + x])
3000            .count();
3001        assert!(
3002            left > 5 && right > 5,
3003            "seam not continuous: left={left} right={right}"
3004        );
3005    }
3006
3007    /// Render `grid` from `camera` at render `mip` and return the hit
3008    /// mask.
3009    fn render_mask_mip(grid: GridView<'_>, camera: &Camera, w: u32, h: u32, mip: u32) -> Vec<bool> {
3010        let n = (w as usize) * (h as usize);
3011        let mut fb = vec![0u32; n];
3012        let mut zb = vec![f32::INFINITY; n];
3013        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
3014        {
3015            let mut sink = RasterSink::new(&mut fb, &mut zb);
3016            render_dda(
3017                camera,
3018                &settings,
3019                grid,
3020                w as usize,
3021                &DdaEnv::default(),
3022                mip,
3023                &mut sink,
3024            );
3025        }
3026        fb.iter().map(|&c| c != 0).collect()
3027    }
3028
3029    /// DDA.6: rendering a mip-built grid at a coarse mip stays complete
3030    /// (hole-free silhouette) with roughly the same screen coverage as
3031    /// mip 0 — LOD coarsens detail, it doesn't punch holes or shrink the
3032    /// shape. (DDA has no axis-aligned mip beam — the artifact is
3033    /// structurally impossible with honest per-cell traversal.)
3034    #[test]
3035    fn mip_render_is_coarse_but_complete() {
3036        let mut vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
3037            let surf = 24 + ((x / 3 + y / 5) % 17);
3038            (z >= surf).then_some(VoxColor(0x80_50_70_90))
3039        });
3040        vxl.generate_mips(4);
3041        assert!(vxl.mip_count() >= 3, "need mips built for this test");
3042        let grid = GridView::from_single_vxl(&vxl);
3043        let (w, h) = (96u32, 96u32);
3044        let cam = Camera::orbit(0.7, 0.6, 110.0, [32.0, 32.0, 36.0]);
3045
3046        let m0 = render_mask_mip(grid, &cam, w, h, 0);
3047        let m2 = render_mask_mip(grid, &cam, w, h, 2);
3048
3049        let c0 = m0.iter().filter(|&&b| b).count();
3050        let c2 = m2.iter().filter(|&&b| b).count();
3051        assert!(c0 > 200 && c2 > 200, "both mips visible (c0={c0} c2={c2})");
3052        // Coverage within ~30 % — a coarse-mip silhouette closely tracks
3053        // the fine one (LOD coarsens detail, it doesn't lose the shape).
3054        // (Terrain silhouettes are non-convex — sky shows through
3055        // valleys — so a hole-free invariant doesn't apply here; that's
3056        // the convex single-voxel test's job.)
3057        let ratio = c2 as f32 / c0 as f32;
3058        assert!(
3059            (0.7..1.4).contains(&ratio),
3060            "mip-2 coverage {c2} vs mip-0 {c0} (ratio {ratio:.2}) diverged"
3061        );
3062    }
3063
3064    /// Headless perf bench (run: `cargo test -p roxlap-core --release
3065    /// dda::tests::bench_terrain -- --ignored --nocapture`). Single-
3066    /// thread `render_dda` over a hilly chunk at a horizon pose; prints
3067    /// ms/frame + per-frame traversal counters (cells / bricks /
3068    /// surface_color calls) to locate the bottleneck.
3069    #[test]
3070    #[ignore = "perf benchmark — run explicitly with --ignored"]
3071    fn bench_terrain() {
3072        use std::time::Instant;
3073        // Multi-chunk grid like the demo: NC×NC chunks of 128, hills.
3074        const NC: i32 = 6;
3075        let cs = crate::grid_view::CHUNK_SIZE_Z; // 256, but vsid is 128
3076        let _ = cs;
3077        let mut vxls: Vec<roxlap_formats::vxl::Vxl> = Vec::new();
3078        for cy in 0..NC {
3079            for cx in 0..NC {
3080                let (ox, oy) = (cx * 128, cy * 128);
3081                let mut v = roxlap_formats::vxl::Vxl::from_dense(128, |x, y, z| {
3082                    let (gx, gy) = (ox + x as i32, oy + y as i32);
3083                    let surf = 90 + ((gx / 7 + gy / 9).rem_euclid(40)) + ((gx / 23).rem_euclid(20));
3084                    (z as i32 >= surf).then_some(VoxColor(0x80_50_70_90 + (x ^ y) % 0x30))
3085                });
3086                v.generate_mips(4);
3087                vxls.push(v);
3088            }
3089        }
3090        let views: Vec<Option<GridView>> = vxls
3091            .iter()
3092            .map(|v| Some(GridView::from_single_vxl(v)))
3093            .collect();
3094        let cg = crate::ChunkGrid {
3095            chunks: &views,
3096            origin_chunk_xy: [0, 0],
3097            origin_chunk_z: 0,
3098            chunks_x: NC as u32,
3099            chunks_y: NC as u32,
3100            chunks_z: 1,
3101        };
3102        let grid = GridView::from_chunk_grid(&cg, 128);
3103
3104        let (w, h) = (960u32, 600u32);
3105        let mut settings = OpticastSettings::for_oracle_framebuffer(w, h);
3106        settings.max_scan_dist = 512;
3107        let n = (w * h) as usize;
3108        let mut fb = vec![0u32; n];
3109        let mut zb = vec![f32::INFINITY; n];
3110        let centre = [f64::from(NC * 128) / 2.0, f64::from(NC * 128) / 2.0, 60.0];
3111
3112        // Two poses: eye-level toward horizon (long rays) + looking down
3113        // at nearby terrain (short rays, demo-typical).
3114        let poses = [
3115            (
3116                "horizon",
3117                Camera::from_yaw_pitch([20.0, 20.0, 40.0], 0.6, 0.15),
3118            ),
3119            ("down", Camera::orbit(0.7, 1.0, 130.0, centre)),
3120        ];
3121        for (name, cam) in poses {
3122            {
3123                let mut sink = RasterSink::new(&mut fb, &mut zb);
3124                prof::reset();
3125                render_dda(
3126                    &cam,
3127                    &settings,
3128                    grid,
3129                    w as usize,
3130                    &DdaEnv::default(),
3131                    0,
3132                    &mut sink,
3133                );
3134            }
3135            let (cells, bricks, surf) = prof::read();
3136            let iters = 6;
3137            let t0 = Instant::now();
3138            for _ in 0..iters {
3139                let mut sink = RasterSink::new(&mut fb, &mut zb);
3140                render_dda(
3141                    &cam,
3142                    &settings,
3143                    grid,
3144                    w as usize,
3145                    &DdaEnv::default(),
3146                    0,
3147                    &mut sink,
3148                );
3149            }
3150            let ms = t0.elapsed().as_secs_f64() * 1000.0 / f64::from(iters);
3151            let hits = fb.iter().filter(|&&c| c != 0).count();
3152            eprintln!(
3153                "[{name}] {w}x{h} 1-thread: {ms:.1} ms | hits={hits}/{n} | per-px: cells={:.1} bricks={:.1} surf={:.1}",
3154                cells as f64 / n as f64,
3155                bricks as f64 / n as f64,
3156                surf as f64 / n as f64,
3157            );
3158        }
3159    }
3160
3161    /// DDA.7: the tile-parallel driver is bit-identical to the
3162    /// sequential one — DDA pixels are independent, so banding can't
3163    /// change a pixel.
3164    #[test]
3165    fn parallel_matches_sequential() {
3166        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
3167            let surf = 28 + ((x / 4 + y / 6) % 13);
3168            (z >= surf).then_some(VoxColor(0x80_40_60_80 + (x ^ y) % 0x30))
3169        });
3170        let grid = GridView::from_single_vxl(&vxl);
3171        let (w, h) = (96u32, 96u32);
3172        let cam = Camera::orbit(0.8, 0.55, 100.0, [32.0, 32.0, 40.0]);
3173        let env = DdaEnv {
3174            sky: None,
3175            fog_color: 0x00_20_30_40,
3176            fog_max_dist: 120.0,
3177            side_shades: [0, 0, 0, 0, 0x30, 0x10],
3178            materials: None,
3179            terrain_materials: &[],
3180            lights: CpuLights::default(),
3181            world_shadow: None,
3182        };
3183
3184        let (seq_fb, seq_zb) = render_brickmap_env(grid, &cam, w, h, &env);
3185
3186        let n = (w * h) as usize;
3187        let mut par_fb = vec![0u32; n];
3188        let mut par_zb = vec![f32::INFINITY; n];
3189        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
3190        let (cache, mip) = local_cache(&grid, 0);
3191        render_dda_parallel(
3192            &cam,
3193            &settings,
3194            grid,
3195            &mut par_fb,
3196            &mut par_zb,
3197            w as usize,
3198            &env,
3199            &cache,
3200            mip,
3201        );
3202        assert!(par_fb == seq_fb, "parallel colour differs from sequential");
3203        assert!(
3204            par_zb
3205                .iter()
3206                .zip(&seq_zb)
3207                .all(|(a, b)| a.to_bits() == b.to_bits()),
3208            "parallel depth differs from sequential"
3209        );
3210    }
3211
3212    /// DDA.2 correctness: a heightmap column's interior is solid even
3213    /// though voxlap only stores a colour for its surface. `voxel_color`
3214    /// returns `None` for an interior voxel, but `surface_color` must
3215    /// return the run's surface colour — otherwise oblique rays striking
3216    /// a cliff *side* would pass straight through (see-through terrain).
3217    #[test]
3218    fn cliff_side_is_solid_not_see_through() {
3219        const TOP_Z: u32 = 50;
3220        const COL: VoxColor = VoxColor(0x80_77_88_99);
3221        let vxl = roxlap_formats::vxl::Vxl::from_dense(8, |_, _, z| (z >= TOP_Z).then_some(COL));
3222        let grid = GridView::from_single_vxl(&vxl);
3223
3224        // Surface voxel: coloured directly.
3225        assert_eq!(grid.voxel_color(4, 4, TOP_Z), Some(COL));
3226        // Interior voxel: voxlap stores no colour …
3227        assert_eq!(grid.voxel_color(4, 4, 150), None);
3228        // … but it is solid, and surface_color bleeds the run-top colour
3229        // down the cliff face → a real hit, not see-through.
3230        assert_eq!(grid.surface_color(4, 4, 150), Some(COL));
3231        // Bedrock-style air above the surface stays air.
3232        assert_eq!(grid.surface_color(4, 4, 10), None);
3233    }
3234
3235    /// DDA.2: a camera embedded in solid material hits its own voxel
3236    /// immediately — every ray reports a hit (no skip / no garbage).
3237    #[test]
3238    fn camera_inside_solid_hits_everywhere() {
3239        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, _| Some(VoxColor(0x80_55_55_55)));
3240        let grid = GridView::from_single_vxl(&vxl);
3241        let cam = Camera {
3242            pos: [8.0, 8.0, 128.0],
3243            right: [1.0, 0.0, 0.0],
3244            down: [0.0, 1.0, 0.0],
3245            forward: [0.0, 0.0, 1.0],
3246        };
3247        let (w, h) = (32u32, 32u32);
3248        let mask = render_mask(grid, &cam, w, h);
3249        assert!(
3250            mask.iter().all(|&b| b),
3251            "every ray must hit when the camera is inside solid"
3252        );
3253    }
3254
3255    /// Headline DDA.1 gate: a single solid voxel viewed obliquely
3256    /// projects to a convex silhouette with **no interior holes** —
3257    /// the artifact class (`tiny_grid_1x1x1` silhouette notch) the
3258    /// voxlap renderer cannot avoid. DDA casts independent per-pixel
3259    /// rays, so the silhouette is hole-free by construction.
3260    #[test]
3261    fn single_voxel_silhouette_has_no_notch() {
3262        const C: VoxColor = VoxColor(0x80_FF_80_40);
3263        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |x, y, z| {
3264            (x == 8 && y == 8 && z == 8).then_some(C)
3265        });
3266        let grid = GridView::from_single_vxl(&vxl);
3267
3268        // Orbit the voxel centre obliquely so all three faces show and
3269        // the silhouette is a sizeable hexagon (dist 4 → ~12 px wide).
3270        let cam = Camera::orbit(0.7, 0.6, 4.0, [8.5, 8.5, 8.5]);
3271        let (w, h) = (96u32, 96u32);
3272        let mask = render_mask(grid, &cam, w, h);
3273
3274        let hits = mask.iter().filter(|&&b| b).count();
3275        assert!(
3276            hits > 30,
3277            "silhouette too small to be meaningful: {hits} px"
3278        );
3279        assert!(
3280            rows_have_no_holes(&mask, w, h),
3281            "row-interior gap in single-voxel silhouette (notch)"
3282        );
3283        assert!(
3284            cols_have_no_holes(&mask, w, h),
3285            "column-interior gap in single-voxel silhouette (notch)"
3286        );
3287    }
3288}