简体   繁体   中英

Does OpenGL prevent unnecessary API calls?

Currently I have around 15 render passes in my pipeline. For every pass, I set the correct settings before drawing and reset them afterwards. Those settings include viewport size, depth test on or off, blending function or off, stencil function, stencil operation, and more.

I wonder whether OpenGL is clever enough to ignore API calls which set a state that is already present. Because otherwise I would keep track of the state with a lot of flags and before a render pass only set the state if it is actually necessary.

The short answer: It depends on the driver.

OpenGL itself doesn't do much of anything. It is up to vendors to implement the functions specified by the standard any way they see fit. Do they usually test the existing state to avoid unnecessarily stalling the pipeline? Perhaps, but short of reading advice from vendors or measuring performance yourself, there is no way to know for sure.

The consensus of the advice I have seen (no reference for this as it is spread around all over the place), is that you should avoid calling OpenGL with redundant state changes. It can't do very much harm and it might do some good.

In your case (changing state a few times per frame between passes), it probably won't make much difference.

Because otherwise I would keep track of the state with a lot of flags and before a render pass only set the state if it is actually necessary.

Please note that this might or might be not faster than leaving it as-is. OpenGL is, as others said, only an API specification, leaving the implementation to GPU vendors (or Open Source communities such as Mesa). In general, you should expect every call to produce some result, but if your main concern is performance, the only way to really choose is profiling .

These results may vary from platform to platform or even the version of graphic driver, and sometimes such unsuspected things like running from battery power. Until you measure, you can't say what's a real performance problem in your application.

a lot of these seem to be flags, and it would likely be more expensive to set a flag than to test and set it. so the question would be whether OpenGL is dumb enough to do that rather than clever enough.

for the rest i doubt if the cost of computation, perhaps setting up the transformation matrix would be significant compared to the work of actual rendering, and accordingly not worth optimizing at the library level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM