16
GAM532 DPS932 – Week 3 Fragment Shaders

GAM532 DPS932 – Week 3

  • Upload
    natala

  • View
    51

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: GAM532 DPS932 – Week 3

GAM532DPS932 – Week 3Fragment Shaders

Page 2: GAM532 DPS932 – Week 3

The Shader PipelineVertex

Processing

Primitive Assembly /

Processing

Rasterization

Fragment

Process

Pixel Output

Vertex Data Pixel Color

Page 3: GAM532 DPS932 – Week 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)

Page 4: GAM532 DPS932 – Week 3

Fragment 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

Page 5: GAM532 DPS932 – Week 3

Linear Interpolation

[ 0, 25, 10]

35

[ 0.707, 0.707, 0 ]

-5

[ -0.707, 0.707, 0 ]

[ 10, 25, 50]

31 23 15 7 -1

1, 25, 14 3, 25, 22 5, 25, 30 7, 25, 38 9, 25, 46

0.566, 0.707, 0 0.283, 0.707, 0 0, 0.707, 0 -0.283, 0.707, 0

-0.566, 0.707, 0

FV = v1 + (v2 – v1) * (FI + 0.5)/FN

VF = v1 + (v2 – v1) * A

Page 6: GAM532 DPS932 – Week 3

Linear Interpolation Issues

31 23 15 7 -1

1, 25, 14 3, 25, 22 5, 25, 30 7, 25, 38 9, 25, 46

0.566, 0.707, 0 0.283, 0.707, 0 0, 0.707, 0 -0.283, 0.707, 0

-0.566, 0.707, 0

35-5237 15 31-1

NOT A Unit Length Vector IS A Unit Length Vector

Page 7: GAM532 DPS932 – Week 3

Interpolated UVs & Texture Sampling

[ 0, 0 ]

[ 1, 0 ]

[ 0, 1 ]

[ 1, 1 ]

[ 0, 10, 0 ]

[ 7, -7, 0 ][ -7, -7, 0 ]

[ 0.5, 0 ]

[ 0, 1 ]

[ 1, 1 ]

[ 0, 1.5, 0 ][ 0.5, 0.5 ]

[ -3 , -2, 0 ][ 0.28, 0.29 ]

Texel

Page 8: GAM532 DPS932 – Week 3

Texture Filtering

Point Sampling(Sample Once)

UV = [0.2,0]

[0, 0, 0]

Linear Sampling(Sample Two Closest, Average)

UV = [0.2,0]

[26,26,26]

[ 0, 0, 0 ] * 1.0

0.2-(1/3)/(1/3)=0.1

[ 0, 0, 0 ] * 0.9 +

[255,255,255] * 0.1

Bilinear Sampling

(Sample Four Closest, Average)

UV = [0.2,0]

[26,26,26]

~Equation above, but in 4 directions

Page 9: GAM532 DPS932 – Week 3

Fragment Shader Inputs & Outputs

Interpolated Fragment Values Uniform Buffers

- Clip Space Position

- UV Coordinates

- World/View Space Surface Normal

- Light Information

- Diffuse Textures

- Diffuse Sampler

Fragment Shader Output

- Pixel Color

Page 10: GAM532 DPS932 – Week 3

Writing Fragment Shaders I/O#version 430struct Light { vec4 diffuse; vec4 specular; vec4 attenuation;};

layout (std140, binding = 9) uniform singleLight { Light light;};

layout(binding = 1) uniform sampler2D diffuse;

struct FragInput { vec4 position; vec3 normal; … };

layout (location = 0) in FragInput fin;layout (location = 0) out vec4 color;void main() {…}

struct Light { float4 diffuse; float4 specular; float4 attenuation;};

cbuffer singleLight : register(b1) { Light light;}

Texture2D diffuse : register(t1);SamplerState diffSamp : register(s1);

struct FragInput { float4 position : SV_POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; float4 fragPos : TEXCOORD1; float3 toLight : TEXCOORD2;};

float4 FragmentShaderName(FragInput fin) : SV_Target {…}

Page 11: GAM532 DPS932 – Week 3

Writing Fragment Shader Program

…void FragmentShaderName() { vec4 dTexel = sample2D(diffuse, fin.uv).rgba; color = dTexel;}

…float4 FragmentShaderName(FragInput fin) : SV_Target { float4 dTexel = diffuse.Sample(diffSamp, fin.uv); return dTexel; }

Page 12: GAM532 DPS932 – Week 3

Working with Multiple Textures & Normals

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

Dot([0,1,0],[0,-1,0]) = -1.0

Texture1

Texture2

Texture3

Texture4

Sampler1

Sampler2

Page 13: GAM532 DPS932 – Week 3

Adding to Fragment Shader

…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;}

Texture2D diffuse : register(t1);SamplerState diffSamp : 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; }

Page 14: GAM532 DPS932 – Week 3

HLSL Semantics

Fragment Shader Input Fragment Shader Output

NORMALn X

COLORn X X

TEXCOORDn X

SV_IsFrontFace X

SV_Position X

SV_Target X

SV_Depth X

DEPTHn X

Page 15: GAM532 DPS932 – Week 3

Effects of the Fragment Shader

Cell Shading Normal Mapping Sub-Surface Scattering

Page 16: GAM532 DPS932 – Week 3

To Do• Read over Lab 2

• Read Week 3 Notes

• Create Team Wiki Page

• Split assignment work between group members