Lesson 9 – Geometry Shaders PV227 – GPU Rendering Jiˇrí Chmelík, Jan ˇCejka Fakulta informatiky Masarykovy univerzity 01. 12. 2015 PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 1 / 15 Geometry Shader new programmable stage (optional), between vertex shader and fragment shader, before the rasterizer. Figure: Taken from lighthouse3d.com PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 2 / 15 Geometry Shader (cont.) needs input and output format, recieves the assembled primitives (no strip, fan or loop), full knowledge of the primitive. PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 3 / 15 Input Types 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 Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 4 / 15 Input Data build-in: array gl_in of input vertices, default vertex attributes. 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 ; user-defined: same way as usual, array, data for each vertex. 1 in Data 2 { 3 vec3 normal ; 4 } vertexData [ ] ; PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 5 / 15 Output Types 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 Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 6 / 15 Output Data 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 Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 7 / 15 Examples culling, explosion, tessellation, normal visualization. PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 8 / 15 Culling render only triangles visible from a point, do not emit triangles for the others. Figure: Point view culling PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 9 / 15 Explosion move vertices along the common triangle normal, color the vertices with R, G, B. Figure: Explosion in t = 0.5f PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 10 / 15 Tessellation only minor amplification, create new point in the barycenter. Figure: Extruded tessellation in t = 0.5f PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 11 / 15 Tessellation (triangle) one new point and three triangles. Figure: Tessellated triangle. PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 12 / 15 Triangle Strips mind the emit order of primitives, must follow the winding order of triangle strips. Figure: Taken from atspace.co.uk PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 13 / 15 Normal Visualization draw lines for normals, visualize both kinds of normals. Figure: Visualized vertex and face normals. PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 14 / 15 Geometry Shaders Today not for tessellation, surpassed by tessellation shaders probably not for culling (not necessary) expanding a point to a quad (particle systems), compete with instancing expanding a line to a quad (grass, hair), in combination with tessellation shaders transform feedback layered rendering: render to cube map, render to stereo buffers instanced geometry shaders PV227 – GPU Rendering (FI MUNI) Lesson 9 – Geometry Shaders 01. 12. 2015 15 / 15