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: Multiple shader objects in a program

  1. #1
    Intern Newbie
    Join Date
    Jul 2002
    Location
    Guildford, UK
    Posts
    36

    Multiple shader objects in a program

    Hi,

    This one has been bugging me for a while now. In the GL_ARB_shader_objects spec, under 2.14.2 Program Objects, it says

    ----
    It is permissible to attach multiple shader objects of the same type to a single program object, and it is permissible to attach a shader object to more than one program object.
    -----

    The latter half of this is fine (sharing vertex/fragment shaders across multiple programs).

    The former half, though, has got me exercising those little grey cells. So I think I must be missing something fundamental.

    Why would you want to attach two (or more) vertex/fragment shaders to a program object? What purpose does this achieve?

    Are they run on each vertex/fragment in the order in which they are attached?

    Is it for having "combo" effects, such that you don't have to write specialist shaders for each combination; instead just attach different effects?

    What are the performance implications of doing so?

    If anyone can shed any light on this, it'd be appreciated.

    Cheers,


    Mark

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Multiple shader objects in a program

    I think the meaning of this to have a shader object for a function. For example in Vertex Shader one function that translates the gl_vertex to gl_position. I think that you are able to use one seperate shader object for this function and this function can then be called by another shader object.

    Maybe I'm wrong but I think this is the idea behind that.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  3. #3
    Intern Newbie
    Join Date
    Jul 2002
    Location
    Guildford, UK
    Posts
    36

    Re: Multiple shader objects in a program

    Ooh, right, so instead of inlining functions other than 'main' in a single shader object, you separate them (hence allowing code sharing) into different source files and hence different shader objects.

    There's no #include preprocessor directive in the language so I guess you won't need prototypes... and I guess any missing functions would be picked up as a linkage error.

    That's a rather plausible explanation. Thanks!


    Mark

  4. #4
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Multiple shader objects in a program

    Yeah, I just tried that with the following shaders:

    // Test Vertex Shader 1

    vec4 Translate();

    void main()
    {
    gl_Position = Translate();
    }

    // Test Vertex Shader 2

    vec4 Translate()
    {
    return gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    // Test Fragment Shader

    void main()
    {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }


    It works without problems with ATI 6.14.10.6404 on a R350.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  5. #5
    Intern Newbie
    Join Date
    Jul 2002
    Location
    Guildford, UK
    Posts
    36

    Re: Multiple shader objects in a program

    Cool.

    Do you need the

    vec4 Translate();

    prototype at the top of vertex shader 1?

    Is there a compiler error if you omit it?


    Mark

  6. #6
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Multiple shader objects in a program

    Yeah, you have to define the function.
    Without that the shader won't be compiled. Info Log:


    ERROR: 0:7: 'Translate' : no matching overloaded function found
    ERROR: 0:7: 'assign' : cannot convert from 'const float' to 'Position 4-component vector of float'
    ERROR: 2 compilation errors. No code generated.

    [This message has been edited by Corrail (edited 12-16-2003).]
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,576

    Re: Multiple shader objects in a program

    There isn't a #include header definition, but you are allowed to pass multiple strings in to the shader compiler for a single shader. That way, you can create standard includes that you can share between different shaders that are going to be linked together.

Posting Permissions

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