Can you pass an array to a geometry shader?

Can you pass an array of vec2’s to a geometry shader as inputs? If so what is the syntax?

Tell us what you’re trying to do rather than how you’re trying to do it. There may be a better way.

I am trying to render perlin noise to a texture. Currently I have a vertex shader with two inputs: one is the upper left corner vertex and an array of 4 vec2’s that are the gradients at each corner of the cell. I need to pass the 4 vec2’s through the geometry shader to the fragment shader to calculate the brightness of that fragment.

Not directly, but you can have an array of vec2 inside an interface block which is (must be) declared as an array when used as a geometry shader input. E.g.

// Vertex shader
out corners
  vec2 gradients[4];
} vs_out;

//Geometry Shader
in corners
  vec2 gradients[4];
} gs_in[];

Not directly

I have seen nothing in the GLSL specification to corroborate this. Or at least, not recent ones. This may have been true in 3.2 when GS’s came out, but at least since 4.3 (with arrays of arrays) it is very much not true.

Good point. Although unless you need at least 4.3 for other reasons, I wouldn’t require it simply to avoid using an interface block.

My understanding is that prior to 4.3 (or ARB_arrays_of_arrays), the prohibition on multi-dimensional arrays includes the case where the extra dimension is required for geometry or tessellation shaders.

I am currently using 4.4 and it seems that it is possible with the following syntax. Although I am puzzled that I wasn’t able to find this in the documentation anywhere, is it not up to date?

//Vertex Shader
out vec2 gradients[4];

//Geometry shader
in vec2 gradients[][4];

In what documentation? The specification makes it clear that the inputs are arrayed. What more does it need to say? An array of int is an int[]. An array of vec2[4] is a vec2[][4].

It doesn’t appear to give an example of an unsized multi-dimensional array. However, §4.1.9 (Arrays) says:

An array of arrays can be declared as

vec4 a[3][2]; // size-3 array of size-2 array of vec4

which declares a one-dimensional array of size 3 of one-dimensional arrays of size 2 of vec4s.


For unsized arrays, only the outermost dimension can be lacking a size.

It should also be legal to use a sized array, e.g. assuming that the input type is points:

layout(points) in;
in vec2 gradients[1][4];

Note that multi-dimensional arrays require GLSL 4.3 or the ARB_arrays_of_arrays extension. An arrayed interface block should work with any version which supports geometry shaders.