Upload
morrison
View
41
Download
0
Tags:
Embed Size (px)
DESCRIPTION
GAM532 DPS932 – Week 1. Rendering Pipeline and Shaders. The Shader Pipeline. Vertex Data. Pixel Color. Vertex Processing. 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). - PowerPoint PPT Presentation
Citation preview
GAM532DPS932 – Week 1Rendering Pipeline and Shaders
The Shader Pipeline
Vertex Processing
Primitive Assembly / Processing
Rasterization
Fragment Process Pixel Output
Vertex Data Pixel Color
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)
Geometry Assembly / Processing
Clip Space Vertices(Flattened into 2D screen space)
Connect Associated Vertices (Winding order
preserved)
Construct Geometry
Rasterization
Clip Space Geometry
Clipping and Backface Culling (Removes
pieces that will not be
seen)
Rasterize (Split the geometry into
fragments, interpolating vertex values)
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
The Shader Pipeline
Vertex Processing
Primitive Assembly / Processing
Rasterization
Fragment Process Pixel Output
Programmable Programmable Closed Programmable Closed
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
How Do You Make Shader Programs?
Shader LanguagesGLSL
HLSL
OpenGL Shader Language
High Level Shader Language (Direct X)
GLSLHLSL ~C++ GLSL
HLSL!=C++
Shader Data TypesGLSL HLSL
Boolean bool boolSigned 32 bit Integer int int
Unsigned 32 bit Integer uint uint / dwordSigned 32 bit Floating Point float floatSigned 64 bit Floating Point double doublen Element Boolean Vector bvecn boolnn Element Signed Integer
Vectorivecn intn
n Element Unsigned Integer Vector
uvecn uintn
n Element Float (32bit) Vector vecn floatnn Element Double (64bit)
Vectordvecn doublen
n By n Element Float Matrix matn floatnxnn By m Element Float Matrix matnxm floatnxn
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++}
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
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;}
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;
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);
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);
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)