PDA

View Full Version : Can you pass an array to a geometry shader?



cmcrook
11-20-2017, 08:02 PM
Can you pass an array of vec2's to a geometry shader as inputs? If so what is the syntax?

mhagain
11-20-2017, 08:31 PM
Tell us what you're trying to do rather than how you're trying to do it. There may be a better way.

cmcrook
11-20-2017, 08:45 PM
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.

GClements
11-20-2017, 11:22 PM
Can you pass an array of vec2's to a geometry shader as inputs?
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[];

Alfonse Reinheart
11-21-2017, 10:38 AM
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.

GClements
11-21-2017, 12:04 PM
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.

cmcrook
11-22-2017, 12:18 AM
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];

Alfonse Reinheart
11-22-2017, 08:14 AM
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?

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]`.

GClements
11-22-2017, 11:57 AM
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?

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.

and:


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.