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 4 of 4

Thread: Exporting Functions within a Shader Program.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    3

    Exporting Functions within a Shader Program.

    Hello all.

    I'm currently trying to minimize Compile time and resources by reusing Shaders. My target is not GLSL 4+ with Subroutines, but, I do want to emulate multiple effects per Shader Program.

    The Setup is:

    - Shader Program
    - Shader1
    - Shader2

    Assume Shader1 has function, "myfunc", and Shader2 wants to use it. Without adding the source code together and compiling it into one Shader Program, how do you 'import' per-se, the function in Shader2? I've seen on the good ol' interwebs that it could be:

    Shader1:

    Code :
    void myfunc() {
      // Do something
    }

    Shader2:

    Code :
     
    void myfunc();
     
    void main() {
      myfunc();
    }

    Then attaching the objects/linking the program. This, however, comes up as an error as:

    error C3002: call to undefined function "void myfunc();"
    Any thoughts as to why this is happening?

    - Dennis

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,105
    how do you 'import' per-se, the function in Shader2
    You can't and OpenGL does not support includes which is really annoying ( I believe DirectX does). I wrote a preprocessor to do my own includes so I can have shared code.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    3
    Hmm.. It's not so much Includes that I want, it's the ability to compile Shaders into Modules, and, like a 'c' program the GLSL tries to emulate, you can export functions and variables between Objects (with the 'external' keyword). The documentation states that including no storage qualifier will result in a global variable (I'm guessing variables that get shared within the program?). If variables are shared within the program, you would think functions would be too. This would save on a lot of source code concatenation magick, resulting in a faster compile (I would assume).

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    3
    I actually ended up getting it to work. I set it up so each Shader Module can have a 'library' of other Modules, and it automatically builds its "main" with the other Shaders functions. Each Shader library has a 'VertexMain_[Name]' and 'PixelMain_[Name]', and the main Shader Module will automatically external reference them. For example:

    Code :
    void VertexMain_Default();
    void VertexMain_Lighting();
     
    void main() {
      VertexMain_Default();
      VertexMain_Lighting();
    }

    Each Shader 'library' is compiled independently and attached to a Main Shader. In the example above, the "Default" Shader (that has a Lighting Library added to it).

    Thank you bearing with my noob-ish beginner mistakes.

Posting Permissions

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