Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity Game Optimization - Third Edition

You're reading from  Unity Game Optimization - Third Edition

Product type Book
Published in Nov 2019
Publisher Packt
ISBN-13 9781838556518
Pages 404 pages
Edition 3rd Edition
Languages
Authors (2):
Dr. Davide Aversa Dr. Davide Aversa
Profile icon Dr. Davide Aversa
Chris Dickinson Chris Dickinson
Profile icon Chris Dickinson
View More author details

Table of Contents (15) Chapters

Preface 1. Section 1: Base Scripting Optimization
2. Evaluating Performance Problems 3. Scripting Strategies 4. Section 2: Graphical Optimizations
5. The Benefits of Batching 6. Optimizing Your Art Assets 7. Faster Physics 8. Dynamic Graphics 9. Section 3: Advance Optimizations
10. Optimizations for Virtual and Augmented Reality 11. Masterful Memory Management 12. The Data-Oriented Technology Stack 13. Tactical Tips and Tricks 14. Other Books You May Enjoy

The Benefits of Batching

In 3D graphics and games, batching is a very general term used to describe the process of grouping a large number of wayward pieces of data together and processing them as a single, large block of data. This situation is ideal for CPUs, and particularly GPUs, which can handle the simultaneous processing of multiple tasks with their multiple cores. Having a single core switching back and forth between different locations in memory takes time, so the less this needs to be done, the better.

In some cases, the act of batching refers to large sets of meshes, vertices, edges, UV coordinates, and other different data types that are used to represent a 3D object; however, the term could just as easily refer to the act of batching audio files, sprites, texture files, and other large datasets.

So, just to clear up any confusion, when the topic of batching is mentioned...

Draw calls

Before we discuss dynamic batching and static batching, let's first learn about the problems that they are both trying to solve within the Rendering Pipeline. We will try to keep our analysis fairly light on the technicalities as we will explore this topic in greater detail in Chapter 6, Dynamic Graphics.

The primary goal of these batching methods is to reduce the number of draw calls required to render all objects in the current view. At its most basic form, a draw call is a request sent from the CPU to the GPU asking it to draw an object.

Draw call is the common industry vernacular for this process, although they are sometimes referred to as SetPass Calls in Unity since some low-level methods are named as such. Think of this as configuring options before initiating the current rendering pass. We will refer to them as draw calls throughout the remainder of this...

Materials and shaders

Render State in Unity is essentially exposed to us via materials. Materials are containers around shaders, short programs that define how the GPU should render incoming vertex and texture data. A shader on its own does not have the necessary knowledge of the state to accomplish anything of value. A shader requires input such as diffuse textures, normal maps, and lighting information, and effectively dictates what Render State variables need to be set in order to render the incoming data.

Shaders are named this way because, many years ago, their original implementation was to only handle the lighting and shading of an object (applying shadows where originally there were none). Their purpose has grown enormously since then, and now they have the much more generic purpose of being a programmable access point to many different kinds of parallel tasks, but the...

The Frame Debugger

Before we dive into how batching can save us draw calls, let's explore a useful tool that can help us to determine how batching is affecting our scene—the Frame Debugger.

We can open Frame Debugger by selecting Window | Analysis | Frame Debugger from the main window or clicking on the Frame Debugger button in Breakdown View Options in the rendering area of the Profiler. Either approach will open the Frame Debug window.

Clicking on the Enable button in the Frame Debug window will allow us to observe how our scene is being constructed, one draw call at a time. The following screenshot shows the user interface of the Frame Debugger, with a list of GPU instructions in the left-hand panel and more detailed information in the right-hand panel:

There is a lot of information in this window that can provide us with useful information if we want to debug...

Dynamic batching

Dynamic batching has the following three important qualities:

  • Batches are generated at runtime (batches are dynamically generated)
  • The objects that are contained within a batch can vary from one frame to the next, depending on what meshes are currently visible to the Main Camera view (batch contents are dynamic)
  • Even objects that can move around the scene can be batched (it works on dynamic objects)

These attributes lead us to the name Dynamic Batching.

If we return to the Player Settings page and enable Dynamic Batching, we should see that the number of batches drops from nine down to six. Dynamic Batching automatically recognizes that our objects share material and mesh information and, therefore, combines some of them into a larger batch for processing. We should also see a different list of items in the Frame Debugger, demonstrating that meshes are now being...

Static batching

Unity offers a second batching mechanism known as static batching. This batching feature is similar to dynamic batching in a couple of ways, in that the objects that are to be batched are determined at runtime based on what's visible to the camera, and the contents of these batches will vary from frame to frame. However, there is one very important difference: it only works on objects that are marked Static, hence the name static batching.

The static batching system has its own set of requirements:

  • As the name implies, the meshes must be flagged as Static (specifically, Batching Static)
  • Additional memory must be set aside for each mesh that is being statically batched
  • There is an upper limit on the number of vertices that can be combined in a static batch that varies per graphics API and platform, which is around 32,000–64,000 vertices (check out the...

Summary

It is clear that the dynamic batching and static batching systems are not a silver bullet. We cannot blindly apply them to any given Scene and expect improvements. If our application and Scene happen to fit a particular set of parameters, then these methods are very effective at reducing CPU load and rendering bottlenecks. However, if they do not, then some additional work is required to prepare our Scene to meet batching feature requirements. Ultimately, only a good understanding of these batching systems and how they function can help us determine where and when this feature can be applied, and, hopefully, this chapter has given us all of the information we need to make informed decisions.

You will learn more about the Rendering Pipeline and performance improvement techniques in Chapter 6, Dynamic Graphics. But now, let's move onto a different topic and look into...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity Game Optimization - Third Edition
Published in: Nov 2019 Publisher: Packt ISBN-13: 9781838556518
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}