Sprite Kit是在后台渲染还是在主线程上渲染?它是如何工作的? [英] Does Sprite Kit render in the background, or on the main thread? How does it work?

查看:149
本文介绍了Sprite Kit是在后台渲染还是在主线程上渲染?它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档显示了Sprite Kit的简化runloop 。

The documentation shows a simplified runloop of Sprite Kit.

我试图了解他们是如何实现它的。 SKView 调用-update:SKScene 。然后首先评估动作,然后模拟物理,并让子类调整场景。对场景进行更改后, SKView 最终呈现节点树到屏幕。

I am trying to understand how they implemented it. A SKView calls -update: on a SKScene. This then first evaluates actions, then simulates physics, and lets the subclass adjust the scene. After changes are made to the scene, SKView finally renders the node tree to the screen.

我不明白的是细节。 Sprite Kit是否通过使用不同的线程或GCD队列将场景计算与场景渲染分离?它是在后台执行所有OpenGL渲染调用,还是在主线程上发生了所有事情? Sprite Kit在后台和主线程之间来回切换,以及如何同步处理场景并渲染场景呢?场景更新花费的时间过长会发生什么?

What I don't understand are the fine details. Does Sprite Kit decouple scene calculations from scene rendering by using different threads or GCD queues? Does it perform all the OpenGL rendering calls in the background, or is everything happening on the main thread? At which points is Sprite Kit switching back and forth between background and main thread, and how does it sync processing the scene with rendering the scene then? What happens when your scene updates take too long?

推荐答案

链接文档中的饼图显示了Sprite Kit的渲染循环。循环的每个部分在主线程上同步执行。 OpenGL渲染由渲染循环结束时的SKView执行。

The pie chart in the linked documentation shows the render loop for Sprite Kit. Each part of the loop is executed synchronously on the main thread. The OpenGL rendering is performed by the SKView at the end of the render loop.

与任何渲染循环非常相似,如果更新部分花费的时间太长,那么帧时间间隔超过了。这将导致帧在下一个vsync上呈现,即帧速率降低。

Much like any render loop, if the update part takes too long, then the frame time interval is exceeded. This will result in the frame being rendered on the next vsync ie a reduction in frame rate.

渲染本身与GLKit一样执行:渲染循环同步到显示刷新率使用 CADisplayLink frameInterval 属性控制每个渲染循环的显示刷新次数。模型更新完成后,然后将GL状态发送到GPU并调用 [EAGLContext presentRenderbuffer:] 以显示SKView渲染缓冲区。循环现在已经完成,并且在显示链接触发之前不会再次启动,表示前一帧已呈现,另一帧可以发送到屏幕。

The rendering itself is performed just as with GLKit: the render loop is synced to the display refresh rate using a CADisplayLink, with the frameInterval property controlling how many display refreshes there are per render loop. Once the model update is complete, the GL state is then sent to the GPU and [EAGLContext presentRenderbuffer:] is called to display the SKView renderbuffer. The loop is now complete, and won't start again until the display link fires, signalling that the previous frame was rendered and another one can be sent to the screen.

渲染是否发生在主线程之外没有记录,也不可能知道,但它只是一个实现细节。如果它确实发生在不同的线程上,那么整个模型树必须在渲染线程中重复,或者足够的状态,以便可以渲染一个帧,同时为下一帧更新模型。最有可能的是它只是在主线程上同步。锁定将毫无意义,如果你要在完成渲染之前阻止主线程访问状态,你可能只是在主线程上进行渲染。

Whether or not the rendering occurs off the main thread is not documented and not possible to know, but it is only an implementation detail. If it did occur on a different thread, then the entire model tree would have to be duplicated in the render thread, or sufficient state so that one frame could be rendered, while the model is updated for the next frame. Most likely it is just synchronous on the main thread. Locking would be fairly pointless, if you are going to prevent the main thread from accessing state until you are done rendering, you might as well just do the rendering on the main thread.

这篇关于Sprite Kit是在后台渲染还是在主线程上渲染?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