Skip to content

Batching

EntropyPortal uses GPU Instancing to reduce driver overhead.

The InstanceBatcher is a high-performance system for grouping compatible draw calls.

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.

Instances are grouped into a batch if they share:

  1. Shader Type (Pipeline compatibility).
  2. Mesh (Vertex buffer compatibility).
  3. Material (Uniform data compatibility).

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]