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
impl MaterialTable
Sourcepub fn new(
device: &dyn Device,
desc: &MaterialTableDesc<'_>,
) -> Result<Self, MaterialTableError>
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.
Sourcepub const fn buffer(&self) -> BufferHandle
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.
Sourcepub const fn capacity(&self) -> u32
pub const fn capacity(&self) -> u32
How many materials the table can hold, which is the bound every material id is below.
Sourcepub fn mode(&self, index: u32) -> u32
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.
Sourcepub fn insert(
&mut self,
device: &dyn Device,
material: &GpuMaterial,
) -> Result<MaterialHandle, MaterialTableError>
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.
Sourcepub fn set(
&mut self,
device: &dyn Device,
handle: MaterialHandle,
material: &GpuMaterial,
) -> Result<bool, HalError>
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.
Sourcepub fn index(&self, handle: MaterialHandle) -> Option<u32>
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.
Sourcepub fn remove(
&mut self,
device: &dyn Device,
handle: MaterialHandle,
) -> Result<bool, HalError>
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.