GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

17
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders

Transcript of GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Page 1: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

GAM532DPS932 – Week 1Rendering Pipeline and Shaders

Page 2: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

The Shader Pipeline

Vertex Processing

Primitive Assembly / Processing

Rasterization

Fragment Process Pixel Output

Vertex Data Pixel Color

Page 3: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Vertex Processing

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)

Page 4: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Geometry Assembly / Processing

Clip Space Vertices(Flattened into 2D screen space)

Connect Associated Vertices (Winding order

preserved)

Construct Geometry

Page 5: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Rasterization

Clip Space Geometry

Clipping and Backface Culling (Removes

pieces that will not be

seen)

Rasterize (Split the geometry into

fragments, interpolating vertex values)

Page 6: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Fragment Processing

Fragment (Containing interpolated

vertex information)

- UV Coord- Normal- World Pos- Light Dir

Texture Sampled With UVS (Color stored locally)

Lighting and Other

Transformations Applied (Applied to stored color

value)

[142,107,6,255]

Final Color Exits Pipeline

Page 7: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

The Shader Pipeline

Vertex Processing

Primitive Assembly / Processing

Rasterization

Fragment Process Pixel Output

Programmable Programmable Closed Programmable Closed

Page 8: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

What is a Shader Program?

#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(); }

Grapics Card Running Shaders in Shader Cores

Page 9: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

How Do You Make Shader Programs?

Shader LanguagesGLSL

HLSL

OpenGL Shader Language

High Level Shader Language (Direct X)

GLSLHLSL ~C++

GLSLHLSL!=C++

Page 10: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Shader Data Types

GLSL HLSL

Boolean bool bool

Signed 32 bit Integer int int

Unsigned 32 bit Integer uint uint / dword

Signed 32 bit Floating Point float float

Signed 64 bit Floating Point double double

n Element Boolean Vector bvecn booln

n Element Signed Integer Vector

ivecn intn

n Element Unsigned Integer Vector

uvecn uintn

n Element Float (32bit) Vector vecn floatn

n Element Double (64bit) Vector

dvecn doublen

n By n Element Float Matrix matn floatnxn

n By m Element Float Matrix matnxm floatnxn

Page 11: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Shader Languages Cont

#version 430//Line above indicates what shader version

struct Input { vec4 position; vec3 normal; vec2 uv;}; //structs work just like C++

void main() { //void main is the entry point for //all glsl shader programs

}

struct Input { float4 position; float3 normal; float2 uv;}//structs work just like C++

float4 ShaderFunctionName(Input shadIn) : BINDINGS { //entry points are functions which will have their //names defined as entry points in C++}

Page 12: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Shader Input and Output

Vertex Shader

Geometry Shader

Rasterization

Fragment Shader

Vertex Data

Clip Space Vertex Data

Clip Space Geometry

Rasterized Fragment

Pixel Color

Uniform Buffers

Page 13: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Programming Shader I/O#version 430struct FragInput { vec4 position; vec3 normal; vec2 uv;};//Define the structure of the shader’s input

struct Light { vec4 diffuse; vec4 specular;};//Define the structure of uniform buffer element

//Identifies a uniform buffer, aligns to first registerlayout(binding = 1) uniform Light light;

//Identifies shader’s IO with bound global variableslayout(location = 0) in FragInput fin;layout(location = 0) out vec4 color;

void main() { color = vec4(1,0,0,1);} //setting global will change shader’s output

struct FragInput { float4 position : SV_POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0;//binds var to shader output};//Define the structure of the shader’s input

//cbuffer identifies a uniform buffer, aligns it to //uniform buffer’s first registercbuffer light : register(b1) { float4 diffuse; float4 specular;}

//Identifies shader’s IO as parameter and return typefloat4 fragShader(FragInput fin) : SV_Target { float4 color = float4(1,0,0,1); … return color;}

Page 14: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Extra Shader Code

vec4 a;a.x = 10;a.y = 31;a.zw = a.xy;

vec3 b = a.xyz;

vec4 c = vec4(a.x, a.y, a.z, 1.0);

float4x4 d = float4x4(a,c,a,c);

float r = dot(c, d * a);

d[0][1] = 22;

float4 a;a.x = 10;a.y = 31;a.zw = a.xy;

float3 b = a.xyz;

float4 c = float4(a, 1.0);

float4x4 d = float4x4(a,c,a,c);

float r = dot(c, mul(d, a));

d[0][1] = 22;

Page 15: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Loading Shader File (C++)

String shaderString;File vShader(fileName, in);String ts;while(vShader) { getLine(vShader, ts); shaderString += ts + "\n"; }vShader.close();

vs = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vs, 1, &(shaderData.c_str()), 0);glCompileShader(vs);

ID3D10Blob* sh = 0;ID3D10Blob* em = 0;

D3DX11CompileFromFile(fileName, 0, 0, function, "vs_5_0", 1 << 15, 0, 0, &sh, &em, 0);

dev->CreateVertexShader(sh->GetBufferPointer(), sh->GetBufferSize(), 0, &vs);

D3D11_INPUT_ELEMENT_DESC* desc;auto& ia = v.getVertexDescription();_dxTranVertex(ia, &desc);

dev->CreateInputLayout(desc, ia.size(), sh->GetBufferPointer(), sh->GetBufferSize(), &layout);

Page 16: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Binding Shaders

//Vertex, (geometry) and fragment shader must be //bound to an program first, check engine for //details

glUseProgram(prg);

con->VSSetShader(vs, 0, 0);

Page 17: GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

To Do• Clone old repo to a new GAM532 repo on bitbucket

• Add name to student list

• Form groups

• Read over Lab 1

• Read Week 1 Notes

• (Review 531 material heavily if new to this course stream)