Danicco
Danicco

Reputation: 1673

OpenGL program/shader uninitialization

What's the proper way to do this?

I'm doing these steps:

  1. Create Shader(s)
  2. Compile Shader(s)
  3. Create Program
  4. Attach Shader(s) to Program
  5. Link Program
  6. Delete Shader(s)

In http://www.opengl.org/wiki/GLSL_Object it says: You do not have to explicitly detach shader objects, even after linking the program. However, it is a good idea to do so once linking is complete, as otherwise the program object will keep its attached shader objects alive when you try to delete them.

And also from Proper way to delete GLSL shader? says it'll increase the memory if I don't delete the shaders.

So checking on http://www.opengl.org/sdk/docs/man/xhtml/glDetachShader.xml, it says If shader has already been flagged for deletion by a call to glDeleteShader and it is not attached to any other program object, it will be deleted after it has been detached.

So my #6 is useless unless I detach it after right?

Should I detach and delete after the Program has been compiled correctly (to save the memory) or should I detach/delete only when my application is closing down?

Upvotes: 7

Views: 2106

Answers (1)

derhass
derhass

Reputation: 45372

So my #6 is useless unless I detach it after right?

Yes. What the GL does is basically reference counting. As long as some other object is referencing the shader object, it will stay alive. If you delete the object, the actual deletion will be deferred until the last reference is removed.

Should I detach and delete after the Program has been compiled correctly (to save the memory) or should I detach/delete only when my application is closing down?

That is up to you. You can delete it as soon as you don't need it any more. If you do not plan to relink that shader, you can destroy all attached shader objects immediately after the initial link operation. However, shader objects aren't consuming much memory after all (and don't go to the GPU memory, only the final programs will) it is typically not a big deal if you delete them later, or even don't delete them at all, as all the GL resources will be destroyed when the GL context is destroyed (including the case that the application exits). Of course, if you create shaders dynamically at runtime, you should also dynamically delete the old and unused objects to avoid accumulating lots of unused objects and effectively leaking memory/object names and so on.

Upvotes: 7

Related Questions