160 likes | 339 Views
GAM532 DPS932 – Week 4. Geometry Shaders. The Shader Pipeline. Vertex Data. Pixel Color. Basic Triangle Construction. Clip Space Vertices (Flattened into 2D screen space). Connect Associated Vertices (Winding order preserved). Construct Geometry . Modified Triangle Construction.
E N D
GAM532DPS932 – Week 4 Geometry Shaders
The Shader Pipeline Vertex Data Pixel Color
Basic Triangle Construction Clip Space Vertices (Flattened into 2D screen space) Connect Associated Vertices (Winding order preserved) Construct Geometry
Modified Triangle Construction Original and New Vertices Linked Together Forming New Geometry Clip Space Vertices (Flattened into 2D screen space) New Vertices Added Based on Vertex Data and Uniform Buffers Construct Geometry
Geometry Shader Inputs & Outputs Geometry Shader Input Uniform Buffers - Geometric Transformation Data - Array of clip space vertices Geometry Shader Output • Geometry streams (made up of clip space vertices)
Writing Geometry Shaders I/O Struct structGeometryInput { float4 position : SV_POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; }; structGeometryInput { float4 position : SV_POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; uint id : TEXCOORD1; }; Input struct should match output struct of your vertex shader Positional values are in clip space, transformations will take place in clip space structGeometryInput { vec4 position; vec3 normal; vec2 uv; }; structGeometryInput{ vec4 position; vec3 normal; vec2 uv; uint id; }; Output struct should match input struct of your fragment shader Data can be different between input and output, more or less information can be sent to the fragment shader
Writing Geometry Shaders Bindings [maxvertexcount(3)] void GeometryShader(triangle GeometryInputinput[3], inoutTriangleStream<BasicGSInput> os) {…} Indicate the max number of vertices to be output Indicate the incoming primitive type layout(triangles) in; layout(triangle_strip) out; layout(max_vertices=3) out; layout (location = 0) in GeometryInput gin[]; layout (location = 0) out GeometryOutput gout; void main() {…} Indicate the outgoing primitive type Bind incoming data to input struct and outgoing data to output struct
Writing Geometry Shader Program [maxvertexcount(3)] void BasicGeometryShader(triangle GeometryInput input[3], inoutTriangleStream<GeometryOutput> os) { for(inti = 0; i < 3; i++) { GeometryOutput a; a.position = input[i].position; a.normal = input[i].normal; a.uv = input[i].uv; os.Append(a); } os.RestartStrip(); } Construct vertex to be outputted Add vertex to current primitive void main() { inti; for(i=0; i<gl_in.length(); i++) { gl_Position = gin[i].position; gout.position= gin[i].position; gout.normal= gin[i].normal; gout.uv = gin[i].uv; EmitVertex(); } EndPrimitive(); } Indicate end of current primitive and prepare for next
Simple Geometric Billboards Points Clip Space Quad Billboards can be aligned directly to the screen or transformed to add basic perspective or orientation to the billboard.
More Advanced Geometry Shader [maxvertexcount(3)] void BasicGeometryShader(triangle BasicGSInput input[3], inoutTriangleStream<BasicGSInput> os) { BasicGSInput output = (BasicGSInput)0; for(inti = 0; i < 3; i++) { os.Append(input[i]); int j = (i + 1) % 3; output.position = interpolate(input[i].position, input[j].position, 0.5f); output.normal = interpolate(input[i].normal, input[j].normal, 0.5f); output.uv = interpolate(input[i].uv, input[j].uv, 0.5f); output.fragPos = interpolate(input[i].fragPos, input[j].fragPos, 0.5f); output.toLight = interpolate(input[i].toLight, input[j].toLight, 0.5f); os.Append(output); j = (i + 2) %3; output.position = interpolate(input[i].position, input[j].position, 0.5f); output.normal = interpolate(input[i].normal, input[j].normal, 0.5f); output.uv = interpolate(input[i].uv, input[j].uv, 0.5f); output.fragPos = interpolate(input[i].fragPos, input[j].fragPos, 0.5f); output.toLight = interpolate(input[i].toLight, input[j].toLight, 0.5f); os.Append(output); os.RestartStrip(); } }
Uses of the Geometry Shader Advantages Disadvantages • Only part of the pipeline that allows new elements to be created • Can morph and adjust the structure of meshes • Can make certain screen space effects possible • Forces pipeline to skip out on a number of optimizations it could make • Output vertices in a non indexed fashion, causing primitive assembly to be most costly • Operations take place in clip space, may need extra transformations to do world-space computations
Geometry Shader Intrusion Indexed Vertices Simple Geometry Unoptimized Geometry Indexed Vertices Un-Indexed Vertices
Effects of the Geometry Shader Tessellation Dynamic Terrain
To Do • Read over Lab 3 • Read Week 4 Notes • Select Enhancement and possible game idea • Begin to prepare proposal document