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}
74
75impl Default for DdaEnv<'_> {
76    fn default() -> Self {
77        Self {
78            sky: None,
79            fog_color: 0,
80            fog_max_dist: 0.0,
81            side_shades: [0; 6],
82            materials: None,
83            terrain_materials: &[],
84        }
85    }
86}
87
88/// Per-pixel output target for the DDA renderer.
89///
90/// Abstracts "where does a ray hit go" so the traversal core stays
91/// free of framebuffer mechanics. The production impl is
92/// [`RasterSink`] (raw fb/zb pointers); tests use a recording sink.
93/// Only *hits* are reported — misses (sky) leave the destination
94/// untouched, matching the caller-pre-fills-sky convention.
95pub trait PixelSink {
96    /// Record a ray hit at framebuffer index `idx` (`py * pitch + px`)
97    /// with packed ARGB `color` and perpendicular `dist` (smaller =
98    /// closer).
99    fn put(&mut self, idx: usize, color: u32, dist: f32);
100}
101
102/// [`PixelSink`] over a borrowed `(framebuffer, zbuffer)` pair.
103///
104/// Wraps a [`RasterTarget`] so the DDA path writes through the same
105/// raw-pointer mechanism the scalar rasterizer uses — which keeps the
106/// door open for the same strip/tile-disjoint parallel writes in
107/// DDA.7.
108pub struct RasterSink<'a> {
109    target: RasterTarget<'a>,
110    len: usize,
111}
112
113impl<'a> RasterSink<'a> {
114    /// Build a sink from exclusive framebuffer + zbuffer borrows.
115    /// Both slices must have the same length (the pixel count).
116    #[must_use]
117    pub fn new(framebuffer: &'a mut [u32], zbuffer: &'a mut [f32]) -> Self {
118        debug_assert_eq!(framebuffer.len(), zbuffer.len());
119        let len = framebuffer.len();
120        Self {
121            target: RasterTarget::new(framebuffer, zbuffer),
122            len,
123        }
124    }
125}
126
127impl PixelSink for RasterSink<'_> {
128    fn put(&mut self, idx: usize, color: u32, dist: f32) {
129        if idx < self.len {
130            // SAFETY: bounds checked above; single-threaded writer in
131            // DDA.0 so the disjoint-write invariant holds trivially.
132            unsafe {
133                self.target.write_color(idx, color);
134                self.target.write_depth(idx, dist);
135            }
136        }
137    }
138}
139
140/// A resolved ray hit: surface colour + perpendicular distance.
141#[derive(Debug, Clone, Copy)]
142struct Hit {
143    color: u32,
144    dist: f32,
145}
146
147/// Test-only per-thread traversal counters for the perf bench.
148#[cfg(test)]
149pub(crate) mod prof {
150    use std::cell::Cell;
151    thread_local! {
152        pub static CELLS: Cell<u64> = const { Cell::new(0) };
153        pub static BRICKS: Cell<u64> = const { Cell::new(0) };
154        pub static SURF: Cell<u64> = const { Cell::new(0) };
155    }
156    pub fn reset() {
157        CELLS.with(|x| x.set(0));
158        BRICKS.with(|x| x.set(0));
159        SURF.with(|x| x.set(0));
160    }
161    pub fn read() -> (u64, u64, u64) {
162        (
163            CELLS.with(Cell::get),
164            BRICKS.with(Cell::get),
165            SURF.with(Cell::get),
166        )
167    }
168}
169
170/// Apply the voxel's baked directional brightness (Substage DDA.5).
171///
172/// Voxlap (and the GPU marcher, `grid_dda.wgsl`) store per-voxel
173/// brightness in the colour's high byte on a `0..128` scale — `0x80`
174/// is full brightness — written by `Grid::bake_lightmode` (estnorm
175/// directional shading). The shaded channel is `c · a / 128`, so the
176/// DDA matches the GPU look; an unbaked / full-bright voxel (`a =
177/// 0x80`) passes through unchanged. Output alpha is normalised to
178/// `0x80` (the standard "lit" flag; the present blit ignores it).
179///
180/// The renderer only *reads* the baked byte — it computes no normals
181/// itself, so per-impact relight is free (re-bake the chunk and the
182/// byte updates). The estnorm bake that produces the byte is the
183/// voxlap-derived piece slated for a clean-room rewrite in DDA.10.
184///
185/// `bright_sub` is the per-face `side_shades` reduction (DDA.5): voxlap
186/// subtracts it from the brightness byte before the multiply, so a
187/// shaded face is uniformly darker. `0` = no side shading.
188#[inline]
189pub(crate) fn shade(color: u32, bright_sub: u32) -> u32 {
190    let a = ((color >> 24) & 0xff).saturating_sub(bright_sub);
191    let ch = |shift: u32| -> u32 { ((((color >> shift) & 0xff) * a) >> 7).min(255) };
192    0x8000_0000 | (ch(16) << 16) | (ch(8) << 8) | ch(0)
193}
194
195/// Blend `color` toward `env.fog_color` by perpendicular `depth`
196/// (linear, fully fogged at `env.fog_max_dist`). No-op when fog is
197/// disabled (`fog_max_dist <= 0`).
198#[inline]
199fn apply_fog(color: u32, depth: f32, env: &DdaEnv<'_>) -> u32 {
200    if env.fog_max_dist <= 0.0 {
201        return color;
202    }
203    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
204    let f = ((depth / env.fog_max_dist).clamp(0.0, 1.0) * 256.0) as u32; // 0..256
205    let g = 256 - f;
206    let fog = env.fog_color;
207    let mix = |shift: u32| -> u32 {
208        let src = (color >> shift) & 0xff;
209        let dst = (fog >> shift) & 0xff;
210        ((src * g + dst * f) >> 8).min(255)
211    };
212    0x8000_0000 | (mix(16) << 16) | (mix(8) << 8) | mix(0)
213}
214
215/// TV: resolve a terrain voxel's [`Material`] from its colour via the env's
216/// colour→material map + palette. Returns [`Material::OPAQUE`] when no
217/// material table is set, the map is empty, or the colour is unmapped — so
218/// the march stays on the opaque first-hit path.
219#[inline]
220fn terrain_material(env: &DdaEnv<'_>, color: u32) -> Material {
221    match env.materials {
222        Some(table) if !env.terrain_materials.is_empty() => {
223            table.get(material_for_color(env.terrain_materials, color))
224        }
225        _ => Material::OPAQUE,
226    }
227}
228
229/// Composite premultiplied `accum` (+ remaining `trans`) over a packed
230/// background colour → packed `0x80RRGGBB`.
231#[inline]
232fn composite_over(accum: [f32; 3], trans: f32, bg: u32) -> u32 {
233    let b = rgb_to_f32(bg);
234    f32_to_rgb([
235        accum[0] + trans * b[0],
236        accum[1] + trans * b[1],
237        accum[2] + trans * b[2],
238    ])
239}
240
241/// Finalize a translucent terrain ray that exited the grid (sky). Returns
242/// `None` when nothing was accumulated (the opaque first-hit path — the
243/// caller's sky handling stands, bit-identical), else the accumulated
244/// layers composited over the sky at `dist`.
245#[inline]
246fn finalize_exit(
247    touched: bool,
248    accum: [f32; 3],
249    trans: f32,
250    env: &DdaEnv<'_>,
251    dir: [f32; 3],
252    dist: f32,
253) -> Option<Hit> {
254    if !touched {
255        return None;
256    }
257    let bg = match env.sky {
258        Some(s) => sample_sky(s, dir),
259        None => 0x8000_0000 | (env.fog_color & 0x00ff_ffff),
260    };
261    Some(Hit {
262        color: composite_over(accum, trans, bg),
263        dist,
264    })
265}
266
267/// Unpack `0x__RRGGBB` to `0..1` float channels (RGB; the high byte is
268/// dropped — it has already been folded into the colour by `shade`/`fog`).
269#[inline]
270#[allow(clippy::cast_precision_loss)]
271fn rgb_to_f32(c: u32) -> [f32; 3] {
272    [
273        ((c >> 16) & 0xff) as f32 / 255.0,
274        ((c >> 8) & 0xff) as f32 / 255.0,
275        (c & 0xff) as f32 / 255.0,
276    ]
277}
278
279/// Repack `0..1` float channels (clamped) into `0x80RRGGBB`.
280#[inline]
281#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
282fn f32_to_rgb(c: [f32; 3]) -> u32 {
283    let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u32;
284    0x8000_0000 | (q(c[0]) << 16) | (q(c[1]) << 8) | q(c[2])
285}
286
287/// Sample the sky panorama in ray direction `dir` (need not be
288/// normalised), returning a packed `0x80RRGGBB` colour.
289///
290/// Clean-room equirectangular mapping (not voxlap's `lng`/`lat` asm
291/// search): the texture's x axis is elevation (`asin` of the vertical
292/// component), the y axis is azimuth (`atan2` around the vertical). A
293/// `ysiz == 1` panorama (e.g. [`Sky::blue_gradient`]) is a pure
294/// horizon→zenith gradient.
295#[allow(
296    clippy::cast_possible_truncation,
297    clippy::cast_sign_loss,
298    clippy::cast_precision_loss
299)]
300fn sample_sky(sky: &Sky, dir: [f32; 3]) -> u32 {
301    let len = (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]).sqrt();
302    if len < 1e-9 {
303        return 0x8000_0000;
304    }
305    let d = [dir[0] / len, dir[1] / len, dir[2] / len];
306    let xsiz_full = sky.lat.len().max(1) as i32; // original column count
307    let pi = std::f32::consts::PI;
308    // Elevation → x, matching the GPU `sky_color` (scene_dda.wgsl): z is
309    // down, so `acos(-z)` is 0 at the zenith (looking up) and π at the nadir
310    // (looking down); `/π` puts the zenith at x=0 and the nadir at x=xsiz.
311    let elev01 = (-d[2]).clamp(-1.0, 1.0).acos() / pi; // 0 (up) .. 1 (down)
312    let x = (elev01 * xsiz_full as f32) as i32;
313    let x = x.clamp(0, xsiz_full - 1);
314    // Azimuth → y (wrapped).
315    let y = if sky.ysiz <= 1 {
316        0
317    } else {
318        let az = d[1].atan2(d[0]); // -pi..pi
319        let yf = ((az / (pi * 2.0)) + 0.5) * sky.ysiz as f32;
320        (yf as i32).rem_euclid(sky.ysiz)
321    };
322    let idx = (y * xsiz_full + x) as usize;
323    let px = sky.pixels.get(idx).copied().unwrap_or(0) as u32;
324    0x8000_0000 | (px & 0x00ff_ffff)
325}
326
327/// Fill the panorama [`Sky`] into every **background** pixel — one whose
328/// z-buffer entry is still `+∞` (no grid/terrain hit). The per-grid DDA only
329/// samples the sky inside each grid's screen rect (and only its sky-owning
330/// grid); pixels outside any grid — most of a sprite/effect-only view, or the
331/// margins around a small world grid — would otherwise keep the caller's flat
332/// clear colour. This paints the real panorama there while leaving terrain
333/// (finite z) and composited translucent pixels untouched. The z-buffer is
334/// not modified. `cam`/`settings` are the same per-frame projection the
335/// renderer used.
336#[allow(clippy::cast_possible_truncation)]
337pub fn render_sky_fill(
338    fb: &mut [u32],
339    zb: &[f32],
340    pitch_pixels: usize,
341    width: u32,
342    height: u32,
343    cam: &CameraState,
344    settings: &OpticastSettings,
345    sky: &Sky,
346) {
347    for py in 0..height {
348        let row = py as usize * pitch_pixels;
349        for px in 0..width {
350            let idx = row + px as usize;
351            if zb[idx].is_finite() {
352                continue; // a grid/terrain hit owns this pixel
353            }
354            let (_origin, dir) = pixel_ray(cam, settings, px, py);
355            fb[idx] = sample_sky(sky, dir);
356        }
357    }
358}
359
360/// World-space ray for screen pixel `(px, py)` under opticast's
361/// pinhole: origin is the camera position, direction is
362/// `(px - hx)·right + (py - hy)·down + hz·forward`.
363///
364/// This is the exact ray `camera_math::derive` bakes into its corner
365/// vectors (`corn[0]` is `pixel (0, 0)`'s direction), so the DDA
366/// renderer samples the same rays the voxlap path's frustum is built
367/// from. The direction is **not** normalised — callers that need a
368/// unit ray (and a true Euclidean distance) normalise themselves;
369/// DDA.1 will track perpendicular distance via the forward-projection
370/// instead, matching the engine's z-buffer convention.
371#[must_use]
372pub fn pixel_ray(
373    cs: &CameraState,
374    settings: &OpticastSettings,
375    px: u32,
376    py: u32,
377) -> ([f32; 3], [f32; 3]) {
378    // u32 → f32 is exact for any realistic screen coordinate.
379    #[allow(clippy::cast_precision_loss)]
380    let sx = px as f32 - settings.hx;
381    #[allow(clippy::cast_precision_loss)]
382    let sy = py as f32 - settings.hy;
383    let dir = [
384        sx * cs.right[0] + sy * cs.down[0] + settings.hz * cs.forward[0],
385        sx * cs.right[1] + sy * cs.down[1] + settings.hz * cs.forward[1],
386        sx * cs.right[2] + sy * cs.down[2] + settings.hz * cs.forward[2],
387    ];
388    (cs.pos, dir)
389}
390
391/// Ray ↔ axis-aligned box `[lo, hi]` slab test. Returns the
392/// `(t_enter, t_exit)` parameter interval along `dir` (already clamped
393/// so `t_enter >= 0`, i.e. a camera inside the box starts at `t = 0`),
394/// or `None` if the ray misses the box. `dir` need not be normalised —
395/// `t` is in units of `|dir|`.
396pub(crate) fn intersect_aabb(
397    o: [f32; 3],
398    dir: [f32; 3],
399    lo: [f32; 3],
400    hi: [f32; 3],
401) -> Option<(f32, f32)> {
402    let mut t0 = 0.0f32;
403    let mut t1 = f32::INFINITY;
404    for a in 0..3 {
405        if dir[a].abs() < 1e-9 {
406            // Ray parallel to this slab — must already be inside it.
407            if o[a] < lo[a] || o[a] > hi[a] {
408                return None;
409            }
410        } else {
411            let inv = 1.0 / dir[a];
412            let mut ta = (lo[a] - o[a]) * inv;
413            let mut tb = (hi[a] - o[a]) * inv;
414            if ta > tb {
415                core::mem::swap(&mut ta, &mut tb);
416            }
417            t0 = t0.max(ta);
418            t1 = t1.min(tb);
419            if t0 > t1 {
420                return None;
421            }
422        }
423    }
424    Some((t0, t1))
425}
426
427/// Brick edge length in voxels — one occupancy bit per `BRICK³` block.
428const BRICK: i32 = 8;
429
430/// Per-chunk brick occupancy map for two-level DDA empty-space skip
431/// (Substage DDA.3).
432///
433/// One bit per `BRICK³` block of the active chunk, set iff any voxel in
434/// the block is solid. The ray steps the coarse brick grid (8× longer
435/// strides) and only descends into a per-voxel walk inside occupied
436/// bricks, so a ray through open air crosses ~`length / 8` empty bricks
437/// instead of `length` air voxels — each of which would otherwise walk
438/// the column slab chain via `surface_color`.
439///
440/// Built per frame from a [`GridView`] in [`render_dda`]. A persistent
441/// per-chunk cache with edit-driven invalidation (locked decision #2 in
442/// `PORTING-DDA.md`) is a later perf refinement.
443#[derive(Debug)]
444pub(crate) struct BrickMap {
445    /// Brick counts along x / y / z (one entry per `BRICK³` cells).
446    nb: [i32; 3],
447    /// Brick occupancy bitset; brick `(bx, by, bz)` is bit
448    /// `(bz * nb[1] + by) * nb[0] + bx`.
449    bits: Vec<u64>,
450    /// Super-brick counts (one entry per `BRICK³` *bricks* = `SUPER³`
451    /// cells), `ceil(nb / BRICK)`.
452    ns: [i32; 3],
453    /// Super-brick occupancy (DDA.7 perf): a coarse level so a ray
454    /// through open air above the terrain skips `SUPER` cells per outer
455    /// step instead of `BRICK`. A super-brick is set iff any child brick
456    /// is set.
457    super_bits: Vec<u64>,
458}
459
460/// Super-brick edge in cells (`BRICK` bricks per axis).
461const SUPER: i32 = BRICK * BRICK;
462
463impl BrickMap {
464    /// Scan every mip-`mip` column of `grid`, building brick + super-
465    /// brick occupancy. `mip` must be `< grid.mip_count()`.
466    #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
467    fn build(grid: &GridView<'_>, mip: u32) -> Self {
468        let vsid_m = (grid.vsid >> mip).max(1) as i32;
469        let z_m = (crate::grid_view::CHUNK_SIZE_Z >> mip).max(1) as i32;
470        let nb = [
471            (vsid_m + BRICK - 1) / BRICK,
472            (vsid_m + BRICK - 1) / BRICK,
473            (z_m + BRICK - 1) / BRICK,
474        ];
475        let ns = [
476            (nb[0] + BRICK - 1) / BRICK,
477            (nb[1] + BRICK - 1) / BRICK,
478            (nb[2] + BRICK - 1) / BRICK,
479        ];
480        let count = (nb[0] * nb[1] * nb[2]) as usize;
481        let scount = (ns[0] * ns[1] * ns[2]) as usize;
482        let mut bits = vec![0u64; count.div_ceil(64)];
483        let mut super_bits = vec![0u64; scount.div_ceil(64)];
484        for y in 0..vsid_m {
485            for x in 0..vsid_m {
486                let (bx, by) = (x / BRICK, y / BRICK);
487                grid.for_each_run_mip(x as u32, y as u32, mip, |top, bot| {
488                    for bz in (top / BRICK)..=((bot - 1) / BRICK) {
489                        let idx = ((bz * nb[1] + by) * nb[0] + bx) as usize;
490                        bits[idx / 64] |= 1u64 << (idx % 64);
491                        let sidx =
492                            (((bz / BRICK) * ns[1] + by / BRICK) * ns[0] + bx / BRICK) as usize;
493                        super_bits[sidx / 64] |= 1u64 << (sidx % 64);
494                    }
495                });
496            }
497        }
498        Self {
499            nb,
500            bits,
501            ns,
502            super_bits,
503        }
504    }
505
506    /// Whether brick `b` is in range and holds any solid voxel.
507    #[inline]
508    #[allow(clippy::cast_sign_loss)]
509    fn occupied(&self, b: [i32; 3]) -> bool {
510        if b[0] < 0
511            || b[0] >= self.nb[0]
512            || b[1] < 0
513            || b[1] >= self.nb[1]
514            || b[2] < 0
515            || b[2] >= self.nb[2]
516        {
517            return false;
518        }
519        let idx = ((b[2] * self.nb[1] + b[1]) * self.nb[0] + b[0]) as usize;
520        (self.bits[idx / 64] >> (idx % 64)) & 1 != 0
521    }
522
523    /// Whether super-brick `s` is in range and holds any solid voxel.
524    #[inline]
525    #[allow(clippy::cast_sign_loss)]
526    fn occupied_super(&self, s: [i32; 3]) -> bool {
527        if s[0] < 0
528            || s[0] >= self.ns[0]
529            || s[1] < 0
530            || s[1] >= self.ns[1]
531            || s[2] < 0
532            || s[2] >= self.ns[2]
533        {
534            return false;
535        }
536        let idx = ((s[2] * self.ns[1] + s[1]) * self.ns[0] + s[0]) as usize;
537        (self.super_bits[idx / 64] >> (idx % 64)) & 1 != 0
538    }
539}
540
541/// Per-axis 3D-DDA stepping state for a cell size of `cell` voxels.
542/// `t_max[a]` is the ray parameter at which the next `a`-boundary is
543/// crossed; `t_delta[a]` is the parameter increment per cell. An
544/// axis-parallel component gets `t_max = t_delta = +inf` so it's never
545/// chosen as the stepping axis.
546pub(crate) fn dda_setup(
547    origin: [f32; 3],
548    dir: [f32; 3],
549    cell: [i32; 3],
550    cell_size: f32,
551) -> ([i32; 3], [f32; 3], [f32; 3]) {
552    let mut step = [0i32; 3];
553    let mut t_max = [f32::INFINITY; 3];
554    let mut t_delta = [f32::INFINITY; 3];
555    for a in 0..3 {
556        if dir[a] > 1e-9 {
557            step[a] = 1;
558            #[allow(clippy::cast_precision_loss)]
559            let boundary = (cell[a] + 1) as f32 * cell_size;
560            t_max[a] = (boundary - origin[a]) / dir[a];
561            t_delta[a] = cell_size / dir[a];
562        } else if dir[a] < -1e-9 {
563            step[a] = -1;
564            #[allow(clippy::cast_precision_loss)]
565            let boundary = cell[a] as f32 * cell_size;
566            t_max[a] = (boundary - origin[a]) / dir[a];
567            t_delta[a] = -cell_size / dir[a];
568        }
569    }
570    (step, t_max, t_delta)
571}
572
573/// Index of the axis with the smallest `t_max` (the next boundary the
574/// ray crosses).
575#[inline]
576pub(crate) fn min_axis(t_max: [f32; 3]) -> usize {
577    if t_max[0] <= t_max[1] && t_max[0] <= t_max[2] {
578        0
579    } else if t_max[1] <= t_max[2] {
580        1
581    } else {
582        2
583    }
584}
585
586/// Persistent, cross-frame brick occupancy cache (Substage DDA.7
587/// perf). Keyed by `(chunk x, y, z, mip)` with the chunk's edit
588/// `version`; an entry is reused until its chunk's version changes, so a
589/// static / streamed-once world pays **zero** brick-build cost after the
590/// first frame (the per-frame rebuild was the dominant DDA cost).
591///
592/// Owned by the caller across frames (the scene's `Grid`), populated
593/// single-threaded via [`Self::ensure`], then borrowed immutably by the
594/// parallel render bands.
595#[derive(Debug, Default)]
596pub struct BrickCache {
597    maps: HashMap<(i32, i32, i32, u32), (u64, BrickMap)>,
598}
599
600impl BrickCache {
601    #[must_use]
602    pub fn new() -> Self {
603        Self::default()
604    }
605
606    /// Ensure a current mip-`mip` brick map exists for `chunk` (built
607    /// from `view`); rebuilds only when the cached `version` differs.
608    pub fn ensure(&mut self, chunk: [i32; 3], mip: u32, version: u64, view: &GridView<'_>) {
609        let key = (chunk[0], chunk[1], chunk[2], mip);
610        let stale = self.maps.get(&key).map_or(true, |(v, _)| *v != version);
611        if stale {
612            self.maps.insert(key, (version, BrickMap::build(view, mip)));
613        }
614    }
615
616    #[inline]
617    fn get(&self, chunk: [i32; 3], mip: u32) -> Option<&BrickMap> {
618        self.maps
619            .get(&(chunk[0], chunk[1], chunk[2], mip))
620            .map(|(_, m)| m)
621    }
622
623    /// Drop cached entries whose chunk fails `keep` — bounds memory as
624    /// streaming evicts chunks. Called once per frame by the scene.
625    pub fn retain_chunks(&mut self, keep: impl Fn([i32; 3]) -> bool) {
626        self.maps.retain(|k, _| keep([k.0, k.1, k.2]));
627    }
628}
629
630/// Build a throwaway [`BrickCache`] covering every populated chunk of
631/// `grid` at the effective mip — for the sequential [`render_dda`] /
632/// tests, where no persistent cache is threaded in. Returns
633/// `(cache, effective_mip)`.
634#[allow(clippy::cast_possible_wrap)]
635fn local_cache(grid: &GridView<'_>, requested_mip: u32) -> (BrickCache, u32) {
636    let mip = effective_mip(grid, requested_mip);
637    let mut cache = BrickCache::new();
638    if let Some(cg) = grid.chunk_grid {
639        for dz in 0..cg.chunks_z as i32 {
640            for dy in 0..cg.chunks_y as i32 {
641                for dx in 0..cg.chunks_x as i32 {
642                    let slot = ((dz * cg.chunks_y as i32 + dy) * cg.chunks_x as i32 + dx) as usize;
643                    if let Some(Some(view)) = cg.chunks.get(slot) {
644                        let ch = [
645                            cg.origin_chunk_xy[0] + dx,
646                            cg.origin_chunk_xy[1] + dy,
647                            cg.origin_chunk_z + dz,
648                        ];
649                        cache.ensure(ch, mip, 0, view);
650                    }
651                }
652            }
653        }
654    } else {
655        cache.ensure([0, 0, 0], mip, 0, grid);
656    }
657    (cache, mip)
658}
659
660/// Clamp a requested render mip to one every populated chunk actually
661/// has built — so the uniform-mip traversal never under-samples a chunk
662/// that lacks the requested level (which would punch holes). `0` short-
663/// circuits (always available).
664#[must_use]
665pub fn effective_mip(grid: &GridView<'_>, requested: u32) -> u32 {
666    if requested == 0 {
667        return 0;
668    }
669    let mut m = requested;
670    if let Some(cg) = grid.chunk_grid {
671        for c in cg.chunks.iter().flatten() {
672            m = m.min(c.mip_count().saturating_sub(1));
673        }
674    } else {
675        m = m.min(grid.mip_count().saturating_sub(1));
676    }
677    m
678}
679
680/// Cross-chunk voxel sampler (Substage DDA.4 / DDA.7).
681///
682/// Resolves a grid-local voxel coordinate to the chunk that owns it
683/// (via [`GridView::chunk_at_xyz`]) and answers the DDA's per-voxel hit
684/// query — brick-gated [`GridView::surface_color`]. It borrows the
685/// shared immutable [`BrickMaps`] and caches the **current chunk**
686/// (`cur_*`: view + brick-map reference): a ray usually stays in one
687/// chunk for many voxels, so the per-voxel cost is a single index
688/// compare + an O(1) brick bit test — no hashing, no mutation. Holding
689/// only shared borrows, a `Sampler` is cheap to spin up per render band.
690///
691/// Single-chunk grids are the degenerate case: every voxel maps to
692/// chunk `[0, 0, 0]` (= the view itself).
693struct Sampler<'a> {
694    grid: GridView<'a>,
695    bricks: &'a BrickCache,
696    /// Effective render mip (DDA.6). Traversal cells are mip-`mip`
697    /// cells; sampling reads mip-`mip` data.
698    mip: u32,
699    /// Chunk size in mip-`mip` cells is a power of two; store it as
700    /// `log2` (shift) + `size - 1` (mask) so [`Self::locate`] splits a
701    /// cell into `(chunk, in-chunk)` with a shift + an `&` per axis
702    /// instead of a signed `div_euclid` — the dominant per-cell cost.
703    /// Arithmetic `>>` floors toward -∞ (= `div_euclid` for a positive
704    /// power-of-two divisor) and `& mask` gives the non-negative
705    /// remainder (= `rem_euclid`) even for negative cells (two's
706    /// complement), so results are identical to the division form.
707    xy_shift: u32,
708    xy_mask: i32,
709    z_shift: u32,
710    z_mask: i32,
711    cur_ch: [i32; 3],
712    cur_view: Option<GridView<'a>>,
713    cur_brick: Option<&'a BrickMap>,
714    has_cur: bool,
715}
716
717impl<'a> Sampler<'a> {
718    fn new(grid: GridView<'a>, bricks: &'a BrickCache, mip: u32) -> Self {
719        let cs_xy = (grid.chunk_size_xy >> mip).max(1);
720        let cs_z = (crate::grid_view::CHUNK_SIZE_Z >> mip).max(1);
721        debug_assert!(
722            cs_xy.is_power_of_two() && cs_z.is_power_of_two(),
723            "chunk dims must be powers of two for the shift/mask split"
724        );
725        #[allow(clippy::cast_possible_wrap)]
726        Self {
727            grid,
728            bricks,
729            mip,
730            xy_shift: cs_xy.trailing_zeros(),
731            xy_mask: cs_xy as i32 - 1,
732            z_shift: cs_z.trailing_zeros(),
733            z_mask: cs_z as i32 - 1,
734            cur_ch: [0; 3],
735            cur_view: None,
736            cur_brick: None,
737            has_cur: false,
738        }
739    }
740
741    /// Refresh the current-chunk cache (view + brick map) for `ch`.
742    fn select_chunk(&mut self, ch: [i32; 3]) {
743        if self.has_cur && self.cur_ch == ch {
744            return;
745        }
746        self.cur_view = self.grid.chunk_at_xyz(ch);
747        self.cur_brick = self.bricks.get(ch, self.mip);
748        self.cur_ch = ch;
749        self.has_cur = true;
750    }
751
752    /// Split a grid-local **mip-`mip` cell** index into `(chunk index,
753    /// in-chunk mip-cell)` via shift + mask (see field docs). Chunk
754    /// indices are mip-independent; only the per-chunk resolution
755    /// shrinks with mip.
756    #[allow(clippy::cast_sign_loss)]
757    fn locate(&self, c: [i32; 3]) -> ([i32; 3], [u32; 3]) {
758        let ch = [
759            c[0] >> self.xy_shift,
760            c[1] >> self.xy_shift,
761            c[2] >> self.z_shift,
762        ];
763        let loc = [
764            (c[0] & self.xy_mask) as u32,
765            (c[1] & self.xy_mask) as u32,
766            (c[2] & self.z_mask) as u32,
767        ];
768        (ch, loc)
769    }
770
771    /// Hit colour for grid-local mip-cell `c`, or `None` for air / empty
772    /// chunk / uncoloured bedrock. Brick-gated so air inside a populated
773    /// chunk costs only a bit test, not a slab walk.
774    #[allow(clippy::cast_possible_wrap)]
775    fn hit(&mut self, c: [i32; 3]) -> Option<u32> {
776        #[cfg(test)]
777        prof::SURF.with(|x| x.set(x.get() + 1));
778        let (ch, loc) = self.locate(c);
779        self.select_chunk(ch);
780        let occupied = self.cur_brick.is_some_and(|bm| {
781            bm.occupied([
782                loc[0] as i32 / BRICK,
783                loc[1] as i32 / BRICK,
784                loc[2] as i32 / BRICK,
785            ])
786        });
787        if !occupied {
788            return None;
789        }
790        self.cur_view?
791            .surface_color_mip(loc[0], loc[1], loc[2], self.mip)
792    }
793
794    /// Chunk size in mip-cells along XY / Z (always a power of two).
795    #[inline]
796    fn cells_per_chunk_xy(&self) -> i32 {
797        1 << self.xy_shift
798    }
799    #[inline]
800    fn cells_per_chunk_z(&self) -> i32 {
801        1 << self.z_shift
802    }
803
804    /// Whether the brick at brick-index `brick` (in `BRICK`-mip-cell
805    /// units) holds any solid voxel. Used by the outer brick-DDA to skip
806    /// empty space `BRICK` cells at a time. Assumes bricks nest within
807    /// chunks (caller gates on [`Self::cells_per_chunk_xy`]`>= BRICK`).
808    #[allow(clippy::cast_sign_loss)]
809    fn brick_occupied(&mut self, brick: [i32; 3]) -> bool {
810        // First mip-cell of the brick (BRICK = 8 → `<< 3`).
811        let c0 = [brick[0] << 3, brick[1] << 3, brick[2] << 3];
812        let ch = [
813            c0[0] >> self.xy_shift,
814            c0[1] >> self.xy_shift,
815            c0[2] >> self.z_shift,
816        ];
817        self.select_chunk(ch);
818        self.cur_brick.is_some_and(|bm| {
819            bm.occupied([
820                (c0[0] & self.xy_mask) >> 3,
821                (c0[1] & self.xy_mask) >> 3,
822                (c0[2] & self.z_mask) >> 3,
823            ])
824        })
825    }
826
827    /// Whether the super-brick at super-index `s` (in `SUPER`-mip-cell
828    /// units) holds any solid voxel. Outer-most empty-space skip (steps
829    /// `SUPER` cells). Assumes super-bricks nest in chunks (caller gates
830    /// on `cells_per_chunk >= SUPER`).
831    #[allow(clippy::cast_sign_loss)]
832    fn super_occupied(&mut self, s: [i32; 3]) -> bool {
833        // First mip-cell of the super-brick (SUPER = 64 → `<< 6`).
834        let c0 = [s[0] << 6, s[1] << 6, s[2] << 6];
835        let ch = [
836            c0[0] >> self.xy_shift,
837            c0[1] >> self.xy_shift,
838            c0[2] >> self.z_shift,
839        ];
840        self.select_chunk(ch);
841        self.cur_brick.is_some_and(|bm| {
842            bm.occupied_super([
843                (c0[0] & self.xy_mask) >> 6,
844                (c0[1] & self.xy_mask) >> 6,
845                (c0[2] & self.z_mask) >> 6,
846            ])
847        })
848    }
849}
850
851/// Walk mip-cells along the ray within `[lo_c, hi_c)` and return the
852/// first solid hit, with leak-free empty-space skipping (DDA.7 redux).
853///
854/// **Why one continuous DDA, not nested level-walks.** The previous
855/// design ran an outer brick/super DDA that *jumped* whole bricks and
856/// only descended into occupied ones. Stepping a coarse cell at a time
857/// lets the ray slip diagonally **past an occupied coarse cell it only
858/// touches at a shared edge/corner** — a leak that showed as bright
859/// sky seams across thin diagonal walls (the cave-demo report). Here a
860/// *single* cell-granularity DDA carries the exact `(cellc, t_max)`
861/// state for the whole ray; it only ever **fast-forwards across an
862/// empty super-brick / brick**, where skipping cannot miss anything.
863/// The exit axis lands on the integer box-boundary cell (no float
864/// re-floor on the critical axis), so the entry cell of the next —
865/// possibly occupied — box is always visited densely. Result: hits are
866/// bit-identical to the dense per-cell reference, with the empty-space
867/// speed-up retained.
868///
869/// `cell_size` is the mip-cell edge in mip-0 voxels (`1 << mip`);
870/// `fwd_dot = dir·forward` → perpendicular depth.
871#[allow(
872    clippy::too_many_arguments,
873    clippy::cast_possible_truncation,
874    clippy::cast_sign_loss,
875    clippy::cast_precision_loss
876)]
877fn cell_walk_skip(
878    origin: [f32; 3],
879    dir: [f32; 3],
880    fwd_dot: f32,
881    sampler: &mut Sampler<'_>,
882    lo_c: [i32; 3],
883    hi_c: [i32; 3],
884    cell_size: f32,
885    t_enter: f32,
886    t_exit: f32,
887    max_dist: f32,
888    env: &DdaEnv<'_>,
889) -> Option<Hit> {
890    let has_super = sampler.cells_per_chunk_xy() >= SUPER && sampler.cells_per_chunk_z() >= SUPER;
891    let has_brick = sampler.cells_per_chunk_xy() >= BRICK && sampler.cells_per_chunk_z() >= BRICK;
892
893    let start = t_enter + 1e-4;
894    let p = [
895        origin[0] + dir[0] * start,
896        origin[1] + dir[1] * start,
897        origin[2] + dir[2] * start,
898    ];
899    let mut cellc = [
900        ((p[0] / cell_size).floor() as i32).clamp(lo_c[0], hi_c[0] - 1),
901        ((p[1] / cell_size).floor() as i32).clamp(lo_c[1], hi_c[1] - 1),
902        ((p[2] / cell_size).floor() as i32).clamp(lo_c[2], hi_c[2] - 1),
903    ];
904    let (step, mut t_max, t_delta) = dda_setup(origin, dir, cellc, cell_size);
905    // Reciprocal direction → the per-skip box-boundary t and the t_max
906    // refresh use multiplies instead of divisions (the dominant skip
907    // cost). `0.0` where `step == 0` (that axis' t_max stays +∞).
908    let inv = [
909        if step[0] != 0 { 1.0 / dir[0] } else { 0.0 },
910        if step[1] != 0 { 1.0 / dir[1] } else { 0.0 },
911        if step[2] != 0 { 1.0 / dir[2] } else { 0.0 },
912    ];
913    let mut t_curr = t_enter;
914    let mut last_axis = 3usize;
915
916    // TV: front-to-back translucent accumulation. While no translucent voxel
917    // is hit (`touched` stays false) every return is unchanged — the opaque
918    // world renders bit-identically. `prev_*` drive per-span compositing (one
919    // alpha layer per contiguous solid run or material change).
920    let mut accum = [0.0f32; 3];
921    let mut trans = 1.0f32;
922    let mut touched = false;
923    let mut prev_solid = false;
924    let mut prev_mat = 0u8;
925
926    // Each iteration either advances ≥1 cell (dense) or ≥1 box (skip),
927    // so the total cell span bounds the loop.
928    let span = (hi_c[0] - lo_c[0]) + (hi_c[1] - lo_c[1]) + (hi_c[2] - lo_c[2]);
929    let max_steps = span.max(0) as usize + 16;
930    for _ in 0..max_steps {
931        if cellc[0] < lo_c[0]
932            || cellc[0] >= hi_c[0]
933            || cellc[1] < lo_c[1]
934            || cellc[1] >= hi_c[1]
935            || cellc[2] < lo_c[2]
936            || cellc[2] >= hi_c[2]
937        {
938            return finalize_exit(touched, accum, trans, env, dir, max_dist);
939        }
940        let depth = t_curr * fwd_dot;
941        if depth > max_dist || t_curr > t_exit {
942            return finalize_exit(touched, accum, trans, env, dir, max_dist);
943        }
944        // Fog is fully opaque at `fog_max_dist`: nothing beyond is
945        // visible, so stop the ray there and return the fog colour
946        // rather than traversing (and skip/step-counting) to the far box
947        // wall. Both correct and the dominant perf win for foggy worlds —
948        // it caps every ray's length at the fog distance.
949        if env.fog_max_dist > 0.0 && depth >= env.fog_max_dist {
950            let fog = 0x8000_0000 | (env.fog_color & 0x00ff_ffff);
951            let color = if touched {
952                composite_over(accum, trans, fog)
953            } else {
954                fog
955            };
956            return Some(Hit {
957                color,
958                dist: env.fog_max_dist,
959            });
960        }
961
962        // Empty-space skip: a whole empty super-brick, else an empty
963        // brick. Skipping only empty boxes can never miss a surface.
964        let skip_shift = if has_super
965            && !sampler.super_occupied([cellc[0] >> 6, cellc[1] >> 6, cellc[2] >> 6])
966        {
967            Some(6u32)
968        } else if has_brick
969            && !sampler.brick_occupied([cellc[0] >> 3, cellc[1] >> 3, cellc[2] >> 3])
970        {
971            Some(3u32)
972        } else {
973            None
974        };
975        if let Some(sh) = skip_shift {
976            #[cfg(test)]
977            prof::BRICKS.with(|x| x.set(x.get() + 1));
978            // Nearest box boundary along the ray (in cell units).
979            let mut best_t = f32::INFINITY;
980            let mut best_axis = 3usize;
981            let mut plane = [0i32; 3];
982            for a in 0..3 {
983                if step[a] == 0 {
984                    continue;
985                }
986                let idx = cellc[a] >> sh;
987                plane[a] = if step[a] > 0 {
988                    (idx + 1) << sh
989                } else {
990                    idx << sh
991                };
992                let tb = (plane[a] as f32 * cell_size - origin[a]) * inv[a];
993                if tb < best_t {
994                    best_t = tb;
995                    best_axis = a;
996                }
997            }
998            if best_axis == 3 {
999                return finalize_exit(touched, accum, trans, env, dir, max_dist);
1000            }
1001            // Land just across the boundary; pin the exit axis to the
1002            // integer boundary cell so float error can't skip the next
1003            // box's entry cell. Other axes haven't crossed their box
1004            // boundary (best_t is the min), so the point's floor is safe.
1005            let pb = [
1006                origin[0] + dir[0] * (best_t + 1e-4),
1007                origin[1] + dir[1] * (best_t + 1e-4),
1008                origin[2] + dir[2] * (best_t + 1e-4),
1009            ];
1010            let mut nc = [
1011                (pb[0] / cell_size).floor() as i32,
1012                (pb[1] / cell_size).floor() as i32,
1013                (pb[2] / cell_size).floor() as i32,
1014            ];
1015            nc[best_axis] = if step[best_axis] > 0 {
1016                plane[best_axis]
1017            } else {
1018                plane[best_axis] - 1
1019            };
1020            // The skip crossed a box boundary; if that takes the ray out
1021            // of the grid box it has exited (sky) — return rather than
1022            // clamping back in-bounds, which would spin at the edge.
1023            if nc[0] < lo_c[0]
1024                || nc[0] >= hi_c[0]
1025                || nc[1] < lo_c[1]
1026                || nc[1] >= hi_c[1]
1027                || nc[2] < lo_c[2]
1028                || nc[2] >= hi_c[2]
1029            {
1030                return finalize_exit(touched, accum, trans, env, dir, max_dist);
1031            }
1032            cellc = nc;
1033            // Refresh t_max for the new cell (dir unchanged → t_delta and
1034            // step constant; axes with step==0 keep their +∞).
1035            for a in 0..3 {
1036                if step[a] > 0 {
1037                    t_max[a] = ((cellc[a] + 1) as f32 * cell_size - origin[a]) * inv[a];
1038                } else if step[a] < 0 {
1039                    t_max[a] = (cellc[a] as f32 * cell_size - origin[a]) * inv[a];
1040                }
1041            }
1042            t_curr = best_t.max(t_curr);
1043            last_axis = best_axis;
1044            prev_solid = false; // skipped empty space → next hit starts a run
1045            continue;
1046        }
1047
1048        // Occupied brick: dense per-cell surface test.
1049        #[cfg(test)]
1050        prof::CELLS.with(|x| x.set(x.get() + 1));
1051        if let Some(color) = sampler.hit(cellc) {
1052            let bright_sub = side_shade_sub(env, last_axis, step);
1053            let lit = apply_fog(shade(color, bright_sub), depth.max(0.0), env);
1054            let m = terrain_material(env, color);
1055            if m.is_opaque() {
1056                // Opaque surface: the background. Return the first hit verbatim
1057                // when nothing translucent preceded it (bit-identical), else
1058                // composite the accumulated layers over it.
1059                let color = if touched {
1060                    composite_over(accum, trans, lit)
1061                } else {
1062                    lit
1063                };
1064                return Some(Hit {
1065                    color,
1066                    dist: depth.max(0.0),
1067                });
1068            }
1069            // Translucent: one alpha layer per solid-run entry or material
1070            // change (per-span — avoids the voxel-grid striping through a
1071            // thick glass/water slab).
1072            let mat_id = material_for_color(env.terrain_materials, color);
1073            if !prev_solid || mat_id != prev_mat {
1074                let a = f32::from(m.alpha) / 255.0;
1075                let c = rgb_to_f32(lit);
1076                accum[0] += trans * a * c[0];
1077                accum[1] += trans * a * c[1];
1078                accum[2] += trans * a * c[2];
1079                if !matches!(m.mode, roxlap_formats::material::BlendMode::Additive) {
1080                    trans *= 1.0 - a; // AlphaBlend occludes; Additive does not.
1081                }
1082                touched = true;
1083                prev_mat = mat_id;
1084                if trans < 1.0 / 256.0 {
1085                    return Some(Hit {
1086                        color: f32_to_rgb(accum),
1087                        dist: depth.max(0.0),
1088                    });
1089                }
1090            }
1091            prev_solid = true;
1092        } else {
1093            prev_solid = false;
1094        }
1095        let axis = min_axis(t_max);
1096        last_axis = axis;
1097        t_curr = t_max[axis];
1098        cellc[axis] += step[axis];
1099        t_max[axis] += t_delta[axis];
1100    }
1101    None
1102}
1103
1104/// Per-face brightness reduction for the hit face. `axis` is the axis
1105/// the ray crossed to enter the hit voxel (`3` = entry voxel, no face);
1106/// `step[axis]` gives the crossing direction. Maps to the
1107/// `[x-, x+, y-, y+, z-, z+]` `side_shades` entry of the face the ray
1108/// looks at (a `+step` crossing enters through the low / `-` face).
1109#[inline]
1110fn side_shade_sub(env: &DdaEnv<'_>, axis: usize, step: [i32; 3]) -> u32 {
1111    if axis >= 3 {
1112        return 0;
1113    }
1114    let face = axis * 2 + usize::from(step[axis] < 0);
1115    env.side_shades[face].max(0) as u32
1116}
1117
1118/// Cast one ray into the grid and return the first solid hit.
1119///
1120/// **DDA.4:** cross-chunk per-pixel 3D-DDA over the grid's full voxel
1121/// box ([`GridView::voxel_bounds`], spanning every chunk in XY **and**
1122/// Z). The [`Sampler`] resolves each stepped voxel to its chunk and
1123/// brick-gates the slab walk. Cross-chunk look-down (the case the
1124/// voxlap renderer needed the whole virtual-column stack for) falls out
1125/// of the box simply spanning `chunks_z` along Z.
1126fn cast_ray(
1127    origin: [f32; 3],
1128    dir: [f32; 3],
1129    forward: [f32; 3],
1130    sampler: &mut Sampler<'_>,
1131    settings: &OpticastSettings,
1132    env: &DdaEnv<'_>,
1133) -> Option<Hit> {
1134    let (lo_i, hi_i) = sampler.grid.voxel_bounds();
1135    #[allow(clippy::cast_precision_loss)]
1136    let lo_f = [lo_i[0] as f32, lo_i[1] as f32, lo_i[2] as f32];
1137    #[allow(clippy::cast_precision_loss)]
1138    let hi_f = [hi_i[0] as f32, hi_i[1] as f32, hi_i[2] as f32];
1139    let (t_enter, t_exit) = intersect_aabb(origin, dir, lo_f, hi_f)?;
1140    let fwd_dot = dir[0] * forward[0] + dir[1] * forward[1] + dir[2] * forward[2];
1141    #[allow(clippy::cast_precision_loss)]
1142    let max_dist = settings.max_scan_dist.max(1) as f32;
1143    let cell = 1i32 << sampler.mip;
1144    let cell_size = cell as f32;
1145    let lo_c = [
1146        lo_i[0].div_euclid(cell),
1147        lo_i[1].div_euclid(cell),
1148        lo_i[2].div_euclid(cell),
1149    ];
1150    let hi_c = [
1151        hi_i[0].div_euclid(cell),
1152        hi_i[1].div_euclid(cell),
1153        hi_i[2].div_euclid(cell),
1154    ];
1155    cell_walk_skip(
1156        origin, dir, fwd_dot, sampler, lo_c, hi_c, cell_size, t_enter, t_exit, max_dist, env,
1157    )
1158}
1159
1160/// Render one grid into `sink` with per-pixel 3D-DDA.
1161///
1162/// `camera` is the grid-local pose, `settings`
1163/// ([`OpticastSettings`]) carries the projection + viewport (including
1164/// the `y_start..y_end` strip bound), and `grid` is the per-frame
1165/// [`GridView`] borrow. `pitch_pixels` is the framebuffer
1166/// row stride in pixels (matches `ScalarRasterizer::new`'s argument).
1167///
1168/// On a miss, a textured sky ([`DdaEnv::sky`]) is sampled per ray
1169/// direction and written at `+inf` depth; with no textured sky the miss
1170/// writes nothing, so the caller's solid sky pre-fill shows (the
1171/// `render_scene_composed` path pre-fills it).
1172pub fn render_dda(
1173    camera: &Camera,
1174    settings: &OpticastSettings,
1175    grid: GridView<'_>,
1176    pitch_pixels: usize,
1177    env: &DdaEnv<'_>,
1178    mip: u32,
1179    sink: &mut impl PixelSink,
1180) {
1181    let cs = camera_math::derive(
1182        camera,
1183        settings.xres,
1184        settings.yres,
1185        settings.hx,
1186        settings.hy,
1187        settings.hz,
1188    );
1189
1190    // Sequential path builds a throwaway per-call cache (tests / single
1191    // grid). The parallel path takes a persistent cross-frame cache.
1192    let (cache, mip) = local_cache(&grid, mip);
1193    let mut sampler = Sampler::new(grid, &cache, mip);
1194
1195    for py in settings.y_start..settings.y_end {
1196        let row = py as usize * pitch_pixels;
1197        for px in 0..settings.xres {
1198            if let Some((color, dist)) = pixel_result(&cs, settings, &mut sampler, env, px, py) {
1199                sink.put(row + px as usize, color, dist);
1200            }
1201        }
1202    }
1203}
1204
1205/// Resolve one pixel: a shaded + fogged hit colour, a sampled textured
1206/// sky on a miss, or `None` (miss with no textured sky → caller's
1207/// pre-fill stands). Shared by the sequential ([`render_dda`]) and
1208/// parallel ([`render_dda_parallel`]) drivers.
1209#[inline]
1210fn pixel_result(
1211    cs: &CameraState,
1212    settings: &OpticastSettings,
1213    sampler: &mut Sampler<'_>,
1214    env: &DdaEnv<'_>,
1215    px: u32,
1216    py: u32,
1217) -> Option<(u32, f32)> {
1218    let (origin, dir) = pixel_ray(cs, settings, px, py);
1219    if let Some(hit) = cast_ray(origin, dir, cs.forward, sampler, settings, env) {
1220        Some((hit.color, hit.dist))
1221    } else {
1222        env.sky.map(|sky| (sample_sky(sky, dir), f32::INFINITY))
1223    }
1224}
1225
1226/// Tile-parallel [`render_dda`] writing straight into `(fb, zb)`.
1227///
1228/// DDA pixels are independent, so the framebuffer splits into disjoint
1229/// horizontal bands rendered concurrently (rayon) — **bit-identical**
1230/// to the sequential render regardless of thread count, unlike voxlap's
1231/// per-strip discretisation. Each band spins up its own lightweight
1232/// [`Sampler`] over the shared, immutable `cache`.
1233///
1234/// `cache` must already hold current brick maps for every chunk at
1235/// `mip` (populate via [`BrickCache::ensure`]); `mip` is the effective
1236/// render mip ([`effective_mip`]). `(fb, zb)` use the standard
1237/// conventions (`0x80RRGGBB`; z = perp distance, smaller = closer); a
1238/// miss writes nothing unless [`DdaEnv::sky`] is set. `pitch_pixels` is
1239/// the row stride.
1240#[allow(clippy::cast_possible_truncation, clippy::too_many_arguments)]
1241pub fn render_dda_parallel(
1242    camera: &Camera,
1243    settings: &OpticastSettings,
1244    grid: GridView<'_>,
1245    fb: &mut [u32],
1246    zb: &mut [f32],
1247    pitch_pixels: usize,
1248    env: &DdaEnv<'_>,
1249    cache: &BrickCache,
1250    mip: u32,
1251) {
1252    debug_assert_eq!(fb.len(), zb.len());
1253    let (y0, y1) = (settings.y_start, settings.y_end);
1254    if y1 <= y0 {
1255        return;
1256    }
1257    let cs = camera_math::derive(
1258        camera,
1259        settings.xres,
1260        settings.yres,
1261        settings.hx,
1262        settings.hy,
1263        settings.hz,
1264    );
1265    let target = RasterTarget::new(fb, zb);
1266
1267    // Split the y-range into ~one band per worker thread.
1268    let nthreads = rayon::current_num_threads().max(1);
1269    let rows = (y1 - y0) as usize;
1270    let band = rows.div_ceil(nthreads).max(1) as u32;
1271    let bands: Vec<(u32, u32)> = (y0..y1)
1272        .step_by(band as usize)
1273        .map(|s| (s, (s + band).min(y1)))
1274        .collect();
1275
1276    bands.par_iter().for_each(|&(by0, by1)| {
1277        let mut sampler = Sampler::new(grid, cache, mip);
1278        for py in by0..by1 {
1279            let row = py as usize * pitch_pixels;
1280            for px in 0..settings.xres {
1281                if let Some((color, dist)) = pixel_result(&cs, settings, &mut sampler, env, px, py)
1282                {
1283                    let idx = row + px as usize;
1284                    // SAFETY: bands cover disjoint row ranges, so writes
1285                    // never alias across threads; `idx` is in-bounds for
1286                    // a `pitch * height`-sized buffer.
1287                    unsafe {
1288                        target.write_color(idx, color);
1289                        target.write_depth(idx, dist);
1290                    }
1291                }
1292            }
1293        }
1294    });
1295}
1296
1297/// Dense per-voxel reference cast for a **single-chunk** grid: walks
1298/// every voxel of `[0, vsid)² × [0, CHUNK_SIZE_Z)` calling
1299/// [`GridView::surface_color`] directly — no brick gate, no chunk
1300/// resolution. The equivalence oracle the brickmap + sampler
1301/// [`cast_ray`] is checked against in tests.
1302#[cfg(test)]
1303#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
1304fn cast_ray_reference(
1305    origin: [f32; 3],
1306    dir: [f32; 3],
1307    forward: [f32; 3],
1308    grid: &GridView<'_>,
1309    settings: &OpticastSettings,
1310) -> Option<Hit> {
1311    let nx = grid.vsid as f32;
1312    let nz = f32::from(u16::try_from(crate::grid_view::CHUNK_SIZE_Z).unwrap_or(256));
1313    #[allow(clippy::cast_possible_wrap)]
1314    let n_i = [
1315        grid.vsid as i32,
1316        grid.vsid as i32,
1317        crate::grid_view::CHUNK_SIZE_Z as i32,
1318    ];
1319    let (t_enter, t_exit) = intersect_aabb(origin, dir, [0.0; 3], [nx, nx, nz])?;
1320    let fwd_dot = dir[0] * forward[0] + dir[1] * forward[1] + dir[2] * forward[2];
1321    let max_dist = settings.max_scan_dist.max(1) as f32;
1322
1323    let start = t_enter + 1e-4;
1324    let p = [
1325        origin[0] + dir[0] * start,
1326        origin[1] + dir[1] * start,
1327        origin[2] + dir[2] * start,
1328    ];
1329    let mut voxel = [
1330        (p[0].floor() as i32).clamp(0, n_i[0] - 1),
1331        (p[1].floor() as i32).clamp(0, n_i[1] - 1),
1332        (p[2].floor() as i32).clamp(0, n_i[2] - 1),
1333    ];
1334    let (step, mut t_max, t_delta) = dda_setup(origin, dir, voxel, 1.0);
1335    let mut t_curr = t_enter;
1336    let max_steps = (n_i[0] + n_i[1] + n_i[2]) as usize + 8;
1337    for _ in 0..max_steps {
1338        if voxel[0] < 0
1339            || voxel[0] >= n_i[0]
1340            || voxel[1] < 0
1341            || voxel[1] >= n_i[1]
1342            || voxel[2] < 0
1343            || voxel[2] >= n_i[2]
1344        {
1345            return None;
1346        }
1347        let depth = t_curr * fwd_dot;
1348        if depth > max_dist || t_curr > t_exit {
1349            return None;
1350        }
1351        #[allow(clippy::cast_sign_loss)]
1352        if let Some(color) = grid.surface_color(voxel[0] as u32, voxel[1] as u32, voxel[2] as u32) {
1353            return Some(Hit {
1354                color: shade(color, 0),
1355                dist: depth.max(0.0),
1356            });
1357        }
1358        let axis = min_axis(t_max);
1359        t_curr = t_max[axis];
1360        voxel[axis] += step[axis];
1361        t_max[axis] += t_delta[axis];
1362    }
1363    None
1364}
1365
1366#[cfg(test)]
1367mod tests {
1368    use super::*;
1369
1370    /// Recording sink: collects `(idx, color, dist)` puts for tests.
1371    #[derive(Default)]
1372    struct Recorder {
1373        puts: Vec<(usize, u32, f32)>,
1374    }
1375    impl PixelSink for Recorder {
1376        fn put(&mut self, idx: usize, color: u32, dist: f32) {
1377            self.puts.push((idx, color, dist));
1378        }
1379    }
1380
1381    fn oracle_camera() -> Camera {
1382        // Identity-basis camera at origin: ray math is integer-exact.
1383        Camera {
1384            pos: [0.0, 0.0, 0.0],
1385            right: [1.0, 0.0, 0.0],
1386            down: [0.0, 0.0, 1.0],
1387            forward: [0.0, 1.0, 0.0],
1388        }
1389    }
1390
1391    /// Render `grid` from `camera` into a `w × h` framebuffer and
1392    /// return the per-pixel hit mask (`true` where a ray hit a voxel).
1393    fn render_mask(grid: GridView<'_>, camera: &Camera, w: u32, h: u32) -> Vec<bool> {
1394        let n = (w as usize) * (h as usize);
1395        let mut fb = vec![0u32; n]; // sky sentinel = 0
1396        let mut zb = vec![f32::INFINITY; n];
1397        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
1398        {
1399            let mut sink = RasterSink::new(&mut fb, &mut zb);
1400            render_dda(
1401                camera,
1402                &settings,
1403                grid,
1404                w as usize,
1405                &DdaEnv::default(),
1406                0,
1407                &mut sink,
1408            );
1409        }
1410        fb.iter().map(|&c| c != 0).collect()
1411    }
1412
1413    /// A silhouette is "row-convex" if every framebuffer row's hit
1414    /// pixels form a single contiguous run (no interior gap). The
1415    /// voxlap silhouette notch is exactly such an interior gap, so this
1416    /// is the headline DDA.1 acceptance check.
1417    fn rows_have_no_holes(mask: &[bool], w: u32, h: u32) -> bool {
1418        let w = w as usize;
1419        for y in 0..h as usize {
1420            let row = &mask[y * w..(y + 1) * w];
1421            let first = row.iter().position(|&b| b);
1422            let last = row.iter().rposition(|&b| b);
1423            if let (Some(f), Some(l)) = (first, last) {
1424                if row[f..=l].iter().any(|&b| !b) {
1425                    return false;
1426                }
1427            }
1428        }
1429        true
1430    }
1431
1432    /// Same contiguity check down each column.
1433    fn cols_have_no_holes(mask: &[bool], w: u32, h: u32) -> bool {
1434        let w = w as usize;
1435        let h = h as usize;
1436        for x in 0..w {
1437            let col: Vec<bool> = (0..h).map(|y| mask[y * w + x]).collect();
1438            let first = col.iter().position(|&b| b);
1439            let last = col.iter().rposition(|&b| b);
1440            if let (Some(f), Some(l)) = (first, last) {
1441                if col[f..=l].iter().any(|&b| !b) {
1442                    return false;
1443                }
1444            }
1445        }
1446        true
1447    }
1448
1449    /// The principal-point pixel `(hx, hy)` looks straight down the
1450    /// forward axis, scaled by `hz`.
1451    #[test]
1452    fn center_pixel_ray_is_forward() {
1453        let settings = OpticastSettings::for_oracle_framebuffer(640, 480);
1454        let cs = camera_math::derive(&oracle_camera(), 640, 480, 320.0, 240.0, 320.0);
1455        // hx = hy = 320 / 240 → use the exact principal point.
1456        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
1457        let (origin, dir) = pixel_ray(&cs, &settings, settings.hx as u32, settings.hy as u32);
1458        assert_eq!(origin, [0.0, 0.0, 0.0]);
1459        // hz·forward = 320·[0,1,0].
1460        assert_eq!(
1461            dir.map(f32::to_bits),
1462            [0.0f32, 320.0, 0.0].map(f32::to_bits)
1463        );
1464    }
1465
1466    /// Pixel `(0, 0)`'s ray equals `camera_math`'s `corn[0]` — proving
1467    /// the DDA renderer samples the same rays the voxlap frustum is
1468    /// built from.
1469    #[test]
1470    fn corner_pixel_ray_matches_camera_corn0() {
1471        let settings = OpticastSettings::for_oracle_framebuffer(640, 480);
1472        let cs = camera_math::derive(&oracle_camera(), 640, 480, 320.0, 240.0, 320.0);
1473        let (_origin, dir) = pixel_ray(&cs, &settings, 0, 0);
1474        assert_eq!(dir.map(f32::to_bits), cs.corn[0].map(f32::to_bits));
1475    }
1476
1477    /// The renderer's independent slab decoder
1478    /// ([`GridView::voxel_color`]) must agree with the reference
1479    /// [`roxlap_formats::vxl::Vxl::voxel_color`] for every cell —
1480    /// including a column with an air gap, which exercises the
1481    /// ceiling-colour-list branch.
1482    #[test]
1483    fn gridview_voxel_color_matches_reference() {
1484        // Two solid runs per column separated by air → ceiling list.
1485        let vxl = roxlap_formats::vxl::Vxl::from_dense(8, |x, _, z| {
1486            let lo = (10..=12).contains(&z);
1487            let hi = (40..=42).contains(&z);
1488            (lo || hi).then_some(0x80_10_20_30 + x)
1489        });
1490        let grid = GridView::from_single_vxl(&vxl);
1491        for x in 0..8 {
1492            for y in 0..8 {
1493                for z in 0..64 {
1494                    assert_eq!(
1495                        grid.voxel_color(x, y, z),
1496                        vxl.voxel_color(x, y, z),
1497                        "mismatch at ({x},{y},{z})"
1498                    );
1499                }
1500            }
1501        }
1502    }
1503
1504    /// An all-air grid produces no hits (every ray misses).
1505    #[test]
1506    fn empty_grid_no_hits() {
1507        let vxl = roxlap_formats::vxl::Vxl::empty(64);
1508        let grid = GridView::from_single_vxl(&vxl);
1509        let settings = OpticastSettings::for_oracle_framebuffer(64, 48);
1510        let mut rec = Recorder::default();
1511        render_dda(
1512            &oracle_camera(),
1513            &settings,
1514            grid,
1515            64,
1516            &DdaEnv::default(),
1517            0,
1518            &mut rec,
1519        );
1520        assert!(rec.puts.is_empty(), "all-air grid must produce no hits");
1521    }
1522
1523    /// Camera above a solid floor, looking straight down: every ray
1524    /// hits, the recovered colour is the floor colour, and the centre
1525    /// pixel's depth ≈ the camera's height above the floor.
1526    #[test]
1527    fn floor_seen_from_above() {
1528        const FLOOR_Z: u32 = 40;
1529        const FLOOR_COL: u32 = 0x80_30_60_90;
1530        let vxl =
1531            roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| (z >= FLOOR_Z).then_some(FLOOR_COL));
1532        let grid = GridView::from_single_vxl(&vxl);
1533
1534        // Eye above the floor (z is down), looking down (+z).
1535        let cam = Camera {
1536            pos: [16.0, 16.0, 10.0],
1537            right: [1.0, 0.0, 0.0],
1538            down: [0.0, 1.0, 0.0],
1539            forward: [0.0, 0.0, 1.0],
1540        };
1541        let settings = OpticastSettings::for_oracle_framebuffer(48, 48);
1542        let mut rec = Recorder::default();
1543        render_dda(&cam, &settings, grid, 48, &DdaEnv::default(), 0, &mut rec);
1544
1545        assert!(!rec.puts.is_empty(), "floor must be visible");
1546        // Centre pixel looks straight down → depth ≈ FLOOR_Z - eye_z.
1547        let centre = 24usize * 48 + 24;
1548        let hit = rec
1549            .puts
1550            .iter()
1551            .find(|(idx, _, _)| *idx == centre)
1552            .expect("centre ray must hit the floor");
1553        assert_eq!(hit.1 & 0x00ff_ffff, FLOOR_COL & 0x00ff_ffff);
1554        let expected = (FLOOR_Z as f32) - 10.0;
1555        assert!(
1556            (hit.2 - expected).abs() < 1.5,
1557            "centre depth {} not ≈ {}",
1558            hit.2,
1559            expected
1560        );
1561    }
1562
1563    /// DDA.2: a camera looking at the horizon splits the frame into
1564    /// sky (upward rays miss → no write) and floor (downward rays hit).
1565    /// The top of the frame must be mostly sky, the bottom mostly
1566    /// floor.
1567    #[test]
1568    fn horizon_splits_sky_and_floor() {
1569        const FLOOR_Z: u32 = 40;
1570        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |_, _, z| {
1571            (z >= FLOOR_Z).then_some(0x80_44_66_88)
1572        });
1573        let grid = GridView::from_single_vxl(&vxl);
1574
1575        // At z=30 (above the z=40 floor), looking +y horizontally,
1576        // down = +z. Upward rays (low py) escape through the box top
1577        // (z=0) → sky; downward rays (high py) strike the floor.
1578        let cam = Camera {
1579            pos: [32.0, 4.0, 30.0],
1580            right: [-1.0, 0.0, 0.0],
1581            down: [0.0, 0.0, 1.0],
1582            forward: [0.0, 1.0, 0.0],
1583        };
1584        let (w, h) = (64u32, 64u32);
1585        let mask = render_mask(grid, &cam, w, h);
1586
1587        let count_band = |y0: usize, y1: usize| -> usize {
1588            (y0 * w as usize..y1 * w as usize)
1589                .filter(|&i| mask[i])
1590                .count()
1591        };
1592        let top = count_band(0, h as usize / 4);
1593        let bottom = count_band(3 * h as usize / 4, h as usize);
1594        assert!(mask.iter().any(|&b| b), "floor must be visible");
1595        assert!(mask.iter().any(|&b| !b), "sky must be visible");
1596        assert!(
1597            bottom > top,
1598            "bottom band ({bottom}) should hit more floor than top band ({top})"
1599        );
1600    }
1601
1602    /// Render `grid` from `camera` with the dense reference cast (no
1603    /// brickmap), returning `(colour, depth)` buffers.
1604    fn render_reference(
1605        grid: GridView<'_>,
1606        camera: &Camera,
1607        w: u32,
1608        h: u32,
1609    ) -> (Vec<u32>, Vec<f32>) {
1610        let n = (w as usize) * (h as usize);
1611        let mut fb = vec![0u32; n];
1612        let mut zb = vec![f32::INFINITY; n];
1613        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
1614        let cs = camera_math::derive(camera, w, h, settings.hx, settings.hy, settings.hz);
1615        for py in 0..h {
1616            for px in 0..w {
1617                let (o, d) = pixel_ray(&cs, &settings, px, py);
1618                if let Some(hit) = cast_ray_reference(o, d, cs.forward, &grid, &settings) {
1619                    let i = (py * w + px) as usize;
1620                    fb[i] = hit.color;
1621                    zb[i] = hit.dist;
1622                }
1623            }
1624        }
1625        (fb, zb)
1626    }
1627
1628    /// Render `grid` from `camera` via the production brickmap path.
1629    fn render_brickmap(
1630        grid: GridView<'_>,
1631        camera: &Camera,
1632        w: u32,
1633        h: u32,
1634    ) -> (Vec<u32>, Vec<f32>) {
1635        render_brickmap_env(grid, camera, w, h, &DdaEnv::default())
1636    }
1637
1638    /// As [`render_brickmap`] but with an explicit [`DdaEnv`] (fog /
1639    /// textured sky / side shades).
1640    fn render_brickmap_env(
1641        grid: GridView<'_>,
1642        camera: &Camera,
1643        w: u32,
1644        h: u32,
1645        env: &DdaEnv<'_>,
1646    ) -> (Vec<u32>, Vec<f32>) {
1647        let n = (w as usize) * (h as usize);
1648        let mut fb = vec![0u32; n];
1649        let mut zb = vec![f32::INFINITY; n];
1650        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
1651        {
1652            let mut sink = RasterSink::new(&mut fb, &mut zb);
1653            render_dda(camera, &settings, grid, w as usize, env, 0, &mut sink);
1654        }
1655        (fb, zb)
1656    }
1657
1658    /// Regression for the cave-demo "bright sky seams" report: the
1659    /// empty-space-skip walk must not leak past an occupied box the ray
1660    /// only grazes at a shared edge/corner. A 1-voxel-thick diagonal
1661    /// wall (`x+y==64`, voxels edge-connected) with air on both sides is
1662    /// the canonical case. The production skip walk must hit exactly the
1663    /// same pixels as the dense per-cell reference — zero divergence.
1664    #[test]
1665    fn no_sky_leak_through_diagonal_wall() {
1666        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
1667            ((x + y == 64) && (2..62).contains(&z)).then_some(0x80_40_80_60)
1668        });
1669        let grid = GridView::from_single_vxl(&vxl);
1670        let (w, h) = (160u32, 160u32);
1671        let c = [10.0, 10.0, 32.0];
1672        let poses = [
1673            Camera::from_yaw_pitch(c, 0.785, 0.0),
1674            Camera::from_yaw_pitch(c, 0.6, 0.1),
1675            Camera::from_yaw_pitch(c, 0.95, -0.1),
1676            Camera::from_yaw_pitch(c, 0.785, 0.3),
1677            Camera::from_yaw_pitch(c, 0.5, 0.0),
1678        ];
1679        for (i, cam) in poses.iter().enumerate() {
1680            let (fb_b, _) = render_brickmap(grid, cam, w, h);
1681            let (fb_r, _) = render_reference(grid, cam, w, h);
1682            let leak = (0..(w * h) as usize)
1683                .filter(|&k| (fb_b[k] != 0) != (fb_r[k] != 0))
1684                .count();
1685            assert_eq!(leak, 0, "pose {i}: {leak} px diverge from dense reference");
1686        }
1687    }
1688
1689    /// TV terrain transparency: a glass-coloured voxel slab in front of an
1690    /// opaque floor. With no terrain material map the glass is an opaque first
1691    /// hit; with the map it becomes translucent and the floor tints through.
1692    #[test]
1693    fn terrain_glass_tints_floor_behind() {
1694        let glass = 0x80_40_C0_E0; // cyan
1695        let floor = 0x80_C0_40_40; // red
1696        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| {
1697            if z == 4 {
1698                Some(glass)
1699            } else if z >= 10 {
1700                Some(floor)
1701            } else {
1702                None
1703            }
1704        });
1705        let grid = GridView::from_single_vxl(&vxl);
1706        // Camera above the grid looking straight down (+z), centred.
1707        let cam = Camera {
1708            pos: [8.0, 8.0, 0.0],
1709            right: [1.0, 0.0, 0.0],
1710            down: [0.0, 1.0, 0.0],
1711            forward: [0.0, 0.0, 1.0],
1712        };
1713        let (w, h) = (32u32, 32u32);
1714        let centre = (h / 2 * w + w / 2) as usize;
1715
1716        // Opaque: the glass voxel stops the ray (no terrain materials).
1717        let (fb_op, _) = render_brickmap(grid, &cam, w, h);
1718        assert_eq!(
1719            fb_op[centre] & 0x00ff_ffff,
1720            0x0040_C0E0,
1721            "opaque glass first-hit"
1722        );
1723
1724        // Translucent: glass colour → material 1 (alpha-blend).
1725        let mut table = MaterialTable::new();
1726        table.set(1, Material::alpha_blend(128));
1727        let env = DdaEnv {
1728            materials: Some(&table),
1729            terrain_materials: &[(glass & 0x00ff_ffff, 1)],
1730            ..DdaEnv::default()
1731        };
1732        let (fb_tr, _) = render_brickmap_env(grid, &cam, w, h, &env);
1733        assert_ne!(
1734            fb_tr[centre], fb_op[centre],
1735            "glass should composite over the floor, not stay opaque"
1736        );
1737        let r_op = (fb_op[centre] >> 16) & 0xff; // glass red ≈ 0x40
1738        let r_tr = (fb_tr[centre] >> 16) & 0xff; // + floor red bleeds in
1739        assert!(
1740            r_tr > r_op,
1741            "floor red tints through the glass (op={r_op:02x} tr={r_tr:02x})"
1742        );
1743    }
1744
1745    /// DDA.5: distance fog blends a hit toward the fog colour. A far
1746    /// floor pixel is closer to the fog colour than a near one.
1747    #[test]
1748    fn distance_fog_blends_toward_fog_color() {
1749        let vxl =
1750            roxlap_formats::vxl::Vxl::from_dense(64, |_, _, z| (z >= 40).then_some(0x80_FF_FF_FF));
1751        let grid = GridView::from_single_vxl(&vxl);
1752        let cam = Camera {
1753            pos: [32.0, 2.0, 38.0],
1754            right: [1.0, 0.0, 0.0],
1755            down: [0.0, 0.0, 1.0],
1756            forward: [0.0, 1.0, 0.0],
1757        };
1758        let env = DdaEnv {
1759            sky: None,
1760            fog_color: 0x00_00_00_00, // black fog → distance darkens
1761            fog_max_dist: 64.0,
1762            side_shades: [0; 6],
1763            materials: None,
1764            terrain_materials: &[],
1765        };
1766        let (w, h) = (64u32, 64u32);
1767        let (fog, _) = render_brickmap_env(grid, &cam, w, h, &env);
1768        let (nofog, zb) = render_brickmap(grid, &cam, w, h);
1769        let (idx, depth) = zb.iter().enumerate().filter(|(_, z)| z.is_finite()).fold(
1770            (0usize, 0.0f32),
1771            |acc, (i, &z)| {
1772                if z > acc.1 {
1773                    (i, z)
1774                } else {
1775                    acc
1776                }
1777            },
1778        );
1779        assert!(depth > 20.0, "need a deep pixel to test fog (got {depth})");
1780        let lum = |c: u32| (c & 0xff) + ((c >> 8) & 0xff) + ((c >> 16) & 0xff);
1781        assert!(
1782            lum(fog[idx]) < lum(nofog[idx]),
1783            "fogged pixel {:08x} not darker than {:08x}",
1784            fog[idx],
1785            nofog[idx]
1786        );
1787    }
1788
1789    /// DDA.5: with a textured sky, miss pixels are filled from the sky
1790    /// panorama (direction-dependent) instead of left at the pre-fill.
1791    #[test]
1792    fn textured_sky_fills_misses() {
1793        let sky = crate::sky::Sky::blue_gradient();
1794        let vxl = roxlap_formats::vxl::Vxl::empty(32); // all air → all miss
1795        let grid = GridView::from_single_vxl(&vxl);
1796        let env = DdaEnv {
1797            sky: Some(&sky),
1798            fog_color: 0,
1799            fog_max_dist: 0.0,
1800            side_shades: [0; 6],
1801            materials: None,
1802            terrain_materials: &[],
1803        };
1804        let cam = Camera::from_yaw_pitch([16.0, 16.0, 128.0], 0.3, -0.4);
1805        let (w, h) = (48u32, 48u32);
1806        let (fb, _) = render_brickmap_env(grid, &cam, w, h, &env);
1807        assert!(fb.iter().all(|&c| c >> 24 == 0x80), "all misses sky-filled");
1808        let top = fb[0];
1809        let bottom = fb[(h - 1) as usize * w as usize];
1810        assert_ne!(top, bottom, "sky gradient should vary with elevation");
1811    }
1812
1813    /// Sky elevation orientation matches the GPU `sky_color` (acos(-z)/π):
1814    /// looking **up** (−z) samples panorama column 0 (zenith), looking
1815    /// **down** (+z) samples the last column (nadir). Regression for the
1816    /// CPU up/down inversion.
1817    #[test]
1818    fn sky_elevation_zenith_at_column_zero() {
1819        let mut pixels = vec![0i32; 8];
1820        pixels[0] = 0x0011_1111; // zenith marker
1821        pixels[7] = 0x0099_9999; // nadir marker
1822        let sky = crate::sky::Sky::from_pixels(pixels, 8, 1);
1823        let up = sample_sky(&sky, [0.0, 0.0, -1.0]); // −z is up
1824        let down = sample_sky(&sky, [0.0, 0.0, 1.0]); // +z is down
1825        assert_eq!(
1826            up & 0x00ff_ffff,
1827            0x0011_1111,
1828            "looking up → column 0 (zenith)"
1829        );
1830        assert_eq!(
1831            down & 0x00ff_ffff,
1832            0x0099_9999,
1833            "looking down → last column (nadir)"
1834        );
1835    }
1836
1837    /// `render_sky_fill` paints the panorama for a **gridless** view — the
1838    /// same per-pixel sky sample the miss-ray path uses, with no grid present
1839    /// (the CPU empty-scene background, matching the GPU).
1840    #[test]
1841    fn sky_fill_paints_panorama_gridless() {
1842        let sky = crate::sky::Sky::blue_gradient();
1843        let cam = Camera::from_yaw_pitch([0.0, 0.0, 0.0], 0.3, -0.4);
1844        let (w, h) = (48u32, 48u32);
1845        let cs = crate::camera_math::derive(&cam, w, h, 24.0, 24.0, 24.0);
1846        let settings = crate::opticast::OpticastSettings::for_oracle_framebuffer(w, h);
1847        let mut fb = vec![0u32; (w * h) as usize];
1848        // All-background z-buffer (+∞) → every pixel gets the sky.
1849        let zb = vec![f32::INFINITY; (w * h) as usize];
1850        render_sky_fill(&mut fb, &zb, w as usize, w, h, &cs, &settings, &sky);
1851        assert!(
1852            fb.iter().all(|&c| c >> 24 == 0x80),
1853            "every pixel sky-filled with the brightness byte set"
1854        );
1855        let top = fb[0];
1856        let bottom = fb[(h - 1) as usize * w as usize];
1857        assert_ne!(top, bottom, "sky gradient should vary with elevation");
1858        // A finite-z (terrain) pixel is left untouched.
1859        let mut fb2 = vec![0x1234_5678u32; (w * h) as usize];
1860        let mut zb2 = vec![f32::INFINITY; (w * h) as usize];
1861        zb2[0] = 10.0; // pretend a terrain hit at pixel 0
1862        render_sky_fill(&mut fb2, &zb2, w as usize, w, h, &cs, &settings, &sky);
1863        assert_eq!(fb2[0], 0x1234_5678, "finite-z pixel is not overwritten");
1864    }
1865
1866    /// DDA.5: side shading darkens the hit face by its `side_shades`
1867    /// entry. A top-facing floor (ray crosses +z to enter) gets the
1868    /// `z-` face reduction (index 4).
1869    #[test]
1870    fn side_shades_darken_hit_face() {
1871        let vxl =
1872            roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| (z >= 8).then_some(0x80_FF_FF_FF));
1873        let grid = GridView::from_single_vxl(&vxl);
1874        let cam = Camera {
1875            pos: [8.0, 8.0, 2.0],
1876            right: [1.0, 0.0, 0.0],
1877            down: [0.0, 1.0, 0.0],
1878            forward: [0.0, 0.0, 1.0],
1879        };
1880        let centre = 16 * 32 + 16;
1881        let (plain, _) = render_brickmap(grid, &cam, 32, 32);
1882        let env = DdaEnv {
1883            sky: None,
1884            fog_color: 0,
1885            fog_max_dist: 0.0,
1886            side_shades: [0, 0, 0, 0, 0x40, 0],
1887            materials: None,
1888            terrain_materials: &[],
1889        };
1890        let (shaded, _) = render_brickmap_env(grid, &cam, 32, 32, &env);
1891        let lum = |c: u32| (c & 0xff) + ((c >> 8) & 0xff) + ((c >> 16) & 0xff);
1892        assert!(
1893            lum(shaded[centre]) < lum(plain[centre]),
1894            "side-shaded face {:08x} not darker than {:08x}",
1895            shaded[centre],
1896            plain[centre]
1897        );
1898    }
1899
1900    /// The two-level brick-skip cast closely approximates the dense
1901    /// per-voxel reference. The outer brick DDA re-seeds the inner cell
1902    /// walk at each occupied brick, so a few silhouette-boundary pixels
1903    /// jitter by one voxel (different hit cell → different colour/depth)
1904    /// — visually invisible, and the gain is ~`BRICK`× fewer air steps.
1905    /// Assert the divergence is tiny: coverage (hit/sky mask) is nearly
1906    /// identical and only a small fraction of pixels differ. (The
1907    /// thread-invariance guarantee is the separate, exact
1908    /// `parallel_matches_sequential`.)
1909    #[test]
1910    fn brickmap_approximates_dense_reference() {
1911        // Rolling heightmap + a floating block (air above and below).
1912        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
1913            let surf = 30 + ((x / 5 + y / 7) % 11);
1914            let ground = z >= surf;
1915            let block = (20..=24).contains(&z) && (10..20).contains(&x) && (40..50).contains(&y);
1916            (ground || block).then_some(0x80_30_50_70 + (x ^ y) % 0x40)
1917        });
1918        let grid = GridView::from_single_vxl(&vxl);
1919
1920        let (w, h) = (80u32, 80u32);
1921        let poses = [
1922            Camera::orbit(0.6, 0.5, 90.0, [32.0, 32.0, 40.0]),
1923            Camera::orbit(2.1, 0.2, 70.0, [32.0, 32.0, 35.0]),
1924            Camera::orbit(-1.0, 0.9, 120.0, [32.0, 32.0, 45.0]),
1925        ];
1926        let n = (w * h) as usize;
1927        for (i, cam) in poses.iter().enumerate() {
1928            let (fb_b, zb_b) = render_brickmap(grid, cam, w, h);
1929            let (fb_r, _zb_r) = render_reference(grid, cam, w, h);
1930            // Coverage (hit vs sky) must match almost exactly.
1931            let cov_b = fb_b.iter().filter(|&&c| c != 0).count();
1932            let cov_r = fb_r.iter().filter(|&&c| c != 0).count();
1933            assert!(cov_b > 200, "pose {i} rendered ~empty (cov {cov_b})");
1934            let cov_diff = cov_b.abs_diff(cov_r);
1935            assert!(
1936                cov_diff * 100 <= n, // < 1 % of pixels flip hit↔sky
1937                "pose {i} coverage diverged: brick {cov_b} vs dense {cov_r}"
1938            );
1939            // Colour diffs (boundary-voxel jitter) must be a small slice.
1940            let diffs = fb_b.iter().zip(&fb_r).filter(|(a, b)| a != b).count();
1941            assert!(
1942                diffs * 100 <= n * 3, // < 3 % of pixels differ
1943                "pose {i} too many pixel diffs vs dense: {diffs}/{n}"
1944            );
1945            // Depth must be sane (finite where hit), not wildly off.
1946            for k in 0..n {
1947                if fb_b[k] != 0 {
1948                    assert!(zb_b[k].is_finite(), "pose {i} px {k} non-finite depth");
1949                }
1950            }
1951        }
1952    }
1953
1954    /// DDA.5: a voxel's baked brightness byte darkens its colour. A
1955    /// half-bright voxel (`a = 0x40`) renders at roughly half RGB; a
1956    /// full-bright one (`a = 0x80`) is unchanged.
1957    #[test]
1958    fn baked_brightness_darkens_color() {
1959        // Half brightness: alpha 0x40 (64/128). White RGB → ~mid grey.
1960        let dim =
1961            roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| (z >= 8).then_some(0x40_FF_FF_FF));
1962        let grid = GridView::from_single_vxl(&dim);
1963        let cam = Camera {
1964            pos: [8.0, 8.0, 2.0],
1965            right: [1.0, 0.0, 0.0],
1966            down: [0.0, 1.0, 0.0],
1967            forward: [0.0, 0.0, 1.0],
1968        };
1969        let (fb, _) = render_brickmap(grid, &cam, 32, 32);
1970        let centre = 16 * 32 + 16;
1971        // 0xFF * 64 >> 7 = 127 per channel; alpha normalised to 0x80.
1972        assert_eq!(fb[centre], 0x80_7F_7F_7F, "got {:08x}", fb[centre]);
1973
1974        // Full brightness passes RGB through unchanged.
1975        let full =
1976            roxlap_formats::vxl::Vxl::from_dense(16, |_, _, z| (z >= 8).then_some(0x80_FF_FF_FF));
1977        let gridf = GridView::from_single_vxl(&full);
1978        let (fbf, _) = render_brickmap(gridf, &cam, 32, 32);
1979        assert_eq!(fbf[centre], 0x80_FF_FF_FF, "got {:08x}", fbf[centre]);
1980    }
1981
1982    /// DDA.4 headline gate: cross-chunk look-down. A camera in an
1983    /// all-air upper chunk (chz=0) looking straight down must see the
1984    /// floor in the *lower* stacked chunk (chz=1), through the chunk-Z
1985    /// boundary. This is exactly the case the voxlap renderer needed the
1986    /// whole virtual-column stack (S4B.6.j / VC) for; the DDA gets it
1987    /// for free from the outer box spanning `chunks_z`.
1988    #[test]
1989    fn cross_chunk_lookdown_sees_lower_stacked_floor() {
1990        const FLOOR_LOCAL_Z: u32 = 40;
1991        const FLOOR_COL: u32 = 0x80_22_88_44;
1992        let upper = roxlap_formats::vxl::Vxl::empty(32); // all air + bedrock
1993        let lower = roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| {
1994            (z >= FLOOR_LOCAL_Z).then_some(FLOOR_COL)
1995        });
1996        let v_up = GridView::from_single_vxl(&upper);
1997        let v_lo = GridView::from_single_vxl(&lower);
1998        // Z-stack: index (dz*chunks_y+dy)*chunks_x+dx → [upper, lower].
1999        let chunks = [Some(v_up), Some(v_lo)];
2000        let cg = crate::ChunkGrid {
2001            chunks: &chunks,
2002            origin_chunk_xy: [0, 0],
2003            origin_chunk_z: 0,
2004            chunks_x: 1,
2005            chunks_y: 1,
2006            chunks_z: 2,
2007        };
2008        let grid = GridView::from_chunk_grid(&cg, 32);
2009
2010        // Camera in the upper chunk (world z=100), looking straight down.
2011        let cam = Camera {
2012            pos: [16.0, 16.0, 100.0],
2013            right: [1.0, 0.0, 0.0],
2014            down: [0.0, 1.0, 0.0],
2015            forward: [0.0, 0.0, 1.0],
2016        };
2017        let (w, h) = (48u32, 48u32);
2018        let (fb, zb) = render_brickmap(grid, &cam, w, h);
2019        let centre = 24 * 48 + 24;
2020        assert!(
2021            fb[centre] & 0x00ff_ffff == FLOOR_COL & 0x00ff_ffff,
2022            "centre ray must reach the lower-chunk floor (got {:08x})",
2023            fb[centre]
2024        );
2025        // Floor world-z = 256 + 40 = 296; camera z = 100 → depth ≈ 196.
2026        let expected = 296.0 - 100.0;
2027        assert!(
2028            (zb[centre] - expected).abs() < 2.0,
2029            "look-down depth {} not ≈ {expected}",
2030            zb[centre]
2031        );
2032    }
2033
2034    /// DDA.4: a floor spanning two side-by-side chunks (chunks_x=2)
2035    /// renders continuously across the chunk-XY seam — hits on both
2036    /// sides, no gap column.
2037    #[test]
2038    fn cross_chunk_xy_floor_is_seamless() {
2039        let mk = || {
2040            roxlap_formats::vxl::Vxl::from_dense(32, |_, _, z| (z >= 20).then_some(0x80_50_50_50))
2041        };
2042        let (c0, c1) = (mk(), mk());
2043        let v0 = GridView::from_single_vxl(&c0);
2044        let v1 = GridView::from_single_vxl(&c1);
2045        let chunks = [Some(v0), Some(v1)];
2046        let cg = crate::ChunkGrid {
2047            chunks: &chunks,
2048            origin_chunk_xy: [0, 0],
2049            origin_chunk_z: 0,
2050            chunks_x: 2,
2051            chunks_y: 1,
2052            chunks_z: 1,
2053        };
2054        let grid = GridView::from_chunk_grid(&cg, 32);
2055
2056        // High above the seam (x=32), looking straight down.
2057        let cam = Camera {
2058            pos: [32.0, 16.0, 4.0],
2059            right: [1.0, 0.0, 0.0],
2060            down: [0.0, 1.0, 0.0],
2061            forward: [0.0, 0.0, 1.0],
2062        };
2063        let (w, h) = (64u32, 64u32);
2064        let mask = render_mask(grid, &cam, w, h);
2065        // Both the left chunk (screen left) and right chunk (screen
2066        // right) must show floor on the centre row.
2067        let row = (h / 2) as usize * w as usize;
2068        let left = (0..w as usize / 2).filter(|&x| mask[row + x]).count();
2069        let right = (w as usize / 2..w as usize)
2070            .filter(|&x| mask[row + x])
2071            .count();
2072        assert!(
2073            left > 5 && right > 5,
2074            "seam not continuous: left={left} right={right}"
2075        );
2076    }
2077
2078    /// Render `grid` from `camera` at render `mip` and return the hit
2079    /// mask.
2080    fn render_mask_mip(grid: GridView<'_>, camera: &Camera, w: u32, h: u32, mip: u32) -> Vec<bool> {
2081        let n = (w as usize) * (h as usize);
2082        let mut fb = vec![0u32; n];
2083        let mut zb = vec![f32::INFINITY; n];
2084        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
2085        {
2086            let mut sink = RasterSink::new(&mut fb, &mut zb);
2087            render_dda(
2088                camera,
2089                &settings,
2090                grid,
2091                w as usize,
2092                &DdaEnv::default(),
2093                mip,
2094                &mut sink,
2095            );
2096        }
2097        fb.iter().map(|&c| c != 0).collect()
2098    }
2099
2100    /// DDA.6: rendering a mip-built grid at a coarse mip stays complete
2101    /// (hole-free silhouette) with roughly the same screen coverage as
2102    /// mip 0 — LOD coarsens detail, it doesn't punch holes or shrink the
2103    /// shape. (DDA has no axis-aligned mip beam — the artifact is
2104    /// structurally impossible with honest per-cell traversal.)
2105    #[test]
2106    fn mip_render_is_coarse_but_complete() {
2107        let mut vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
2108            let surf = 24 + ((x / 3 + y / 5) % 17);
2109            (z >= surf).then_some(0x80_50_70_90)
2110        });
2111        vxl.generate_mips(4);
2112        assert!(vxl.mip_count() >= 3, "need mips built for this test");
2113        let grid = GridView::from_single_vxl(&vxl);
2114        let (w, h) = (96u32, 96u32);
2115        let cam = Camera::orbit(0.7, 0.6, 110.0, [32.0, 32.0, 36.0]);
2116
2117        let m0 = render_mask_mip(grid, &cam, w, h, 0);
2118        let m2 = render_mask_mip(grid, &cam, w, h, 2);
2119
2120        let c0 = m0.iter().filter(|&&b| b).count();
2121        let c2 = m2.iter().filter(|&&b| b).count();
2122        assert!(c0 > 200 && c2 > 200, "both mips visible (c0={c0} c2={c2})");
2123        // Coverage within ~30 % — a coarse-mip silhouette closely tracks
2124        // the fine one (LOD coarsens detail, it doesn't lose the shape).
2125        // (Terrain silhouettes are non-convex — sky shows through
2126        // valleys — so a hole-free invariant doesn't apply here; that's
2127        // the convex single-voxel test's job.)
2128        let ratio = c2 as f32 / c0 as f32;
2129        assert!(
2130            (0.7..1.4).contains(&ratio),
2131            "mip-2 coverage {c2} vs mip-0 {c0} (ratio {ratio:.2}) diverged"
2132        );
2133    }
2134
2135    /// Headless perf bench (run: `cargo test -p roxlap-core --release
2136    /// dda::tests::bench_terrain -- --ignored --nocapture`). Single-
2137    /// thread `render_dda` over a hilly chunk at a horizon pose; prints
2138    /// ms/frame + per-frame traversal counters (cells / bricks /
2139    /// surface_color calls) to locate the bottleneck.
2140    #[test]
2141    #[ignore = "perf benchmark — run explicitly with --ignored"]
2142    fn bench_terrain() {
2143        use std::time::Instant;
2144        // Multi-chunk grid like the demo: NC×NC chunks of 128, hills.
2145        const NC: i32 = 6;
2146        let cs = crate::grid_view::CHUNK_SIZE_Z; // 256, but vsid is 128
2147        let _ = cs;
2148        let mut vxls: Vec<roxlap_formats::vxl::Vxl> = Vec::new();
2149        for cy in 0..NC {
2150            for cx in 0..NC {
2151                let (ox, oy) = (cx * 128, cy * 128);
2152                let mut v = roxlap_formats::vxl::Vxl::from_dense(128, |x, y, z| {
2153                    let (gx, gy) = (ox + x as i32, oy + y as i32);
2154                    let surf = 90 + ((gx / 7 + gy / 9).rem_euclid(40)) + ((gx / 23).rem_euclid(20));
2155                    (z as i32 >= surf).then_some(0x80_50_70_90 + (x ^ y) % 0x30)
2156                });
2157                v.generate_mips(4);
2158                vxls.push(v);
2159            }
2160        }
2161        let views: Vec<Option<GridView>> = vxls
2162            .iter()
2163            .map(|v| Some(GridView::from_single_vxl(v)))
2164            .collect();
2165        let cg = crate::ChunkGrid {
2166            chunks: &views,
2167            origin_chunk_xy: [0, 0],
2168            origin_chunk_z: 0,
2169            chunks_x: NC as u32,
2170            chunks_y: NC as u32,
2171            chunks_z: 1,
2172        };
2173        let grid = GridView::from_chunk_grid(&cg, 128);
2174
2175        let (w, h) = (960u32, 600u32);
2176        let mut settings = OpticastSettings::for_oracle_framebuffer(w, h);
2177        settings.max_scan_dist = 512;
2178        let n = (w * h) as usize;
2179        let mut fb = vec![0u32; n];
2180        let mut zb = vec![f32::INFINITY; n];
2181        let centre = [f64::from(NC * 128) / 2.0, f64::from(NC * 128) / 2.0, 60.0];
2182
2183        // Two poses: eye-level toward horizon (long rays) + looking down
2184        // at nearby terrain (short rays, demo-typical).
2185        let poses = [
2186            (
2187                "horizon",
2188                Camera::from_yaw_pitch([20.0, 20.0, 40.0], 0.6, 0.15),
2189            ),
2190            ("down", Camera::orbit(0.7, 1.0, 130.0, centre)),
2191        ];
2192        for (name, cam) in poses {
2193            {
2194                let mut sink = RasterSink::new(&mut fb, &mut zb);
2195                prof::reset();
2196                render_dda(
2197                    &cam,
2198                    &settings,
2199                    grid,
2200                    w as usize,
2201                    &DdaEnv::default(),
2202                    0,
2203                    &mut sink,
2204                );
2205            }
2206            let (cells, bricks, surf) = prof::read();
2207            let iters = 6;
2208            let t0 = Instant::now();
2209            for _ in 0..iters {
2210                let mut sink = RasterSink::new(&mut fb, &mut zb);
2211                render_dda(
2212                    &cam,
2213                    &settings,
2214                    grid,
2215                    w as usize,
2216                    &DdaEnv::default(),
2217                    0,
2218                    &mut sink,
2219                );
2220            }
2221            let ms = t0.elapsed().as_secs_f64() * 1000.0 / f64::from(iters);
2222            let hits = fb.iter().filter(|&&c| c != 0).count();
2223            eprintln!(
2224                "[{name}] {w}x{h} 1-thread: {ms:.1} ms | hits={hits}/{n} | per-px: cells={:.1} bricks={:.1} surf={:.1}",
2225                cells as f64 / n as f64,
2226                bricks as f64 / n as f64,
2227                surf as f64 / n as f64,
2228            );
2229        }
2230    }
2231
2232    /// DDA.7: the tile-parallel driver is bit-identical to the
2233    /// sequential one — DDA pixels are independent, so banding can't
2234    /// change a pixel.
2235    #[test]
2236    fn parallel_matches_sequential() {
2237        let vxl = roxlap_formats::vxl::Vxl::from_dense(64, |x, y, z| {
2238            let surf = 28 + ((x / 4 + y / 6) % 13);
2239            (z >= surf).then_some(0x80_40_60_80 + (x ^ y) % 0x30)
2240        });
2241        let grid = GridView::from_single_vxl(&vxl);
2242        let (w, h) = (96u32, 96u32);
2243        let cam = Camera::orbit(0.8, 0.55, 100.0, [32.0, 32.0, 40.0]);
2244        let env = DdaEnv {
2245            sky: None,
2246            fog_color: 0x00_20_30_40,
2247            fog_max_dist: 120.0,
2248            side_shades: [0, 0, 0, 0, 0x30, 0x10],
2249            materials: None,
2250            terrain_materials: &[],
2251        };
2252
2253        let (seq_fb, seq_zb) = render_brickmap_env(grid, &cam, w, h, &env);
2254
2255        let n = (w * h) as usize;
2256        let mut par_fb = vec![0u32; n];
2257        let mut par_zb = vec![f32::INFINITY; n];
2258        let settings = OpticastSettings::for_oracle_framebuffer(w, h);
2259        let (cache, mip) = local_cache(&grid, 0);
2260        render_dda_parallel(
2261            &cam,
2262            &settings,
2263            grid,
2264            &mut par_fb,
2265            &mut par_zb,
2266            w as usize,
2267            &env,
2268            &cache,
2269            mip,
2270        );
2271        assert!(par_fb == seq_fb, "parallel colour differs from sequential");
2272        assert!(
2273            par_zb
2274                .iter()
2275                .zip(&seq_zb)
2276                .all(|(a, b)| a.to_bits() == b.to_bits()),
2277            "parallel depth differs from sequential"
2278        );
2279    }
2280
2281    /// DDA.2 correctness: a heightmap column's interior is solid even
2282    /// though voxlap only stores a colour for its surface. `voxel_color`
2283    /// returns `None` for an interior voxel, but `surface_color` must
2284    /// return the run's surface colour — otherwise oblique rays striking
2285    /// a cliff *side* would pass straight through (see-through terrain).
2286    #[test]
2287    fn cliff_side_is_solid_not_see_through() {
2288        const TOP_Z: u32 = 50;
2289        const COL: u32 = 0x80_77_88_99;
2290        let vxl = roxlap_formats::vxl::Vxl::from_dense(8, |_, _, z| (z >= TOP_Z).then_some(COL));
2291        let grid = GridView::from_single_vxl(&vxl);
2292
2293        // Surface voxel: coloured directly.
2294        assert_eq!(grid.voxel_color(4, 4, TOP_Z), Some(COL));
2295        // Interior voxel: voxlap stores no colour …
2296        assert_eq!(grid.voxel_color(4, 4, 150), None);
2297        // … but it is solid, and surface_color bleeds the run-top colour
2298        // down the cliff face → a real hit, not see-through.
2299        assert_eq!(grid.surface_color(4, 4, 150), Some(COL));
2300        // Bedrock-style air above the surface stays air.
2301        assert_eq!(grid.surface_color(4, 4, 10), None);
2302    }
2303
2304    /// DDA.2: a camera embedded in solid material hits its own voxel
2305    /// immediately — every ray reports a hit (no skip / no garbage).
2306    #[test]
2307    fn camera_inside_solid_hits_everywhere() {
2308        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |_, _, _| Some(0x80_55_55_55));
2309        let grid = GridView::from_single_vxl(&vxl);
2310        let cam = Camera {
2311            pos: [8.0, 8.0, 128.0],
2312            right: [1.0, 0.0, 0.0],
2313            down: [0.0, 1.0, 0.0],
2314            forward: [0.0, 0.0, 1.0],
2315        };
2316        let (w, h) = (32u32, 32u32);
2317        let mask = render_mask(grid, &cam, w, h);
2318        assert!(
2319            mask.iter().all(|&b| b),
2320            "every ray must hit when the camera is inside solid"
2321        );
2322    }
2323
2324    /// Headline DDA.1 gate: a single solid voxel viewed obliquely
2325    /// projects to a convex silhouette with **no interior holes** —
2326    /// the artifact class (`tiny_grid_1x1x1` silhouette notch) the
2327    /// voxlap renderer cannot avoid. DDA casts independent per-pixel
2328    /// rays, so the silhouette is hole-free by construction.
2329    #[test]
2330    fn single_voxel_silhouette_has_no_notch() {
2331        const C: u32 = 0x80_FF_80_40;
2332        let vxl = roxlap_formats::vxl::Vxl::from_dense(16, |x, y, z| {
2333            (x == 8 && y == 8 && z == 8).then_some(C)
2334        });
2335        let grid = GridView::from_single_vxl(&vxl);
2336
2337        // Orbit the voxel centre obliquely so all three faces show and
2338        // the silhouette is a sizeable hexagon (dist 4 → ~12 px wide).
2339        let cam = Camera::orbit(0.7, 0.6, 4.0, [8.5, 8.5, 8.5]);
2340        let (w, h) = (96u32, 96u32);
2341        let mask = render_mask(grid, &cam, w, h);
2342
2343        let hits = mask.iter().filter(|&&b| b).count();
2344        assert!(
2345            hits > 30,
2346            "silhouette too small to be meaningful: {hits} px"
2347        );
2348        assert!(
2349            rows_have_no_holes(&mask, w, h),
2350            "row-interior gap in single-voxel silhouette (notch)"
2351        );
2352        assert!(
2353            cols_have_no_holes(&mask, w, h),
2354            "column-interior gap in single-voxel silhouette (notch)"
2355        );
2356    }
2357}