Reducing Draw Calls (also named SetPass calls) on Unity5.

So I have been working on the standard shader support for Unity 5 and doing some tests regarding how feasible is to reduce draw calls with the new standard shader and actually the results where pretty positive!.

Lets talk about the standard shader a little bit. The standard shader is a physically based shader that is consistent across diverse lighting conditions, its multi-platform and texture slots enable different shader features.

Here’s a small introductory video on what can be achieved with this one new shader.

 

 

This standard shader “mutates” and becomes more faster/slower depending on the textures you are using on the texture slots. being said that these two shaders are different (even if they are using the standard shader):

StandardShaders

So these two shaders (even if they are “Standard”); they are totally different shaders when rendering a scene, hence they have to be handled totally different if we want to reduce some draw calls (Now called SetPass calls).

Before Talking about how to reduce draw calls in Unity 5 I will talk about how draw calls are reduced regardless the shader is being used and then I will talk about how to reduce draw calls with the Standard shader in Unity5.

 

Reducing Draw Calls in Unity The Manual Way.

So, if we want to reduce draw calls in Unity or in any other engine the way to do it is using as less materials as possible.

Basically it all gets reduced to these single steps:

 

  1. Sort all the materials and gather them by the type of shader they are using.
  2. With the materials that share the same shader gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

 

 1. Sort all the materials and gather them by the type of shader.

I’m going to use for easiness of explanation purposes one simple scene that will have all the meshes use the same type of materials but different textures.

In our case we are going to use this scene (which contains 4 materials each of them with a Bumped Diffuse shader):

0SampleScene4Materials

 

 

 

 

 

 

 

 

 

 

 

Notice the SetPass calls: 4

So we have  for this scene these materials to process:

SceneMaterials

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. With the materials that share the same shader gather their textures in a bigger texture (Atlas texture).

In our case we have 2 textures per shader as the Bumped Diffuse shader uses a normal map and a base texture, so what we need to do is to have 2 textures like this:

AtlassedTextures

 

 

 

 

 

 

 

 

Save these textures in the project view, we are going to need them later on:

ProjectView

 

 

 

 

 

 

3. Create one material that will contain the shader and the atlas texture(s).

Create a material and assign (on this case) the two textures to the material with the shader used (in our case “Bumped Diffuse”):

3Material

 

 

 

 

 

 

 

 

4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.

This modification can be done on any 3D editor (Maya/ 3D studio / Max / Blender / etc) of your choice.

This is the most boring/demanding step as it involves you modifying your UV coords of your meshes and make them match the  texture’s atlas pixels (the texture we created on step 2).

So what we do is that to each of our UV coords located all around [0,0 – 1,1] we Re-position them on a smaller sub-set of [0,0-1,1] that matches the pixels on the atlas texture we created on step 2.

4UVREMAP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And… we do this for each of our meshes that share the same shader.

 

 

 5. Assign to the remapped meshes the material created on step 3.

We are almost there! still we have a couple of more steps to do:

 

  1. Select all your UV-remapped-meshes and place them where the old meshes where located and deactivate the old meshes.
  2. Assign the created material on step 3 to all your UV-remapped-meshes.

5ObjectsSameMAt

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And by here, we are finished!, now lets hit play:

5OptimizedScene

 

 

 

 

 

 

 

 

 

 

 

 

Before Optimizing if you go back to Step 1, we had 4 SetPass calls and after all the optimization has been done we end up with 1 SetPass calls!. which is amazing! its 75% draw call reduce on this case.

 

Reducing Draw Calls in Unity 5 with the Standard shader.

By here you should know more or less how the optimization process is done with the standard shader. as I wrote on the beginning,  the standard shader internally “mutates” and becomes more faster/slower depending on the textures you are using on the shader’s texture slots (and also renders differently depending on the textures that are used). being said that we have to gather our object’s materials if they are using the Standard shader by the textures used. i.e: Gather all the meshes that only have colors; Gather all the meshes that only have an Albedo texture.. and so on.

After gathering our objects by the textures used we just need to follow the same steps we use for any other shader:

 

  1. Sort all the materials and gather them by the type of shader they are using (In the standard shader case gather them by the textures used).
  2. With the materials that share the same “shader” gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

Here’s a small scene I created for testing purposes:

StandardShaderScene

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you compare the original draw calls (91) and the optimized scene’s draw calls (22) you can see that there’s ~75% draw call decrease!. Which is amazing!

Also, if you can try to combine meshes as much as possible, this also helps a lot!.

Reducing Draw Calls on Unity Automatically

Knowing that this is a really cumbersome process I decided to create a tool that automatically gathers your objects and sort them by the type of shaders they use and automagically “Bakes” all your unoptimized scenes!, you can find the package here, and if your scene only uses Diffuse and Bumped Diffuse shaders please be my guest and use it for free!

 

 

 

Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).

Any comments/suggestions/feedback let me know :).

 

11 Responses to “Reducing Draw Calls (also named SetPass calls) on Unity5.”

  1. Juan Sebastian says:

    hey Jonathan,

    It doesnt, for that you have the sprite editor (which I think it was on beta the last time I checked (long time ago)). For the tool to work it needs Meshes.