按需渲染如何改善移动性能

It’s not always desirable to render a project at the highest frame rate possible, for a variety of reasons, especially on mobile platforms. Historically, Unity developers have used Application.targetFrameRate or Vsync count to throttle the rendering speed of Unity. This approach impacts not just rendering but the frequency at which every part of Unity runs. The new on-demand rendering API allows you to decouple the rendering frequency from the player loop frequency.

由于多种原因,并非总是希望以尽可能高的帧速率渲染项目,尤其是在移动平台上。 过去,Unity开发人员曾使用Application.targetFrameRate或Vsync计数来限制Unity的渲染速度。 这种方法不仅影响渲染,而且还会影响Unity每个部分的运行频率。 新的按需渲染API允许您将渲染频率与播放器循环频率解耦。

什么是按需渲染? (What is on-demand rendering?)

On-demand rendering allows you to skip rendering frames while still running the rest of the player loop at a high frequency. This can be especially useful on mobile; bypassing rendering can bring significant performance and power savings, while still allowing the application to be responsive to touch events.

按需渲染允许您跳过渲染帧,同时仍然以较高的频率运行播放器循环的其余部分。 这在移动设备上尤其有用。 绕过渲染可以带来显着的性能和功耗节省,同时仍然允许应用程序对触摸事件做出响应。

为什么要使用按需渲染? (Why would I use on-demand rendering?)

Here are some example scenarios of when you may want to lower the frame rate:

以下是一些可能需要降低帧频的示例场景:

  1. Menus (e.g., the application entry point or a pause menu): Menus tend to be relatively simple Scenes and as such do not need to render at full speed. If you render menus at a lower frame rate, you will still receive input during a frame that is not rendered, allowing you to reduce power consumption and to keep the device temperature from rising to a point where the CPU frequency may be throttled, while keeping a smooth UI interaction.

    菜单(例如,应用程序入口点或暂停菜单):菜单往往是相对简单的场景,因此不需要全速渲染。 如果以较低的帧速率渲染菜单,则在未渲染的帧期间仍会收到输入,从而可以降低功耗并防止设备温度升高到可能会降低CPU频率的程度,同时保持流畅的用户界面交互。

  2. Turn-based games (e.g., chess): Turn-based games have periods of low activity when users think about their next move or wait for other users to make their move. During such times, you can lower the frame rate to prevent unnecessary power usage and prolong the battery life.

    回合制游戏(例如国际象棋):当用户考虑下一步行动或等待其他用户采取行动时,回合制游戏活动较少。 在这种情况下,您可以降低帧频以防止不必要的电源使用并延长电池寿命。

  3. Static content: You can lower the frame rate in applications where the content is static for much of the time, such as automotive user interface (UI).

    静态内容:您可以在大部分时间为静态的应用程序中降低帧速率,例如汽车用户界面(UI)。

  4. Performance management: If you want to manage power usage and device thermals to maximize battery life and prevent CPU throttling, particularly if you are using the Adaptive Performance package, you can adjust the rendering speed.

    性能管理:如果要管理电源使用和设备散热以最大程度地延长电池寿命并防止CPU节流,尤其是在使用 Adaptive Performance软件包时 ,可以调整渲染速度。

  5. Machine learning or AI applications: Reducing the amount of work the CPU devotes to rendering may give you a little bit of a performance boost for the heavy processing that is the central focus of your application.

    机器学习或AI应用程序:减少CPU专门用于渲染的工作量,可以使繁重的处理(这是应用程序的重点)的性能有所提高。

在哪里支持? (Where is it supported?)

Everywhere! On-demand rendering works on Unity 2019.3 with every supported platform (see the system requirements) and rendering API (built-in render pipeline, Universal Render Pipeline and High Definition Render Pipeline).

到处! 按需渲染可在Unity 2019.3上与每个受支持的平台(请参阅 系统要求 )和渲染API(内置渲染管道,通用渲染管道和高清渲染管道)一起使用。

如何使用按需渲染? (How do I use on-demand rendering?)

The on-demand rendering API consists of only three properties in the namespace UnityEngine.Rendering.

按需呈现API仅在名称空间UnityEngine.Rendering中包含三个属性。

OnDemandRendering.renderFrameIntervalThis is the most important part. It allows you to get or set the render frame interval, which is a dividing factor of Application.targetFrameRate or QualitySettings.vSyncCount, to define the new frame rate. For example, if you set Application.targetFrameRate to 60 and OnDemandRendering.renderFrameInterval to 2, only every other frame will render, yielding a frame rate of 30 fps.

OnDemandRendering.renderFrameInterval 这是最重要的部分。 它允许您获取或设置渲染帧间隔,它是Application.targetFrameRate或QualitySettings.vSyncCount的划分因子,以定义新的帧速率。 例如,如果将Application.targetFrameRate设置为60并将OnDemandRendering.renderFrameInterval设置为2,则仅每隔一帧将渲染一次,从而产生30 fps的帧速率。

