Game Development
Batching
Learn to cut draw calls with static and dynamic batching.
What are draw calls and batching?
Each render command that the CPU sends to the graphics card to draw an object is called a Draw Call. When hundreds of independent objects are rendered, draw calls surge and choke the CPU. Batching is the technique of combining objects sharing the same material into a single draw call.
// If 100 different objects use the same material:
// Without batching: 100 Draw Calls
// With batching: 1 Draw Call
Reducing draw calls significantly alleviates the workload on the processor.
Static and dynamic batching
Static Batching: Merges meshes of objects that never move (marked Static in Inspector) into one large combined mesh. Dynamic Batching dynamically packages vertices of small moving objects on the CPU every frame.
// Use sharedMaterial instead of renderer.material
// Because calling 'material' creates an instance and breaks batching!
Material m = renderer.sharedMaterial;
Marking static objects properly and avoiding material instancing improves batching efficiency.
Exercise Calculate draw call savings
Calculate savedCalls gained by batching by subtracting uniqueMaterials from totalObjects, and print it with Console.WriteLine.
Show example solution
using System;
class BatchingSim
{
static void Main()
{
int totalObjects = 6;
int uniqueMaterials = 2;
int savedCalls = totalObjects - uniqueMaterials;
Console.WriteLine($"Tasarruf: {savedCalls} Draw Call");
}
}
Test yourself
-
What is the main goal of the batching technique?
- Minimize game disk size
- Gather all polygons in the scene at a single point
- Compress in-game music and sound files
- Reduce draw call count to alleviate CPU workload
Show answer
Reduce draw call count to alleviate CPU workload — Batching groups objects sharing the same material into a single drawing call, reducing the number of commands the CPU sends to the GPU.
-
Access shared material
// Batching bozulmasin diye sharedMaterial alinir
Material mat = renderer.____;
Show answer
// Batching bozulmasin diye sharedMaterial alinir
Material mat = renderer.sharedMaterial;
-
Batching types
- Draw Call
- Drawing command sent by CPU to GPU
- Static Batching
- Combining meshes of non-moving objects
- Dynamic Batching
- Grouping small moving objects each frame
- GPU Instancing
- Same mesh at many positions in one call
Next lesson: Object pooling