Skip to main content

MaterialTable

Struct MaterialTable 

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

One GpuMaterial array, written in place.

See the module docs for why there is no ring and why a cleared row is black — both are decisions rather than accidents.

Implementations§

Source§

impl MaterialTable

Source

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

Creates the table, cleared.

Cleared rather than left as the driver handed it over, for the reason crate::mesh_pool::MeshPool clears its own table: a row nothing has written is read by any instance naming it, and “a zeroed row is black” is only a contract if it holds for the rows nobody has touched as well as for the ones MaterialTable::remove clears. A driver’s leftovers are not zeroes, and they are not a colour anybody chose either.

§Errors

MaterialTableError::Hal from any seam call. A failure part-way through releases the buffer it had already created.

Source

pub const fn buffer(&self) -> BufferHandle

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

One buffer for every frame’s bind group, unlike InstancePool::buffers — see the module docs.

Source

pub const fn capacity(&self) -> u32

How many materials the table can hold, which is the bound every material id is below.

Source

pub const fn len(&self) -> usize

How many materials are live.

Source

pub const fn is_empty(&self) -> bool

Whether no material is live.

Source

pub fn mode(&self, index: u32) -> u32

Row index’s GpuMaterial::mode — the bits a draw is routed by.

What the caller does with it is write it into an instance: crate::forward shifts it into GpuInstance::flags at MATERIAL_MODE_SHIFT, and draw_gen.slang buckets the instance by its mesh id and that field together. So a scene’s cutout materials cost a fragment stage in the depth passes for their own meshes and for no others.

An instance’s mode is captured when its record is written. Rewriting a row with MaterialTable::set moves this answer and does not re-key the instances already naming the row — see that method’s own note.

Zero — OPAQUE — for a row past the table’s capacity and for a removed one: MaterialTable::remove clears it to GpuMaterial::default, whose flags are zero.

Source

pub fn insert( &mut self, device: &dyn Device, material: &GpuMaterial, ) -> Result<MaterialHandle, MaterialTableError>

Takes a row, writes material into it, and returns its handle.

§Errors

MaterialTableError::TableFull when every row is in use — the table never grows, see the module docs — and MaterialTableError::Hal if the row could not be written. A refused insert takes no row.

Source

pub fn set( &mut self, device: &dyn Device, handle: MaterialHandle, material: &GpuMaterial, ) -> Result<bool, HalError>

Rewrites handle’s row.

Returns whether the handle named a live material; a stale one writes nothing rather than recolouring whatever took its row.

A start-up call, like crate::mesh_pool::MeshPool::upload: the table has no ring, so a row rewritten while a frame is in flight is a read-after-write hazard across submissions. The module docs say what would have to change first.

§A rewritten mode does not re-key the instances that name the row

GpuMaterial::mode decides which draw bucket an instance of this material is scattered into, and that decision is baked into the instance’s own record when the record is written — the draw-argument pass has no binding for this table and cannot get one. So a row that goes from OPAQUE to MASK here leaves every instance already naming it in the opaque bucket, whose depth pass has no fragment stage: the cutout would cast a solid shadow.

Nothing in the tree can reach that state — crcbl_render::ForwardRenderer takes its materials from the scene description at build and exposes no insert or rewrite — which is why this is a documented precondition rather than a guard. The fix, if a caller ever needs one, is a renderer-side re-key over the live instances rather than anything this type can do alone; docs/backlog.md carries it.

§Errors

HalError if the row could not be written.

Source

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

Which row handle is, or None if the handle is stale.

This is the number a GpuInstance::material carries, which is why it is public: an instance has to be able to say which material it is shaded with.

Source

pub fn remove( &mut self, device: &dyn Device, handle: MaterialHandle, ) -> Result<bool, HalError>

Frees handle’s row for reuse, retires the handle, and clears the row to black.

Returns whether the handle named a live material. The clear is what makes an instance still naming the row shade black rather than take the colour of whatever material lands there next — the same defect a stale mesh-table entry produces, and the same answer crate::mesh_pool::MeshPool::free gives it. What neither can defend against is a row reused: a material id is a bare u32 with no generation in it, and only MaterialHandle can tell those apart.

§Errors

HalError if the row could not be cleared. The slot is freed either way: a handle that failed to clear is still retired, and the row holds its old colour until something writes it.

Source

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

Releases the buffer. The device must be idle.

Trait Implementations§

Source§

impl Debug for MaterialTable

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.