Skip to main content

InstancePool

Struct InstancePool 

Source
pub struct InstancePool { /* private fields */ }
Expand description

One GpuInstance array, mirrored on the host and uploaded by delta.

See the module docs for the coalescing rule, the ring, and what a removal leaves behind — all three are decisions rather than accidents.

Implementations§

Source§

impl InstancePool

Source

pub fn new( device: &dyn Device, desc: &InstancePoolDesc<'_>, ) -> Result<Self, InstancePoolError>

Creates the ring, with every buffer cleared.

Cleared rather than left as the driver handed it over, and for crate::mesh_pool::MeshPool’s reason rather than for tidiness: a slot nothing has written is read by anything that walks the array, and “a zeroed element is a dead instance” is only a contract if it holds for the slots no one has touched as well as for the ones InstancePool::remove clears. A driver’s leftovers are not zeroes, and a leftover with GpuInstance::LIVE set is an instance the cull pass keeps, at a transform nobody chose.

This is why it was not always so: until the flags word had a defined bit, zeroing bought nothing an element written before it is addressed did not already have.

The device buffers are created before the host mirror, so a capacity no device could hold arrives as a HalError naming the buffer rather than as an aborting host allocation of the same size.

§Errors

InstancePoolError::Hal from any seam call. A failure part-way through releases whatever was already created.

§Panics

If frames_in_flight is zero: a ring with no buffers has nothing to bind, and every later index into it would be the real failure.

Source

pub fn buffers(&self) -> &[BufferHandle]

The buffers, one per frame in flight, in slot order.

A caller builds one bind group per slot naming the buffer at that slot, and draws frame n with the group whose slot InstancePool::begin_frame returned.

Source

pub const fn capacity(&self) -> u32

How many instances the pool can hold.

Source

pub const fn len(&self) -> usize

How many instances are live.

Source

pub const fn is_empty(&self) -> bool

Whether no instance is live.

Source

pub const fn revision(&self) -> u64

Array elements anything walking the pool has to cover: one past the highest slot ever handed out.

Not InstancePool::len, which counts live instances and so shrinks when one is removed while the slots above it stay occupied. A cull dispatch sized by len would silently stop testing the instances past the gap, which reads as “those were culled”.

It never shrinks, because a slot handed out once may be handed out again and nothing compacts the array. Every element below it is either a live instance or a dead record — InstancePool::remove leaves one, and InstancePool::new clears the buffers so an untouched slot is one too — which is what makes walking the whole range safe. How many times any element of the array has been written, over the pool’s whole life.

A number to compare with an older reading of itself, not to interpret — two readings that differ say the instances a pass draws are not the ones it drew last time it looked, and two that agree say they are. What wants that is a consumer whose output is a function of the array and can be kept instead of recomputed. ForwardRenderer’s shadow atlas was the one there was, and it now asks take_written instead, which says which elements moved rather than that one did.

It moves on every write, including ones no draw could see: a rewrite with identical bytes (which set deliberately does not filter), a re-point of a skinned object’s bases, and the settling of a previous_transform the frame after a move stopped. Reading it as “the scene changed” is therefore conservative in the safe direction — it says changed more often than a byte comparison would, and never says unchanged when a byte moved.

It counts writes rather than hashing the array because the array is the pool’s own capacity in bytes and this is read once a frame.

Source

pub fn take_written(&mut self, into: &mut Vec<u32>)

Appends every element written since the last call to into, each once and in the order it was first written, and forgets them here.

revision says that the array changed; this says which elements did, on the same terms — every write counts, including a rewrite with identical bytes, a re-point of a skinned object’s bases and the settling of a previous_transform, so an element is left out only when nothing wrote it. What wants that is a consumer that can tell which of its outputs an element could reach: ForwardRenderer’s shadow atlas tests each written element’s bounds, before and after, against each shadow group’s cull, so one moving instance redraws the maps it can be seen in rather than every map.

The log is bounded by capacity, because an element already in it is not added again — a pool nobody drains costs at most one entry per element.

Source

pub fn record(&self, index: u32) -> Option<GpuInstance>

What element index holds, live or not, decoded from the host mirror, or None at or past slot_count.

By element rather than by handle, for a caller that learned the index from take_written: a removed instance’s element still holds its last transform and mesh with GpuInstance::LIVE cleared, which is what says it draws nothing now.

Source

pub const fn slot_count(&self) -> u32

Source

pub fn insert( &mut self, instance: &GpuInstance, ) -> Result<InstanceHandle, InstancePoolError>

Takes a slot, writes instance into it live, and marks it dirty everywhere.

GpuInstance::LIVE is set whatever instance.flags carried — see the module docs on why the bit is the pool’s rather than the caller’s.

§Errors

InstancePoolError::PoolFull when every slot is in use. The pool never grows — see the module docs.

Source

pub fn set(&mut self, handle: InstanceHandle, instance: &GpuInstance) -> bool

Rewrites handle’s instance and marks it dirty everywhere.

Returns whether the handle named a live instance; a stale one writes nothing rather than overwriting whatever took its slot.

GpuInstance::LIVE is set, exactly as InstancePool::insert sets it: a live instance cannot be made dead by rewriting it, only by InstancePool::remove.

A rewrite with the same bytes still marks the slot dirty. Filtering that would trade a comparison per write for an upload that mostly does not happen, and it would make “what got uploaded” depend on the old value — which is exactly the kind of thing a caller cannot reason about. Callers that know a value did not change do not call this.

Source

pub fn set_bases( &mut self, handle: InstanceHandle, base_vertex: u32, previous_base_vertex: u32, ) -> bool

Rewrites handle’s two base vertices and nothing else — the previous transform the record already holds included.

Returns whether the handle named a live instance; a stale one writes nothing, on InstancePool::set’s terms. The record keeps GpuInstance::LIVE, because every path that puts one in the mirror is a path that set the bit.

It exists because a re-point is not a move. ForwardRenderer::point_skinned_instances rewrites every skinned object’s pair of bases once a frame, between the rotate and the upload, and doing that through set made it a transform write: set takes the previous transform from the record it overwrites, and by then that record already carries this frame’s transform, so previous_transform came out equal to transform for every skinned instance on every frame. A character that walked contributed nothing to the motion target from the walk.

It also leaves the element out of the next rotate’s carry-forward, which is the same reason from the other side: a call that runs for every skinned object every frame would keep each of them permanently written-this-frame, nothing would ever be put back at rest, and one move would be reported for ever. What enrols an element is a caller’s own write — insert and set.

Source

pub fn set_flags(&mut self, handle: InstanceHandle, flags: u32) -> bool

Rewrites handle‘s flags word and nothing else, on set_bases’ terms: a flag is not a move, so the transform history is left as it is and the element is not enrolled in the next rotate’s carry-forward.

GpuInstance::LIVE is kept set whatever flags says, because the pool owns that bit. Returns whether the handle named a live instance; a stale one writes nothing.

Source

pub fn set_bounds( &mut self, handle: InstanceHandle, owns: u32, bounds_min: [f32; 3], bounds_max: [f32; 3], ) -> bool

Rewrites handle‘s own box and the GpuInstance::OWNS_BOUNDS bit that says the culls read it, and nothing else, on set_bases’ terms: a box is not a move, so the transform history is left as it is and the element is not enrolled in the next rotate’s carry-forward.

owns is the bit or zero; any other bit in it is ignored. Returns whether the handle named a live instance; a stale one writes nothing.

Source

pub fn clear_flags(&mut self, bits: u32)

Clears bits from the flags of every live instance that carries any of them, and writes only those.

A walk over every slot ever handed out, for a change that is rare by nature — a view being released, whose bit every record has to stop carrying. GpuInstance::LIVE is never cleared here, whatever bits holds.

Source

pub fn get(&self, handle: InstanceHandle) -> Option<GpuInstance>

What handle’s slot currently holds, decoded from the host mirror, or None if the handle is stale.

The record carries GpuInstance::LIVE, because the pool put it there: this is what the buffer holds, not what the caller passed.

Source

pub fn live(&self) -> Vec<GpuInstance>

Every live element of the array, in slot order.

The records the device would draw, decoded from the same mirror the buffers are filled from — including GpuInstance::LIVE, which the pool sets, so a caller filters on the bit rather than on a second list it kept. Slots nothing has written are cleared, and a cleared record has the bit off.

Added for crate::probe_visibility, which needs the scene as it stands rather than as it was described: a probe’s visibility map is about where the walls are, and the walls arrive through InstancePool::insert rather than through the description.

Source

pub fn records(&self) -> Vec<GpuInstance>

Every element the pool has handed out, in slot order, dead ones included — the array slot_count elements long that a cull dispatch walks, decoded from the host mirror.

For crate::cull’s oracle, which indexes it the way the shader does: live drops the dead slots and with them the indices.

Source

pub fn index(&self, handle: InstanceHandle) -> Option<u32>

Which element of the array handle is, or None if the handle is stale.

This is the number a shader indexes with, which is why it is public: a draw has to be able to say which instance it draws.

Source

pub fn remove(&mut self, handle: InstanceHandle) -> bool

Frees handle’s slot for reuse, retires the handle, and clears the element’s GpuInstance::LIVE bit.

Returns whether the handle named a live instance. The rest of the element’s bytes are left as they are — see the module docs on why clearing the bit is the whole of what a removal owes the GPU, and why zeroing the record would be worse than leaving it.

The clear reaches the device the way every other write does, through InstancePool::begin_frame: the slot is marked dirty in every buffer’s range set, so a removal costs the same one small upload per frame in flight that a rewrite does.

Source

pub fn begin_frame(&mut self, device: &dyn Device) -> Result<usize, HalError>

Rotates to the next frame’s buffer and writes that buffer’s outstanding changes into it, returning the slot it rotated to.

A frame in which nothing was written performs no seam call at all. That is §3.2’s claim about this design, and it is the thing to check when wondering whether delta upload is really happening.

§Errors

HalError if a write failed. The runs it had already written stay written and the rest stay dirty, so a later frame retries them — a half-uploaded array that forgot the rest would be the worse failure.

Source

pub fn rotate(&mut self) -> usize

Rotates to the next frame’s buffer without uploading anything, and returns the slot it rotated to.

The half of begin_frame that decides which buffer this frame writes, split out for the one caller that has an instance to write which depends on the slot: ForwardRenderer::begin_skinned_frame points every skinned instance at the half of its region this frame’s dispatch will fill, and the parity it reads comes from a Skinning that has to be handed this slot first. Rotating and uploading in one call would leave that write for the next frame’s upload, which is a character posed one frame late.

flush must follow it, or this frame draws whatever the frame before last left in this buffer.

This is also the frame boundary the previous transform is measured against: an element the frame before last wrote and this one did not is put back at rest here, so an instance that stopped moving stops reporting a move. See GpuInstance::previous_transform and the module docs.

Source

pub fn flush(&mut self, device: &dyn Device) -> Result<(), HalError>

Writes the current slot’s outstanding changes into its buffer.

The other half of begin_frame. Idempotent: a second call with nothing newly written performs no seam call at all.

§Errors

HalError if a write failed, on begin_frame’s terms — the runs already written stay written and the rest stay dirty.

Source

pub fn destroy(self, device: &dyn Device)

Releases every buffer. The device must be idle.

Trait Implementations§

Source§

impl Debug for InstancePool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.