Skip to main content

MeshPool

Struct MeshPool 

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

One vertex pool and one index pool, with a free-list suballocator over each.

See the module docs for the fragmentation, growth and residency policies, all three of which are decisions rather than accidents.

Implementations§

Source§

impl MeshPool

Source

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

Creates both pools, empty.

§Errors

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

Source

pub const fn attribute_base(&self) -> u32

The same boundary as a word index, which is what the shaders take.

FrameUniforms::vertex_pool’s x lane and crcbl_shaders::skinning::Params::attribute_base are both this number: a vertex v’s position is at word 3 v and its attributes at word attribute_base() + 5 v. It is the one thing about the split a shader cannot derive, because the pool’s capacity has never been something a shader was told.

MeshPool::new refuses a capacity above i32::MAX, so the product with POSITION_STRIDE / 4 cannot overflow a u32: three times two billion is short of four.

Source

pub const fn vertex_buffer(&self) -> BufferHandle

The vertex pool. Bound as a storage buffer; the vertex stage indexes it.

Source

pub const fn index_buffer(&self) -> BufferHandle

The index pool. Bound once, at offset zero, for every mesh in it.

Source

pub const fn table_buffer(&self) -> BufferHandle

The mesh table. Bound as a read-only storage buffer; the vertex stage indexes it with an instance’s mesh id.

Source

pub fn table_entries(&self) -> Vec<GpuMesh>

The mesh table as the device holds it: every slot, a resident mesh’s entry where one is and the cleared entry everywhere else.

Built from the residents rather than mirrored, because the one place an entry is written writes exactly a resident’s range — so this is what a readback of table_buffer would decode to, without a buffer the device has to make readable. For crate::cull’s oracle, which indexes it by an instance’s mesh id.

Source

pub const fn mesh_capacity(&self) -> u32

How many meshes the table holds, which is the bound every mesh id is below.

Source

pub fn upload( &mut self, device: &dyn Device, queue: QueueHandle, mesh: &MeshUpload<'_>, ) -> Result<MeshHandle, MeshPoolError>

Suballocates room for a mesh and records a staging copy into it.

The returned mesh is not resident until MeshPool::flush has run, and MeshPool::mesh says so.

§Errors

MeshPoolError::EmptyMesh or MeshPoolError::VertexStrideMismatch for data this pool cannot describe, MeshPoolError::PoolExhausted when either pool has no block large enough, MeshPoolError::MeshTableFull when the table has no entry left, and MeshPoolError::Hal from any seam call. Nothing is left allocated on any failing path: a vertex range taken before the index pool refused is given back.

Source

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

Waits for every submitted upload, making its meshes resident, and releases the staging buffers they used.

§Errors

MeshPoolError::UploadTimedOut if the transfers have not completed within UPLOAD_TIMEOUT, in which case nothing becomes resident and nothing is released — the staging buffers may still be being read. MeshPoolError::Hal if the wait itself failed.

Source

pub fn mesh(&self, handle: MeshHandle) -> Option<MeshRange>

Where handle’s mesh is, or None if there is nothing safe to draw.

None covers both halves of that: a handle this pool never issued or has since freed, and a mesh whose upload has not completed. A caller cannot obtain a range for a mesh the GPU may not have received yet, which is §3.1’s “renderer consumes meshes only after the timeline value passes” expressed as a type rather than as a rule to remember.

Source

pub fn table_index(&self, handle: MeshHandle) -> Option<u32>

Which entry of the mesh table handle is: the number an instance’s GpuInstance::mesh carries.

None on the same terms as MeshPool::mesh, and for the same reason: handing out an id for a mesh whose bytes may not have landed would move §3.1’s residency gate from “the caller cannot get one” to “the caller must remember not to ask”.

Source

pub fn free( &mut self, device: &dyn Device, handle: MeshHandle, ) -> Result<bool, MeshPoolError>

Clears a mesh’s table entry and returns its space to both free lists.

The space is reusable immediately, which is only true because uploads are drained by MeshPool::flush before anything draws — freeing a mesh a submitted-but-unfinished copy still writes into would be a hazard, and P9’s streaming path is where that stops being free.

The entry is cleared before the space goes back, which is why this needs a device at all: an entry that outlived its mesh would resolve an instance’s mesh id to space some other mesh now owns. Cleared, it reads as the empty range — see the module docs, which also name the case clearing cannot cover.

Returns whether the handle named a live mesh; a second free of the same handle answers false rather than corrupting the free list, because the handle’s generation has moved on.

§Errors

MeshPoolError::Hal if the entry could not be cleared, in which case nothing is freed — the mesh is still live and the caller can retry.

Source

pub fn vertex_count(&self, handle: MeshHandle) -> Option<u32>

How many vertices handle’s mesh occupies in the vertex pool, or None on MeshPool::mesh’s terms.

The fourth integer MeshRange deliberately does not carry, because a draw pulls through indices and needs none of it. A crate::skinning region does need it — a skinned run is exactly as long as the bind pose it deforms — and this is the only place the number exists.

Source

pub fn reserve_vertices(&mut self, count: u32) -> Result<u32, MeshPoolError>

Takes count vertices of the pool for a run no upload fills, and returns where it starts.

The same suballocator MeshPool::upload draws from, without the staging copy, the mesh-table entry or the residency gate — because the caller is not uploading a mesh. crate::skinning’s transient skinned region is what this exists for: a run of the pool a compute pass writes and the vertex stage then pulls, whose bytes never come from the host at all.

Two consequences, both of them the point rather than gaps:

  • The space is undefined until something writes it. There is no staging buffer and no copy, so a draw over a reserved run that no dispatch has filled reads whatever the allocation came with.
  • It is disjoint from every other live allocation, which is what makes the skinning kernel’s precondition true rather than merely asserted: the free list never hands out an address twice, so a range reserved here cannot overlap the bind-pose range a mesh occupies.

It comes back through release_vertices rather than free: there is no handle, no table entry and no generation here, so nothing can tell a second release from a first.

§Errors

MeshPoolError::EmptyReservation for a zero-length request, which has no address to hand back, and MeshPoolError::PoolExhausted when no single free block is large enough — see the module docs on why that can happen with the space free in total.

Source

pub fn release_vertices(&mut self, base: u32, count: u32)

Gives a run taken by reserve_vertices back.

base and count must be exactly what that call handed out and asked for. Releasing a run twice, or one that was never reserved, corrupts the free list — a debug build catches the overlap it produces, and a release build hands the same address out twice.

Source

pub fn vertex_space(&self) -> (u32, u32)

Vertices free, and the largest single run of them: the pair that tells fragmentation from a full pool. Indices in MeshPool::index_space.

Source

pub fn index_space(&self) -> (u32, u32)

Indices free, and the largest single run of them.

Source

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

Releases both pools and everything still staged. The device must be idle.

Trait Implementations§

Source§

impl Debug for MeshPool

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.