Const data from vertex shader to fragment shader

I have a table (vec3 xx[100]) that is created in the vertex shader, which I want to be available in the fragment shader. I do not need any interpolation of it, as it is constant.

I could compute it outside of the vertex shader, as a uniform, but I would prefer to have the vertex shader compute it. It is the second stage in a deferred shader, so there are very few vertices.

How do you send data of this type from the vertex shader to the fragment shader?

I think you can safely define the array in both stages as const as the compiler will probably optimize this kind of code.

I tried to define “vec xx[100];” in the vertex shader and in the fragment shader. But the fragment shader will always get zeroes only, no matter what I do.

Trying with a simple “float x=1.0;” in the vertex shader, and a “float x;” in the fragment shader, doesn’t work either.

For variable scoping, the specification says:

If it is declared outside all function definitions, it has global scope, which starts from where it is declared and persists to the end of the shader it is declared in.

That would mean the variable is only available in the current shader, not the other ones. There is another comment in the specification:

(Globals forming the interface between two different shader languages are discussed in other sections.)

That would indicate that there is a way to share globals between vertext and fragment shaders, but I can’t find it.

The only way to pass data, created in the vertex shader, to the fragment shader is varyings. The ‘flat’ qualifier ensures that there’s no interpolation.

I have several objections, though.

  1. How can data that is being created in the vertex shader be constant? Is it a constant table per vertex that you expect?
    If you have 3 tables per triangle then, which one should be passed to the fragment shader?

  2. Passing 100 vec4’s to the fragment shader will probably exceed the existing varying limit

  3. If all you want is to create the data on the GPU and keep it there, consider Transform Feedback or Render-to-PBO. This in turn allows you to access the generated data in the fragment shader in a variety of ways, like: via Uniform Buffer Objects, via Textures, via Texture Buffer Objects or via direct memory access (shader image load/store mechanics)

Option 3, or a variant thereof, seems the way to go for me.

Thank you for the clarification!

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