How to enable geometry shader in OpenGL 4.2?

| | August 6, 2015

I’m porting my Direct3D-based engine to OpenGL and I’m using geometry shaders for rendering text characters (basically, textured billboards).

D3D version works fine, but in OpenGL mode it gives only a flickering point right in the center of the screen.

It seems that somehow the geometry shader stage is not enabled (in AMD GPUPerfStudio the box, representing GS stage, is grayed-out).

I tripple-checked the OpenGL back-end and the shader code but couldn’t find any mistakes.
Maybe, I’m missing something.

Below is the source code of the relevant shaders:

Vertex shader:

in vec4 in_xy_wh;
in vec4 in_tl_br;   // UVs for top left and bottom right corners
out vec4 v_xy_wh;
out vec4 v_tl_br;
void main()
{
    v_xy_wh = in_xy_wh;
    v_tl_br = in_tl_br;
}

Geometry shader:

layout(points) in;
layout(triangle_strip, max_vertices=4) out;

in vec4 v_xy_wh[];
in vec4 v_tl_br[];  // UVs for top left and bottom right corners

out vec2 v_uv;

void main()
{
    vec2    pos = v_xy_wh[0].xy;
    float   width = v_xy_wh[0].z;
    float   height = v_xy_wh[0].w;

    vec2    tl = v_tl_br[0].xy;
    vec2    br = v_tl_br[0].zw;

    gl_Position = vec4( pos.x, pos.y, 0.0f, 1.0f );
    v_uv = vec2( tl.x, tl.y );
    EmitVertex();

    gl_Position = vec4( pos.x + width, pos.y, 0.0f, 1.0f );
    v_uv = vec2( br.x, tl.y );
    EmitVertex();

    gl_Position = vec4( pos.x, pos.y - height, 0.0f, 1.0f );
    v_uv = vec2( tl.x, br.y );
    EmitVertex();

    gl_Position = vec4( pos.x + width, pos.y - height, 0.0f, 1.0f );
    v_uv = vec2( br.x, br.y );
    EmitVertex();

    EndPrimitive();
}

Fragment shader:

in vec2 v_uv;
out vec4 v_color;

uniform sampler2D   s_font;

void main()
{
    vec4 color = texture( s_font, v_uv ).rgba;
    if( color.w < 1.0/255.0 ) {
        discard;
    }
    v_color = color;
}

I’ve added the “#version 420 core” preamble to each shader.
I know I shouldn’t use the glProgramParameteriEXT function to define GL_GEOMETRY_INPUT_TYPE_EXT, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_GEOMETRY_VERTICES_OUT_EXT,
because these parameters are specified in the geometry shader (which is core in OpenGL 4.2).

EDIT:
My mistake was incorrect usage of glVertexAttribPointer (wrong value of the the last parameter).
Thanks for all the answers and sorry for your wasted time!

One Response to “How to enable geometry shader in OpenGL 4.2?”

  1. Geometry shader is only available in GLSL version 1.5 and later, and you need to specify that explicitly in the geometry shader source. I guess it’s #version 400 in your case. Here is the list of GLSL verseions.

Leave a Reply