The right way to remove GLSL shaders?

My code approaches the management of GLSL shaders in such a way that it creates every shader and its associated program and deletes each shader and program. I recently read http://www.opengl.org/wiki/GLSL_Object and it says that:

The shader object associated with the program object will continue to exist even if you delete the shader object. This will be the remote system when it is no longer tied to any program object (and when the user asked to delete it, of course).

Do I get it right if I call glDeleteShader() on a shader object after a link to a program, I only need to track the program? Is it possible to assume that this is always true?

+47
opengl glsl
02 Feb 2018-12-12T00:
source share
4 answers

Yes - in fact, it is very advisable to detach and remove your shader objects as soon as possible. Thus, the driver can free up all the memory it uses to store a copy of the shader source and unrelated object code, which can be quite significant. The measurements I performed indicate that NOT deleting shader objects increases the use of incremental memory in each shader by 5-10x

+56
Feb 02 '12 at 17:58
source share

In general, the way to manage shader objects is simple. Shader objects actually do nothing, so it makes no sense to track them at all. Shader objects must exist long enough to successfully associate a program object. After this time, the shaders must be separated from the program and removed.

The above assumes that you are not trying to use a shader object to communicate with another program, of course. This is of course possible. In this case, you should delete your shader objects after you have linked all your programs.

+13
Feb 02 '12 at 15:57
source share

Yes. Then you can safely remove the shader. This is actually the preferred method because you have less maintenance. You do not need to keep track of what needs to be removed, and you cannot forget to do it. And it will work anyway.

The "removal" of the shader, as with all OpenGL objects, simply sets a flag that says that you no longer need it. OpenGL will support it until it is needed, and will perform the actual uninstall at any time later (most likely, but not necessarily, after uninstalling the program).

+6
Feb 02 2018-12-12T00:
source share

In short: after glLinkProgram() call glDeleteShader() for each shader, this marks them for deletion, and when the program is no longer needed, a call to glDeleteProgram() - this call not only removes the program, but also separates all the shaders attached to it and removes it them (if not used by any other program).

Thus, you should usually not call glDetachShader() . Read the docs for glDeleteProgram() .

+3
Nov 27 '13 at 16:22
source share



All Articles