Batch optimization in unity with GPU instancing "Go"

Source: Internet
Author: User
Tags unity 5

We all want to be able to put 1 million objects in the scene, unfortunately, rendering and managing a large number of game objects is at the expense of CPU and GPU performance, because there are too many draw call issues, and finally we have to find other solutions.

In this article, we'll discuss two optimization techniques that can help you reduce the number of draw call in unity games to improve overall performance: batch processing and GPU Instancing.

Batch Processing
One of the most common problems that developers encounter in their daily work is the lack of performance, due to the lack of CPU and GPU running capacity. Some games can run on a PC, but not on mobile devices. The smoothness of running the game is greatly affected by the number of draw call. There are several solutions that can help you solve this problem. The most common is batch processing, including Static batching and dynamic batching.

The Static batching can let the engine reduce draw call for any size grid, as shown in:


<ignore_js_op>


to make an object in the scene use a static batching, you need to mark it as static and share the same material in the mesh renderer, because the static batching does not make vertex transitions on the CPU, So it's usually more effective than the dynamic batching. But it will use more memory, such as having multiple copies of the same object in your scene, unity will combine them into a large grid and may increase memory usage. unity combine as many meshes as possible into a static mesh and submit it as a draw call . The disadvantage of this approach is that an object marked as static cannot be moved during its life cycle. When

Dynamic batching is enabled, unity will attempt to automatically bulk move objects into a draw call. To allow objects to be dynamically batched, they should share the same material, but there are other limitations:

    • Vertex count: Dynamic batching each vertex of an object in a scene has some overhead, so the batch works only for mesh objects that are less than 900 vertex attributes. For example, if your shader uses vertex positions, normals, and a UV, then you can dynamically batch up to 300 vertices, and if your shader uses vertex positions, normals, Uv0,uv1, and tangents, then there are only 180 vertices. It is important to note that the attribute count limit may change in the future.
    • Image information: If the object contains transform with image information, such as the size of a object (1f, 1f, 1f), and the size of the B object is ( -1f, -1f, -1f), you cannot do batch processing.
      Material: If objects use different material instances, they will not be processed in batches, even if they are essentially the same. And shadow caster rendering is an exception.
    • renderer: An object with a light map has other renderer parameters, such as the offset and scaling of a light map index or a light map. In general, the game object for a dynamic light map should point to the exact location of the same light map that you want to batch.
    • cannot use the Multi-Pass shader: Almost all unity shaders support the forward rendering mode (Forward Rendering) for multiple lights, which requires additional rendering times, so the "extra per pixel light" is not batched when drawn The Legacy Deferred (light pre-pass) render path cannot be dynamically batched because it must draw objects two times.


Dynamic batching works by converting the vertices of all objects into world space on the CPU, so it can only be used to render high performance when the work of draw call is less than the CPU vertex conversion effort . When using a game console or a modern api,draw call such as metal, the overhead is usually much lower, and Dynamic batching cannot improve performance. Once you understand the above limitations, you can significantly improve the performance of your game if you use batch processing wisely.

GPU instancing
Another good way to improve graphics performance is to use GPU instancing. The biggest advantage of GPU instancing is that it can reduce memory usage and CPU overhead. When using GPU instancing, there is no need to turn on batch processing, and the purpose of GPU instancing is that a grid can be pushed to the GPU along with a series of additional parameters. To take advantage of GPU instancing, you must use the same material, and you can pass additional parameters to the shader, such as color, floating-point number, and so on.

Unity supports GPU instancing starting with version 5.4. The only limitation is to use the same material and grid on the game object. The following platforms are currently supported:

    • Windows dx11/dx12 and SM 4.0 or higher/OPENGL 4.1 or higher
    • OS X and Linux:opengl 4.1 and above
    • Mobile: OpenGL ES 3.0 or higher/metal
    • PlayStation 4
    • Xbox One


You can also use the graphics.drawmeshinstanced method if you want to make further optimizations, such as reducing the overhead of managing scene objects. You only need to pass your mesh, material and attached properties to draw your object. The current limit is up to 1023 instances at a time. In Unity 5.6, we added a new method for Graphics.drawmeshinstancedindirect, which can be used to specify the number of instances that need to be rendered.

GPU instancing Case
To create a basic standard surface shader that supports GPU instancing, you can click inside your project:
Create->shader->standardsurfaceshader (instanced).

<ignore_js_op>


Then, in the material properties, select the newly created shader.

<ignore_js_op>


Although instanced objects share the same meshes and materials, you can use the Materialpropertyblock API to set individual shader properties for each object.

If a game object is marked as "static" and the static batching is turned on, the game object will not be GPU instancing, and a warning box will appear in the viewer indicating that the "static batch" flag can be set in the player (player Settings) is canceled. If the game object supports dynamic batching, but it uses a material that can be instantiated, the game object will not be batched and will be automatically instantiated.

When using forward rendering rendering mode, objects affected by multiple lights cannot be effectively instantiated. Only the base pass can effectively take advantage of instantiation rather than adding a pass. In addition, objects that use a light map or are affected by different light or reflection probe cannot be instantiated. As shown, you can find in frame debug that the draw call associated with the GPU instancing is labeled "Draw Mesh (instanced)".

<ignore_js_op>


GPU Instancing is a very powerful feature. In Unity 5.6, you can draw a large number of meshes using graphics.drawmeshinstancedindirect . In Mac Pro, we were able to draw about 680,000 moving cubes with different colors and maintain a stable frame rate of 60 frames per second.

Is an example scenario where more than 6,000 buns fly around a large bowl in the sky, and they both project and receive shadows. With GPU instancing, there is little performance overhead. The bun model here uses the Standardsurface Shader (instanced).

<ignore_js_op>


Summarize
In this article, we describe two of the most popular techniques for optimizing rendering Performance: Batch processing and GPU instancing. We show you how to use them in practice and discuss possible applications. Thanks to the presence of optimization technologies such as batch processing and GPU instancing, we are able to draw a large number of objects and maintain stable performance.

To get more technology sharing on unity in time, scan the QR code below and follow the "Unity official platform" public number!

<ignore_js_op>


Reprint please indicate source: Unity official Chinese Community (forum.china.unity3d.com). Do not change any copyright information privately.

Batch optimization in unity with GPU instancing "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.