PresentAndSync导致程序卡顿 本篇从多个角度介绍了针对PresentAndSync的措施

关于VSync在iOS平台上的表现

1.iOS devices are always vsynced and that can’t be changed. vsync is allways ON, for iOS and cannot be turned off
2.By default the framerate is capped at 30;
3.you can use Application.targetFrameRate to change it to 60.

Graphics.PresentAndSync 是什么?

1.主线程进行Present时的等待时间和等待垂直同步的时间
2.其真实的意思应该是为了在渲染子线程(Rendering Thread)中进行Present,当前主线程(MainThread)需要等待的时间。
3.引擎会在主线程中进行Present(当前绝大多数的移动游戏均在使用该中操作),当然,Present操作同样需要等待GPU完成上一次的渲染。如果GPU渲染开销很大,则CPU的Present操作将一直处于等待操作,其等待时间,即为当前帧的Graphics.PresentAndSync时间

为什么会导致Graphics.PresentAndSync过高💥

CPU端开销很高,使得Present执行时错过了VSync操作,这样,Present将不得不等待下一次VSync的到来,从而造成了Gfx.WaitForPresent 和 Graphics.PresentAndSync的CPU开销较高。这种情况在CPU端加载过量资源时特别容易发生,比如WWW加载较大的AssetBundle、Resource.Load加载大量的Texture等等。

造成这两个参数的CPU占用较高的原因主要有以下三种原因:
1.CPU开销非常低,所以CPU在等待GPU完成渲染工作或等待VSync的到来;
2.CPU开销很高,使Present错过了当前帧的VSync,即不得不等待下一次VSync的到来
3.GPU开销很高,CPU的Present需要等待GPU上一帧渲染工作的完成。

一些论坛的FAQ有一定参考意义

这个是硬件设备强制开启的,Unity是无法对其进行关闭的

出现这种情况的问题可能有以下几种:
1、渲染成本过高,可能使用了iPad2当前OS无法很好支持的渲染功能。
2、是否在其他线程中有大量加载资源的情况出现,该情况同样会导致该选项过高(原因是垂直同步)

通过对帧数的限制来防止垂直同步的等待

发现在unity上对帧数限制不管用,后来在xcode上面的接收unity帧数的函数里强行对帧行进了限制

解决卡顿的一些建议

1.强制关闭垂直同步方案,强制设置帧率至60(仍然需要做CPU优化)
2.均摊计算到不同帧,降低CPU计算 使用Profiler查找开销源
3.优化Shader GPU 垂直同步问题是双向的,既要优化CPU也要优化GPU运算

一些类似的问题

场景1 IPhone4 UNITY3D Version4.X 遇到

1
2
3
4
5
6
7
8
As it turns out, we hade to modify our shader code. The same shaders that had very little performance impact in 3.3 had massive performance impact on iOS in 3.4. So if you are stuck, check there. Also, in AppController.mm, change this:

define USE_OPENGLES20_IF_AVAILABLE 1
To this:
define USE_OPENGLES20_IF_AVAILABLE 0
Which we found also gave us a significant FPS increase (nearly doubled our framerate on iOS).

url : http://answers.unity3d.com/questions/152966/massive-performance-decrease-in-34-devicepresent-t.html

场景2 UNITY3D 5.X

1
In 5.4 uncheking the "Auto Graphics API " ( in player settings) and deleting GLES3 Gave me back a few FPS

参考资料

http://answers.unity3d.com/questions/602628/why-does-my-game-require-so-much-rendering-power.html
http://answers.unity3d.com/questions/152966/massive-performance-decrease-in-34-devicepresent-t.html
https://forum.unity3d.com/threads/graphics-presentandsync-problem.318705/
https://docs.unity3d.com/Manual/class-QualitySettings.html