一般狀況下,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();
Descriptionblog
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