PV227 GPU programming Marek Vinkler Department of Computer Graphics and Design PV227 GPU programming 1 / 14 Geometry shader new programmable stage (optional), between vertex shader and the fragment shader, before the rasterizer, bit different than the other two. PV227 GPU programming 2 / 14 Geometry shader needs input and output format, recieves the assembled primitives (no strip, fan or loop), full knowledge of the primitive. PV227 GPU programming 3 / 14 Input primitive #vertices points 1 lines 2 lines_adjacency 4 triangles 3 triangles_adjacency 6 primitive type must match the draw command, layout ( triangles ) in; Figure: Taken from lighthouse3d.com PV227 GPU programming 4 / 14 Input build-in: array gl_in of input vertices, default vertex attributes. user-defined: same way as usual, array, data for each vertex. 1 in gl_PerVertex 2 { 3 vec4 gl_Position ; 4 f l o a t gl_PointSize ; 5 f l o a t gl_ClipDistance [ ] ; 6 } gl_in [ ] ; / / # of ve rt ic e s : gl_in . length ( ) 7 8 in i n t g l _ P r i m i t i v e I D I n ; 1 in Data 2 { 3 vec3 normal ; 4 } vertexData [ ] ; PV227 GPU programming 5 / 14 Output primitive points line_strip triangle_strip output type need not match the input type, GL_MAX_GEOMETRY_OUTPUT_VERTICES (1024), can output [0, max] primitives, input primitive is discarded, layout ( line_strip , max_vertices = 4) out; PV227 GPU programming 6 / 14 Output outputs vertices, attributes passed the same way as in the vertex shader, vertex definition ended with EmitVertex(); need enough vertices to form primitives, primitive definition ended with EndPrimitive(); PV227 GPU programming 7 / 14 Examples culling, explosion, tesselation, normal visualization. PV227 GPU programming 8 / 14 Culling render only triangles visible from a point, do not emit triangles for the others. Figure: Point view culling PV227 GPU programming 9 / 14 Explosion move vertices along the common triangle normal, color the vertices with R, G, B. Figure: Explosion in t = 0.5f PV227 GPU programming 10 / 14 Tessellation only minor amplification, create new point in the barycenter. Figure: Extruded tessellation in t = 0.5f PV227 GPU programming 11 / 14 Tessellation one new point and three triangles. a b c d Figure: Tessellated triangle. PV227 GPU programming 12 / 14 Tessellation mind the emit order of primitives, must follow the winding order of triangle strips. Figure: Taken from atspace.co.uk PV227 GPU programming 13 / 14 Normal visualization draw lines for normals, visualize both kinds of normals. Figure: Visualized vertex and face normals. PV227 GPU programming 14 / 14