Expand description
The material table: one storage-buffer row per material, and the id an instance carries to select one.
insert(device, material) ──▶ takes a slot ──▶ one write_buffer of one row
└─▶ index(handle) is what a GpuInstance carries
remove(device, handle) ──▶ frees the slot ──▶ the row is cleared to blackTopic 03 §3.2: “Material table (SSBO) …
Material id indexes the table”. crcbl_shaders::mesh::GpuInstance::material
is that id and crcbl_shaders::mesh::GpuMaterial is that row — the layout
lives in the crate that owns mesh.slang, because it is a contract with the
shader.
§The factors and one texture, which is an ArrayPages layer
§3.2’s sentence is “the table holds texture indices + factors”, and a row
now holds both: GpuMaterial::base_color and
GpuMaterial::base_color_texture. The second is a layer of the one
Texture2DArray mesh.slang binds — an ArrayPages page — and not a
BindingModel::Bindless descriptor slot, which is the binding-model
decision this table used to be waiting on. A layer index needs nothing of a
device; a descriptor array needs DESCRIPTOR_INDEXING, which crcbl-mtl
withdraws. So one column serves every backend, and this table does not have
to know which model the device it is bound on selected.
This type does not own the page. It stores an index and nothing else:
which image the layers live in, how many there are and which page kind they
belong to are crate::forward’s, exactly as which mesh a GpuInstance::mesh
names is crate::mesh_pool’s. So a row naming a layer the page does not
have samples out of range and nothing here can tell — which is why the
type that does own both refuses it: ForwardRenderer::with_scene checks
every SceneDesc::materials row against the page’s layer count before it
creates anything, and names the row and the layer when it declines. A row
reaching MaterialTable::insert has already been through that.
Still absent, and still deliberately: every other texture slot a material
could have — normal, metallic-roughness, emissive. docs/plan/37-materials.md
owns the shape a real material takes. The page’s mip chain is crate::mip’s
and goes up with the page, and the sampler reads it trilinear.
§One buffer, and no ring — unlike crate::instance_pool
This is the decision worth reading before changing anything here, because the two tables look alike and are not.
InstancePool keeps one buffer per
frame in flight and a set of dirty runs per buffer, because the instance
array is rewritten every frame while the previous frame is still drawing
from it: a moving object writes its transform each frame, so the coalesced
delta upload is the whole reason that type exists rather than a Vec the
renderer re-uploads.
A material is written when it is created. That is crate::mesh_pool’s
mesh table exactly — “one host-visible buffer rather than a ring … nothing
rewrites an entry between frames, so there is no frame still reading the
value a write would replace” — and this follows it: one buffer, shared by
every frame’s bind group, written in place. Copying the ring and the dirty
runs across would be a delta upload whose deltas are always one row, plus
frames_in_flight copies of a table nothing varies per frame; and factoring
the run-coalescing out to share it would be an abstraction with one real
user and one that only wants write_buffer.
The cost is stated rather than hidden: rewriting a material while a frame
is in flight is a read-after-write hazard across submissions, the one
stage 2 called its headline risk. So
MaterialTable::set is a start-up call, like crate::mesh_pool::MeshPool::upload
and crate::texture::upload_texture beside it. An animated material is
what makes this a ring, and on that day the coalescing in
crate::instance_pool is worth sharing because there would be two callers
that need it.
§The table never grows
Capacity is fixed by MaterialTableDesc and MaterialTable::insert
fails by name when it runs out — crate::instance_pool’s reason, since
every row is the same size: growing a device buffer means allocating a
larger one, copying, and re-pointing every bind group that names the old
one.
§A cleared row is black, which is the point
MaterialTable::new clears the buffer and MaterialTable::remove
clears the row it frees, so a row nobody has written and a row whose
material was removed read the same: a base colour of zero, which shades
black.
There is no “no material” value to write instead. A mesh table has one —
index_count == 0 is an entry naming no mesh — because a mesh id resolves
to a range, and the empty range draws nothing. Every RGBA factor is a
material somebody could want, so the empty value and a legitimate one are
the same bytes. Black is therefore chosen for how it fails: an instance
whose material was never created is a black object, which is seen, where a
white one is an object that looks correct.
Structs§
- Material
Table - One
GpuMaterialarray, written in place. - Material
Table Desc - How large a
MaterialTableis, and what to call it.
Enums§
- Material
- Marker type for
MaterialHandle. Uninhabited. - Material
Table Error - What a
MaterialTablerefuses to do.
Type Aliases§
- Material
Handle - A material in a
MaterialTable. Generational, so a removed material’s handle stops resolving rather than naming whatever took its row.