Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Unity rendering optimization in Render

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the method of Unity rendering optimization in Render". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What are the performance problems caused by rendering?

First of all, let's be clear: only when CPU and GPU have completed all their tasks can a frame be rendered successfully. The timeout of any task will result in a rendering delay for the entire frame.

Rendering problems can generally be divided into two categories, the first caused by inefficient rendering pipelines. One or more steps in rendering work takes too long, inefficient execution, and interrupts the smooth data flow, which will lead to inefficient rendering pipelines, which is also called rendering bottleneck (bottlenecks). The second is the rendering bottleneck caused by sending too many instructions to the rendering pipeline in one breath.

When we find that rendering a frame in the game takes a long time and is related to the efficiency of CPU execution, this situation is called CPU bottleneck (CPU bound). Similarly, when we find that the game takes too long to render a frame and GPU spends a lot of time on rendering calculation, this situation is called GPU bottleneck (GPU bound).

Before making changes to our code, it is very important to use appropriate performance analysis tools to locate rendering performance issues, to drug the game, and of course it is also important to evaluate our changes reasonably. Performance optimization is a tradeoff, and performance improvements in this area may have a negative impact on other parts.

Two performance analysis tools

Profiler window tool

Profiler window tool

The Profiler window tool allows you to monitor game performance in real time. We can see many aspects of our game through the Profiler tool, including memory usage, rendering pipeline usage, and user script usage (code quality).

Frame Debugger

Frame Debugger window

Frame Debugger allows us to see how each frame of the game is rendered step by step. It allows us to see more details, including what each draw call makes GPU render, which attributes it affects in the shader code block, and the events that CPU sends to GPU, which information will help us understand how our game is rendered and how to improve our performance.

If the rendering problem is caused by a CPU bottleneck (CPU bound)

Generally speaking, the steps related to rendering in this part of CPU are as follows:

Determine the objects to be rendered

Prepare the data to be sent to GPU

Send instructions to GPU

These steps consist of many separate tasks, which are handled by multiple threads. Multiple threads can handle multiple tasks at the same time. This means that the whole rendering can be faster. We also call this working mode multithreaded rendering (multithreaded rendering).

The rendering process of Unity is mainly executed by three types of threads: the main thread, the rendering thread and the worker thread.

The main thread mainly handles most of the tasks of CPU, including some rendering tasks.

The rendering thread, as its name implies, is dedicated to tasks that handle instructions sent by CPU to GPU.

Each thread in the worker thread handles a task, such as culling tasks or material skinning tasks.

Which tasks are handled by which threads mainly depends on our settings in the project (settings) and the hardware devices that our project is running (hardware). For example, the more CPU you have in the kernel, the more worker threads you have. As a result, the same game may behave differently on different devices.

Since multithreaded rendering is a complex and hardware-dependent task, we have to consider what causes our CPU bottleneck in terms of optimization. If the problem is that CPU spends a lot of time performing culling work, then it doesn't help that you optimize the task of sending instructions from CPU to GPU, because they are not in the same thread.

Of course, not all platforms support multithreaded rendering, at least at the time of this writing, WebGL does not support this feature. If a device does not support multithreaded rendering, all CPU tasks will be executed by the same thread, so although the rendering efficiency is not as efficient as other devices, but once there is a CPU bottleneck, any optimization measures you use for CPU will improve the performance of CPU.

Graphics jobs option

This option can be found by opening player settings

PlayerSettings

Its function is to determine whether unity uses worker threads to handle rendering tasks, and if unchecked, it mainly uses the main thread or rendering thread. On platforms that support graphics jobs, checking this option can greatly improve performance. You can test and observe the performance effect by yourself.

Send rendering instructions to GPU

Too many rendering instructions are sent to GPU in one frame, which is the most common problem in CPU CPU bound. On most platforms, this task occurs on rendering threads, but some platforms (such as PS4, etc.) occur on worker threads.

The most expensive operation of such tasks is attributed to sending SetPass call instructions to GPU, and reducing the number of requests is the best way to optimize such tasks.

