Batching
EntropyPortal uses GPU Instancing to reduce driver overhead.
The Instance Batcher
Section titled “The Instance Batcher”The InstanceBatcher is a high-performance system for grouping compatible draw calls.
Slot-Based Architecture
Section titled “Slot-Based Architecture”To support multithreading without locking, the batcher uses Slots.
- Slot: A dedicated region of memory and staging vectors.
- Assignment: Each parallel render job (e.g., “Shadow Cascade 1”, “Main Camera”) is assigned a unique slot index.
- Independence: Thread A writing to Slot 0 never contends with Thread B writing to Slot 1.
Batching Criteria
Section titled “Batching Criteria”Instances are grouped into a batch if they share:
- Shader Type (Pipeline compatibility).
- Mesh (Vertex buffer compatibility).
- Material (Uniform data compatibility).
Sorting Strategy
Section titled “Sorting Strategy”The batcher sorts instances to minimize state changes and maximize performance. The primary sort key is the Render Queue, followed by Shader, Mesh, and Material.
The final sort key is Depth, which depends on the depthWrite flag of the instance:
- Depth Write Enabled (Opaque): Sorted Front-to-Back.
- Reason: Maximizes Early-Z Rejection. The GPU can discard pixels of distant objects if a closer object has already been drawn.
- Depth Write Disabled (Transparent): Sorted Back-to-Front.
- Reason: Required for correct Alpha Blending. Distant objects must be composited before closer ones.
graph TD
Start[Batcher.Begin] --> Submit["Submit Instances"]
Submit --> End[Batcher.End]
End --> Sort["Sort Pending List"]
Sort --> Pack["Pack to GPU Buffer"]
Sort --> Ranges["Generate Batch Ranges"]
Pack --> Upload["Upload to GPU"]
Ranges --> Draw[DrawIndexedInstanced]