# Geometry Shader Examples

GS = Geometry Shader

This is a short document about Geometry Shaders in OpenGL.

The first extension to be introduced was GL_EXT_geometry_shader4 on Oct 1, 2007.

Then it became ARB approved GL_ARB_geometry_shader4 on July 8, 2008.

GL_EXT_geometry_shader4 is available on nVidia Gf 8 series and up. Gf 8 is a SM 4.0 GPU

With ATI/AMD, all the Radeons with a HD in their name are SM 4.0 GPUs but their drivers did not support GS.

On June 10, 2009, Catalyst 9.6 gets released and supports various new extensions including GS

http://www.geeks3d.com/20090610/ati-catalyst-96-beta-a-stack-of-new-opengl-extensions

Fact #1 : The current version of GL is 3.1 and GS is not core.

Fact #2 : GL_EXT_geometry_shader4 and GL_ARB_geometry_shader4 both seem like identical extensions.

Here is the specification

http://www.opengl.org/registry/specs/ARB/geometry_shader4.txt

## Introduction

The GS unit comes AFTER the VS unit. In the VS unit, you simply transform vertices, normals, texcoords and whatever else is in the stream. The GS unit has some connectivity information. The GS unit can be used for generating new geometry. For example, if input is a triangle, you can put out a few triangles. If input is a line, you can output a few lines. If input is a point, you can output some points. Once you emit the new extra geometry, it goes down to the rest of the fixed function vertex and primitive processor and reaches the fragment stage for rasterization. The order in which it reaches rasterization is not necessarily predictable, which is a problem if you are using order dependent blending such as glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

In the GS, you can sample a texture, you can transform a vertex with another matrix, with the projection matrix, with modelview matrix ..... you can do whatever you want just like in the other shader units.

## Short Example

The language used here is GLSL.

The language used in the nVidia demo (on nv website!), is the low level GL_NV_geometry_program4.

Since GL_ARB_geometry_shader4 is an extension, you need to put

extension GL_ARB_geometry_shader4 : enable

//GEOMETRY SHADER #version 120 #extension GL_ARB_geometry_shader4 : enable /////////////////////// void main() { //increment variable int i; vec4 vertex; ///////////////////////////////////////////////////////////// //This example has two parts // step a) draw the primitive pushed down the pipeline // there are gl_VerticesIn # of vertices // put the vertex value into gl_Position // use EmitVertex => 'create' a new vertex // use EndPrimitive to signal that you are done creating a primitive! // step b) create a new piece of geometry // I just do the same loop, but swizzle the x and y values // result => the line we want to draw, and the same line, but along the other axis //Pass-thru! for(i = 0; i < gl_VerticesIn; i++) { gl_Position = gl_PositionIn[i]; EmitVertex(); } EndPrimitive(); //New piece of geometry! for(i = 0; i < gl_VerticesIn; i++) { vertex = gl_PositionIn[i]; vertex.z = -vertex.z; gl_Position = vertex; EmitVertex(); } EndPrimitive(); }

The first for loop just emits the incomming primitive unchanged.

For every vertex, we call EmitVertex();

For every primitive, we call EndPrimitive();

Those two are special instructions for the GS unit.

We are then generating a new primitive that is mirrored around the xy-plane.

The example doesn't show it but if you will be using custom varyings, you have to name them something like **varying vec2 GeomTexCoord0** and **varying vec2 FragTexCoord0**

In the vertex shader, since the name must match, you should call it **varying vec2 GeomTexCoord0**

In the fragment shader, **varying vec2 FragTexCoord0**

## Additional Info

The GS unit in current generation of GPUs, such as the Gf 8 is considered too slow to be practical by some people. It is also considered too limited by some because there is a limit to how many new primitives you can emit.

The next generation of GPUs will be more flexible and will support other shader units, such as the tesselation unit which may or may not make the GS unit obsolete.