Call external shader function and retreive the data

I am wondering if it is possible to call external calculation function from a shader.
I know that it is possible in OpenCL but a never try in OpenGL and i do not know if it is possible.

here an example of what i need.

private static final String[] vertexShaderCode9 = {
void main() {
vec2 coord = vec2(dxaPosition.x, dyaPosition.y);
RGB = texture(uTexture, coord).rgb;
float[10] = call function test(RGB); // that is what i would like to be able to do or some thing else ;))

thanks for the answer


What do you mean by that? Do you mean calling some random CPU code? No, you can’t do that from a shader in OpenGL. Or at least, not without building infrastructure for doing that.

If you mean calling a function defined in a different GLSL file (for some definition of “file”), there are ways to do that.


Yes i want to call a function from another GLSL file, so i could use it in different GLSL (vertex or fragment shader) main() {} .

Every shader compilation function allows you to provide multiple shader strings. These strings are effectively compiled as one textual sequence, in order, front to back.

So you can provide multiple strings, with your “other file” functions in one string, and your main function in another.

Of course, the shader header area (your #version and #extension block) makes this a bit trickey, since your main shader string likely has these, and they have to be the first things in your shader. So you’d have to split the header out into its own string from the rest of the shader text.


I spend fews hours today to anderstand the problem and you are right. The only way is to use many shader strings and build form it the shader source. ;))

Tha best way for doing it should be to make 5 shader strings.

1 the header string and these variable
2 the function variable if necessary
3 the main shader
4 the function traitment incerted in the main shader
5 the rest of the main shader

not very easy to organize if you want many function file ;))

i would habe prefered some GLES20.glCreateShaderFunction(GLES20.GL_Function_SHADER)

and some glAttachShaderfunction(program, function)

I ill think about doing it or not.

Thanks anyway ;))

I didn’t say “only way”; it’s just the simplest way (and the way least likely to encounter driver bugs).

You could link multiple shader objects for the same stage into a single program. This would allow one shader object to have a declaration for a function that is defined in another shader object for that stage. But since this is uncommonly done in the real world, this code path is not routinely exercised on OpenGL implementations. So you could encounter bugs.

You can have multiple shader objects for each stage. Unlike C, you don’t need to provide prototypes for functions defined in one shader and used in another. Variables do need to be declared in each shader which uses them, and you need to ensure that the declarations are consistent.

That’s rigth GClements ;))

i could have only one piéce of source with the variables and the function wo does all the calculation.
So i would need only 3 piéce of code.

1 header shader and shader variables
2 function and function variables
3 the main() shader code.

It looks fine ;)) a little bit of extra works at the begining but usefful at the end ;))

easyer ;))