WHAT IS GPU INSTANCING?

GPU Instancing is an optimization technique that Unity gives us to render multiple copies of the same object on screen with very few impacts on performance, and I mean lots of copies of that object, this is useful, for example, to render large amounts of grass or tree tops.

NOTE: Los ejemplos que veremos a continuación hablan del material estándar de Unity. 

Hacer esto es muy sencillo con los materiales que Unity nos proporciona por defecto, basta con activar la casilla "Enable GPU Instancing" en el inspector y ya está.


In this example, I am showing you where the checkbox is (is in the material) and how the number of draw calls is dramatically reduced.


GPU Instancing enabled



GPU Instancing disabled


Here we can compare the Draw Calls number between have (or not) our GPU Instancing enable.

Remember, the more complex the material you're using is, you will have more calls on your SetPass/Draw Calls. 

EJEMPLO
1 MESH + 1 MATERIAL WITH JUST ONE COLOR PASS = 1 DRAWCALL & 1 SET PASS CALL

It's important to have in mind that since it can be very expensive for the performance of the game, the limits will be marked by the destination platform.  It's kind of difficult to have absolute control about this and that's why the "Quality Options" exists on games.


UNLIT MATERIAL EXAMPLE + GPU INSTANCING ENABLED


With a pretty simple material (in this case just emitting one color), the resource demand is much less demanding.



REQUIREMENTS

There are very clear limitations for this to work properly, not every mesh and materials can be grouped in the same way, GPU Instancing only works with objects that meet the following conditions:
  • Objects must use the same mesh
  • Objects must share the same material

NOTES: 
  • Objects with GPU Instancing, although must use the same mesh, they can use different scales, rotation and position. . 
  • There are methods to achieve that these objects on GPU Instancing have different properties like textures or color, we're going to talk about this in another article.
  • Unity only shows the "GPU Instancing" checkbox if the material allows it, if you need a material with special properties you can write your own shader and make it work with GPU Instancing. 
  • If you're doing 'Batching', Unity prioritizes the "Static batching" over 'GPU Instancing', so, if you have some of these objects checked as 'Static', Unity will delete the 'GPU Instancing' instruction at the moment of make the 'Batching'. Also, Unity will show you a console Alert advising you to deactivate the 'Static' mode from those objects.
  • GPU Instancing allows global illumination with LightProbes.

RECOMMENDATIONS

You can combine that famous Atlas texture technique with GPU Instancing, just assign that material to the meshes that use that texture, you will have some more draw calls that's inevitable (cause some of those meshes will be different) but even that it will be pretty optimum. 



The mesh is different cause it's UV coordinates are different, although, GPU Instancing groups the new objects cause they are identical copies, keeping the number of draw calls pretty low after all, and of course, these two spheres are using the same material.

EXAMPLE ABOUT UV MAPS 


This way, you can create hundreds of instances of the same object with just one material and a very low impact on your performance. Have in mind that this won't allow you to create millions of polygons on your scene like magic, this will depend on the device capacity, the triangles amount of your objects, the complexity of your material and the screen resolution. However,  here you have an excellent technique that allows you to optimize your game or app and now you can create those grassy scenes like those video games you love.



For any kind of doubt, just let me know in the comments or send me a direct message here. If you want to make a donation for the site you can do it in my Ko-fi account, this will help me a lot to continue creating content for you!



Comentarios