LibraryBatching and Draw Calls

Batching and Draw Calls

Learn about Batching and Draw Calls as part of Game Development with Unity and C#

Game Development: Advanced C# and Optimization - Batching and Draw Calls

In game development, particularly with engines like Unity, understanding and optimizing how your game renders graphics is crucial for achieving smooth performance. Two fundamental concepts that directly impact rendering efficiency are Draw Calls and Batching.

What are Draw Calls?

A draw call is essentially a command sent from the CPU to the GPU, instructing it to draw a specific object or set of objects. Each draw call involves a significant amount of overhead, as the CPU needs to prepare data, set up rendering states, and communicate with the GPU. The more draw calls your game makes, the more work the CPU has to do, which can become a bottleneck, especially in complex scenes.

What is the primary role of a draw call in game rendering?

A draw call is a CPU command to the GPU to draw an object or set of objects.

The Problem with Too Many Draw Calls

When a game has many objects that are rendered individually, each requiring its own draw call, the CPU can become overwhelmed. This can lead to frame rate drops and stuttering, as the CPU struggles to keep up with the demands of preparing and sending these commands to the GPU. Imagine a scene with hundreds of small, identical props – each one might trigger a separate draw call, quickly accumulating overhead.

High draw call counts are a common performance bottleneck, especially on mobile devices and older hardware.

Introducing Batching

Batching is a technique used to reduce the number of draw calls by grouping multiple objects together into a single draw call. This significantly reduces the CPU overhead associated with rendering. Unity offers several types of batching to help optimize your game.

Static Batching

Static batching is performed by Unity at build time. It combines meshes of static objects (objects that do not move or change during gameplay) into larger meshes. This is highly effective for static geometry like buildings, terrain, and props that remain in place. The trade-off is increased memory usage, as the combined meshes are stored in memory.

Dynamic Batching

Dynamic batching is performed by Unity at runtime. It combines small, moving meshes that share the same material into a single draw call. This is useful for smaller, frequently moving objects. However, dynamic batching has limitations: objects must have fewer than a certain number of vertices (typically 900), and they cannot have certain shader properties or be part of a GPU instancing setup.

GPU Instancing

GPU instancing is another method to render multiple copies of the same mesh with the same material in a single draw call. Unlike dynamic batching, it's handled by the GPU itself. This is particularly efficient for rendering large numbers of identical objects, such as trees, grass, or crowds, where each instance might have slightly different properties (like color or position) passed via data buffers.

Imagine a scene with 100 identical trees. Without batching, each tree might require a separate draw call. Static batching would combine all static trees into one large mesh. Dynamic batching might combine a few moving trees if they are small enough. GPU instancing allows you to send a single command to draw all 100 trees, with the GPU handling the variations for each instance, significantly reducing CPU work.

📚

Text-based content

Library pages focus on text content

Key Considerations for Optimization

When optimizing for draw calls and batching, consider the following:

  • Materials: Objects sharing the same material are prime candidates for batching. Try to consolidate materials where possible.
  • Static vs. Dynamic: Mark objects as static in the Inspector if they won't move. This enables static batching.
  • Vertex Count: Be mindful of vertex limits for dynamic batching.
  • Profiling: Use Unity's Profiler to identify draw call bottlenecks and understand which batching methods are most effective for your scene.
What is the primary requirement for objects to be considered for dynamic batching?

Objects must share the same material and have a low vertex count (typically under 900).

Batching in Practice

Effective use of batching techniques can dramatically improve rendering performance. By reducing the number of draw calls, you free up the CPU to handle other game logic, AI, and physics, leading to a smoother and more responsive gaming experience. Always profile your game to understand the impact of these optimizations.

Learning Resources

Unity Manual: Draw Calls(documentation)

Official Unity documentation explaining the concept of draw calls and their impact on performance.

Unity Manual: Static Batching(documentation)

Learn how to enable and configure static batching in Unity for optimizing static scene elements.

Unity Manual: Dynamic Batching(documentation)

Details on Unity's dynamic batching feature, its requirements, and limitations.

Unity Manual: GPU Instancing(documentation)

An explanation of GPU instancing and how it can be used to render many identical objects efficiently.

Unity Blog: Optimizing Graphics Performance(blog)

A blog post from Unity discussing various graphics optimization techniques, including draw call reduction.

Unity Learn: Graphics Performance Optimization(tutorial)

A learning path on Unity Learn covering essential graphics optimization strategies.

GDC Talk: Unity Graphics Performance(video)

A presentation from GDC discussing graphics performance in Unity, often touching upon draw calls and batching.

Real-Time Rendering: Batching(blog)

A blog post explaining batching techniques in real-time graphics, providing a broader context.

Unity Profiler Overview(documentation)

Essential documentation on using Unity's Profiler to diagnose performance issues, including draw call analysis.

Understanding Draw Calls and Batching in Unity(blog)

An article that breaks down draw calls and batching with practical examples for Unity developers.