【OpenGL】glFinish()和glFlush()函数详解-[转]

一般状况下,OpenGL指令不是当即执行的。它们首先被送到指令缓冲区,而后才被送到硬件执行。glFinish和glFlush都是强制将命令缓冲区的内容提交给硬件执行。函数

 

1、glFinish()函数性能

 

OenGL手册上关于glFinish:ui

Name
glFinish — block until all GL execution is complete

C Specification
void glFinish(void);
this


Description

glFinish does not return until the effects of all previously called GL commands are complete. Such effects include all changes to GL state, all changes to connection state, and all changes to the frame buffer contents.

Notes
glFinish requires a round trip to the server.

Errors
GL_INVALID_OPERATION is generated if glFinish is executed between the execution of glBegin and the corresponding execution of glEnd.

spa

 

glFinish()将缓冲区的指令当即送往硬件执行,可是要一直等到硬件执行完这些指令以后才返回。.net

若是直接绘制到前缓冲,那么在你想保存屏幕截图以前,就须要调用这个函数,确保绘制完毕。线程

若是使用双缓冲,则这个函数不会有太大做用。调试

 

 

2、glFlush()server

 

Name
glFlush — force execution of GL commands in finite time

C Specification
void glFlush();

Description
blog

Different GL implementations buffer commands in several different locations, including network buffers and the graphics accelerator itself. glFlush empties all of these buffers, causing all issued commands to be executed as quickly as they are accepted by the actual rendering engine. Though this execution may not be completed in any particular time period, it does complete in finite time.

Because any GL program might be executed over a network, or on an accelerator that buffers commands, all programs should call glFlush whenever they count on having all of their previously issued commands completed. For example, call glFlush before waiting for user input that depends on the generated image.

Notes
glFlush can return at any time. It does not wait until the execution of all previously issued GL commands is complete.

Errors
GL_INVALID_OPERATION is generated if glFlush is executed between the execution of glBegin and the corresponding execution of glEnd.

 

glFlush()清空缓冲区,将指令送往缓硬件当即执行,可是它是将命令传送完毕以后当即返回,不会等待指令执行完毕。这些指令会在有限时间内执行完毕。

若是直接绘制到前缓冲,那么OpenGL的绘制将不会有任何延迟。设想有一个复杂的场景,有不少物体须要绘制。当调用glFlush时,物体会一个一个地出如今屏幕上。可是,若是使用双缓冲,这个函数将不会有什么影响,由于直到交换缓冲区的时候变化才显现出来。

 

若是你使用的是双缓冲,那么可能这两个函数都不须要用到。缓冲区交换操做会隐式将命令送去执行。

 

3、glFinish和glFlush的区别

 

看起来这两个函数很类似,可是仍然是有区别的。

通常,使用glFlush的目的是确保在调用以后,CPU没有OpenGL相关的事情须要作-命令会送到硬件执行。调用glFinish的目的是确保当返回以后,没有相关工做留下须要继续作。

 

glFinish会形成性能降低

若是调用glFinish,一般会带来性能上的损失。由于它会是的GPU和CPU之间的并行性丧失。

通常,咱们提交给驱动的任务被分组,而后被送到硬件上(在缓冲区交换的时候)。若是调用glFinish,就强制驱动将命令送到GPU。而后CPU等待直到被传送的命令所有执行完毕。这样在GPU工做的整个期间内,CPU没有工做(至少在这个线程上)。而在CPU工做时(一般是在对命令分组),GPU没有工做。所以形成性能上的降低。

所以,应该尽可能减小使用此函数。此函数的一个应用是:调试bug。若是我传输到硬件的某条命令形成了GPU的崩溃,找出使得GPU崩溃的那条指令的简单方法是在每一个绘制操做以后调用这个函数。这样就能够准确找出形成崩溃的命令。

另外,Direct3D不支持Finish概念。

 

转自:http://blog.csdn.net/xiajun07061225/article/details/7756187

相关文章
相关标签/搜索