Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Shader subroutines: when to call glGetSubroutineUniformLocation()?

  1. #1
    Intern Contributor
    Join Date
    Sep 2012
    Posts
    76

    Shader subroutines: when to call glGetSubroutineUniformLocation()?

    Greetings:
    I am learning shader subroutines and wrote a toy program along the lines of one in the shading language cookbook (I also referred to the recent red book edn.). The program seems to run fine.

    In the application program in the initialization routine, I get my two subroutine function indexes with two calls to glGetSubroutineIndex(). Then, in the drawing routine:

    glUniformSubroutinesuiv( GL_VERTEX_SHADER, 1, &firstSubroutineIndex);
    // Enable vertex attribute arrays and drawing statement for object1

    glUniformSubroutinesuiv( GL_VERTEX_SHADER, 1, &secondSubroutineIndex);
    // Enable vertex attribute arrays and drawing statement for object2

    So, here's my question:
    I don't seem to need glGetSubroutineUniformLocation(), which the cookbook makes no use of, but which the RB seems to indicate as needed. What gives? Do we need the subroutine uniform location or not? Maybe for some cases we do...

    Thanks in advance for your clarification,
    Sam

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    glGetSubroutineUniformLocation is used to know which index is assoicate with which subroutine uniform. The first index is not necessarily the index of the first subroutine uniform. If I remember correctly nVidia do them in order and AMD do them reverse order (but it can change at any time). If you only have one subroutine uniform then you don't have a problem knowing the index.

  3. #3
    Intern Contributor
    Join Date
    Sep 2012
    Posts
    76
    Thanks, tonyo_au.
    I think I see what you mean and there's a bit in the RB which seems to indicate something similar. But I have a follow-up question in that case.

    So, a subroutine can be of multiple types. Here's the relevant extract from the RB:

    subroutine void Type_1();
    subroutine void Type_2();
    subroutine void Type_3();

    subroutine (Type_1, Type_2) Func_1();
    subroutine (Type_1, Type_3) Func_2();

    subroutine uniform Type_1 func_1;
    subroutine uniform Type_2 func_2;
    subroutine uniform Type_3 func_3;

    So, in this case if I want to call subroutine Func_1, I need to specify if Type_1 (uniform variable func_1) or Type_2 (uniform func_2). Therefore, I need to set the uniform as well, not just the subroutine index. Makes sense.

    But glGetSubroutineUniformLocation() only gets a uniform location. But how do I set it? In other words, e.g., above how would I specify subroutine Func_1 of, say, Type_1?

    Thanks again.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    Neither `Func_1` nor `Type_1` are uniforms. The uniforms here are `func_1`, `func_2`, and `func_3`. `glGetSubroutineUniformLocation` retrieves the index in the array of values for that particular uniform. The location is just the index, not the actual subroutine. Just like for regular uniforms, the location is the value you need to specify the uniform's actual data, not the data itself.

    Setting those uniforms requires getting the uniform locations, then building an array, where at each location you specify the proper value. Then you use `glSubroutineUniforms` to set those uniforms.

    If you have explicit_uniform_location, you can set subroutine indices and subroutine uniform locations manually in the shader, so you don't have to query them.

  5. #5
    Intern Contributor
    Join Date
    Sep 2012
    Posts
    76
    Thank you, Alfonse.
    However, I can't find the command glSubroutineUniforms() in the 4.x docs. Can you say again how to set a uniform?

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    It's the function you mentioned: glUniformSubroutine. I just wrote it backwards.

  7. #7
    Intern Contributor
    Join Date
    Sep 2012
    Posts
    76
    Thanks again, Alfonse.
    I was actually confused by your initial response but that was because I was missing the point that glUniformSubroutine() takes as parameter an array indexed by subroutine uniforms with values equal to the chosen subroutine index. The example at the bottom of the RB page also clarifies this.

Posting Permissions

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