简体   繁体   English

OpenGL ES 2.0 iPhone - 在后台线程块主线程上渲染

[英]OpenGL ES 2.0 iPhone - Rendering on background thread block main thread

I'm rendering OpenGL Context on a background thread with a different EAGLContext than the main thread. 我在后台线程上使用与主线程不同的EAGLContext渲染OpenGL上下文。

I use something like this: 我使用这样的东西:

- (void)renderInBackground {
      EAGLContext *context = [[EAGLContext] alloc] init];
      [EAGLContext setCurrentContext:context];


       Rendering..
    }

However, even though this is performed in a background thread, when using a heavy shader, the main thread gets blocked and the UI gets stuck. 但是,即使这是在后台线程中执行,当使用重型着色器时,主线程也会被阻塞,并且UI会卡住。

Why is the background thread blocking the main thread? 为什么后台线程阻塞主线程? the methods are not synchronized. 方法不同步。

You do have finite CPU and GPU resources to perform all of the rendering and UI interactions, so if you max out the GPU you will slow down everything else in your application. 您确实拥有有限的CPU和GPU资源来执行所有渲染和UI交互,因此如果您最大化GPU,您将减慢应用程序中的所有其他内容。

That said, you should be able to render on a background thread without completely halting all UI elements. 也就是说,您应该能够在后台线程上呈现而不会完全停止所有UI元素。 I do some fairly intense rendering in my open source Molecules application, all of it using a background GCD queue, yet you can still scroll in popovers and otherwise interact with the interface. 我在我的开源Molecules应用程序中做了一些相当强烈的渲染,所有这些都使用了后台GCD队列,但你仍然可以在弹出窗口中滚动并以其他方式与界面交互。

I describe the process I use in this answer , but the basic setup is a single-wide GCD queue that relies on a dispatch semaphore to prevent the enqueueing of additional rendering frames while one is still being processed. 我在这个答案中描述了我使用的过程,但是基本设置是一个单宽的GCD队列,它依赖于一个调度信号量来防止在一个渲染帧被处理时排队。 Threads and blocks have some overhead to them, so if they are being fired off faster than they can be processed, this can lead to resource exhaustion. 线程和块对它们有一些开销,因此如果它们被触发的速度快于它们可以处理的速度,则可能导致资源耗尽。 The semaphore prevents this. 信号量阻止了这一点。

Wrapping all interactions with my OpenGL ES context in this queue provides lock-free usage of this shared resource, and I see a significant performance boost over simply running this all on the main thread on the multicore devices. 在此队列中包含与我的OpenGL ES上下文的所有交互提供了对此共享资源的无锁使用,并且我发现在多核设备上的主线程上简单地运行此操作会显着提升性能。 As I said, I'm still able to interact with the UI during even heavy rendering here. 正如我所说,在这里,即使是重度渲染,我仍然可以与UI进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM