61

My code approaches GLSL shader management in the way, that it creates each shader and the associated program and deletes each shader and program. I recently read http://www.opengl.org/wiki/GLSL_Object and there it is stated that:

The shader object, due to being attached to the program object, will continue to exist even if you delete the shader object. It will only be deleted by the system when it is no longer attached to any program object (and when the user has asked to delete it, of course).

Do I get this correctly, if I call glDeleteShader() on the shader object after linking to the program, I only need to track the program? Is it safe to assume this is always true?

rioki
  • 5,988
  • 5
  • 32
  • 55

4 Answers4

71

Yes -- in fact it is highly desireable to detach and delete your shader objects as soon as possible. That way the driver can free up all the memory it is using to hold a copy of the shader source and unlinked object code, which can be quite substantial. Measurements I have done indicate that NOT deleting the shader objects increases the incremental memory use per shader by 5-10x

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 16
    Although what you say is correct according to [the documentation](http://www.opengl.org/sdk/docs/man/xhtml/glLinkProgram.xml) ("After the link operation, applications are free to modify attached shader objects, compile attached shader objects, detach shader objects, delete shader objects, and attach additional shader objects."), I've come across some ill-behaved drivers that do not like it when you detach shaders, particularly on mobile platforms. Since a problem related to this left me frustrated for several days, I'd recommend you NOT detach shaders after a link (deleting is fine). – Logan Pickup Jul 28 '13 at 03:55
  • 4
    Problem is that just deleting the shader without detaching it might not free up the memory -- it might just decrement a reference count that won't go to 0 until you detach the shader. As long as the shader is still attached, the user might call link again to relink the program, so the driver needs to keep around enough info to do that. So you need to trade off memory use with potential problems on broken drivers... – Chris Dodd Aug 28 '13 at 17:33
  • 1
    Most people don't know this, but shader programs remember their uniforms. If you want to take advantage of this to avoid re-uploading your unchanged uniforms to the GPU, you can link multiple shader program with the same shader objects. It's a nice trick for OpenGL ES 2.0 since you don't have the luxury of using UBOs – mchiasson Jan 03 '15 at 04:50
13

In general, the way shader object management works is simple. Shader objects don't really do anything, so there's no point in tracking them at all. Shader objects should exist for just long enough to successfully link a program object. After which time, the shaders should be detached from the program and deleted.

The above assumes that you aren't trying to use the shader object to link with a different program, of course. That's certainly possible. In that case, you should delete your shader objects after you have linked all of your programs.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
8

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

So normally you don't have to ever call glDetachShader(). Read the docs for glDeleteProgram().

vallentin
  • 23,478
  • 6
  • 59
  • 81
user1656730
  • 184
  • 2
  • 6
  • I just realized after reading this I was using glDeleteShader and not glDeleteProgram. Simple things one misses sometimes.. – Mike O Jan 17 '19 at 15:28
6

Yes. You can safely delete the shader then. In fact, this is the preferred way, because you have less maintenance. You need not keep track of what to delete, and you cannot forget to do it. And, it will still work.

"Deleting" the shader, as with all OpenGL objects, merely sets a flag that says you don't need it any more. OpenGL will keep it around for as long as it needs it itself, and will do the actual delete any time later (most likely, but not necessarily, after the program is deleted).

Damon
  • 67,688
  • 20
  • 135
  • 185
  • 1
    In fact you can even detach the shaders after linking (they actually only contain code which then completely goes into the program) so they get deleted immediately. – Christian Rau Feb 02 '12 at 15:00