Expand description
The instance array: one storage-buffer element per drawable object, written by delta upload — the changed elements only, coalesced into runs.
set(handle, instance) ──▶ mirror[index] rewritten
└─▶ index marked dirty in every frame's range set
begin_frame(device) ──▶ rotate to the next frame's buffer
└─▶ one write_buffer per dirty run, then clear
└─▶ nothing dirty ⇒ nothing written at allTopic 03 §3.2: “GpuInstance array (SSBO):
transform, mesh id, material id, flags. Written by delta upload each frame
(changed instances only — dirty ranges, not full re-upload).” The
emphasis is the whole reason this type exists rather than a Vec re-uploaded
every frame, which is what crate::sprite_pass does and what §3.2 is
written against.
The record itself — and which of its fields are reserved rather than working
— is crcbl_shaders::mesh::GpuInstance, because the layout is a contract
with mesh.slang and belongs in the crate that owns that source.
§Dirty runs, and the case they exist for
The pool holds a sorted list of half-open runs that never touch and never overlap. Marking an index that abuts a run extends it; marking one that closes the gap between two runs merges them; marking one that is already inside a run does nothing. So writing instances 3, 4 and 5 produces one upload of three elements and writing 3 and 900 produces two uploads of one — whatever order the marks arrive in.
Nothing merges across a gap, however small: two runs one element apart stay two. A gap-tolerant merge trades bytes for calls, and choosing where that trade sits needs a real scene to measure — §3.3’s, which is also the first thing that will move more than one instance per frame.
§A ring, because the previous frame is still reading
There is one buffer per frame in flight, for the reason
crate::forward’s uniform ring gives: an instance the CPU rewrites while
the GPU is still drawing last frame is a read-after-write hazard across
submissions, which is stage 2’s headline risk for
that stage.
The consequence is that a write is dirty in every slot’s range set, not
one: each slot is a separate copy of the same logical array and each has to
be caught up before it is drawn from. So one changed instance costs one small
upload per frame for the next frames_in_flight frames, and then nothing.
That is still delta upload — the cost is the size of what changed, not the
size of the array — and InstancePool::begin_frame writes nothing at all
once every slot has caught up.
§Where each instance was last frame
GpuInstance::previous_transform is the pool’s field, not the caller’s,
the way the liveness bit is. InstancePool::set fills it from what the
record already holds, InstancePool::insert fills it from the new
transform — a spawn did not travel from anywhere — and
InstancePool::rotate puts back at rest whatever moved a frame ago and
has not moved again. InstancePool::set_bases is the one write that is
not a move: it rewrites a record’s two base vertices and leaves this
field and the carry-forward enrolment exactly as they stand, because the
per-frame re-point a skinned object goes through is not a caller saying the
object travelled.
That last step is what makes the field mean last frame rather than the last time anyone wrote this. An instance that moves for a while and then stands still is written on the frames it moves and not after; without the carry-forward its record would go on naming the transform it left, and every temporal consumer would go on smearing it along a journey that finished.
It is the frame before the one being assembled that settles, because a
caller writes a frame’s instances and then calls
InstancePool::begin_frame — so the moves of the frame about to be drawn
have already happened by the time the rotate runs, and settling those would
erase each one before it was ever drawn.
The cost is one comparison per element per frame after the last write to it, and one extra upload of that element on the first frame it stands still. Nothing reads the field yet — topic 43 §9 is why it is here before its readers are.
§The pool never grows
Capacity is fixed by InstancePoolDesc and InstancePool::insert fails
by name when it runs out. Same reason crate::mesh_pool gives: growing a
device buffer means allocating a larger one, copying, and re-pointing every
bind group that names the old one, in the middle of a frame.
Unlike the geometry pools there is no fragmentation to report, because every
instance is the same size: a slot that comes back fits whatever asks for it
next. InstancePoolError::PoolFull therefore names one number where
crate::mesh_pool::MeshPoolError::PoolExhausted names three.
§What a removed instance leaves behind, and the one bit that says so
InstancePool::remove frees the slot and retires the handle, and it
rewrites one field of the element: it clears
GpuInstance::LIVE from the record’s flags and marks
the slot dirty, so the next InstancePool::begin_frame carries the
removal to the GPU. Everything else the removed instance held — its
transform, its mesh id — stays exactly as it was.
That is the whole contract, and it is what §3.3’s cull pass needs: the pass
walks the array from element zero, so a freed slot has to be able to say it
is one. cull.slang asks the bit before it reads anything else in the
record, and crate::cull::visible_instances — the CPU oracle — does the
same.
The bit is the pool’s, not the caller’s. InstancePool::insert and
InstancePool::set set it whatever the caller passed, so an instance
cannot arrive dead by omission, and a caller cannot clear it without going
through remove. The other bits of the word are the caller’s and are left
alone.
§Why the record is not zeroed
crate::mesh_pool::MeshPool::free clears its table entry to all zeroes,
and the analogous move here would be to zero the whole instance. It is the
wrong one. What the mesh table is doing is writing the empty value of the
field that answers the question — index_count — and the fields it clears
with it are ones a stale entry would otherwise be misread through; the
parallel here is clearing the liveness bit, which is that same write.
Zeroing the rest would make it worse rather than tidier. A zeroed transform is a degenerate box at the origin and a zeroed mesh id names table entry 0, which is an ordinary resident mesh — so a consumer that skipped the liveness check would see a removed instance as visible whenever the origin is, where leaving the record alone at least leaves it wherever it was. The failure a shortcut produces should not be the one that looks plausible.
Structs§
- Instance
Pool - One
GpuInstancearray, mirrored on the host and uploaded by delta. - Instance
Pool Desc - How large an
InstancePoolis, and what to call it.
Enums§
- Instance
- Marker type for
InstanceHandle. Uninhabited. - Instance
Pool Error - What an
InstancePoolrefuses to do.
Type Aliases§
- Instance
Handle - An instance in an
InstancePool. Generational, so a removed instance’s handle stops resolving rather than naming whatever took its slot.