1 / 16

GAM532 DPS932 – Week 3

GAM532 DPS932 – Week 3. Fragment Shaders. The Shader Pipeline. Vertex Data. Pixel Color. Clip Space to Fragment Transformation. Clip Space Vertices (Flattened into 2D screen space). Connect Associated Vertices (Winding order preserved). Construct Geometry.

natala
Download Presentation

GAM532 DPS932 – Week 3

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 3 Fragment Shaders

  2. The Shader Pipeline Vertex Data Pixel Color

  3. Clip Space to Fragment Transformation Clip Space Vertices (Flattened into 2D screen space) Connect Associated Vertices (Winding order preserved) Construct Geometry Clipping and Backface Culling (Removes pieces that will not be seen) Rasterize (Split the geometry into fragments, interpolating vertex values)

  4. Fragment 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

  5. Linear Interpolation -5 35 [ 10, 25, 50] [ 0, 25, 10] [ -0.707, 0.707, 0 ] [ 0.707, 0.707, 0 ] VF = v1 + (v2 – v1) * A FV = v1 + (v2 – v1) * (FI + 0.5)/FN

  6. Linear Interpolation Issues -5 35 -1 7 23 31 15 IS A Unit Length Vector NOT A Unit Length Vector

  7. Interpolated UVs & Texture Sampling [ 0, 10, 0 ] [ 0.5, 0 ] [ 1, 0 ] [ 0, 0 ] [ 0, 1.5, 0 ] [ 0.5, 0.5 ] [ -3 , -2, 0 ] [ 0.28, 0.29 ] [ 0, 1 ] [ 1, 1 ] [ -7, -7, 0 ] [ 7, -7, 0 ] Texel [ 0, 1 ] [ 1, 1 ]

  8. Texture Filtering [0, 0, 0] Point Sampling (Sample Once) UV = [0.2,0] [ 0, 0, 0 ] * 1.0 [26,26,26] 0.2-(1/3)/(1/3)=0.1 [ 0, 0, 0 ] * 0.9 +[255,255,255] * 0.1 Linear Sampling (Sample Two Closest, Average) UV = [0.2,0] [26,26,26] ~Equation above, but in 4 directions Bilinear Sampling (Sample Four Closest, Average) UV = [0.2,0]

  9. Fragment Shader Inputs & Outputs Interpolated Fragment Values Uniform Buffers - Light Information - Clip Space Position - Diffuse Textures - UV Coordinates - Diffuse Sampler - World/View Space Surface Normal Fragment Shader Output - Pixel Color

  10. Writing Fragment Shaders I/O structLight { float4 diffuse; float4 specular; float4 attenuation; }; cbuffersingleLight : register(b1) { Light light; } Texture2D diffuse : register(t1); SamplerStatediffSamp: register(s1); structFragInput { float4 position : SV_POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; float4 fragPos : TEXCOORD1; float3 toLight : TEXCOORD2; }; float4 FragmentShaderName(FragInputfin) : SV_Target {…} #version 430 structLight { vec4 diffuse; vec4 specular; vec4 attenuation; }; layout (std140, binding = 9) uniform singleLight { Light light; }; layout(binding = 1) uniform sampler2D diffuse; structFragInput { vec4 position; vec3 normal; … }; layout (location = 0) in FragInput fin; layout (location = 0) out vec4 color; void main() {…}

  11. Writing Fragment Shader Program … float4 FragmentShaderName(FragInputfin) : SV_Target { float4 dTexel = diffuse.Sample(diffSamp, fin.uv); return dTexel; } … void FragmentShaderName() { vec4 dTexel = sample2D(diffuse, fin.uv).rgba; color = dTexel; }

  12. Working with Multiple Textures & Normals Sampler2 Sampler1 Texture1 Texture2 Dot Product with Directions Dot([0,1,0],[0,1,0]) = 1.0 Dot([0,1,0],[0.707,0.707,0]) = 0.707 Dot([0,1,0],[1,0,0]) = 0.0 Dot([0,1,0],[0.707,-0.707,0]) = -0.707 Texture3 Texture4 Dot([0,1,0],[0,-1,0]) = -1.0

  13. Adding to Fragment Shader … Texture2D diffuse : register(t1); SamplerStatediffSamp : register(s1); Texture2D diff2 : register(t2); SamplerState diffSamp2 : register(s2); … float4 FragmentShaderName(FragInput fin) : SV_Target { float4 dTexel = diffuse.Sample(diffSamp, fin.uv); float4 eTexel = diff2.Sample(diffSamp2, fin.uv); float4 color = float4(0,0,0,1); float3 nNormal = normalize(vin.normal); color += max(0, dot(nNormal, float3(0,1,0)) * dTexel; color += max(0, dot(nNormal, float3(0,-1,0)) * eTexel; return color; } … layout(binding = 1) uniform sampler2D diffuse; layout(binding = 2) uniform sampler2D diff2; .. void FragmentShaderName() { vec4 dTexel = sample2D(diffuse, fin.uv).rgba; vec4 eTexel = sample2D(diff2, fin.uv).rgba; color = float4(0,0,0,1); vec3 nNormal = normalize(vin.normal); color += max(0, dot(nNormal, vec3(0,1,0)) * dTexel; color += max(0, dot(nNormal, vec3(0,-1,0)) * eTexel; }

  14. HLSL Semantics

  15. Effects of the Fragment Shader Cell Shading Normal Mapping Sub-Surface Scattering

  16. To Do • Read over Lab 2 • Read Week 3 Notes • Create Team Wiki Page • Split assignment work between group members

More Related