问题描述
我无法区分调用 glFlush()
和 glFinish()
之间的实际区别.
I'm having trouble distinguishing the practical difference between calling glFlush()
and glFinish()
.
文档说 glFlush()
和 glFinish()
会将所有缓冲操作推送到 OpenGL,以便可以确保它们都将被执行,区别在于glFlush()
立即返回,因为 glFinish()
阻塞,直到所有操作完成.
The docs say that glFlush()
and glFinish()
will push all buffered operations to OpenGL so that one can be assured they will all be executed, the difference being that glFlush()
returns immediately where as glFinish()
blocks until all the operations are complete.
阅读定义后,我想如果我要使用 glFlush()
,我可能会遇到向 OpenGL 提交的操作多于它可以执行的问题.所以,为了尝试,我将 glFinish()
换成了 glFlush()
并且瞧,我的程序运行了(据我所知),完全相同的;帧率、资源使用情况,一切都一样.
Having read the definitions, I figured that if I were to use glFlush()
that I would probably run into the problem of submitting more operations to OpenGL than it could execute. So, just to try, I swapped out my glFinish()
for a glFlush()
and lo and behold, my program ran (as far as I could tell), the exact same; frame rates, resource usage, everything was the same.
所以我想知道这两个调用之间是否有很大区别,或者我的代码是否使它们运行没有什么不同.或者应该在哪里使用一个而不是另一个.我还认为 OpenGL 会有一些像 glIsDone()
的调用来检查 glFlush()
的所有缓冲命令是否完整(所以一个不t 将操作发送到 OpenGL 的速度比它们可以执行的速度快),但我找不到这样的功能.
So I'm wondering if there's much difference between the two calls, or if my code makes them run no different. Or where one should be used vs. the other.I also figured that OpenGL would have some call like glIsDone()
to check whether or not all the buffered commands for a glFlush()
are complete or not (so one doesn't send operations to OpenGL faster than they can be executed), but I could find no such function.
我的代码是典型的游戏循环:
My code is the typical game loop:
while (running) {
process_stuff();
render_stuff();
}
推荐答案
注意这些命令在 OpenGL 的早期就存在.glFlush 确保以前的 OpenGL 命令必须在有限的时间内完成(OpenGL 2.1 规范,第 245 页).如果直接绘制到前端缓冲区,这将确保 OpenGL 驱动程序开始绘制时不会有太多延迟.当您在每个对象之后调用 glFlush 时,您可以想象一个复杂的场景,在屏幕上一个接一个地出现.但是,当使用双缓冲时,glFlush 实际上根本没有任何影响,因为在您交换缓冲区之前更改将不可见.
Mind that these commands exist since the early days of OpenGL. glFlush ensures that previous OpenGL commands must complete in finite time (OpenGL 2.1 specs, page 245). If you draw directly to the front buffer, this shall ensure that the OpenGL drivers starts drawing without too much delay. You could think of a complex scene that appears object after object on the screen, when you call glFlush after each object. However, when using double buffering, glFlush has practically no effect at all, since the changes won't be visible until you swap the buffers.
glFinish 直到之前发出的命令的所有效果 [...] 完全实现后才会返回.这意味着您的程序的执行会在这里等待,直到绘制了每个最后一个像素并且 OpenGL 无事可做.如果直接渲染到前端缓冲区,glFinish 是在使用操作系统调用截取屏幕截图之前进行的调用.它对于双缓冲的用处要小得多,因为您看不到强制完成的更改.
glFinish does not return until all effects from previously issued commands [...] are fully realized. This means that the execution of your program waits here until every last pixel is drawn and OpenGL has nothing more to do. If you render directly to the front buffer, glFinish is the call to make before using the operating system calls to take screenshots. It is far less useful for double buffering, because you don't see the changes you forced to complete.
因此,如果您使用双缓冲,您可能既不需要 glFlush 也不需要 glFinish.SwapBuffers 隐式地将 OpenGL 调用定向到正确的缓冲区,没有必要先调用 glFlush.并且不要介意强调 OpenGL 驱动程序:glFlush 不会因太多命令而窒息.不能保证此调用立即返回(无论这意味着什么),因此它可能需要任何时间来处理您的命令.
So if you use double buffering, you probably won't need neither glFlush nor glFinish. SwapBuffers implicitly directs the OpenGL calls to the correct buffer, there's no need to call glFlush first. And don't mind stressing the OpenGL driver: glFlush will not choke on too many commands. It is not guaranteed that this call returns immediately (whatever that means), so it can take any time it needs to process your commands.
这篇关于opengl:glFlush() 与 glFinish()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!