What does glUseProgram(0) do?

北城以北 提交于 2019-12-03 12:08:08

问题


The OpenGL docs for glUseProgram claim that calling it with an argument of zero will cause the results of shader execution to be undefined.

However, after a little searching I've seen a couple examples of people using glUseProgram to uninstall the current shader program.

Is this behavior reliable? If not, then what exactly does glUseProgram(0) do?


回答1:


glUseProgram means that the given program object is the current program that will be used for things that use programs (glUniform, rendering commands, etc). 0 is a lot like NULL for OpenGL objects. It represents not an object (for most objects). Therefore, glUseProgram means that no program is current, and therefore no program will be used for things that use programs.

If you attempt to call the glUniform functions when no program is current, they will fail with an error. If you attempt to render when no program is current, one of two things will happen.

  • In OpenGL 3.1+, core profile, you will get a GL_INVALID_OPERATION error, because core OpenGL must render with a program.
  • In compatibility profiles or version 3.0 or less, you will get fixed-function rendering.



回答2:


Contrary to a lot of answers here and elsewhere, glUseProgram(0) is not safe. You can use it to set the rendering state to an invalid program object, but if it's still bound to this when rendering occurs, the behaviour is undefined.

This may be useful to help avoid using the wrong program by mistake, but you should not use it to mean "use fixed-function mode". In most cases this is what happens, but it is not defined by the spec and should not be relied upon.

From the doc:

"If program is zero, then the current rendering state refers to an invalid program object and the results of shader execution are undefined"

Therefore the results are entirely specific to OS, driver and graphics card. In many cases it appears to revert to fixed-function mode. However, it could just as easily keep the last shader, render garbage, or cause a segfault (I've seen this happen).




回答3:


It tells OpenGL to use the fixed-function pipeline.




回答4:


Once you are using glUseProgram(myShader) all subsequent rendering will be done using the shader ID myShader.

If you are calling this each time you have a material change in a modern openGL 3.1+, there is little use of glUseProgram(0).

However, if you are planning to integrate some fixed-function rendering in there, like glVertex3f(..), then glUseProgram(0) will make sure that the last used shader is not active anymore, and it will use the fixed-function pipeline, like glColor3f(...)



来源:https://stackoverflow.com/questions/13546461/what-does-gluseprogram0-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!