6
votes

I'm trying to write a simple geometry shader what just passes through vertices before attempting to modify stuff.

My vertex shader is

#version 150 core
in vec3 inPosition;
in vec4 inColor;

out vec4 vertexColor;

void main() {
    vertexColor = inColor;
    gl_Position = vec4(inPosition, 1.0);
}

My geometry shader is

#version 150 core
layout (triangles) in;
layout (triangle_strip,  max_vertices=3) out;

void main() {
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();

    gl_Position = gl_in[1].gl_Position;
    EmitVertex();

    gl_Position = gl_in[2].gl_Position;
    EmitVertex();
    EndPrimitive();
}

And my fragment shader is

#version 150 core
in vec4 vertexColor;
out vec4 fragColor;

void main() {
    fragColor = vertexColor;
}

Without the geometry shader linked in, everything works fine. However when I link in the geometry shader it stops working. What is it that I am missing? Does my Geometry shader require an input for the vertexColor from my vertex shader and if so how is that done?

1
That doesn't answer my question. My geometry shader is exactly the same as the one listed on that page with the exception of the for loop removed. However it doesn't work. Nothing gets drawn. Also the vertex and fragment shaders shown on that page are for glsl 1.20 There is a problem linking all these together that I do not understand nor can I find any information on how to fix.user1139069

1 Answers

7
votes

My geometry shader is exactly the same as the one listed on that page

Yes, but your complimentary vertex and fragment shaders are not.

Information flows through the OpenGL pipeline as follows: First, the vertex shader gets stuff. It passes its outputs to the geometry shader if present. The geometry shader passes its outputs to the fragment shader (after the customary triangle rasteriation, of course). And the fragment shader passes its outputs to the blend stage.

Your vertex shader has two outputs: gl_Position, and vertexColor. Your geometry shader however only takes one input: gl_in[0].gl_Position. This is not legal in GLSL: if one stage outputs a value, the next stage must input it. The only exceptions are for GLSL-defined values like gl_Position, which is consumed by the rasterizer.

Your pass-through GS needs to actually pass the data through if you want it to be pass-through. You need to take the proper input in your GS:

in vec4 vertexColor[];

However, global variables in GLSL can't be named the same. So you can't take vertexColor as an input and as an output. So instead, you have to change the name of the output (or use interface blocks):

out vec4 gsColor;

Your fragment shader must now take in vec4 gsColor; and work with that.

When you attempted to link these shaders, your compiler should have given you an appropriate info-log explaining about the mismatch. Are you getting your info-logs when your shaders fail to link? If not, you should.