Attaching multiple shaders of the same type in a single OpenGL program?

前端 未结 2 573
南笙
南笙 2020-12-13 10:41

In reading the OpenGL specs, I have noticed throughout that it mentions that you can include multiple shaders of the same kind in a single program (i.e. more than one GL_VER

相关标签:
2条回答
  • 2020-12-13 10:54

    I found that including more than one void main() caused linking errors

    For each shader stage there must be only a main entry function.

    a practical example where this is used (pre-GL4)?

    You can declare a function in a shader source and not define it, and at linking time you can provide a definition from another shader source (very similar to c/c++ linking).

    Example:

    generate_txcoord.glsl:

    #version 330
    precision highp float;
    
    const vec2 madd = vec2(0.5, 0.5);
    
    vec2 generate_txcoord(vec2 v)
    { 
      return v * madd + madd; 
    }
    

    vertex.glsl:

    #version 330
    precision highp float;
    
    in vec2 vxpos;
    
    out vec2 out_txcoord;
    
    vec2 generate_txcoord(vec2 vxpos); // << declared, but not defined
    
    void main()
    {
      // generate 0..+1 domain txcoords and interpolate them
      out_txcoord = generate_txcoord(vxpos);
    
      // interpolate -1..+1 domain vxpos
      gl_Position = vec4(vxpos, 0.0, 1.0);
    }
    
    0 讨论(0)
  • 2020-12-13 11:00

    You can put common functions in separate shader. Then compile it only once, and link in multiple programs.

    It's similar how you compile your cpp files only once to get static or shared library. Then you link this library into multiple executable programs thus saving compilation time.

    Let's say you have complex lighting function:

    vec3 ComputeLighting(vec3 position, vec3 eyeDir)
    {
        // ...
        return vec3(...);
    }
    

    Then for each of shader where you want to use this functionality do this:

    vec3 ComputeLighting(vec3 position, vec3 eyeDir);
    
    void main()
    {
        vec3 light = ComputeLighting(arg1, arg2);
        gl_FragColor = ...;
    }
    

    Then you compile separately common shader and your main shader. But do the compilation of common shader only once.

    0 讨论(0)
提交回复
热议问题