Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Does glDeleteProgram free shaders?

  1. #1
    Intern Newbie
    Join Date
    Jul 2013

    Does glDeleteProgram free shaders?

    In my program I have a function to compile shaders which creates each shader index variable locally to be binded to the program object which it returns like so:

    Code :
    	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    	// compile each shader
    	programObject = glCreateProgram();
    	glAttachShader(programObject, vertexShader);
    	glAttachShader(programObject, fragmentShader);
    	glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
    	if(!linked) return 0;
    	return programObject;

    So that I can then do:

    Code :
    int menuShader = 0;
    short initMenu(void) {
    	menuShader = compileShaders(...);
    void renderMenu(void) {
    	// render

    Now to deinit the menu, is calling:

    Code :
    glDeleteProgram(1, menuShader);

    Enough? Will it free my vertex and fragment shaders as well or just free the program object? If not, does that mean that I have to make vertexShader and fragmentShader global variables so that I can free them as well in the deinit routine?

  2. #2
    Member Regular Contributor
    Join Date
    Dec 2009
    The shader objects are needed only for linking the program, you can delete them after you linked the program.

  3. #3
    Advanced Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    To be more precise: the shader object contains the raw text you passed in. The program object contains the compiled executable corresponding to that text.

    These objects are all ref-counted. When you create a shader, the initial refcount is one. When you attach a shader to a program, the refcount is now two. So, deleting the program decrements the refcount because the attachment is going away, but the shader won't be deleted until the refcount goes to zero, i.e. you must also delete the shader object yourself.

    If you want to ensure that all shader objects are deleted, you should detach and delete shader objects after linking the program. Merely deleting the shader isn't enough because the attachment still maintains a refcount (you can still glGetProgram(...GL_ATTACHED_SHADERS...) for example.)

    Or to simplify this whole process, use SSO's glCreateShaderProgram. See what that pseudo code does?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts