6

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
user1139069
  • 1,505
  • 2
  • 15
  • 27
  • http://en.wikipedia.org/wiki/GLSL#A_sample_trivial_GLSL_geometry_shader ? – n0rd Jan 10 '12 at 06:50
  • 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 Jan 10 '12 at 16:39

1 Answers1

7

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Does gsColor need to be array and if I was out added vertices how would the gsColor output match the added vertices? If gsColor should be an array like I'm guessing it should be, would the indexes of that array match the order of the vertices I'm emitting? – user1139069 Jan 13 '12 at 01:01
  • `gl_Position` in the geometry shader isn't an array either. You write each output, then call `EmitVertex` to produce all of those outputs. You set `gsColor` for each vertex you emit, just like you set `gl_Position` for each vertex emitted. – Nicol Bolas Jan 13 '12 at 01:14
  • So something like `gl_Position = gl_in[0].gl_Position; gsColor = vertexColor[0] EmitVertex();gl_Position = gl_in[1].gl_Position; gsColor = vertexColor[1] EmitVertex();` ? – user1139069 Jan 13 '12 at 01:25