Class DrawMaterials
Runtime copies of a shared material, one per draw slot, for the procedural draws (RenderMesh(in RenderParams, Mesh, int, Matrix4x4, Matrix4x4?) and its kin) that need their own numbers on every draw without a UnityEngine.MaterialPropertyBlock.
public sealed class DrawMaterials
- Inheritance
-
DrawMaterials
- Inherited Members
Remarks
Why. A property block takes its draw out of the SRP Batcher: URP then sets up that draw's whole shader state on the CPU (StdRender.ApplyShader in the profiler). A draw of a material of its own stays in the batcher, which uploads only the material's changed constant buffer and binds its textures.
What the shader must do. Every per-draw number lives in the shader's UnityPerMaterial constant buffer
(the same layout in every pass, with every non-texture property of its Properties block): under the batcher a
material's value for a loose uniform is not what the draw reads. Textures and buffers set on a copy bind as they
would from a block.
A copy keeps what was set on it. A slot's copy is made the first time it is asked for, and refreshed from its source when the source changes (another material, another shader, or in the editor an edit to the source material, seen through its dirty count). Between refreshes it keeps every value last set, where a cleared block fell back to the material: set every per-draw value on every draw, and where a value was set only on a condition, set the source's own value otherwise.
One slot per draw in flight. A block was copied when its draw was queued; a material is read when the camera renders. So two draws of one camera that differ need two slots, while one draw per camera may reuse its slot for the next camera (each camera's hook runs after the previous camera has rendered).
Allocates only when a slot is first used or the slot count grows. Main thread only.
Constructors
DrawMaterials(string)
public DrawMaterials(string label)
Parameters
labelstringPut in each copy's name, for the frame debugger.
Properties
Count
The slots made so far (the highest slot asked for, plus one).
public int Count { get; }
Property Value
Methods
Dispose()
Destroys every copy.
public void Dispose()
For(Material, int)
The copy of source for slot, made or refreshed as needed; null when
source is null.
public Material For(Material source, int slot = 0)
Parameters
sourceMaterialslotint
Returns
- Material