Presentation is loading. Please wait.

Presentation is loading. Please wait.

GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output.

Similar presentations


Presentation on theme: "GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output."— Presentation transcript:

1 GAM532 DPS932 – Week 2 Vertex Shaders

2 The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output Vertex DataPixel Color

3 Vertex Processing in Parallel #version 430 layout(triangles) in; layout(triangle_strip) out; layout(max_vertices=3) out; struct BasicGSInput { vec4 position; vec3 normal; vec2 uv; vec4 fragPos; vec3 toLight; }; struct BasicGSOutput { vec4 position; vec3 normal; vec2 uv; vec4 fragPos; vec3 toLight; }; layout (location = 0) in BasicGSInput gin[]; layout (location = 0) out BasicGSOutput gout; void main() { int i; 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 Local Vertices (in Mesh) World Space Vertices (relative to the center of the scene) 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 [ -3, 0, 0 ] [ 0, 5, 0 ] [ 3, 0, 0 ] Actor’s Local Space Vertices Actor’s (Node’s) Transformation Matrix | 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 | [ 9.9, 2.9, -10 ] [ 8.5, 8.5, -10 ] [ 14.1, 7.1, -10 ] Scene Origin Actor’s World Space Vertices

6 World Space to View Space [ -0.07, -12.1, -5.07 ] [ -1.05, -6.5, -6.05 ] [ 2.87, -7.9, -2.13 ] Actor’s World Space Vertices Camera’s Inverse Transformation Matrix | 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 | Scene Origin Actor’s View Space Vertices [ 9.9, 2.9, -10 ] [ 8.5, 8.5, -10 ] [ 14.1, 7.1, -10 ] Scene Origin

7 View Space to Clip Space [ -0.07, -12.1, -5.07 ] [ -1.05, -6.5, -6.05 ] [ 2.87, -7.9, -2.13 ] Actor’s View Space Vertices Camera’s Projection Matrix | 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 | Scene Origin Actor’s Clip Space Vertices [ -0.09, -25.05, 4.08, 5.08 ] [ -1.35, -13.46, 5.06, 6.06] [ 3.7, -16.35, 1.13, 2.13] Scene Origin

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

9 Vertex Shader Inputs & Outputs Vertex DefinitionUniform Buffers - Local Position - UV Coordinates - Surface Normal - World Matrix - View Matrix - Projection Matrix 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; }; struct VertOut { 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 VertOut vout; void main() {…} //yes, void cbuffer uniMat : register(b0) { float4x4 world; float4x4 view; float4x3 proj } struct VertInput { float3 position : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; }; struct VertOutput { float4 position : SV_POSITION; //System Value float3 normal : NORMAL; float2 uv : TEXCOORD0; }; VertOutput VertexShaderFunc(VertInput vin) {…}

11 Writing Vertex Shader Program … 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; } … VertOutput VertexShaderFunc(VertInput vin) { VertOutput vout = (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; }

12 Optimizing Shader Load # of Fragments: 0 - 2,073,600 (@ 1080P) # of Vertices: 2,000 (constant) LOD – Level of Detail # of Vertices: 2,000 / 1,000, / 600 / 200 # 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 Balancing Requirement: Must be linearly interpolated Total VS Calculations: 2,000 * 28 = 56,000 Total FS Calculations: 300,000 * 24 = 7,200,000 Total Calculations: 9,640,000 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; }; struct VertOut { … vec3 toLight; }; … void main() { … vout.toLight = light.position – vout.position.xyz … } struct Light { float3 position; float4 diffuse; float4 specular; … }; cbuffer singleLight : register(b1) { Light light; } struct VertOutput { … float3 toLight : TEXCOORD1; }; VertOutput VertexShaderFunc(VertInput vin) { … vout.toLight = light.position – vout.position.xyz; …//assuming vout.position is in world cords here }

14 HLSL Semantics Vertex Shader InputVertex Shader Output POSITIONnXX NORMALnXX COLORnXX TEXCOORDnXX BINORMALnX TANGENTnX PSIZEnXX (no n) FOGX SV_PositionX

15 Optimizing Outside the Shader Vout.position = vin.position * world * view * proj; Changes per vertexUniform 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)


Download ppt "GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output."

Similar presentations


Ads by Google