refactor: Removing structureID, keep structure anchor as id

This commit is contained in:
2026-11-12 15:51:05 +01:00
parent a39256a0f1
commit c31f3a9bc3
23 changed files with 1417 additions and 806 deletions
+7 -7
View File
@@ -113,8 +113,8 @@ flowchart LR
Req --> Gather[gather_requests<br/>top-K + radix sort]
TReq --> TGather[table gather_requests]
Gather --> RBuffer[(Request buffer<br/>structure_id, anchor, node_id, child)]
TGather --> TRBuffer[(Table request buffer<br/>structure_id, anchor, slot)]
Gather --> RBuffer[(Request buffer<br/>anchor, node_id, child)]
TGather --> TRBuffer[(Table request buffer<br/>anchor, slot)]
RBuffer --> Host[Host fulfils requests]
TRBuffer --> Host
@@ -198,7 +198,7 @@ Consequences agents should keep in mind:
| `src/gpu/` | Radix sort, top-K, indirect dispatch, compaction helpers | Active infrastructure |
| `src/gpu_profiler/` | Timestamp profiling and memory accounting wrappers | Active infrastructure |
| `src/gi_pipeline.rs` (+ `sparse_set.rs`) | Planned GI pipeline using cache attribute sets | **Compiled but never instantiated** |
| `src/production.rs` | Producer/structure IDs, `StructureAnchor` re-export, `VoxelProducer` trait | Compiled; durable contract |
| `src/production.rs` | Producer ID, `StructureAnchor` re-export, `VoxelProducer` trait | Compiled; durable contract |
| `src/production/host_production.rs` | Rayon-based `HostRequestHandler` | Compiled; `fullfill` still a stub |
| `src/production/tree_locator.rs` | Compact N-tree locator encoding | Compiled; inactive |
| `src/production/request_queue.rs` | Bounded, deduplicating, priority-ordered request queue | Compiled, unit-tested; not yet on the live async path |
@@ -238,10 +238,10 @@ The toroidal table is a dense 3D grid of chunk root pointers. It wraps with
`imod`, so moving the camera across a chunk boundary invalidates the departing
slab and reuses those slots for incoming chunks. Identity is kept separate from
placement: each slot carries a table-defined `StructureAnchor` (world position
plus a version word) and a stable `StructureId`. Anchors that stay in view keep
their id while their slot may change; anchors that leave retire their id, which
is never reused. In-flight root work carries the anchor and the latest slot, and
the GPU rejects an insertion whose anchor/id no longer matches the slot, so queue
plus a version word), and the anchor is the structure identity. Anchors that stay
in view keep their identity while their slot may change; anchors that leave are
retired. In-flight root work carries the anchor and the latest slot, and the GPU
rejects an insertion whose anchor no longer matches the slot, so queue
invalidation is only a cancellation optimisation.
---
+35 -48
View File
@@ -29,8 +29,8 @@ sequenceDiagram
Note over C,T: end of draw
C->>C: top-K + gather + descending radix sort
T->>T: top-K + gather + descending radix sort
C-->>H: request buffer (structure_id, anchor, node_id, child, producer, demand)
T-->>H: table request buffer (structure_id, anchor, slot, 0, producer, demand)
C-->>H: request buffer (anchor, node_id, child, producer, demand)
T-->>H: table request buffer (anchor, slot, 0, producer, demand)
H->>H: queue + produce one node per request (parallel, possibly across frames)
H->>I: upload CacheInsertionData (info, location, parent, attributes, count)
I->>C: validate/compact, recycle pages, write pages, invalidate stale pointers
@@ -51,19 +51,18 @@ response to frame N are first visible in frame N+1.
```rust
struct RequestInformation {
structure_id: u32, // stable tree identity (per producer)
node_id: u32, // locator of the *parent* node
children_index: u32, // which child is missing
producer_id: u32, // owner of the parent node
demand: u32, // request counter for this slot this frame
anchor: StructureAnchor, // opaque, table-defined content identity
table_slot: u32, // external root placement, NO_TABLE_SLOT internally
}
struct RequestMeta {
page_index: u32, // parent page (internal) or 0xFFFFFFFF (external)
children_index: u32,
depth: u32, // parent depth
table_slot: u32, // external root placement, NO_TABLE_SLOT internally
}
```
@@ -71,22 +70,19 @@ struct RequestMeta {
build the insertion's `InsertionParent` without referring back to a request
buffer.
- For external requests from a structure table: `RequestMeta.page_index =
0xFFFFFFFF`, `node_id = 0`, `children_index = 0`, `structure_id` is the stable
tree id, `anchor` is the table-defined content identity, and `table_slot` is
the current placement.
0xFFFFFFFF`, `node_id = 0`, `children_index = 0`, `anchor` is the table-defined
content identity, and `RequestMeta.table_slot` is the current placement.
- The root/child distinction must use `RequestMeta.page_index == 0xFFFFFFFF`,
**not** `node_id == 0`: child 0 of the root also has `node_id = 0`.
- Requests are sorted by demand (highest counter first) and capped at
`max_requests`.
- `producer_id` and `demand` are on the wire so the host queue can deduplicate
and prioritise without guessing.
- `structure_id`, `anchor`, and `table_slot` are three different things:
- `StructureId` is stable tree identity, monotonic per producer, never reused
for another anchor;
- `StructureAnchor` is the table-defined content identity (for the toroidal
table, world position plus a version word);
- `table_slot` is table-private placement and may change while the id and
anchor stay the same. It is deliberately not part of a request key.
- `anchor` and `table_slot` are two different things:
- `StructureAnchor` is the table-defined content identity and the structure
identity (for the toroidal table, world position plus a version word);
- `table_slot` is table-private placement and may change while the anchor
stays the same. It is deliberately not part of a request key.
### 2.2 Response
@@ -99,7 +95,6 @@ struct InsertionInformation<N> {
}
struct LocationPage {
structure_id: u32,
node_id: u32, // locator of the node itself
producer_id: u32,
anchor: StructureAnchor,
@@ -110,7 +105,6 @@ struct InsertionParent {
page_index: u32, // parent page, or 0xFFFFFFFF for an external root
children_index: u32,
depth: u32, // parent depth, or 0xFFFFFFFF
structure_id: u32,
node_id: u32, // parent locator (0 for an external root)
producer_id: u32,
anchor: StructureAnchor,
@@ -123,11 +117,10 @@ Plus one attribute payload per entry (color and normal in the demo).
Before allocating pages, the cache validates every entry against the current
parent:
- internal: the parent page must still have the same structure id, producer,
locator, **and anchor**, and the child slot must still be
`subdividable, unavailable`;
- external: the named table slot must still carry the same anchor and structure
id, and its pointer must still be `subdividable, unavailable`.
- internal: the parent page must still have the same producer, locator, **and
anchor**, and the child slot must still be `subdividable, unavailable`;
- external: the named table slot must still carry the same anchor, and its
pointer must still be `subdividable, unavailable`.
Valid entries are compacted into `valid_indices`/`valid_count`; only those
consume eviction pages. The cache fills `destination_indices[i]` with the page
@@ -199,7 +192,7 @@ The demo's `fullfill_request` (`src/main.rs`) does:
2. Add the playground offset `+5000` to x and z and take `abs()` of y.
3. If `request_meta.page_index == 0xFFFFFFFF` (an external table request),
produce the chunk root at depth 0, emit the root locator, and emit
`InsertionParent::external(structure_id, producer_id, anchor, table_slot)`.
`InsertionParent::external(producer_id, anchor, table_slot)`.
4. Otherwise (an internal child request):
- decode the parent `NTreeNodeLocator<4>` from `node_id`;
- decode `children_index` into `(child_x, child_y, child_z)` with x fastest;
@@ -207,8 +200,7 @@ The demo's `fullfill_request` (`src/main.rs`) does:
- produce the child node at `parent_depth + 1` with the child's node
coordinates;
- emit the child locator as the new `LocationPage.node_id`;
- emit `InsertionParent::internal(meta, structure_id, node_id, producer_id,
anchor)`.
- emit `InsertionParent::internal(meta, node_id, producer_id, anchor)`.
The root/child test must use `RequestMeta.page_index`, not `node_id`: child 0
of the root also has `node_id = 0`.
@@ -340,7 +332,6 @@ Defines a more ambitious interface:
```rust
pub struct ProducerId(pub u32);
pub struct StructureId(pub u32);
pub struct StructureLocator(pub u32);
pub use crate::voxel_cache::schema::StructureAnchor;
@@ -354,7 +345,6 @@ pub trait VoxelProducer<const N: usize> {
fn produce(
&self,
producer_id: ProducerId,
id: StructureId,
anchor: StructureAnchor,
locator: StructureLocator,
) -> (InsertionInformation<N>, [Self::Output; N * N * N]);
@@ -369,22 +359,21 @@ Differences from the demo interface:
- The producer receives a `StructureLocator` rather than depth + coordinates,
pushing locator arithmetic into the producer.
- `anchor` is the content identity. A producer must derive world position from
it (or from the id, which is stable) and never from a table slot.
it and never from a table slot.
### 6.1.1 Identity model
| Concept | Meaning |
|---|---|
| `ProducerId` | Which producer fulfils the request. |
| `StructureId` | Stable identity of one tree instance. Monotonic per producer, never reused for a different anchor. |
| `StructureAnchor` | Opaque, table-defined content identity. Two live slots must never share one anchor. |
| `StructureAnchor` | Opaque, table-defined content identity and the only structure identity. Two live slots must never share one anchor. |
| `TableSlot` | Table-private placement. Recycled freely and never part of tree identity. |
Root identity is `(producer, structure_id, anchor)`; child identity adds the
parent locator and child index. The table slot travels only as attachment
metadata. The toroidal table reuses a `StructureId` for an anchor that stays in
view, allocates fresh ids for anchors entering the window, and retires ids for
anchors that leave. Retired ids are never reassigned.
Root identity is `(producer, anchor)`; child identity adds the parent locator and
child index. The table slot travels only as attachment metadata. The toroidal
table keeps an anchor's identity while it stays in view and retires anchors that
leave. An anchor must keep naming the same content if it later re-enters; bump
its version word when content generation changes for the same coordinates.
### 6.2 `src/production/host_production.rs`
@@ -408,7 +397,6 @@ Key model:
```rust
RequestKey {
producer_id,
structure_id,
anchor, // table-defined content identity
target: Root | Child { parent: StructureLocator, children_index },
}
@@ -430,15 +418,16 @@ Properties:
are dropped and re-emitted next frame.
- Each job has a generation and attempt count. Completions from evicted,
expired, retried, or invalidated jobs are discarded.
- `table_slot` is attachment metadata, not part of the key. It is refreshed on
every re-emission, including while a job is in flight or ready, so a produced
root can follow its anchor to a new slot.
- `invalidate_structure(id)`, `invalidate_producer(id)`, and
`invalidate_anchor(anchor)` remove matching queued/in-flight/ready jobs.
Invalidation is a cancellation/cleanup hint, **not** a correctness mechanism:
the GPU rejects stale insertions by checking the current anchor/id/slot.
- `table_slot` lives in `RequestMeta` as attachment metadata, not part of the
key. Because the whole `RequestMeta` is refreshed on every re-emission,
including while a job is in flight or ready, a produced root can follow its
anchor to a new slot.
- `invalidate_producer(id)` and `invalidate_anchor(anchor)` remove matching
queued/in-flight/ready jobs. Invalidation is a cancellation/cleanup hint,
**not** a correctness mechanism: the GPU rejects stale insertions by checking
the current anchor/slot.
- `ReadyItem::insertion_parent()` builds the `InsertionParent` for the insertion
batch from the key, the latest `RequestMeta`, and the latest `table_slot`.
batch from the key and the latest `RequestMeta` (which carries `table_slot`).
The queue is unit-tested without a GPU (`cargo test production::request_queue`).
@@ -494,10 +483,8 @@ Minimum viable path with the current demo interface:
4. Emit a `LocationPage` matching the produced node. For internal requests,
it must be `parent.child(child_x, child_y, child_z)`.
5. Emit an `InsertionParent`:
- external: `InsertionParent::external(structure_id, producer_id, anchor,
table_slot)`;
- internal: `InsertionParent::internal(meta, structure_id, node_id,
producer_id, anchor)`.
- external: `InsertionParent::external(producer_id, anchor, table_slot)`;
- internal: `InsertionParent::internal(meta, node_id, producer_id, anchor)`.
6. Set `page_count_buffer` to the number of entries. Order does not matter, but
every entry must have matching information, location, parent, and attributes.
7. Update the size passed to `CacheInsertionData::new` if the maximum batch can
@@ -512,7 +499,7 @@ A production-ready producer should additionally:
- handle `destination_indices == 0xFFFFFFFF` by retrying the request;
- implement filtering/mip generation for coarser levels so LOD stops do not
alias;
- support multiple structures/producers per cache via IDs.
- support multiple structures/producers per cache via `ProducerId` and anchors.
---
+4 -5
View File
@@ -407,8 +407,7 @@ group:
- group 0 — table pointers, or request handler;
- group 1 — request buffer, or insertion data;
- group 2 — slot metadata: binding 0 `fixed_table_anchors`,
binding 1 `fixed_table_structure_ids`.
- group 2 — slot metadata: binding 0 `fixed_table_anchors`.
### 9.2 WGSL modules and entry points
@@ -421,9 +420,9 @@ group:
| `shaders/invalidation.wgsl` | `invalidation` | Clear cache pointers that reference pages recycled this insert. |
| `shaders/write_pointers.wgsl` | `write_pointers` | Link internal insertion results into parent pages using the batch's `InsertionParent`. |
| `shaders/eviction.wgsl` | `reset_lru_list`, `indirect_copy` | Build the eviction candidate list: descending depth sort, then gather timestamps for the stable timestamp sort. |
| `fixed_table/write_requests.wgsl` | `write_requests` | Emit root requests for table slots, resolving the slot's current structure id and anchor. |
| `fixed_table/validate_insertion.wgsl` | `validate_external` | Keep only root entries whose named slot still carries the same anchor/id and waits for a page. |
| `fixed_table/insertion.wgsl` | `insertion` | Re-check anchor/id/slot, then point the table slot at the inserted root page. |
| `fixed_table/write_requests.wgsl` | `write_requests` | Emit root requests for table slots, resolving the slot's current anchor. |
| `fixed_table/validate_insertion.wgsl` | `validate_external` | Keep only root entries whose named slot still carries the same anchor and waits for a page. |
| `fixed_table/insertion.wgsl` | `insertion` | Re-check anchor/slot, then point the table slot at the inserted root page. |
| `fixed_table/invalidation.wgsl` | `invalidation` | Clear table slots pointing at recycled pages. |
| `terrain/shaders/toroidal_structure.wgsl` | `invalidate_new` | Reset table slots whose world position changed. |
+67 -34
View File
@@ -68,7 +68,6 @@ For `N = 4`, a pointer page is `8 + 64*4 = 264` bytes.
```rust
struct LocationPage {
structure_id: u32, // stable tree identity
node_id: u32, // application-defined locator within that structure
producer_id: u32, // producer that owns the node
anchor: StructureAnchor, // opaque, table-defined content identity
@@ -77,10 +76,10 @@ struct LocationPage {
The cache never interprets these values; they are copied from insertion data and
returned in requests so the producer knows which node is missing. In the demo,
`node_id` is an `NTreeNodeLocator`, `structure_id` is a stable per-producer tree
id, and `anchor` is the toroidal table's world position plus a version word.
Child requests inherit the anchor from their parent page, so producers never need
a host-side `StructureId -> anchor` directory. `LocationPage` is 28 bytes.
`node_id` is an `NTreeNodeLocator` and `anchor` is the toroidal table's world
position plus a version word. The anchor is the structure identity, so child
requests inherit it from their parent page and producers never need a host-side
identity directory. `LocationPage` is 24 bytes.
### 2.3 Depth page
@@ -271,12 +270,12 @@ pub trait CacheInvalidable<const N: usize> {
```
`validate_insertion` runs before page allocation: the table checks that each
external root's named slot still carries the same `anchor` and `structure_id`
and is still `subdividable, unavailable`, then compacts the valid entries. This
prevents a stale root response from consuming a page, and lets a slot be reused
(toroidal move) without accepting a response for its old world position. The
anchor/id check is what makes host queue invalidation a cancellation hint rather
than a correctness requirement.
external root's named slot still carries the same `anchor` and is still
`subdividable, unavailable`, then compacts the valid entries. This prevents a
stale root response from consuming a page, and lets a slot be reused (toroidal
move) without accepting a response for its old world position. The anchor check
is what makes host queue invalidation a cancellation hint rather than a
correctness requirement.
### 4.3 Insertion data
@@ -310,7 +309,6 @@ struct InsertionParent {
page_index: u32, // parent cache page, or 0xFFFFFFFF for an external root
children_index: u32, // child slot in the parent
depth: u32, // parent depth, or 0xFFFFFFFF for an external root
structure_id: u32, // structure the parent belongs to
node_id: u32, // locator of the parent (0 for an external root)
producer_id: u32, // producer that owns the parent
anchor: StructureAnchor, // content identity of the structure
@@ -319,9 +317,9 @@ struct InsertionParent {
```
For an internal insertion, validation requires the parent page's
`LocationPage` to match `structure_id`, `node_id`, `producer_id`, and `anchor`.
`LocationPage` to match `node_id`, `producer_id`, and `anchor`.
For an external insertion, validation requires the slot indexed by `table_slot`
to currently hold the parent's `structure_id` and `anchor`. `write_pointers.wgsl`
to currently hold the parent's `anchor`. `write_pointers.wgsl`
repeats the internal check before linking, and the table insertion pass repeats
the external check after page allocation.
@@ -386,26 +384,27 @@ flowchart LR
```rust
struct RequestInformation {
structure_id: u32, // from LocationPage (stable tree identity)
node_id: u32, // from LocationPage (the parent node)
children_index: u32, // requested child
producer_id: u32, // from LocationPage
demand: u32, // request counter for this slot this frame
anchor: StructureAnchor, // from LocationPage (content identity)
table_slot: u32, // 0xFFFFFFFF internally; external root placement
}
struct RequestMeta {
page_index: u32, // parent page, or 0xFFFFFFFF if external
children_index: u32,
depth: u32, // parent depth
table_slot: u32, // 0xFFFFFFFF internally; external root placement
}
```
`producer_id` lets a cache mix several producers and lets the host queue route
requests without guessing. `demand` is the raw request counter; the queue uses it
as the primary priority key. `anchor` lets producers stay stateless and lets the
queue key roots by content identity instead of table placement.
queue key roots by content identity instead of table placement. `table_slot`
travels in `RequestMeta` because it is reinsertion/attachment information, not
request identity.
The external-root marker is `RequestMeta.page_index == 0xFFFFFFFF`, **not**
`node_id == 0`. Both an external root and child 0 of the root carry
@@ -431,7 +430,7 @@ The callback is asynchronous by API, but the demo blocks on it with a
For an **internal** request:
- `structure_id`, `node_id`, and `anchor` identify the **parent** node.
- `node_id` and `anchor` identify the **parent** node.
- `children_index` identifies which child is missing.
- The producer must generate that child node and emit a `LocationPage` for the
child (typically `parent_locator.child(x, y, z)`) with the same anchor.
@@ -440,13 +439,12 @@ For an **internal** request:
For an **external** (structure-table) request:
- `structure_id` is the stable tree id and `anchor` is the table-defined content
identity; `table_slot` is where the table currently points.
- `anchor` is the table-defined content identity; `table_slot` is where the
table currently points.
- `node_id` and `children_index` are 0, and `RequestMeta.page_index` is
`0xFFFFFFFF`.
- The producer must generate a chunk root and emit its `LocationPage` plus an
external `InsertionParent` with `structure_id`, `producer_id`, `anchor`, and
`table_slot`.
external `InsertionParent` with `producer_id`, `anchor`, and `table_slot`.
There is no output-order requirement. Each insertion is attached to its parent
by its own `InsertionParent`.
@@ -488,11 +486,10 @@ first, with deeper pages first among equal timestamps. See §7.
Before any page is allocated, each batch is validated:
- **Internal** (`insertion.wgsl`, `validate_internal`): the parent page must
still have the same `structure_id`, `node_id`, `producer_id`, anchor, and
depth, and the requested child pointer must still be `subdividable,
unavailable`.
still have the same `node_id`, `producer_id`, anchor, and depth, and the
requested child pointer must still be `subdividable, unavailable`.
- **External** (`fixed_table/validate_insertion.wgsl`): the named table slot
must still carry the same `structure_id` and `anchor`, and must still be
must still carry the same `anchor`, and must still be
`subdividable, unavailable`.
Valid entries are compacted into `valid_indices` and counted in `valid_count`.
@@ -613,8 +610,8 @@ root page? Structure tables answer that and emit requests when it is missing.
- `raytrace_bind_group`: bindings `[pointers, request_counts]` (group 1 in the
render pipeline).
- `gather_requests`: top-K + sort + `fixed_table/write_requests.wgsl`; emits
`structure_id = slot`, `node_id = 0`, `children_index = 0`,
`producer_id = table producer`, `demand = slot counter`, and
`node_id = 0`, `children_index = 0`, `producer_id = table producer`,
`demand = slot counter`, `anchor = slot anchor`, and
`RequestMeta.page_index = 0xFFFFFFFF`.
- `validate_insertion`: `fixed_table/validate_insertion.wgsl` keeps only slots
that are still `subdividable, unavailable` and compacts their indices.
@@ -638,12 +635,11 @@ The table is dense: allocation is `slot_count * 4` bytes for pointers plus
and runs `terrain/shaders/toroidal_structure.wgsl`: any slot whose world
position changed is reset to `subdividable, unavailable`, so it will be
re-requested.
- Slot identity is preserved across the move: anchors that stay in view keep
their `StructureId` even if they change slot, anchors that enter get fresh
monotonic ids, and anchors that leave retire their id (never reused).
`reassign_slots` returns the retired ids so the host can cancel speculative
queue work; correctness does not depend on it because the GPU validates
`anchor`/`structure_id`/slot before linking a root.
- Slot identity is preserved across the move: the anchor is the structure
identity, so anchors that stay in view keep it even if they change slot, and
anchors that leave are retired. `reassign_slots` returns the retired anchors
so the host can cancel speculative queue work; correctness does not depend on
it because the GPU validates `anchor`/slot before linking a root.
- `position_for_anchor(anchor)` decodes a table anchor back to an absolute
chunk coordinate.
- Everything else delegates to the fixed table.
@@ -652,6 +648,43 @@ The Slang module `toroidal_structure_table.slang` mirrors this indexing and walk
the block of chunks (`ToroidalStructureTableTracer`). `fixed_structure_table.slang`
is imported by `voxel.slang` but unused.
### 8.3 `DynamicStructureTable`
`src/voxel_cache/tables.rs`. A table whose slots are allocated and released at
runtime. It shares the GPU scaffolding with `FixedStructureTable` through the
private `StructureTableCore`; only the host-side slot management differs.
- **Allocator.** A host-side LIFO free list. `allocate(queue, anchor) -> Option<u32>`
pops a slot and installs the anchor; `release(queue, slot) -> Option<StructureAnchor>`
clears the pointer and pushes the slot back. Allocating an already-live anchor
is idempotent and returns its existing slot. `allocate` returns `None` when the
table is full.
- **Stable slot indices.** A slot index never moves for the lifetime of an
allocation, so it is safe to store in a TLAS or any other long-lived handle.
This is why the table uses a free list rather than compaction.
- **GPU liveness.** A free slot has pointer `0` (not subdividable), so the tracer
skips it and insertion/validation reject it. An allocated slot is initialised to
`subdividable, unavailable`, exactly like a fixed-table slot, so the normal
request/insertion path fills it. Released slots keep their stale anchor on the
GPU until reused; the empty pointer is what makes them inert.
- **Stale requests.** `fixed_table/write_requests.wgsl`,
`validate_insertion.wgsl` and `insertion.wgsl` are reused unchanged. They already
re-check the slot anchor and pointer state, so a request produced before a
release/reuse cannot link the wrong root.
- **Growth.** `reserve(device, queue, min_capacity)` grows by doubling and
`grow` doubles directly. Growth copies the pointer and anchor buffers into
larger buffers (zeroing the new tail), rebuilds the size-dependent request
handler, scratch resources and bind groups, and keeps the bind group layouts and
pipelines. It must happen between frames, and any bind group obtained before
growth must be re-fetched. `allocate_or_grow` is a convenience wrapper.
- **Release semantics.** `release` unlinks the root; it does not free cache
pages. Those become unreachable and are reclaimed by normal LRU eviction. The
returned anchor can be passed to the request queue to cancel speculative work.
- **Reuse responsibility.** Slots are reused immediately. The caller must not
reuse a slot while a GPU structure (e.g. a TLAS) still references it. A
deferred-free/retire queue can be layered on top later without changing the
allocator.
---
## 9. Bind groups