CodeBlanks Logo Icon CodeBlanks

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.

Practice Calculate draw call savings

Calculate savedCalls gained by batching by subtracting uniqueMaterials from totalObjects, and print it with Console.WriteLine.

Starter code

Player.cs
using System;

class BatchingSim
{
    static void Main()
    {
        int totalObjects = 6;
        int uniqueMaterials = 2;
        // Draw call tasarrufunu hesapla ve sonucu yazdir
        
    }
}

Sample solution

Player.cs
using System;

class BatchingSim
{
    static void Main()
    {
        int totalObjects = 6;
        int uniqueMaterials = 2;
        int savedCalls = totalObjects - uniqueMaterials;
        Console.WriteLine($"Tasarruf: {savedCalls} Draw Call");
    }
}

Expected output

Tasarruf: 4 Draw Call

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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 Correct answer

Explanation: Batching groups objects sharing the same material into a single drawing call, reducing the number of commands the CPU sends to the GPU.

2 Access shared material

Question
// Batching bozulmasin diye sharedMaterial alinir
Material mat = renderer.____;
Answer
// Batching bozulmasin diye sharedMaterial alinir
Material mat = renderer.sharedMaterial;

Explanation: renderer.material instantiates a new material copy and breaks batching; sharedMaterial keeps the shared reference.

3 Batching types

Term Meaning
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

All lessons in Expert