Files

185 lines
11 KiB
Markdown
Raw Permalink 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:
- `crates/vxls-voxel-cache/src/lib.rs` and `crates/vxls-voxel-cache/src/*` — cache API and GPU passes.
- `crates/vxls-voxel-cache/src/shaders/*.wgsl` — runtime-specialised cache shaders.
- `crates/vxls-attributes/src/*` — producer-facing attribute traits, payload layouts, and the `Attributes` derive.
- `crates/vxls-gpu/src/*` — radix sort, top-K, indirect dispatch, compaction.
- `shaders/*.slang` — render-side ray marching, compiled ahead of time.
- `crates/vxls-voxel-cache/src/tables.rs` + `crates/vxls-terrain/src/lib.rs` — structure tables
(`FixedStructureTable`, the freelist-backed `DynamicStructureTable`, and the
shared `StructureTableCore`) and the toroidal world wrapper.
> **Temporary code:** `crates/vxls/src/main.rs` and `crates/vxls/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 `crates/vxls-voxel-cache/src/*` 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 -p vxls # window + egui overlay
cargo check --workspace # compile check for every crate
cargo test --workspace # unit tests (GPU tests need an adapter)
```
The repository is a virtual Cargo workspace: the binary lives in `crates/vxls`,
the durable engine is split across `crates/vxls-voxel-cache`, `crates/vxls-attributes`,
`crates/vxls-gpu`, `crates/vxls-profiler`, `crates/vxls-production`, `crates/vxls-bvh`,
and `crates/vxls-terrain`. Use `cargo check -p <crate>` for a single crate.
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:** the caller-owned trace stack (`LocalTraceStack`)
holds 5 entries and indexes `entries[10 - scale_exp/2]`, assuming branching
4 and `chunk_power = 5`. Changing either breaks tracing silently. The stack
is caller-owned; the tracer must not depend on where the front-end stores it.
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.
11. **Identity is not placement.** `StructureAnchor` is the opaque, table-defined
content identity and the *only* structure identity (toroidal table: world
position + version). A table must never expose two live slots with the same
anchor, and an anchor must keep naming the same content if it leaves and
later re-enters; bump the version word when content generation changes for
the same coordinates. `TableSlot` is table-private placement and may change
freely. Root identity is `(ProducerId, StructureAnchor)`; the slot travels
only as attachment metadata (`RequestMeta`, `InsertionParent`). Request keys,
`LocationPage`, `RequestInformation`, `RequestMeta`, and `InsertionParent`
must not treat the slot as identity.
## Where to look for common tasks
| Task | Start at |
|---|---|
| Change page/pointer layout | `crates/vxls-voxel-cache/src/schema.rs`, `crates/vxls-voxel-cache/src/shaders/cache_common.wgsl`, `crates/vxls-voxel-cache/src/shaders/common.wgsl` |
| Add/change a cache attribute | `crates/vxls-attributes/src/attributes.rs`, `crates/vxls-voxel-cache/src/layout.rs`, `crates/vxls-voxel-cache/src/insertion.rs` (`write_attributes`), `crates/vxls-voxel-cache/src/attributes.rs`, `crates/vxls-voxel-cache/src/shaders/insertion.wgsl`, Slang `fetch_*` in `shaders/voxel.slang` |
| Change request prioritisation | `crates/vxls-voxel-cache/src/requests.rs`, `crates/vxls-gpu/src/top_k.rs`, `crates/vxls-gpu/src/radix_sort.rs` |
| Change eviction | `crates/vxls-voxel-cache/src/eviction.rs`, `crates/vxls-voxel-cache/src/shaders/eviction.wgsl`, `crates/vxls-voxel-cache/src/shaders/insertion.wgsl` |
| Change insertion semantics | `crates/vxls-voxel-cache/src/lib.rs` (`insert`), `crates/vxls-voxel-cache/src/pipelines.rs`, `crates/vxls-voxel-cache/src/shaders/insertion.wgsl`, `write_pointers.wgsl` |
| Change world/chunk lookup | `crates/vxls-voxel-cache/src/tables.rs`, `crates/vxls-terrain/src/lib.rs`, `shaders/toroidal_structure_table.slang` |
| Add/change slot allocation or table growth | `crates/vxls-voxel-cache/src/tables.rs` (`DynamicStructureTable`, `SlotAllocator`, `StructureTableCore`) |
| Change structure identity / anchors | `crates/vxls-voxel-cache/src/schema.rs` (`StructureAnchor`, wire structs), `crates/vxls-production/src/request_queue.rs`, `crates/vxls-voxel-cache/src/tables.rs`, `crates/vxls-voxel-cache/src/shaders/common.wgsl`, fixed-table shaders |
| Change ray marching/LOD | `shaders/voxel.slang`, `shaders/voxel_cache.slang`, `shaders/lod_policies.slang` |
| Add a producer | `crates/vxls/src/producers.rs` (`TreeProducer`), `crates/vxls-production/src/producer.rs` (`Producer`/`ProducedNode`), `crates/vxls-production/src/request_queue.rs`, [`docs/pipeline-and-producers.md`](./docs/pipeline-and-producers.md) |
| Add/change a secondary-ray pass | `shaders/gi.slang` (AO), `shaders/voxel_tracing.slang` (`TraceSettings`, `ITraceStack`), `shaders/bvh.slang` (`max_t`), [`docs/shaders.md`](./docs/shaders.md#81-ao-pass-gislang) |
## Current demo configuration
```text
VoxelCache<4> branching 64
150_000 pages
1_024 max requests
attributes: color (1 word), normal (3 words), both External
TreeProducer<4> source: dragon.txt sparse tree (SdfGenerator is an unused fixture)
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:** the terrain/ball fixtures and
`VoxelGiPipeline`. Mark them as inactive rather than treating them as current
behaviour. The production request queue and durable producer types are on the
demo's live path.
- **`gi.slang` is a simple AO pass**, not full GI: a few short hemisphere rays
per pixel, no denoiser. It is a secondary-ray smoke test.
- **`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 (`crates/vxls-profiler/src`).
- **Do not "fix" dead code by assuming it is live.** Check `mod` declarations in
`crates/vxls/src/main.rs` first.
- **Keep the playground separate from the engine.** New durable logic belongs in
`crates/vxls-voxel-cache/src/*`, `crates/vxls-gpu/src/*`, 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 -->