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