OnDemandRendering.effectiveFrameRateThis property gives you an estimate of the frame rate that your application will render at. The estimate is determined using the values of OnDemandRendering.renderFrameInterval, Application.targetFrameRate, QualitySettings.vSyncCount and the display refresh rate. But bear in mind that this is an estimate and not a guarantee; your application may render more slowly if the CPU is bogged down by work from other things such as scripts, physics, networking, etc.

OnDemandRendering.effectiveFrameRate 通过此属性,可以估算应用程序将呈现的帧速率。 使用OnDemandRendering.renderFrameInterval,Application.targetFrameRate,QualitySettings.vSyncCount和显示刷新率的值确定估算值。 但是请记住,这仅是估计值,而不是保证。 如果CPU因脚本,物理,网络等其他因素而无法工作,则应用程序的渲染速度可能会变慢。

OnDemandRendering.willThisFrameRenderThis simply tells you if the current frame will be rendered to the screen. You can use non-rendered frames to do some additional CPU-intensive work such as heavy math operations, loading assets or spawning prefabs.

OnDemandRendering.willThisFrameRender 这只是告诉您是否将当前帧渲染到屏幕上。 您可以使用非渲染框架来完成一些额外的CPU密集型工作,例如繁重的数学运算,加载资源或生成预制件。

我还需要知道什么? (What else do I need to know?)

  • Even though frames will not be rendered as often, events will be sent to scripts at a normal pace. This means that you may receive input during a frame that is not rendered. To prevent the appearance of input lag we recommend that you call OnDemandRendering.renderFrameInterval = 1 for the duration of the input to keep buttons, movement, etc. responsive.

    即使帧的渲染频率不高,事件也将以正常速度发送到脚本。 这意味着您可能会在未渲染的帧中收到输入。 为防止出现输入滞后现象,建议您在输入持续时间内调用OnDemandRendering.renderFrameInterval = 1,以保持按钮,移动等的响应。

  • Situations that are very heavy on scripting, physics, animation, etc. but are not rendering will not benefit from using on-demand rendering. The results may appear choppy and with negligible reduction in CPU and power usage.

    在脚本,物理,动画等方面非常繁重而又没有渲染的情况将无法从按需渲染中受益。 结果可能看起来不连贯,并且CPU和功耗的减少可忽略不计。

(Example)

Here is a simple example showing how on-demand rendering could be used in a menu to render at 20 fps unless there is input. 

这是一个简单的示例,显示了除非有输入,否则如何在菜单中使用按需渲染以20 fps进行渲染。

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using UnityEngine;
using UnityEngine.Rendering;
public class Menu : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 60;
   // When the Menu starts, set the rendering to target 20fps
        OnDemandRendering.renderFrameInterval = 3;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0) || (Input.touchCount > 0))
        {
            // If the mouse button or touch detected render at 60 FPS (every frame).
            OnDemandRendering.renderFrameInterval = 1;
        }
        else
        {
            // If there is no mouse and no touch input then we can go back to 20 FPS (every 3 frames).
            OnDemandRendering.renderFrameInterval = 3;
        }
    }
}

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using UnityEngine ;
using UnityEngine . Rendering ;
public class Menu : MonoBehaviour
{
     // Start is called before the first frame update
     void Start ( )
     {
         QualitySettings . vSyncCount = 0 ;
         Application . targetFrameRate = 60 ;
   // When the Menu starts, set the rendering to target 20fps
         OnDemandRendering . renderFrameInterval = 3 ;
     }
     // Update is called once per frame
     void Update ( )
     {
         if ( Input . GetMouseButton ( 0 ) || ( Input . touchCount > 0 ) )
         {
             // If the mouse button or touch detected render at 60 FPS (every frame).
             OnDemandRendering . renderFrameInterval = 1 ;
         }
         else
         {
             // If there is no mouse and no touch input then we can go back to 20 FPS (every 3 frames).
             OnDemandRendering . renderFrameInterval = 3 ;
         }
     }
}

Here is an example project demonstrating how on-demand rendering can be used in a variety of situations.

这是一个 示例项目, 演示了如何在各种情况下使用按需呈现。

您是否使用按需渲染? (Are you using on-demand rendering?)

Let us know in the forums how on-demand rendering is working for you. We’ve tested it on Windows, macOS, WebGL, iOS, and Android, both in the Unity Editor and with Standalone players, but we’re always open to more feedback.

论坛中让我们知道按需渲染如何为您工作。 我们已经在Windows,macOS,WebGL,iOS和Android上对它进行了测试,无论 是在Unity编辑器中还是 在独立播放器中,但我们始终欢迎更多反馈。

翻译自: https://blogs.unity3d.com/2020/02/07/how-on-demand-rendering-can-improve-mobile-performance/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值