Under Construction
Unity28 min183 views

Understanding Static Batching and GPU Instancing

Minh Khoa

Minh Khoa

Author

When developing games, one of the biggest "enemies" of performance (Framerate/FPS) isDraw Call (or Batches in Unity). The higher the number of Draw Calls, CPU the more time it takes to issue GPU rendering commands, leading to stuttering, overheating devices, and battery drain.

image.pngTo solve this problem, Unity provides many batching techniques (Batching) to minimize the number of Draw Calls. In this article, we will dissect and compare the two most powerful and common techniques:Static BatchingandGPU Instancing.


1. Introduction: The core problem of Draw Call

Before optimizing, we need to understand why we have to optimize:

  • CPUacts as the "commander", preparing data (Position, Material, Mesh, Shader,...) and issuing draw commands for GPU.
  • Every time you send a command to draw an object, CPU called aDraw Call.
  • GPU is very fast, but the process CPU of preparing and sending data is very slow. If there are 10.000 trees, CPU have to send 10.000 draw commands. CPU will be overloaded (CPU Bound) while GPU is "sitting idle" waiting for commands.

The goal of Batching is:Group a huge number of objects into one large data block so that CPU you only need to issue a single Draw Call.


2. What is Static Batching?

Static Batchingis a technique in which, during the game Build process (or when the Scene initializes), Unity will silently merge (combine) all Meshes ofstationaryobjects that use the same Material into a single gigantic Mesh.

How it works:

  1. You mark the GameObject that do not move asStatic (specifically, check the boxBatching Staticin the top-right corner of the Editor).
  2. When building the game, Unity will iterate through all the objectsStaticthat share this Material.
  3. Unity copies the data of each small Mesh, transforms its (Vertex) coordinates into world coordinates (World Space), then stitches them together into a gigantic Mesh (VBO stored in memory).
  4. When the game runs, CPU it only calls 1 Draw Call to render that "gigantic Mesh".

Advantages:

  • Extremely perfect rendering speed: GPU likes rendering large contiguous Meshes, so this is the most stable-performance method.
  • **Works with all kinds of different Mesh shapes:**No matter whether you have 10 big trees, 5 round rocks, or 20 wells... as long as they share the SAME Material, Unity can batch them all!
  • 100% compatible with all device lines, including very old phones.

Disadvantages:

  • **Consumes RAM and file size (Build Size):**Because Unity creates copies of the meshes to merge them. Instead of 1 shared data for 10.000 rocks, it creates 10.000 independent rock data blocks stacked on top of each other in RAMmemory. Very memory-intensive!
  • **Absolutely cannot move:**Objects affected by Static Batching will have their position locked forever. You cannot Update their movement, rotation, or scaling.

When is it used in practice?

  • Architectural structures (houses, roads, sidewalks).
  • Fixed scenery (large rocks, hills, walls).
  • Furniture (Tables, chairs, wall cabinets cannot be broken or pushed away).

3. GPU What is Instancing?

GPU Instancingwas created to solve the "Costly RAM" flaw of Static Batching and to overcome the strict limitation on the very small vertex count of Dynamic Batching. This technique shifts the entire computational burden from CPU to GPU processing.

How it works:

  1. You set up a Material that allowsEnable GPU Instancing.
  2. Instead of merging Meshes on CPU, CPU it only sendsexactly 1 original copy of the Meshdown to VRAM of GPU.
  3. Then, CPU it sends an additional array of matrices, a very lightweight parallel data list containing information about each copy (Position, Rotation, Scale, and even Color).
  4. GPU will read that original mesh, then automatically replicate (Instancing) and render them on the screen at the provided coordinates with only1 Draw Call.

Advantages:

  • **Maximum savings RAM and memory:**Render 100.000 trees, the load size RAM is still only the size of ONE original tree plus an array of tens of thousands of coordinates (extremely small).
  • **Allows free movement (Dynamic):**The objects can still move, rotate, and scale normally via script (for example: a swarm of bees, a flock of butterflies flying around, a stream of bullets flying).
  • **Easy custom shader (MaterialPropertyBlock):**You can create a forest 10.000 trees, but use a script so that each tree has a completely different fresh green or withered yellow shade while still sharing exactly 1 Draw Call!

Disadvantages:

  • **Must use the SAME 1 original Mesh:**You cannot combine 1 leaf and 1 tree trunk into 1 Instancing pass. All objects must share exactly the same Mesh (Mesh) physical shape, identical.
  • Requires device support GPU relatively modern (however today 99.9% Smartphone/PC all already support it).

When is it used in practice?

  • Rolling forests, repeated patches of fallen leaves (Foliage / Grass).
  • Monster swarms, low-level enemies with identical Mesh appearances.
  • Rain particle systems, Debris, Ballistic bullets, Crowds.
  • Voxel game (worlds created from blocky square particles-logic like Minecraft).

4. Overview Comparison Table

Evaluation CriteriaStatic BatchingGPU Instancing
Nature of operationCombines many Mesh parts together into a single block on CPU.GPU automatically replicates 1 Mesh based on the matrix list CPU provided.
Shape synchronization (Mesh)Can consist of DIFFERENT geometric shapes.Must share the SAME geometric mesh (Mesh) identical.
Material (Material)Must use the same 1 Material.Must use the same Material (can be personalized via secondary properties).
Mobility❌ Cannot move, stuck in place.✅ Free to move (Translate, Scale, Rotate).
Consumption RAM & Build Size🔺 Very High (As many as there are will be copied out as many times as there are).🟢 Very Low (Consumes little memory space).
Compatible platformAiming for 100% of all device types, including very old platforms.Devices that support advanced graphics libraries (Vulkan, Metal, OpenGL ES 3.0+).

5. Hard-earned experience (Pro-Tips) for Unity Devs

Below are some important notes drawn from real projects that you should keep in mind:

  1. **Understand parallelism well:**Although you enable GPU Instancing, if shadow (Cascaded Shadows) covers the tree trunks in the environment, draw calls will still increase depending on each shadow-casting layer. Careful Shadow optimization always goes hand in hand with Batching/Instancing.
  2. **Who gets higher priority?**Unity always has an Object batching priority order:Static Batching > GPU Instancing > Dynamic Batching. If you accidentally check the boxBatching Staticfor a forest of 10,000 trees and also checkEnable GPU Instancingon its Material, Unity will prioritize Static Batching. Immediately RAM of yours will evaporate and throw a direct Exception Crash Memory.
  3. When should both be used at the same time? (But separately)
    • With houses, castles, sidewalks containing many kinds of debris-like mesh shapes: check the boxStatic (Use Static Batching).
    • With natural green areas (Trees, grass patches repeated thousands of times): UncheckStaticand set render Instancing (Use GPU Instancing).
  4. **The Ultimate Technique - Throw it all out GameObject! :**Instead of spawning (Instantiate) 50.000 the GameObjects grass and making the Hierarchy heavy and consuming Transform Update costs, store the grass's Vector3 data as an Array, combine C# Script calls the function directlyGraphics.DrawMeshInstanced (Or a more advanced version isGraphics.DrawMeshInstancedIndirectthrough Compute Shader). This method completely eliminates the overhead of GameObject, bringing FPS the best peak for the Game!