Shader modularity on 4.5 ATI Drivers?

Hi,

is it already possible to use the “modular coding style” for shaders with the current drivers?

I’m asking that because I have this fragment pogram with its main() function that computes per pixel phong lighting. However the diffuse color and the opacity of the fragment is first obtained by the colorization of a greyscale texture. So I thought I could encapsulate both the texture fetch and colorization in a separate function that would be called from the main(). This would allow me to change the colorization function more easily while keeping the main() (that computes the lighting shader) untouched.

So first do you know if this is actually possible with current drivers? And if so, do I have to declare a separate shader object for each routine, one for the main(), one for the colorize() function, and then attach them both to a single program object, and then compile and link them, or do I have to place them both in a same .frag file?

Thank you!

Try it and see.

I think it’s not necessary cause it’s easy to do string operations and build a single file in memory.

But if you want to try, all you need is construct an array of arrays for the program code, and pass that to glShaderSourceARB.

GLcharARB *shaderArray[2];
GLint LengthArray[2];
GLcharARB shader1[100], shader2[100];

shaderArray[0]=shader1;
shaderArray[1]=shader2;
LengthArray[0]=strlen(shader1);
LengthArray[1]=strlen(shader2);

//For 2…
glShaderSourceARB(object, 2, shaderArray, LengthArray);

You can link more than one fragment or vertex shader.

 
FragmentShader1:

vec4 CalcColor()
{
    return vec4(1.0,1.0,1.0,1.0);
}

FragmentShader2:

vec4 CalcColor();

void main()
{
    gl_FragColor = CalcColor();
}
 

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.