Queries regarding Geometry Shaders

I am dealing with geometry shaders using GL_ARB_geometry_shader4 extension.

My code goes like :

GLfloat vertices[] =
{
0.5,0.25,1.0,
0.5,0.75,1.0,
-0.5,0.75,1.0,
-0.5,0.25,1.0,
0.6,0.35,1.0,
0.6,0.85,1.0,
-0.6,0.85,1.0,
-0.6,0.35,1.0
};

glProgramParameteriEXT(psId, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);

glProgramParameteriEXT(psId, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);

glLinkProgram(psId);

glBindAttribLocation(psId,0,“Position”);

glEnableVertexAttribArray (0);

glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, vertices);

glDrawArrays(GL_TRIANGLE_STRIP,0,4);
My vertex shader is :

#version 150

in vec3 Position;

void main()
{
gl_Position = vec4(Position,1.0);

}
Geometry shader is :

#version 150
#extension GL_EXT_geometry_shader4 : enable

in vec4 pos[3];

void main()
{
int i;
vec4 vertex;

gl_Position = pos[0];
EmitVertex();

gl_Position = pos[1];
EmitVertex();

gl_Position = pos[2];
EmitVertex();

gl_Position = pos[0] + vec4(0.3,0.0,0.0,0.0);
EmitVertex();

EndPrimitive();

}
Nothing is rendered with this code. What exactly should be the mode in glDrawArrays() ? How does the GL_GEOMETRY_OUTPUT_TYPE_EXT parameter will affect glDrawArrays() ?

What I expect is 3 vertices will be passed on to Geometry Shader and using those we construct a primitive of size 4 (assuming GL_TRIANGLE_STRIP requires 4 vertices). Can somebody please throw some light on this ?

If you do not set GL_GEOMETRY_VERTICES_OUT, then the default is zero and you can’t emit anything. Link should have failed, and the link log should tell you this.

You should also call glBindAttribLocation before glLinkProgram; as it is, you are probably lucky that the attribute is at index zero, but there is no guarantee.

Also, you’re mixing up three different things; EXT_geometry_shader4, ARB_geometry_shader4, and GLSL 150. It is probably simplest to just use the core 150 style: explicitly declare the layouts in the shader and stop using glProgramParameteriEXT.

The mode passed to draw calls obviously has to match GEOMETRY_INPUT_TYPE, as that’s the input to the geometry shader.

You are totally right about glBindAttribLocation(). The problem was weird when I changed vec3 Position to vec4 Position it worked ! No idea why is it so . I will also try to avoid mixing EXT_geometry_shader4, ARB_geometry_shader4, and GLSL 150. That was useful thanks a lot.

[QUOTE=arekkusu;1246010]If you do not set GL_GEOMETRY_VERTICES_OUT, then the default is zero and you can’t emit anything. Link should have failed, and the link log should tell you this.

You should also call glBindAttribLocation before glLinkProgram; as it is, you are probably lucky that the attribute is at index zero, but there is no guarantee.

Also, you’re mixing up three different things; EXT_geometry_shader4, ARB_geometry_shader4, and GLSL 150. It is probably simplest to just use the core 150 style: explicitly declare the layouts in the shader and stop using glProgramParameteriEXT.

[/QUOTE]

You are totally right about glBindAttribLocation(). The problem was weird when I changed vec3 Position to vec4 Position it worked ! No idea why is it so . I will also try to avoid mixing EXT_geometry_shader4, ARB_geometry_shader4, and GLSL 150. That was useful thanks a lot.

Just some clarification so that my previous post doesn’t mislead you, GEOMETRY_OUTPUT_TYPE does not affect the mode passed to draw calls, the input does, namely, it has to be as such:

  • If GEOMETRY_INPUT_TYPE is TRIANGLES, then mode has to be either TRIANGLES, TRIANGLE_STRIP or TRIANGLE_FAN
  • If GEOMETRY_INPUT_TYPE is LINES, then mode has to be either LINES, LINE_STRIP or LINE_LOOP
  • If GEOMETRY_INPUT_TYPE is POINTS, then mode has to be POINTS

Similar rules apply for adjacency primitives.

The only exception is when tessellation is used as then the mode is PATCHES, but the GEOMETRY_INPUT_TYPE can be anything as long as it matches the output type of the tessellation evaluation shader.