GLSL reusable / shared functions, common constants (OpenGL ES 2.0)?

Short:

Is it possible to define a function that each shader can use? Or should I define it for each shader?


The whole story:

Thus, the ramps must be defined as ONCE per shader , and the function must be defined all at once , which each shader can use safely.

I have algorithms, the question is about sharing functions and defining constants in GLSL.

Is it possible? Or do I need to copy a function to each shader? Is there any precompilation option?

+5
source share
1 answer

You can do it the same way as in C - you declare functions in the headers and define them in a common C file.

GLSL :

  • () ( COMMON):

    float getCommonValue() { return 42; }
    
  • , , ( SHADER1):

    float getCommonValue();
    void main() { gl_Color = vec4(getCommonValue(), 0, 0, 0); }
    
  • glCompileShader COMMON shader GLuint -

  • glLinkProgram SHADER1, glAttachShader - COMMON SHADER1. , getCommonValue .

  • COMMON shader GLuint sahder (SHADER1, SHADER2,...).

+9

All Articles