Can you have multiple pixel (fragment) shaders in the same program?

11,223

Solution 1

You can do do this, e.g. by doing function calls from the main entrypoint to functions that are implemented in the various shader objects.

main() {
    callToShaderObject1()
    callToShaderObject2()
}

each of those callToShaderObject functions can live in different shader objects, but all objects have to be attached and linked in the same program before it can be used.

Solution 2

They can't run at the same time, but you are free to use different shaders for different geometry, or to render in multiple passes using different shaders.

Share:
11,223
Harry
Author by

Harry

Updated on June 05, 2022

Comments

  • Harry
    Harry almost 2 years

    I would like to have two pixel shaders; the first doing one thing, and then the next doing something else. Is this possible, or do I have to pack everything into the one shader?

  • Harry
    Harry about 14 years
    So would that require a different program? So a different program for each pass? As in, I could have a vertex and fragment shader on one program (which is one pass?) and then have a vertex and fragment shader on another program (the second pass?)? Can I just change glUseProgram() when I feel like it? So I'm essentially rendering a scene twice...?
  • Michael Daum
    Michael Daum about 14 years
    Exactly right. Just like a multi-pass render in fixed pipeline, but instead of changing openGL settings between passes, you're loading different programs.
  • Engineer
    Engineer almost 12 years
    Out of curioisity, why would you do this, as opposed to simply having all functions in the fragment shader in question? Is this so that code could be shared with other shader programs, without necessitating duplication?
  • Bahbar
    Bahbar almost 12 years
    @Nick: I've not seen it used myself. I was surprised when I first read the spec at the idea to build shader programs out of shader objects - hence why I know you can do it. It was built from the standard compile model...
  • Engineer
    Engineer over 11 years
    Update: I've seen this in use now, while studying modded MineCraft shaders. In that case, it was quite useful as each shader has a lot of options that may be turned on and off, with various effects operating in conjunction with various others. In a scenario like that, and where multiple authors are involved, it becomes a lot more useful to separate functions into separate files.
  • wedesoft
    wedesoft over 2 years
    Note that you need to declare the function header in the shader were it is used.