We can see how many SetPass call and batch are sent to GPU through the Rendering section of the Profiler window. Of course, the tolerance for the amount of SetPass call varies depending on the target hardware, for example, PC clients can accept much more than mobile devices.

We can reduce the number of SetPass call and batch by:

Reducing the number of rendered objects can reduce the number of SetPass call and batch

Reducing the number of requests for rendering of rendered objects can reduce the number of SetPass calls

Batch the data of rendered objects together to reduce the number of batch

In view of these three items, we carry out

I. How to reduce the number of rendered objects?

Reducing the number of rendered objects is the easiest way to improve rendering performance. We can do this in the following ways:

Reduces the number of rendered objects visible to the camera (cone) in the scene. For example, if we render a group of different characters, we can try to remove a few and take a look at the effect. This is much faster than using complex techniques to optimize.

We can control it by using the far trimming plane (Far Clip Plane) property of the camera, which is the same principle as the first one.

We can customize the culling distance for rendered objects by changing the Layer Cull Distances attribute of the camera. This method is generally applied to very small objects (such as flowers on the grass). Once it is more than a certain distance from the camera, it does not render even in the camera's cone.

We can also use the Occlusion culling (occlusion culling) option above the camera. As the name implies, if a rendered object is completely obscured by another rendered object (relative to the camera), it will not be rendered. However, the occlusion culling option does not apply to all scenarios, so use it with caution.

II. How to reduce the number of requests for rendering of rendered objects?

We all know that real-time lighting, shadows and reflections can add a lot of realism to the game, but it can also cost a lot of money. Because they can cause the same rendered object to be rendered multiple times, which will greatly affect performance.

Take the rendering path (rendering path) as an example, which rendering path you choose will make a big difference. In most cases, if our device is a relatively high-end hardware device, delayed rendering (Deferred Rendering) is more appropriate than forward rendering (Forward Rendering), because its features allow you to use more real-time lighting, shadows and reflections. On the other hand, forward rendering is more suitable for low-end hardware devices, and it is less expensive for GPU in the same amount of rendering. In the end who is better, a specific analysis of the specific situation.

Dynamic lighting is expensive and complex in terms of optimization, so interested partners can click on the front door. If there are many rendered objects in the scene that will not move, consider using baking. In this way, you don't have to consider the calculation of dynamic light when the game is running.

The quality of shadows can be set by Quality Settings. For example, we can set the shadow distance (Shadow Distance) to control how many shadows are cast between objects within the distance.

The optimization of reflection is more complex, and it is not expanded here, leaving the portal.

III. How to batch the rendered objects to reduce batch?

The rendered object must have the following elements to meet the batch requirements

Share the same material instance

Have the same material configuration (map, shader,shader parameter)

Although batch rendering of objects can improve rendering performance, you also need to consider how to ensure that the consumption generated by batch does not exceed the performance benefits.

Here are some ways to combine batches:

Static batch (Static batching), this method can batch adjacent qualified static render objects. Static batching can lead to high memory footprint, so we also need to consider its pros and cons.

Dynamic batching (Dynamic batching) is another technology that allows unity non-static objects to be grouped, but there are some limitations. Similarly, dynamic batch will lead to more memory use in CPU, and we also need to weigh the pros and cons.

In terms of UI, the UI element is relatively complex, which is subject to our UI layer.

The GPU instancing option allows many of the same rendered objects to batch very efficiently. But it is limited by hardware.

Texture atlasing atlas, you can combine reused images into a larger map. This technology is widely used in 2D games and UI systems, and 3D games can also be used. Unity has a built-in atlas tool called Sprite Packer.

We can also artificially merge meshes and maps through unity editing or code, of course, we need to know that the shadows of these rendered objects, lighting and culling will still be handled separately. This means that your goal of improving performance by merging materials will counteract the effect achieved by culling the rendered object so that it does not render it.

We need to note that in the script, when the material is manipulated through Renderer.material, the engine will copy an instance and return its reference, that is, your operation will only be carried out on the instance it is copied, and will not affect its original material. If you want to modify multiple rendered objects that share materials through code, you need to use Render.sharedMaterial.

This is the end of the content of "what is the method of Unity rendering optimization in Render". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report