Batching
What is Batching
Batching is an important method for optimizing rendering performance. Its core idea is to combine multiple independent draw calls into one or a few draw calls, thereby reducing communication overhead between CPU and GPU. In real-time rendering, a high number of draw calls can cause CPU bottlenecks and lower frame rates. Batching can significantly improve rendering efficiency.
Common Batching Techniques
1. Static Batching
Static batching is suitable for game objects that do not move, rotate, or scale in the scene. The engine merges the meshes of these objects into one large mesh at build time or runtime, then renders it with a single draw call. The advantage is reduced draw calls, but it increases memory usage because the merged mesh takes extra space. It is suitable for many static objects like buildings, props, etc.
2. Dynamic Batching
Dynamic batching targets movable small objects, transforming their vertex data to world space and merging them at runtime. It has limitations: usually each mesh's vertex count must be below a threshold (e.g., 300 vertices in Unity) and the same material must be used. Dynamic batching requires CPU computation and may introduce overhead, but it is effective when vertex counts are low.
3. GPU Instancing
GPU Instancing allows drawing multiple instances of the same mesh with a single draw call, with each instance potentially having different transforms and material properties (via instancing buffers). It is suitable for many identical objects (like vegetation, rocks) and is efficient, but requires objects to use the same mesh and material, and the shader must support instancing.
4. SRP Batcher (Scriptable Render Pipeline Batcher)
In Unity's SRP, the SRP Batcher is an advanced batching mechanism that does not merge meshes but reduces CPU setup overhead by caching material properties. It is suitable for multiple materials using the same shader, especially effective in complex scenes.
Conditions and Limitations of Batching
Batching typically requires the following conditions:
- **Same material**: Must use the same material instance, otherwise cannot batch.
- **Same shader**: At least the shader must be the same; material properties can differ if supported.
- **Consistent vertex format**: Vertex data layout must be consistent.
- **No transparent sorting issues**: Transparent objects are often difficult to batch because they require strict rendering order.
Additionally, dynamic batching has vertex count limits, static batching increases memory, and GPU Instancing requires shader support.
How to Optimize for Batching
- **Use texture atlases**: Combine multiple small textures into one large texture so different objects can use the same material.
- **Reduce material variety**: Merge materials that use the same shader and textures.
- **Utilize engine batching tools**: For example, Unity's Frame Debugger can inspect which objects are not batched and why.
- **Organize scenes properly**: Mark static objects as Static to enable static batching.
- **Use GPU Instancing**: For repeated objects, use instanced rendering.
Relationship between Batching and Draw Calls
Batching directly reduces the number of draw calls, thereby lowering CPU overhead in submitting rendering commands. However, batching is not a silver bullet; excessive batching may increase memory or cause GPU load imbalance. Choose the appropriate batching strategy based on project needs.
Summary
Batching is a key technique for optimizing CPU performance. Understanding the principles and limitations of various batching methods helps efficiently reduce draw calls and improve frame rates in projects.
FAQ
What is the difference between static and dynamic batching?
Static batching merges meshes before runtime, suitable for non-moving objects but increases memory; dynamic batching merges vertices at runtime, suitable for small objects but has vertex count limits and consumes CPU.
What is the difference between GPU Instancing and batching?
GPU Instancing is a form of batching that uses one draw call to draw multiple instances without merging meshes; it distinguishes instances via instancing data. It suits many identical objects.
Why are transparent objects hard to batch?
Transparent objects require back-to-front sorting for correct blending. After sorting, adjacent objects may not meet batching conditions (e.g., different materials), and forced batching would break render order.
How to check which objects are not batched in Unity?
Use the Frame Debugger to see the source of each draw call and the reason for not being batched. The Profiler also shows draw call count and batching statistics.
Does batching always improve performance?
Reducing draw calls usually lowers CPU overhead, but batching can have side effects, such as static batching increasing memory, dynamic batching consuming CPU, and instancing requiring shader support. Trade-offs are needed.