【OpenGL】glFinish()和glFlush()函數詳解-[轉]


通常情況下,OpenGL指令不是立即執行的。它們首先被送到指令緩沖區,然后才被送到硬件執行。glFinish和glFlush都是強制將命令緩沖區的內容提交給硬件執行。

 

一、glFinish()函數

 

OenGL手冊上關於glFinish:

Name
glFinish — block until all GL execution is complete

C Specification
void glFinish(void);


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.

 

glFinish()將緩沖區的指令立即送往硬件執行,但是要一直等到硬件執行完這些指令之后才返回。

如果直接繪制到前緩沖,那么在你想保存屏幕截圖之前,就需要調用這個函數,確保繪制完畢。

如果使用雙緩沖,則這個函數不會有太大作用。

 

 

二、glFlush()

 

Name
glFlush — force execution of GL commands in finite time

C Specification
void glFlush();

Description

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時,物體會一個一個地出現在屏幕上。但是,如果使用雙緩沖,這個函數將不會有什么影響,因為直到交換緩沖區的時候變化才顯現出來。

 

如果你使用的是雙緩沖,那么可能這兩個函數都不需要用到。緩沖區交換操作會隱式將命令送去執行。

 

三、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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM