Skip to main content

roxlap_gpu/
scene.rs

1//! GPU.5 — multi-grid scene upload + shared storage layout.
2//!
3//! Concatenates every chunk of every grid into one set of storage
4//! buffers + a per-grid offsets table. Each grid keeps its own
5//! `vsid`, `chunks_dims`, `origin_chunk`, and runtime transform;
6//! the shader iterates grids 0..grid_count, transforms the world
7//! camera into each grid's local frame, runs that grid's outer-DDA
8//! over chunks, and tracks the closest hit across all grids.
9//!
10//! Why concatenate rather than one bind group per grid? wgpu's
11//! `MAX_BIND_GROUPS` default is 4; demos with 10+ grids
12//! (`roxlap-scene-demo` has ground + ship + 10 marker pillars =
13//! 12) need a single bind-group layout that scales.
14
15#![allow(
16    clippy::cast_sign_loss,
17    clippy::cast_lossless,
18    clippy::cast_possible_truncation,
19    clippy::cast_possible_wrap,
20    clippy::doc_markdown,
21    clippy::missing_panics_doc,
22    clippy::needless_range_loop,
23    clippy::pub_underscore_fields
24)]
25
26use bytemuck::Zeroable;
27use wgpu::util::DeviceExt;
28
29use crate::decompress::{gpu_mip_count, occ_words_per_column_for_mip, ChunkUpload};
30use crate::grid::GridUpload;
31
32/// GPU.11 — max mip levels the per-slot layout reserves room for in
33/// [`GridStaticMeta`]'s relative-offset tables. Matches
34/// [`crate::decompress::GPU_MAX_MIPS`]; the shader's `array<u32, N>`
35/// must use the same N.
36pub const MAX_GPU_MIPS: usize = 6;
37
38/// GPU.11 — per-slot occupancy/color-offset strides + per-mip
39/// within-slot relative offsets for a grid of side `vsid`. All
40/// chunks of a grid share these (uniform mip count by
41/// [`gpu_mip_count`]). `colors` keep their fixed
42/// [`COLORS_PER_CHUNK_WORDS`] stride; each mip's colours are
43/// concatenated within that block and indexed by the chunk's own
44/// (absolute) `color_offsets`.
45#[derive(Debug, Clone, Copy)]
46pub struct MipLayout {
47    pub mip_count: u32,
48    pub occ_words_per_slot: u32,
49    pub offsets_words_per_slot: u32,
50    /// Within-slot u32 offset where mip `m`'s occupancy starts.
51    pub mip_occ_rel: [u32; MAX_GPU_MIPS],
52    /// Within-slot u32 offset where mip `m`'s color_offsets start.
53    pub mip_coff_rel: [u32; MAX_GPU_MIPS],
54}
55
56impl MipLayout {
57    #[must_use]
58    pub fn for_vsid(vsid: u32) -> Self {
59        let mip_count = gpu_mip_count(vsid);
60        let mut mip_occ_rel = [0u32; MAX_GPU_MIPS];
61        let mut mip_coff_rel = [0u32; MAX_GPU_MIPS];
62        let mut occ_acc = 0u32;
63        let mut coff_acc = 0u32;
64        for m in 0..mip_count {
65            mip_occ_rel[m as usize] = occ_acc;
66            mip_coff_rel[m as usize] = coff_acc;
67            let vsid_m = vsid >> m;
68            let cols = vsid_m * vsid_m;
69            // Each mip stores TWO bitmaps back-to-back: the textured
70            // occupancy then the solid occupancy (cliff-face fix). The
71            // shader reads solid at `tex_base + cols*occ_words_per_col`.
72            occ_acc += 2 * cols * occ_words_per_column_for_mip(m);
73            coff_acc += cols + 1;
74        }
75        Self {
76            mip_count,
77            occ_words_per_slot: occ_acc,
78            offsets_words_per_slot: coff_acc,
79            mip_occ_rel,
80            mip_coff_rel,
81        }
82    }
83}
84
85/// Maximum number of grids the shader's per-grid camera uniform
86/// array can hold. The scene-demo has 12 (1 ground + 1 ship + 10
87/// markers); 16 leaves headroom for a future +4 without re-cooking
88/// the shader. The runtime check rejects scenes that overflow.
89pub const MAX_SCENE_GRIDS: u32 = 16;
90
91/// Per-chunk colour-slot stride, in u32 words (256 KiB). Each
92/// chunk's colour data lives at `meta_idx * COLORS_PER_CHUNK_WORDS`
93/// within its grid's colours range. Fixed-stride layout means
94/// every slot — present or absent at upload time — has the same
95/// capacity, so [`GpuSceneResident::refresh_chunk`] can always
96/// write new colour data into the slot when a chunk arrives via
97/// streaming or is re-baked.
98///
99/// 65536 u32s = 256 KiB. Scene-demo's densest ground-hills chunks
100/// run ~36 k colour entries (~144 KiB) — multiple textured voxels
101/// per column at slopes/cliffs; 256 KiB gives ~1.8× headroom.
102/// Memory cost on the demo's 32×32×1 static grid: 1024 slots ×
103/// 256 KiB = 256 MiB colours (~830 MiB resident scene total).
104/// Chunks past the cap truncate with a stderr warn; GPU.7
105/// sliding-window storage removes the cap entirely.
106pub const COLORS_PER_CHUNK_WORDS: u32 = 65536;
107
108/// Number of separate storage bindings the concatenated occupancy
109/// buffer is split ("paged") across. A single storage binding may
110/// not exceed the device's `max_storage_buffer_binding_size` — on
111/// strict drivers that's a hard 128 MiB (lavapipe), which the
112/// streaming demo's occupancy already reaches. Splitting into pages
113/// keeps every binding under the limit while preserving a single
114/// global word index in the shader (each page is a whole number of
115/// chunk slots, so no slot ever straddles a page boundary).
116///
117/// On GPUs with multi-GiB binding limits (NVK, native Vulkan) the
118/// whole buffer fits in page 0, the other bindings get a 1-word
119/// dummy, and the shader's page select is a single perfectly-
120/// predicted uniform branch → zero hot-loop cost. 4 pages covers
121/// 512 MiB of occupancy even on a 128 MiB-per-binding device.
122pub const MAX_OCC_PAGES: usize = 4;
123
124/// Per-grid runtime transform — voxlap-style (world → grid-local).
125/// `rotation` is column-major and encodes the inverse rotation
126/// applied to the world camera basis before passing it to that
127/// grid's marcher. Identity for the ground; non-trivial for the
128/// rotating ship.
129#[derive(Debug, Clone, Copy)]
130pub struct GridRuntimeTransform {
131    /// Grid-local position of the world origin = `-rotation⁻¹ ·
132    /// grid.position` for a `GridTransform { position, rotation }`.
133    /// The host computes this once per frame.
134    pub grid_origin_world: [f64; 3],
135    /// 3×3 inverse rotation (column-major).
136    pub world_to_grid_rotation: [[f32; 3]; 3],
137}
138
139impl Default for GridRuntimeTransform {
140    fn default() -> Self {
141        Self {
142            grid_origin_world: [0.0, 0.0, 0.0],
143            world_to_grid_rotation: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
144        }
145    }
146}
147
148/// CPU-side aggregation of every grid in a scene. Built once at
149/// startup; per-grid transforms are recomputed each frame and
150/// passed to `render_scene` separately.
151pub struct SceneUpload {
152    pub grids: Vec<GridUpload>,
153}
154
155impl SceneUpload {
156    #[must_use]
157    pub fn grid_count(&self) -> u32 {
158        u32::try_from(self.grids.len()).unwrap_or(u32::MAX)
159    }
160}
161
162/// Per-grid static metadata: offsets into the concatenated storage
163/// buffers + the grid's slot-pool dimensions. Uploaded once.
164///
165/// GPU.7 changes: `chunks_dims` and `origin_chunk` were dropped.
166/// The shader uses modular slot indexing
167/// (`chunk_idx & (pool_dims - 1)`) and verifies slot identity via
168/// `slot_chunk_idx[slot]`, so the upload-time bbox is no longer
169/// relevant to the shader.
170#[repr(C)]
171#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug)]
172pub struct GridStaticMeta {
173    /// `occupancy` u32-word offset where this grid's data starts.
174    pub occupancy_offset: u32,
175    pub color_offsets_offset: u32,
176    pub colors_offset: u32,
177    pub chunk_colors_base_offset: u32,
178    pub chunk_occupancy_offset: u32,
179    /// New in GPU.7: u32-word offset where this grid's
180    /// `slot_chunk_idx` array starts (one `vec3<i32>` per slot,
181    /// i.e. 3 u32 words each, plus 1 padding word for std430).
182    pub slot_chunk_idx_offset: u32,
183    pub vsid: u32,
184    pub total_slots: u32,
185    pub pool_dims: [u32; 3],
186    pub _pad0: u32,
187    /// GPU.11 — per-slot occupancy stride (sum over all mips).
188    /// `meta_id`'s occupancy slab starts at
189    /// `occupancy_offset + meta_id * occ_words_per_slot`.
190    pub occ_words_per_slot: u32,
191    /// GPU.11 — per-slot color_offsets stride (sum over all mips).
192    pub offsets_words_per_slot: u32,
193    /// GPU.11 — number of mip levels stored per slot.
194    pub mip_count: u32,
195    pub _pad1: u32,
196    /// GPU.11 — within-slot u32 offset where mip `m`'s occupancy
197    /// starts. `mip_occ_rel[0] == 0` so mip-0 reads are unchanged.
198    pub mip_occ_rel: [u32; MAX_GPU_MIPS],
199    /// GPU.11 — within-slot u32 offset where mip `m`'s color_offsets
200    /// start. `mip_coff_rel[0] == 0`.
201    pub mip_coff_rel: [u32; MAX_GPU_MIPS],
202}
203
204/// Sentinel chunk_idx written into empty slot_chunk_idx entries.
205/// Real chunk indices never use `i32::MIN`, so the shader can
206/// distinguish empty slots from collisions via a single equality
207/// check.
208pub const SLOT_EMPTY_SENTINEL: [i32; 3] = [i32::MIN, i32::MIN, i32::MIN];
209
210/// GPU-resident storage for an entire scene's grids.
211pub struct GpuSceneResident {
212    pub grid_count: u32,
213    /// Concatenated per-slot occupancy, split into up to
214    /// [`MAX_OCC_PAGES`] storage bindings so no single binding
215    /// exceeds the device's `max_storage_buffer_binding_size`. The
216    /// vec is always exactly `MAX_OCC_PAGES` long — pages past
217    /// `occupancy_num_pages` are 1-word dummies kept only so the
218    /// bind group has a buffer for every layout entry. Page p holds
219    /// the global word range `[p*occupancy_page_words,
220    /// (p+1)*occupancy_page_words)`; `occupancy_page_words` is a
221    /// whole number of chunk slots so no slot straddles a boundary.
222    pub occupancy_pages: Vec<wgpu::Buffer>,
223    /// Words per occupancy page (a multiple of `occ_words_per_slot`).
224    pub occupancy_page_words: u32,
225    /// Number of real (non-dummy) pages in `occupancy_pages`.
226    pub occupancy_num_pages: u32,
227    pub all_color_offsets: wgpu::Buffer,
228    pub all_colors: wgpu::Buffer,
229    pub all_chunk_colors_base: wgpu::Buffer,
230    pub all_chunk_occupancy: wgpu::Buffer,
231    /// GPU.7 — per-slot chunk_idx for identity verification in the
232    /// shader. Stored as `vec3<i32>` with std430 16-byte stride
233    /// (each entry is `[i32; 4]` on the host: x, y, z, _pad).
234    pub all_slot_chunk_idx: wgpu::Buffer,
235    pub grid_static_meta: wgpu::Buffer,
236    pub total_bytes: u64,
237    /// Cached static metadata for the host's frame-loop work.
238    pub static_meta: Vec<GridStaticMeta>,
239    /// CPU shadow of the per-grid chunk-occupancy bitmap. Each entry
240    /// is the u32 word at `chunk_occupancy_offset + (mi >> 5)`.
241    /// `refresh_chunk` / `evict_chunk` flip the right bit + write
242    /// the affected word back to the GPU.
243    pub(crate) chunk_occupancy_shadow: Vec<Vec<u32>>,
244    /// CPU shadow of `slot_chunk_idx`. Indexed `[scene_idx][slot]`
245    /// → `[i32; 4]` (vec3 + pad). Host uses this to detect "slot is
246    /// holding a different chunk than expected" + as the eviction
247    /// origin.
248    pub(crate) slot_chunk_idx_shadow: Vec<Vec<[i32; 4]>>,
249}
250
251impl GpuSceneResident {
252    /// Pack + upload `info`. Each grid is uploaded as a contiguous
253    /// slab inside the shared storage buffers; per-grid offsets
254    /// live in `grid_static_meta`.
255    ///
256    /// # Panics
257    /// If `info.grids.len() > MAX_SCENE_GRIDS`.
258    pub fn upload(device: &wgpu::Device, info: &SceneUpload) -> Self {
259        let grid_count = info.grid_count();
260        assert!(
261            grid_count <= MAX_SCENE_GRIDS,
262            "GpuSceneResident: scene has {grid_count} grids, shader supports {MAX_SCENE_GRIDS}",
263        );
264
265        let mut all_occupancy: Vec<u32> = Vec::new();
266        let mut all_color_offsets: Vec<u32> = Vec::new();
267        let mut all_colors: Vec<u32> = Vec::new();
268        let mut all_chunk_colors_base: Vec<u32> = Vec::new();
269        let mut all_chunk_occupancy: Vec<u32> = Vec::new();
270        let mut all_slot_chunk_idx: Vec<i32> = Vec::new();
271        let mut static_meta: Vec<GridStaticMeta> = Vec::with_capacity(info.grids.len());
272        let mut chunk_occupancy_shadow: Vec<Vec<u32>> = Vec::with_capacity(info.grids.len());
273        let mut slot_chunk_idx_shadow: Vec<Vec<[i32; 4]>> = Vec::with_capacity(info.grids.len());
274
275        for grid in &info.grids {
276            let vsid = grid.vsid;
277            // GPU.11 — per-slot strides span the whole mip ladder.
278            let layout = MipLayout::for_vsid(vsid);
279            let occ_words_per_slot = layout.occ_words_per_slot as usize;
280            let offsets_words_per_slot = layout.offsets_words_per_slot as usize;
281            let colors_stride = COLORS_PER_CHUNK_WORDS as usize;
282
283            // Validate pool_dims are powers of 2 — required for the
284            // shader's `chunk_idx & (pool_dims - 1)` modular slot
285            // indexing.
286            assert!(
287                grid.pool_dims[0].is_power_of_two()
288                    && grid.pool_dims[1].is_power_of_two()
289                    && grid.pool_dims[2].is_power_of_two(),
290                "scene grid: pool_dims {:?} must all be powers of 2",
291                grid.pool_dims,
292            );
293            let pool_x = grid.pool_dims[0] as usize;
294            let pool_y = grid.pool_dims[1] as usize;
295            let pool_z = grid.pool_dims[2] as usize;
296            let total_slots = pool_x * pool_y * pool_z;
297
298            let mut grid_occupancy = vec![0u32; total_slots * occ_words_per_slot];
299            let mut grid_color_offsets = vec![0u32; total_slots * offsets_words_per_slot];
300            let mut grid_colors = vec![0u32; total_slots * colors_stride];
301            let mut grid_chunk_colors_base = vec![0u32; total_slots];
302            for i in 0..total_slots {
303                grid_chunk_colors_base[i] = (i * colors_stride) as u32;
304            }
305            let mut grid_chunk_occupancy = vec![0u32; total_slots.div_ceil(32)];
306            // slot_chunk_idx: vec3<i32> per slot, std430 stride = 16
307            // bytes (4 u32 words: x, y, z, _pad). Initialise every
308            // slot to the empty sentinel; populated slots overwrite
309            // with the actual chunk_idx below.
310            let mut grid_slot_chunk_idx: Vec<[i32; 4]> = Vec::with_capacity(total_slots);
311            for _ in 0..total_slots {
312                grid_slot_chunk_idx.push([
313                    SLOT_EMPTY_SENTINEL[0],
314                    SLOT_EMPTY_SENTINEL[1],
315                    SLOT_EMPTY_SENTINEL[2],
316                    0,
317                ]);
318            }
319
320            let mask_x = (grid.pool_dims[0] - 1) as i32;
321            let mask_y = (grid.pool_dims[1] - 1) as i32;
322            let mask_z = (grid.pool_dims[2] - 1) as i32;
323            let chunks_per_layer = pool_x * pool_y;
324
325            for (chunk_idx, chunk) in &grid.chunks {
326                assert_eq!(chunk.vsid, vsid, "scene grid: chunk vsid mismatch");
327                let sx = (chunk_idx[0] & mask_x) as usize;
328                let sy = (chunk_idx[1] & mask_y) as usize;
329                let sz = (chunk_idx[2] & mask_z) as usize;
330                let slot_idx = sx + sy * pool_x + sz * chunks_per_layer;
331
332                // GPU.11 — write each mip at its within-slot offset.
333                // occupancy + color_offsets land in per-mip sub-blocks
334                // (mip-0 first, so its data is byte-identical to the
335                // pre-mip layout); colours of every mip concatenate
336                // into the slot's fixed COLORS_PER_CHUNK_WORDS block in
337                // level order, indexed by each chunk's own absolute
338                // `color_offsets`.
339                let occ_start = slot_idx * occ_words_per_slot;
340                let off_start = slot_idx * offsets_words_per_slot;
341                let col_start = slot_idx * colors_stride;
342                let mut color_cursor = 0usize;
343                for (m, mip) in chunk.mips.iter().enumerate() {
344                    let occ_dst = occ_start + layout.mip_occ_rel[m] as usize;
345                    grid_occupancy[occ_dst..occ_dst + mip.occupancy.len()]
346                        .copy_from_slice(&mip.occupancy);
347                    // Solid bitmap immediately follows the textured one.
348                    let solid_dst = occ_dst + mip.occupancy.len();
349                    grid_occupancy[solid_dst..solid_dst + mip.solid_occupancy.len()]
350                        .copy_from_slice(&mip.solid_occupancy);
351                    let coff_dst = off_start + layout.mip_coff_rel[m] as usize;
352                    grid_color_offsets[coff_dst..coff_dst + mip.color_offsets.len()]
353                        .copy_from_slice(&mip.color_offsets);
354
355                    let remaining = colors_stride.saturating_sub(color_cursor);
356                    let n = mip.colors.len().min(remaining);
357                    if n < mip.colors.len() {
358                        eprintln!(
359                            "roxlap-gpu SceneUpload: scene grid chunk {chunk_idx:?} mip {m} \
360                             colours overflow COLORS_PER_CHUNK_WORDS ({colors_stride}); \
361                             truncating",
362                        );
363                    }
364                    grid_colors[col_start + color_cursor..col_start + color_cursor + n]
365                        .copy_from_slice(&mip.colors[..n]);
366                    color_cursor += n;
367                }
368
369                if !chunk.mips[0].colors.is_empty() {
370                    grid_chunk_occupancy[slot_idx >> 5] |= 1u32 << (slot_idx & 31);
371                }
372                grid_slot_chunk_idx[slot_idx] = [chunk_idx[0], chunk_idx[1], chunk_idx[2], 0];
373            }
374
375            // Slot_chunk_idx storage offset: each entry is 4 u32
376            // words (vec3 padded to 16 bytes in std430).
377            let slot_chunk_idx_offset = u32::try_from(all_slot_chunk_idx.len()).expect("fits");
378            let meta = GridStaticMeta {
379                occupancy_offset: u32::try_from(all_occupancy.len()).expect("fits"),
380                color_offsets_offset: u32::try_from(all_color_offsets.len()).expect("fits"),
381                colors_offset: u32::try_from(all_colors.len()).expect("fits"),
382                chunk_colors_base_offset: u32::try_from(all_chunk_colors_base.len()).expect("fits"),
383                chunk_occupancy_offset: u32::try_from(all_chunk_occupancy.len()).expect("fits"),
384                slot_chunk_idx_offset,
385                vsid,
386                total_slots: total_slots as u32,
387                pool_dims: grid.pool_dims,
388                _pad0: 0,
389                occ_words_per_slot: layout.occ_words_per_slot,
390                offsets_words_per_slot: layout.offsets_words_per_slot,
391                mip_count: layout.mip_count,
392                _pad1: 0,
393                mip_occ_rel: layout.mip_occ_rel,
394                mip_coff_rel: layout.mip_coff_rel,
395            };
396
397            chunk_occupancy_shadow.push(grid_chunk_occupancy.clone());
398            slot_chunk_idx_shadow.push(grid_slot_chunk_idx.clone());
399
400            all_occupancy.extend_from_slice(&grid_occupancy);
401            all_color_offsets.extend_from_slice(&grid_color_offsets);
402            all_colors.extend_from_slice(&grid_colors);
403            all_chunk_colors_base.extend_from_slice(&grid_chunk_colors_base);
404            all_chunk_occupancy.extend_from_slice(&grid_chunk_occupancy);
405            for entry in &grid_slot_chunk_idx {
406                all_slot_chunk_idx.extend_from_slice(entry);
407            }
408            static_meta.push(meta);
409        }
410
411        // Pad an empty scene's storage buffers — wgpu rejects
412        // zero-size storage bindings.
413        if all_occupancy.is_empty() {
414            all_occupancy.push(0);
415        }
416        if all_color_offsets.is_empty() {
417            all_color_offsets.push(0);
418        }
419        if all_colors.is_empty() {
420            all_colors.push(0);
421        }
422        if all_chunk_colors_base.is_empty() {
423            all_chunk_colors_base.push(0);
424        }
425        if all_chunk_occupancy.is_empty() {
426            all_chunk_occupancy.push(0);
427        }
428        if all_slot_chunk_idx.is_empty() {
429            // 4 zeros = single padded vec3<i32>. wgpu rejects
430            // zero-sized storage bindings.
431            all_slot_chunk_idx.extend_from_slice(&[0; 4]);
432        }
433        if static_meta.is_empty() {
434            static_meta.push(GridStaticMeta::zeroed());
435        }
436
437        let occupancy_bytes = (all_occupancy.len() * 4) as u64;
438        let color_offsets_bytes = (all_color_offsets.len() * 4) as u64;
439        let colors_bytes = (all_colors.len() * 4) as u64;
440        let chunk_colors_base_bytes = (all_chunk_colors_base.len() * 4) as u64;
441        let chunk_occupancy_bytes = (all_chunk_occupancy.len() * 4) as u64;
442        let slot_chunk_idx_bytes = (all_slot_chunk_idx.len() * 4) as u64;
443        let static_meta_bytes = (static_meta.len() * std::mem::size_of::<GridStaticMeta>()) as u64;
444        let total_bytes = occupancy_bytes
445            + color_offsets_bytes
446            + colors_bytes
447            + chunk_colors_base_bytes
448            + chunk_occupancy_bytes
449            + slot_chunk_idx_bytes
450            + static_meta_bytes;
451
452        // Split the concatenated occupancy across storage pages so no
453        // single binding exceeds the device limit. Page size is a
454        // whole number of chunk slots (slot-aligned) so no per-slot
455        // refresh write ever straddles two pages.
456        // GPU.11 — page alignment is now the whole-ladder per-slot
457        // occupancy stride so a slot (all its mips) never straddles a
458        // page boundary.
459        let slot_align_words = info
460            .grids
461            .iter()
462            .map(|g| u64::from(MipLayout::for_vsid(g.vsid).occ_words_per_slot))
463            .max()
464            .unwrap_or(1)
465            .max(1);
466        let (occupancy_pages, occupancy_page_words, occupancy_num_pages) =
467            split_occupancy_pages(device, &all_occupancy, slot_align_words);
468        let all_color_offsets =
469            create_storage(device, "roxlap-gpu scene.color_offsets", &all_color_offsets);
470        let all_colors = create_storage(device, "roxlap-gpu scene.colors", &all_colors);
471        let all_chunk_colors_base = create_storage(
472            device,
473            "roxlap-gpu scene.chunk_colors_base",
474            &all_chunk_colors_base,
475        );
476        let all_chunk_occupancy = create_storage(
477            device,
478            "roxlap-gpu scene.chunk_occupancy",
479            &all_chunk_occupancy,
480        );
481        // GPU.7 slot identity verification buffer. i32 storage.
482        let all_slot_chunk_idx_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
483            label: Some("roxlap-gpu scene.slot_chunk_idx"),
484            contents: bytemuck::cast_slice(&all_slot_chunk_idx),
485            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
486        });
487        let grid_static_meta = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
488            label: Some("roxlap-gpu scene.grid_static_meta"),
489            contents: bytemuck::cast_slice(&static_meta),
490            usage: wgpu::BufferUsages::STORAGE,
491        });
492
493        Self {
494            grid_count,
495            occupancy_pages,
496            occupancy_page_words,
497            occupancy_num_pages,
498            all_color_offsets,
499            all_colors,
500            all_chunk_colors_base,
501            all_chunk_occupancy,
502            all_slot_chunk_idx: all_slot_chunk_idx_buf,
503            grid_static_meta,
504            total_bytes,
505            static_meta,
506            chunk_occupancy_shadow,
507            slot_chunk_idx_shadow,
508        }
509    }
510
511    pub fn resident_bytes(&self) -> u64 {
512        self.total_bytes
513    }
514
515    /// Install or refresh a chunk in its modular pool slot. GPU.7
516    /// generalises GPU.6's in-place refresh: any chunk_idx maps to
517    /// a slot via `chunk_idx & (pool_dims - 1)`. The previous
518    /// occupant (if a different chunk) is silently replaced — the
519    /// host is responsible for guaranteeing that the pool is sized
520    /// large enough that two simultaneously-resident chunks never
521    /// collide on the same slot.
522    pub fn refresh_chunk(
523        &mut self,
524        queue: &wgpu::Queue,
525        scene_idx: usize,
526        chunk_idx: [i32; 3],
527        chunk: &ChunkUpload,
528    ) -> RefreshOutcome {
529        let Some(meta) = self.static_meta.get(scene_idx).copied() else {
530            return RefreshOutcome::SceneIdxOob;
531        };
532        let slot_idx = modular_slot_idx(chunk_idx, meta.pool_dims);
533
534        // GPU.11 — the per-slot strides span the full mip ladder; the
535        // resident's layout was built from the same `MipLayout`.
536        let layout = MipLayout::for_vsid(meta.vsid);
537        let occ_words_per_slot = layout.occ_words_per_slot as usize;
538        let offsets_words_per_slot = layout.offsets_words_per_slot as usize;
539        let colors_stride = COLORS_PER_CHUNK_WORDS as usize;
540
541        assert_eq!(
542            chunk.mips.len() as u32,
543            layout.mip_count,
544            "refresh_chunk: mip count mismatch (chunk {} vs grid {})",
545            chunk.mips.len(),
546            layout.mip_count,
547        );
548
549        // ---- occupancy ----
550        // Route each mip's write to its page. Page size is slot-
551        // aligned (see `split_occupancy_pages`) so the whole slot's
552        // occupancy ladder lands in a single page.
553        let slot_occ_base = meta.occupancy_offset as usize + slot_idx * occ_words_per_slot;
554        let page_words = self.occupancy_page_words as usize;
555        let page = slot_occ_base / page_words;
556        let slot_local_word = slot_occ_base % page_words;
557        debug_assert!(
558            slot_local_word + occ_words_per_slot <= page_words,
559            "occupancy slot straddles a page boundary — page size not slot-aligned",
560        );
561        let off_slot_base = meta.color_offsets_offset as usize + slot_idx * offsets_words_per_slot;
562        let col_slot_base = meta.colors_offset as usize + slot_idx * colors_stride;
563
564        let mut outcome = RefreshOutcome::Ok;
565        let mut color_cursor = 0usize;
566        for (m, mip) in chunk.mips.iter().enumerate() {
567            // occupancy (textured) then solid, back-to-back.
568            let local = slot_local_word + layout.mip_occ_rel[m] as usize;
569            queue.write_buffer(
570                &self.occupancy_pages[page],
571                (local * 4) as u64,
572                bytemuck::cast_slice(&mip.occupancy),
573            );
574            queue.write_buffer(
575                &self.occupancy_pages[page],
576                ((local + mip.occupancy.len()) * 4) as u64,
577                bytemuck::cast_slice(&mip.solid_occupancy),
578            );
579            // color_offsets
580            let coff = off_slot_base + layout.mip_coff_rel[m] as usize;
581            queue.write_buffer(
582                &self.all_color_offsets,
583                (coff * 4) as u64,
584                bytemuck::cast_slice(&mip.color_offsets),
585            );
586            // colours (concatenated per slot, truncate to stride)
587            let remaining = colors_stride.saturating_sub(color_cursor);
588            let n = mip.colors.len().min(remaining);
589            if n < mip.colors.len() {
590                eprintln!(
591                    "roxlap-gpu refresh_chunk: scene_idx={scene_idx} chunk_idx={chunk_idx:?} \
592                     mip {m} colours overflow stride {colors_stride}; truncating",
593                );
594                outcome = RefreshOutcome::ColorsTruncated;
595            }
596            if n > 0 {
597                queue.write_buffer(
598                    &self.all_colors,
599                    ((col_slot_base + color_cursor) * 4) as u64,
600                    bytemuck::cast_slice(&mip.colors[..n]),
601                );
602            }
603            color_cursor += n;
604        }
605
606        // ---- chunk_occupancy bit ----
607        self.set_chunk_occupancy_bit(
608            queue,
609            scene_idx,
610            &meta,
611            slot_idx,
612            !chunk.mips[0].colors.is_empty(),
613        );
614
615        // ---- slot_chunk_idx (identity for the shader) ----
616        self.set_slot_chunk_idx(queue, scene_idx, &meta, slot_idx, chunk_idx);
617
618        outcome
619    }
620
621    /// Evict a chunk's slot — clear its `chunk_occupancy` bit and
622    /// reset `slot_chunk_idx` to the empty sentinel. Used by the
623    /// host when a chunk disappears from the CPU-side `Grid::chunks`
624    /// (e.g. streaming eviction past `r_evict`).
625    ///
626    /// Returns `false` if `scene_idx` is past `grid_count` (no-op);
627    /// `true` otherwise.
628    pub fn evict_chunk(
629        &mut self,
630        queue: &wgpu::Queue,
631        scene_idx: usize,
632        chunk_idx: [i32; 3],
633    ) -> bool {
634        let Some(meta) = self.static_meta.get(scene_idx).copied() else {
635            return false;
636        };
637        let slot_idx = modular_slot_idx(chunk_idx, meta.pool_dims);
638        // Only evict if this slot still claims to hold `chunk_idx`.
639        // Otherwise we'd be wiping out a different (newer) chunk
640        // that happens to share the slot.
641        let shadow_entry = self.slot_chunk_idx_shadow[scene_idx][slot_idx];
642        if shadow_entry[0] != chunk_idx[0]
643            || shadow_entry[1] != chunk_idx[1]
644            || shadow_entry[2] != chunk_idx[2]
645        {
646            return true;
647        }
648        self.set_chunk_occupancy_bit(queue, scene_idx, &meta, slot_idx, false);
649        self.set_slot_chunk_idx(queue, scene_idx, &meta, slot_idx, SLOT_EMPTY_SENTINEL);
650        true
651    }
652
653    fn set_chunk_occupancy_bit(
654        &mut self,
655        queue: &wgpu::Queue,
656        scene_idx: usize,
657        meta: &GridStaticMeta,
658        slot_idx: usize,
659        new_bit: bool,
660    ) {
661        let word_idx = slot_idx >> 5;
662        let bit = slot_idx & 31;
663        let shadow = &mut self.chunk_occupancy_shadow[scene_idx][word_idx];
664        let was_bit = (*shadow >> bit) & 1 == 1;
665        if new_bit == was_bit {
666            return;
667        }
668        if new_bit {
669            *shadow |= 1u32 << bit;
670        } else {
671            *shadow &= !(1u32 << bit);
672        }
673        let global_word_idx = meta.chunk_occupancy_offset as usize + word_idx;
674        queue.write_buffer(
675            &self.all_chunk_occupancy,
676            (global_word_idx * 4) as u64,
677            bytemuck::bytes_of(shadow),
678        );
679    }
680
681    fn set_slot_chunk_idx(
682        &mut self,
683        queue: &wgpu::Queue,
684        scene_idx: usize,
685        meta: &GridStaticMeta,
686        slot_idx: usize,
687        chunk_idx: [i32; 3],
688    ) {
689        let entry = [chunk_idx[0], chunk_idx[1], chunk_idx[2], 0];
690        self.slot_chunk_idx_shadow[scene_idx][slot_idx] = entry;
691        let global_word_idx = meta.slot_chunk_idx_offset as usize + slot_idx * 4;
692        queue.write_buffer(
693            &self.all_slot_chunk_idx,
694            (global_word_idx * 4) as u64,
695            bytemuck::cast_slice(&entry),
696        );
697    }
698}
699
700/// Modular slot index for `chunk_idx` given the grid's
701/// power-of-2 `pool_dims`. Negative `chunk_idx` components map via
702/// two's-complement bitwise AND, matching the shader's
703/// `chunk_idx & (pool_dims - 1)`.
704#[must_use]
705pub fn modular_slot_idx(chunk_idx: [i32; 3], pool_dims: [u32; 3]) -> usize {
706    let mask_x = (pool_dims[0] - 1) as i32;
707    let mask_y = (pool_dims[1] - 1) as i32;
708    let mask_z = (pool_dims[2] - 1) as i32;
709    let sx = (chunk_idx[0] & mask_x) as usize;
710    let sy = (chunk_idx[1] & mask_y) as usize;
711    let sz = (chunk_idx[2] & mask_z) as usize;
712    sx + sy * (pool_dims[0] as usize) + sz * (pool_dims[0] as usize) * (pool_dims[1] as usize)
713}
714
715/// Outcome of `GpuSceneResident::refresh_chunk`. Most callers
716/// can ignore the result; `ColorsTruncated` indicates the chunk's
717/// colour data overflowed the per-slot stride and was clipped.
718#[derive(Debug, Clone, Copy, PartialEq, Eq)]
719pub enum RefreshOutcome {
720    Ok,
721    /// The chunk's colour count exceeded `COLORS_PER_CHUNK_WORDS`;
722    /// the GPU sees the first `stride` colours. Bump
723    /// `COLORS_PER_CHUNK_WORDS` for content that hits this.
724    ColorsTruncated,
725    /// Retained for ABI compatibility; the GPU.7 modular pool no
726    /// longer rejects chunks by bbox.
727    ChunkOutOfBbox,
728    /// `scene_idx` is past `grid_count`. Programming error.
729    SceneIdxOob,
730}
731
732fn create_storage(device: &wgpu::Device, label: &str, data: &[u32]) -> wgpu::Buffer {
733    // GPU.6: include COPY_DST so `refresh_chunk` can `queue.write_buffer`
734    // into existing slots without rebuilding the resident.
735    device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
736        label: Some(label),
737        contents: bytemuck::cast_slice(data),
738        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
739    })
740}
741
742/// Split the concatenated occupancy words into up to
743/// [`MAX_OCC_PAGES`] storage buffers, each no larger than the
744/// device's `max_storage_buffer_binding_size`, then pad the page
745/// list with 1-word dummy buffers so the returned vec is always
746/// exactly `MAX_OCC_PAGES` long (one buffer per bind-group entry).
747///
748/// `slot_align_words` is the per-slot occupancy stride: page size is
749/// rounded down to a multiple of it so no chunk slot — and therefore
750/// no per-slot `refresh_chunk` write — straddles a page boundary.
751/// Returns `(pages, page_words, num_pages)`.
752fn split_occupancy_pages(
753    device: &wgpu::Device,
754    words: &[u32],
755    slot_align_words: u64,
756) -> (Vec<wgpu::Buffer>, u32, u32) {
757    let total_words = words.len() as u64;
758    let limit_words = u64::from(device.limits().max_storage_buffer_binding_size) / 4;
759    // Largest slot-aligned page that fits one binding (≥ 1 slot).
760    let page_slots = (limit_words / slot_align_words).max(1);
761    let mut page_words = page_slots.saturating_mul(slot_align_words);
762    // A tiny scene (or the empty-scene 1-word pad) isn't slot-aligned;
763    // cap the page at the data length so we don't allocate emptiness.
764    page_words = page_words.min(total_words.max(1));
765    let num_pages = total_words.div_ceil(page_words);
766    assert!(
767        num_pages as usize <= MAX_OCC_PAGES,
768        "occupancy needs {num_pages} pages (>{MAX_OCC_PAGES}) at this device's \
769         {limit_words}-word binding limit; shrink the streaming pool or raise MAX_OCC_PAGES",
770    );
771
772    let mut pages: Vec<wgpu::Buffer> = Vec::with_capacity(MAX_OCC_PAGES);
773    let page_words_usize = page_words as usize;
774    for p in 0..num_pages as usize {
775        let start = p * page_words_usize;
776        let end = ((p + 1) * page_words_usize).min(words.len());
777        pages.push(create_storage(
778            device,
779            &format!("roxlap-gpu scene.occupancy.page{p}"),
780            &words[start..end],
781        ));
782    }
783    // Dummy 1-word buffers for the unused bindings.
784    while pages.len() < MAX_OCC_PAGES {
785        pages.push(create_storage(
786            device,
787            "roxlap-gpu scene.occupancy.page_dummy",
788            &[0u32],
789        ));
790    }
791    (
792        pages,
793        u32::try_from(page_words).expect("page_words fits u32"),
794        num_pages as u32,
795    )
796}
797
798#[cfg(test)]
799mod tests {
800    use super::*;
801
802    #[test]
803    fn grid_static_meta_matches_wgsl_std430_size() {
804        // scene_dda.wgsl's GridStaticMeta is read as
805        // array<GridStaticMeta>; the std430 array stride must equal
806        // the Rust size_of or wgpu rejects the binding. 12 scalar
807        // words + 2 array<u32,6> = 48 + 64 = 112... padded: see below.
808        // Concretely: 8 u32 (32) + vec3+pad (16) + 4 u32 (16) +
809        // 2*[u32;6] (48) = 112 bytes.
810        assert_eq!(std::mem::size_of::<GridStaticMeta>(), 112);
811        assert_eq!(std::mem::align_of::<GridStaticMeta>(), 4);
812    }
813
814    #[test]
815    fn mip_layout_offsets_accumulate() {
816        // vsid=128 → 6 mips. Relative offsets are cumulative; mip-0
817        // sits at 0 so mip-0 reads are byte-identical to pre-mip.
818        let l = MipLayout::for_vsid(128);
819        assert_eq!(l.mip_count, 6);
820        assert_eq!(l.mip_occ_rel[0], 0);
821        assert_eq!(l.mip_coff_rel[0], 0);
822
823        // Recompute the strides independently and compare. Each mip
824        // stores TWO occupancy bitmaps (textured + solid) back-to-back.
825        let mut occ = 0u32;
826        let mut coff = 0u32;
827        for m in 0..6u32 {
828            assert_eq!(l.mip_occ_rel[m as usize], occ, "occ rel mip {m}");
829            assert_eq!(l.mip_coff_rel[m as usize], coff, "coff rel mip {m}");
830            let v = 128u32 >> m;
831            occ += 2 * v * v * occ_words_per_column_for_mip(m);
832            coff += v * v + 1;
833        }
834        assert_eq!(l.occ_words_per_slot, occ);
835        assert_eq!(l.offsets_words_per_slot, coff);
836
837        // mip-0 occupancy stride is 2 × the historical vsid²·8 (tex +
838        // solid bitmaps).
839        assert_eq!(l.mip_occ_rel[1], 2 * 128 * 128 * 8);
840        // The whole ladder is only ~1/7 larger than mip-0 alone
841        // (geometric 1 + 1/8 + 1/64 + …) — here on the doubled base.
842        assert!(l.occ_words_per_slot < 2 * 128 * 128 * 8 * 5 / 4);
843    }
844}