多个隔离的OpenGL上下文是否会影响性能 [英] Does multiple isolated OpenGL context affect performance

查看:113
本文介绍了多个隔离的OpenGL上下文是否会影响性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的同事正在开发视频渲染引擎.

My co-worker and I are working on a video rendering engine.

整个想法是解析配置文件并将每个帧渲染到屏幕外FBO,然后使用glReadPixel进行视频编码以获取帧渲染结果.

The whole idea is to parse a configuration file and render each frame to offscreen FBO, and then fetch the frame render results using glReadPixel for video encoding.

我们试图通过创建两个分别具有独立OpenGL上下文的线程来优化渲染速度.一个线程渲染奇数帧,另一个线程渲染偶数帧.这两个线程不共享任何gl资源.

We tried to optimize the rendering speed by creating two threads each with an independent OpenGL context. One thread render odd frames and the other render even frames. The two threads do not share any gl resources.

结果非常令人困惑.在我的计算机上,与单线程实现相比,渲染速度有所提高,而在我的伙伴的计算机上,整个速度却下降了.

The results are quite confusing. On my computer, the rendering speed increased compared to our single thread implementation, while on my partner's computer, the entire speed dropped.

我在这里想知道,OpenGL上下文的数量如何影响整体性能.如果它们不共享任何东西,那么创建多个OpenGL线程真的是一个好主意吗.

I wonder here that, how do the amount of OpenGL contexts affect the overall performance. Is it really a good idea to create multiple OpenGL threads if they do not share anything.

推荐答案

上下文切换当然不是免费的.与性能相关的问题几乎总是一样,因此无法用一般术语进行量化.如果您想知道,则需要在您关心的系统上对其进行测量.可能会很昂贵.

Context switching is certainly not free. As pretty much always for performance related questions, it's impossible to quantify in general terms. If you want to know, you need to measure it on the system(s) you care about. It can be quite expensive.

因此,您使用多个上下文会增加一定量的开销.能否得到回报取决于您的瓶颈所在.如果您的GPU已经只有一个CPU线程,那么您将不会真正获得任何收益,因为如果GPU已经完全加载,您将无法更快地完成工作.因此,您会为上下文切换增加开销而没有任何收益,并使整个过程变慢.

Therefore, you add a certain amount of overhead by using multiple contexts. If that pays off depends on where your bottleneck is. If you were already GPU limited with a single CPU thread, you won't really gain anything because you can't get the GPU to do the work quicker if it was already fully loaded. Therefore you add overhead for the context switches without any gain, and make the whole thing slower.

如果您受CPU限制,则使用多个CPU线程可以减少总的经过时间.如果CPU工作的并行化加上同步和上下文切换的额外开销导致净总收益再次取决于您的用例和特定系统.尝试两者并进行测量是唯一要做的事情.

If you were CPU limited, using multiple CPU threads can reduce your total elapsed time. If the parallelization of the CPU work combined with the added overhead for synchronization and context switches results in a net total gain again depends on your use case and the specific system. Trying both and measuring is the only good thing to do.

根据问题描述,您可能还可以使用多线程处理,同时仍然坚持单个OpenGL上下文,并将所有OpenGL调用保持在单个线程中.您可以将其读入PBO(像素缓冲区对象)中,而不是同步使用glReadPixels(),这允许您使用异步读取.这样可以使GPU和CPU的工作更好地分离.如果您还没有这样做,也可以在单独的线程上进行视频编码.这种方法需要一些线程间同步,但是避免使用多个上下文,同时仍然使用并行处理来完成工作.

Based on your problem description, you might also be able to use multithreading while still sticking with a single OpenGL context, and keeping all OpenGL calls in a single thread. Instead of using glReadPixels() synchronously, you could have it read into PBOs (Pixel Buffer Objects), which allows you to use asynchronous reads. This decouples GPU and CPU work much better. You could also do the video encoding on a separate thread if you're not doing that yet. This approach will need some inter-thread synchronization, but it avoids using multiple contexts, while still using parallel processing to get the job done.

这篇关于多个隔离的OpenGL上下文是否会影响性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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