1
\$\begingroup\$

I have a constant for defining an array in the vertex shader. I want to use this constant also in the fragment shader to define another array of the same size. Naive attempts fail (simply use the constant of the vertex shader in the fragment shader or using a #define from the vertex shader). Is this somehow possible?

The only idea I can come up with is using something like an include statement, which seems to be added (source), but I didn't yet find the time to implement this.

Thank you in advance!

\$\endgroup\$
2
\$\begingroup\$

There is a reason why glShaderSource and glCompileShaderProgram both take an array of strings. It's so that you can effectively insert code before your actual shader text or otherwise implement your own "include" system.

The issue here is that you now need to break your shaders down into pieces:

  1. The header preamble. The first thing in the first string needs to be a version declaration. Following that should be any extensions of note.
  2. Any collective definitions, like the ones you're trying to add to the shader.
  3. Your actual shader text.
\$\endgroup\$
1
\$\begingroup\$

Vertex and fragment shaders are two separate programs that are compiled independently of each other. Any definitions that you want to use in both shaders have to be included in the source of both shaders; definitions do not carry over from one shader to another.

I assume most developers implement some kind of include system - whether it's by using the ARB_shading_language_include extension, or by passing shader source files through the C preprocessor before compiling them, or some other method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.