openglglsljoglvertex-shadergeometry-shader

How do you move variables from Vertex shader to Geometry shader?


I have the following Vertex shader:

#version 330

layout (location = 0) in ivec4 inOHLC;
layout (location = 1) in int inVolume;
layout (location = 2) in int inTimestamp;

out ivec4 outOHLC;
out int outVolume;
out int outTimestamp;

void main()
{
    outOHLC = inOHLC;
    outVolume= inVolume;
    outTimestamp = inTimestamp;
}

And I want to receive outOHLC, outVolume and outTimestamp in the Geometry shader. I code this:

#version 330

in ivec4 inOHLC;
in int inVolume;
in int inTimestamp;

layout (line_strip, max_vertices = 2) out;

void main() {  

    float x = (float)inTimestamp / 100.0;
    float y1 = (float)inOHLC[1] / 100.0;
    float y2 = (float)inOHLC[1] / 100.0;
    gl_Position = vec4(x, y1, 0.0, 0.0);
    EmitVertex();    
    gl_Position = vec4(x, y2, 0.0, 0.0);
    EmitVertex();    
    EndPrimitive();

}  

But I get the following error:

run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log: 
ERROR: 7:1: 'inOHLC' : geometry shader input varying variable must be declared as an array
ERROR: 8:1: 'inVolume' : geometry shader input varying variable must be declared as an array
ERROR: 9:1: 'inTimestamp' : geometry shader input varying variable must be declared as an array
ERROR: 15:1: ')' : syntax error syntax error

Following @Rabbid76 comment, I have modified the code as follows:

#version 330

in ivec4 ohlc[];
in int volume[];
in int timestamp[];

layout (line_strip, max_vertices = 2) out;

void main()
{

    float x = (float)timestamp / 100.0;
    float y1 = (float)ohlc[1] / 100.0;
    float y2 = (float)ohlc[2] / 100.0;
    gl_Position = vec4(x, y1, 0.0, 0.0);
    EmitVertex();    
    gl_Position = vec4(x, y2, 0.0, 0.0);
    EmitVertex();    
    EndPrimitive();

} 

And now I get just one error:

run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log: 
ERROR: 40:1: ')' : syntax error syntax error

Which I guess is related to either the (float) cast (does that even exist in GLSL?) or the float variable definitions.


Solution

  • Read the message carefully:

    geometry shader input varying variable must be declared as an array

    The input to the Geometry shader is a primitives. That means all the outputs of the vertex shader which form a primitive are composed. Thus the inputs of the geometry shader are arrays:

    in ivec4 inOHLC[];
    in int inVolume[];
    in int inTimestamp[];
    

    Furthermore you have to specify the input primitive type to the geometry shader. e.g for a line:

    layout(lines​) in;
    

    The primitive type lines implies that the geometry shader receives 2 vertices (array size of inputs is 2).


    GLSL does not have a cast operator like C. If you want to convert an int to float, then you have to construct an new float. The overloaded float constructor accepts an int argument:

    float y1 = (float)ohlc[1] / 100.0;

    float y1 = float(ohlc[1]) / 100.0;
    

    Indices start at 0 not at 1 (like in C, C++, C#, Java, JavaScript, Python, ...):

    float y1 = float(ohlc[0]) / 100.0;
    float y2 = float(ohlc[1]) / 100.0;