1 / 16

GAM532 DPS932 – Week 2

GAM532 DPS932 – Week 2. Vertex Shaders. The Shader Pipeline. Vertex Data. Pixel Color. Vertex Processing in Parallel . #version 430 layout(triangles) in; layout( triangle_strip ) out; layout( max_vertices =3) out; struct BasicGSInput { vec4 position; vec3 normal;

ami
Download Presentation

GAM532 DPS932 – Week 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GAM532DPS932 – Week 2 Vertex Shaders

  2. The Shader Pipeline Vertex Data Pixel Color

  3. Vertex Processing in Parallel #version 430 layout(triangles) in; layout(triangle_strip) out; layout(max_vertices=3) out; structBasicGSInput { vec4 position; vec3 normal; vec2 uv; vec4 fragPos; vec3 toLight; }; structBasicGSOutput { vec4 position; vec3 normal; vec2 uv; vec4 fragPos; vec3 toLight; }; layout (location = 0) in BasicGSInput gin[]; layout (location = 0) out BasicGSOutput gout; 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; gout.fragPos = gin[i].fragPos; gout.toLight = gin[i].toLight; EmitVertex(); } EndPrimitive(); } Uniform Buffers

  4. Vertex Shader Transformation World Space Vertices (relative to the center of the scene) Local Vertices (in Mesh) View Space Vertices (relative to the absolute position of the camera) Clip Space Vertices (Flattened into 2D screen space)

  5. Local Space to World Space Actor’s (Node’s) Transformation Matrix Actor’s World Space Vertices Actor’s Local Space Vertices [ 0, 5, 0 ] [ 8.5, 8.5, -10 ] | 0.7 0.7 0.0 0.0 | | -0.7 0.7 0.0 0.0 | | 0.0 0.0 1.0 0.0 | | 12.0 5.0 -10 1.0 | [ 14.1, 7.1, -10 ] [ 9.9, 2.9, -10 ] [ -3, 0, 0 ] [ 3, 0, 0 ] Scene Origin Scene Origin

  6. World Space to View Space Scene Origin Camera’s Inverse Transformation Matrix Actor’s View Space Vertices Actor’s World Space Vertices | 0.7 0.0 -0.7 0.0 | | 0.0 1.0 0.0 0.0 | | 0.7 0.0 0.7 0.0 | | 0.0 -15.0 -5.0 1.0 | [ -1.05, -6.5, -6.05 ] [ 8.5, 8.5, -10 ] [ 14.1, 7.1, -10 ] [ 2.87, -7.9, -2.13 ] [ 9.9, 2.9, -10 ] [ -0.07, -12.1, -5.07 ] Scene Origin

  7. View Space to Clip Space Scene Origin Camera’s Projection Matrix Actor’s Clip Space Vertices Actor’s View Space Vertices Scene Origin [ -1.35, -13.46, 5.06, 6.06] | 1.29 0.0 0.0 0.0 | | 0.0 2.07 0.0 0.0 | | 0.0 0.0 -1.001 -1.001 | | 0.0 0.0 -1.0 0.0 | [ -1.05, -6.5, -6.05 ] [ 3.7, -16.35, 1.13, 2.13] [ 2.87, -7.9, -2.13 ] [ -0.09, -25.05, 4.08, 5.08 ] [ -0.07, -12.1, -5.07 ]

  8. Understanding Clip Space Vectors +1 y 1 z (back) -1 x +1 x 0 z (front) [ -1.35, -13.46, 5.06, 6.06] -1 y View Port Vectors [ x/w, y/w, z/w ] [ -0.22, -2.22, 0.83 ] [ 3.7, -16.35, 1.13, 2.13] [ 0.02, -4.93, 0.80 ] [ -0.09, -25.05, 4.08, 5.08 ] [ 1.74, -7.68, 0.53 ]

  9. Vertex Shader Inputs & Outputs Vertex Definition Uniform Buffers - World Matrix - Local Position - View Matrix - UV Coordinates - Projection Matrix - Surface Normal Vertex Shader Output - Clip Space Position - UV Coordinates - World/View Space Surface Normal

  10. Writing Vertex Shaders I/O #version 430 layout (std140, binding = 0) uniform uniMat { mat4 world; mat4 view; mat4 proj; }; structVertOut { vec4 position; vec3 normal; vec2 uv; }; layout(location = 0) in vec3 position; layout(location = 1) in vec3 normal; layout(location = 2) in vec2 uv; layout(location = 0) out VertOutvout; void main() {…} //yes, void cbufferuniMat : register(b0) { float4x4 world; float4x4 view; float4x3 proj } structVertInput { float3 position : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; }; structVertOutput { float4 position : SV_POSITION; //System Value float3 normal : NORMAL; float2 uv : TEXCOORD0; }; VertOutputVertexShaderFunc(VertInput vin) {…}

  11. Writing Vertex Shader Program … VertOutputVertexShaderFunc(VertInput vin) { VertOutputvout = (VertOutput)0; vout.position = mul( float4(vin.position, 1.0f), world); //assuming matrix stored row major vout.position = mul(vout.position, view); vout.position = mul(vout.position, proj); vout.normal = mul(vin.normal, (float3x3)world); vout.uv = vin.uv; return vout; } … void main() { vout.position = vec4(position.x, position.y, position.z, 1.0f) * world; vout.position = vout.position * view; vout.position = vout.position * proj; vout.normal = normal * mat3(world); vout.uv = uv; gl_Position = vout.position; }

  12. Optimizing Shader Load # of Vertex Shader Calculations: 20 # of Fragment Shader Calculations: 32 Total VS Calculations: 2,000 * 20 = 40,000 Total FS Calculations: 300,000 * 32 = 9,600,000 Total Calculations: 9,640,000 # of Vertices: 2,000 (constant) Balancing Requirement: Must be linearly interpolated # of Fragments: 0 - 2,073,600 (@ 1080P) Total VS Calculations: 2,000 * 28 = 56,000 Total FS Calculations: 300,000 * 24 = 7,200,000 LOD – Level of Detail # of Vertices: 2,000 / 1,000, / 600 / 200 Total Calculations: 7,256,000 Reduction of 25%

  13. Adding to Vertex Shader struct Light { vec3 position; vec4 diffuse; vec4 specular; … }; layout (std140, binding = 1) uniform singleLight { Light light; }; structVertOut { … vec3 toLight; }; … void main() { … vout.toLight = light.position – vout.position.xyz … } structLight { float3 position; float4 diffuse; float4 specular; … }; cbuffersingleLight : register(b1) { Light light; } structVertOutput { … float3 toLight : TEXCOORD1; }; VertOutputVertexShaderFunc(VertInput vin) { … vout.toLight = light.position – vout.position.xyz; …//assuming vout.position is in world cords here }

  14. HLSL Semantics

  15. Optimizing Outside the Shader Vout.position = vin.position * world * view * proj; Changes per vertex Uniform Vout.position = vin.position * worldView * proj; Or Vout.position = vin.position * world * viewProj;

  16. To Do • Read over Lab 1 • Read Week 2 Notes • Read Assignment Document (will be released this week)

More Related