Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL program/shader uninitialization

Tags:

opengl

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?

like image 211
Danicco Avatar asked Aug 29 '13 16:08

Danicco


People also ask

What is OpenGL Shader Compilation?

OpenGL Shading Language Shader Compilation is the term used to describe the process by which OpenGL Shading Language scripts are loaded into OpenGL to be used as shaders. OpenGL has three ways to compile shader text into usable OpenGL objects. All of these forms of compilation produce a Program Object.

What are the steps in shader and program objects?

Shader and program objects 1 Shader object compilation. The first step is to create shader objects for each shader that you intend to use and compile them. 2 Shader error handling. Compilation may or may not succeed. ... 3 Program setup. ... 4 Before linking. ... 5 Program linking. ... 6 Linking and variables. ... 7 Cleanup. ... 8 Example. ...

What is the name of the GLSL shader's instance name?

The name of the first is "mem", in both GLSL and OpenGL's introspection API. The name of the second is "BlockName2.mem" in the introspection API, but the GLSL shader will call it "instanceName2.mem".

What is a program object in OpenGL?

A program object, or program pipeline object, must be valid to be used in rendering operations. As much of this validity is checked at link-time as possible; however, some of it references the current OpenGL state.


1 Answers

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.

like image 176
derhass Avatar answered Sep 28 '22 10:09

derhass