GEOMETRY SHADER
Breakdown Basics Review – graphics pipeline and shaders What can the geometry shader do? Working with the geometry shader GLSL example Examples Fur, Fins Particles
Recommended Reading Geometry shader is pretty new concept Real-Time Rendering mentions in passing Course text has no real discussion Do some searching online if you are interested in learning more
Basics
Review – Graphics Pipeline
Review – What are Shaders? Shaders are small programs that run on the GPU Shaders allow us to implement effects that we may wish for in our rendered scene Lighting, texturing being the most basic Three types of shader Vertex Geometry Fragment (or pixel)
What is the Geometry Shader? The geometry shader is the stage that occurs directly after the vertex shader stage The geometry shader works with output from the vertex shader in the form of geometry objects Points, lines, triangles Can also include adjacency data
What Can the Geometry Shader Do? Geometry shader can manipulate a whole piece of geometry No longer manipulation of a single vertex Geometry shader can also add geometry No longer single vertex in, single vertex out Combining these two features allows manipulation of your geometry in a new manner And can create some neat effects
Example
Geometry Shader Input The input into a geometry shader comes in the form of pieces of geometry Points, lines, triangles, quads The geometry shader may also take in an adjacency block Vertices adjacent to the piece of geometry The piece of geometry is basically an array of vertex positions
Geometry Shader Output The geometry shader outputs a strip of the defined geometry type e.g. triangles in, triangle strip out The output operates by having a few extra functions in GLSL EmitVertex() – emits a vertex to the next stage EndPrimitive() – ends the vertex creation for the particular primitive Possible to restrict the number of output vertices
Stream Output It is also possible to output the data from the geometry shader stage of the pipeline Output from the geometry shader can be used for further rendering if required Useful for letting the GPU do point physics calculations Each vertex has a position, velocity, acceleration, mass, etc. Output is the new position, velocity
Questions?
Working with the Geometry Shader
GLSL Example Geometry Shader #version 330 compatibility layout(triangles) in; layout(triangle_strip, max_vertices=6) out; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform mat4 modelMatrix; in vec4 normal[]; out vec4 vertex_color;
GLSL Example void main() { mat4 modelViewMatrix = viewMatrix * modelMatrix; mat4 viewProjectionMatrix = projectionMatrix * viewMatrix; int i; //====== First triangle - identical to the input one. for(i=0; i<3; i++) { vec4 view_pos = modelViewMatrix * gl_in[i].gl_Position; gl_Position = projectionMatrix * view_pos; vertex_color = vec4(0.0, 1.0, 0.0, 1.0); EmitVertex(); } EndPrimitive();
GLSL Example //====== Second triangle - translated version for(i=0; i<3; i++) { vec4 N = normal[i]; vec4 world_pos = modelMatrix * (gl_in[i].gl_Position + normalize(N) * 10.0); gl_Position = viewProjectionMatrix * world_pos; vertex_color = vec4(1.0, 1.0, 0.0, 1.0); EmitVertex(); } EndPrimitive(); }
Input Values Notice that we can define the input value types using the layout specification Layout can be used to tell GPU where to send particular pieces of data The input layout is important for using the geometry shader correctly Need to know the type of primitive we are dealing with We can also declare incoming adjacent vertices
Output Values Notice that we also set the outgoing data format for the geometry shader Triangle strip in this instance We can set the output data type, and the number of expected vertices Useful when dealing with potentially large pieces of geometry
Questions?
Examples
Fur om/watch?v=tuAC9Q Gx3vQ om/watch?v=tuAC9Q Gx3vQ
Point Sprite Expansion om/watch?v=jpXgPkft RcM om/watch?v=jpXgPkft RcM
Dynamic Particles
Morphing / Shape Manipulation
Tesselation
Questions?
To do this week… Coursework, coursework, coursework As mentioned, I’ll be in the Games Lab all week Focus on getting the coursework in, then next week we’ll talk about the exam and module as a whole
Summary Geometry shader allows us to do some pretty cool effects beyond the capabilities of the vertex and fragment shader Geometric primitives approach This is such a new area of graphics programming that materials are light on the ground Dig around to see what you can find