9.8 KiB
AGENTS.md
Guidance for agents working in this repository. Read this first, then the
relevant document under 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.rsandsrc/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 (FixedStructureTable, the freelist-backedDynamicStructureTable, and the sharedStructureTableCore) and the toroidal world wrapper.
Temporary code:
src/main.rsandsrc/producers.rsare 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 issrc/voxel_cache/*andshaders/.
Build and run
Requirements: a nightly Rust toolchain (edition 2024, generic_const_exprs,
float_algebraic) and slangc if you modify Slang shaders.
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 |
Vision, principles, module map, frame lifecycle, data flow, memory envelope, architecture-level issues. |
docs/voxel-cache.md |
Page/pointer layouts, attributes, public API, request/insertion pipelines, eviction, structure tables, bind groups. |
docs/shaders.md |
Slang vs WGSL split, build/embedding, tracing algorithm, LOD, shader contracts, shader-level issues. |
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.
- Pointer bits: bit 31 = subdividable, bit 30 = available, bits 0..29 =
page index.
child_pointer_availablerequires both high bits. - Child index:
x + 4*y + 16*z(x fastest), 64 children per node. - Page model: one page = one node = 64 pointers + occupancy mask + location
- depth + attributes. No separate brick pool.
- Request/insertion order: request slot
icorresponds to insertion sloti.write_pointersand table insertion rely on this. - Attribute order: Slang hardcodes attribute 0 as packed RGBA color and
attribute 1 as a 3-word normal, with stride 64. The demo's
CacheLayoutorder is load-bearing. Nis overloaded: RustVoxelCache<N>/VoxelCacheInsertion<N>useNas the branching factor; SlangVoxelCacheParameters<N>usesNas the attribute count. Read the surrounding type before assuming.- Depth assumptions:
VoxelTraceStackholds 5 entries and indexesstack[10 - scale_exp/2], assuming branching 4 andchunk_power = 5. Changing either breaks tracing silently. - Generated WGSL constants:
CHILDREN_COUNT,PAGE_COUNT, and attribute metadata are injected at runtime bypipelines.rs. Edit the Rust generator, not a generated copy. - SPIR-V is generated: never edit
shaders/out/*.spv; edit.slangand runmake -C shaders. - 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.
- Identity is not placement.
StructureAnchoris 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.TableSlotis 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, andInsertionParentmust not treat the slot as identity.
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 |
| Add/change slot allocation or table growth | src/voxel_cache/tables.rs (DynamicStructureTable, SlotAllocator, StructureTableCore) |
| Change structure identity / anchors | src/voxel_cache/schema.rs (StructureAnchor, wire structs), src/production/request_queue.rs, src/voxel_cache/tables.rs, src/voxel_cache/shaders/common.wgsl, fixed-table shaders |
| 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 |
Current demo configuration
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 ofLocationPage(≈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 - 1on 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, andVoxelGiPipeline. Mark them as inactive rather than treating them as current behaviour. gi.slangis a stub with legacy bindings that do not match the Rust GI bind group.fixed_structure_table.slangis imported but unused; the active path is the toroidal wrapper around Rust'sFixedStructureTable.
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
moddeclarations insrc/main.rsfirst. - Keep the playground separate from the engine. New durable logic belongs in
src/voxel_cache/*,src/gpu/*, orshaders/*, not inmain.rs. - Document discrepancies as observations. If code and comments disagree, state what the code does, what the comment claims, and your confidence.
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_exploreanswers 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.