Files
vxls/AGENTS.md
T

162 lines
8.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AGENTS.md
Guidance for agents working in this repository. Read this first, then the
relevant document under [`docs/`](./docs).
## What this is
`vxls` is a Rust + wgpu voxel renderer built around a GPU-resident sparse voxel
cache (GigaVoxels-style). The tree is 4×4×4 (64 children per node), nodes live
in fixed GPU page pools, and there is no separate brick pool. Rendering emits
requests for missing nodes on the GPU; a producer fulfils them on the CPU and
the cache inserts them.
The durable system is:
- `src/voxel_cache.rs` and `src/voxel_cache/*` — cache API and GPU passes.
- `src/voxel_cache/shaders/*.wgsl` — runtime-specialised cache shaders.
- `src/gpu/*` — radix sort, top-K, indirect dispatch, compaction.
- `shaders/*.slang` — render-side ray marching, compiled ahead of time.
- `src/voxel_cache/tables.rs` + `src/terrain.rs` — structure tables and the
toroidal world wrapper.
> **Temporary code:** `src/main.rs` and `src/producers.rs` are a playground/test
> harness built alongside the cache. They contain hardcoded parameters and
> blocking workarounds. Do not treat them as stable architecture, public API, or
> examples of intended concurrency. The real system is `src/voxel_cache/*` and
> `shaders/`.
## Build and run
Requirements: a **nightly** Rust toolchain (edition 2024, `generic_const_exprs`,
`float_algebraic`) and `slangc` if you modify Slang shaders.
```sh
make -C shaders # rebuild SPIR-V into shaders/out/ (checked in)
cargo run # window + egui overlay
cargo check # compile check
```
SPIR-V files are embedded with `include_spirv_source!` / `include_spirv!` at
Rust compile time, so after rebuilding shaders you must also rebuild Rust.
Debug keys: `F` pause cache management, `T` target, `N` normals, `Q` albedo,
`P` position.
## Documentation map
| Document | Read it for |
|---|---|
| [`docs/architecture.md`](./docs/architecture.md) | Vision, principles, module map, frame lifecycle, data flow, memory envelope, architecture-level issues. |
| [`docs/voxel-cache.md`](./docs/voxel-cache.md) | Page/pointer layouts, attributes, public API, request/insertion pipelines, eviction, structure tables, bind groups. |
| [`docs/shaders.md`](./docs/shaders.md) | Slang vs WGSL split, build/embedding, tracing algorithm, LOD, shader contracts, shader-level issues. |
| [`docs/pipeline-and-producers.md`](./docs/pipeline-and-producers.md) | Request/response contract, current host loop, generator fixtures, unwired production subsystem, adding a producer. |
Suggested read order for a new agent:
`AGENTS.md` → `docs/architecture.md` → `docs/voxel-cache.md` →
`docs/shaders.md` → `docs/pipeline-and-producers.md`.
## Load-bearing conventions
Do not change these casually; they are contracts between Rust, WGSL, and Slang.
1. **Pointer bits:** bit 31 = subdividable, bit 30 = available, bits 0..29 =
page index. `child_pointer_available` requires both high bits.
2. **Child index:** `x + 4*y + 16*z` (x fastest), 64 children per node.
3. **Page model:** one page = one node = 64 pointers + occupancy mask + location
+ depth + attributes. No separate brick pool.
4. **Request/insertion order:** request slot `i` corresponds to insertion slot
`i`. `write_pointers` and table insertion rely on this.
5. **Attribute order:** Slang hardcodes attribute 0 as packed RGBA color and
attribute 1 as a 3-word normal, with stride 64. The demo's `CacheLayout`
order is load-bearing.
6. **`N` is overloaded:** Rust `VoxelCache<N>` / `VoxelCacheInsertion<N>` use `N`
as the branching factor; Slang `VoxelCacheParameters<N>` uses `N` as the
attribute count. Read the surrounding type before assuming.
7. **Depth assumptions:** `VoxelTraceStack` holds 5 entries and indexes
`stack[10 - scale_exp/2]`, assuming branching 4 and `chunk_power = 5`.
Changing either breaks tracing silently.
8. **Generated WGSL constants:** `CHILDREN_COUNT`, `PAGE_COUNT`, and attribute
metadata are injected at runtime by `pipelines.rs`. Edit the Rust generator,
not a generated copy.
9. **SPIR-V is generated:** never edit `shaders/out/*.spv`; edit `.slang` and run
`make -C shaders`.
10. **Eviction order:** timestamp-major (least recently used first), depth-minor
(deepest first). The depth pass runs descending and the timestamp pass runs
stable so equal timestamps keep the deepest-first order.
## Where to look for common tasks
| Task | Start at |
|---|---|
| Change page/pointer layout | `src/voxel_cache/schema.rs`, `src/voxel_cache/shaders/cache_common.wgsl`, `src/voxel_cache/shaders/common.wgsl` |
| Add/change a cache attribute | `src/voxel_cache/layout.rs`, `src/voxel_cache/attributes.rs`, `src/voxel_cache/shaders/insertion.wgsl`, Slang `fetch_*` in `shaders/voxel.slang` |
| Change request prioritisation | `src/voxel_cache/requests.rs`, `src/gpu/top_k.rs`, `src/gpu/radix_sort.rs` |
| Change eviction | `src/voxel_cache/eviction.rs`, `src/voxel_cache/shaders/eviction.wgsl`, `src/voxel_cache/shaders/insertion.wgsl` |
| Change insertion semantics | `src/voxel_cache.rs` (`insert`), `src/voxel_cache/pipelines.rs`, `src/voxel_cache/shaders/insertion.wgsl`, `write_pointers.wgsl` |
| Change world/chunk lookup | `src/voxel_cache/tables.rs`, `src/terrain.rs`, `shaders/toroidal_structure_table.slang` |
| Change ray marching/LOD | `shaders/voxel.slang`, `shaders/voxel_cache.slang`, `shaders/lod_policies.slang` |
| Add a producer | `src/producers.rs` (interface), `src/main.rs` (`fullfill_request`), [`docs/pipeline-and-producers.md`](./docs/pipeline-and-producers.md) |
## Current demo configuration
```text
VoxelCache<4> branching 64
150_000 pages
1_024 max requests
attributes: color (1 word), normal (3 words), both External
SdfGenerator<4>::new(5) chunk_power 5 => 4^5 = 1024 voxels/axis per chunk cell
20 x 20 x 20 toroidal chunk extent
request buffers: 1024 (cache), 128 (structure table)
```
In render space one structure-table cell is a unit cube; the cache subdivides
that cube and the producer decides how many voxels it packs into it.
## Known gaps and traps
The full tables live in the docs. The most important ones:
- **Location pages are over-allocated** at `size_of::<StructurePage<N>>()` per
page instead of `LocationPage` (≈39.6 MB vs ≈1.2 MB at 150k pages).
- **The first insertion is a no-op** because of the timestamp guard
(`0 >= new_timestamp - 1` on the first call).
- **The host producer path busy-waits** on `device.poll`, so "async" request
fulfilment blocks the render thread.
- **Insertion can silently drop pages** (`destination_indices == 0xFFFFFFFF`)
with no signal to the producer.
- **Several modules are not wired in:** `src/production/*`, `src/tree.rs`,
`src/host_production.rs`, and `VoxelGiPipeline`. Mark them as inactive rather
than treating them as current behaviour.
- **`gi.slang` is a stub** with legacy bindings that do not match the Rust GI
bind group.
- **`fixed_structure_table.slang` is imported but unused**; the active path is
the toroidal wrapper around Rust's `FixedStructureTable`.
## Working conventions
- **Ask before guessing** when behaviour or intent is undocumented. This repo
contains experimental code and contradictory comments; prefer evidence from
source over inference.
- **Preserve the Rust/WGSL/Slang contract.** If you change a layout, update all
three sides and the docs.
- **Measure before optimising.** Timestamp queries and the memory profiler are
already available (`src/gpu_profiler`).
- **Do not "fix" dead code by assuming it is live.** Check `mod` declarations in
`src/main.rs` first.
- **Keep the playground separate from the engine.** New durable logic belongs in
`src/voxel_cache/*`, `src/gpu/*`, or `shaders/*`, not in `main.rs`.
- **Document discrepancies as observations.** If code and comments disagree,
state what the code does, what the comment claims, and your confidence.
<!-- CODEGRAPH_START -->
## CodeGraph
In repositories indexed by CodeGraph (a `.codegraph/` directory exists at the repo root), reach for it BEFORE grep/find or reading files when you need to understand or locate code:
- **MCP tool** (when available): `codegraph_explore` answers most code questions in one call — the relevant symbols' verbatim source plus the call paths between them, including dynamic-dispatch hops grep can't follow. Name a file or symbol in the query to read its current line-numbered source. If it's listed but deferred, load it by name via tool search.
- **Shell** (always works): `codegraph explore "<symbol names or question>"` prints the same output.
If there is no `.codegraph/` directory, skip CodeGraph entirely — indexing is the user's decision.
<!-- CODEGRAPH_END -->