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?
A geometry shader is optional and does not have to be used. Geometry shader invocations take a single Primitive as input and may output zero or more primitives. There are implementation-defined limits on how many primitives can be generated from a single GS invocation.
Neither WebGL nor WebGL2 support geometry shaders.
The Geometry Shader (GS) stage processes entire primitives: triangles, lines, and points, along with their adjacent vertices. It is useful for algorithms including Point Sprite Expansion, Dynamic Particle Systems, and Shadow Volume Generation. It supports geometry amplification and de-amplification.
gl_Position is a special variable that holds the position of the vertex in clip space. Since a vertex shader's main output is the position in clip space, it must always set gl_Position. This vertex shader just transforms each vertex position (by the VP matrix).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With