170 likes | 309 Views
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).
E N D
GAM532DPS932 – 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) 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 BackfaceCulling (Removes pieces that will not be seen) Rasterize (Split the geometry into fragments, interpolating vertex values)
Fragment Processing • UV Coord • - Normal- World Pos • Light Dir [142,107,6,255] Texture Sampled With UVS (Color stored locally) Lighting and Other Transformations Applied (Applied to stored color value) Final Color Exits Pipeline Fragment (Containing interpolated vertex information)
The Shader Pipeline Programmable Programmable Closed Programmable Closed
What is a Shader Program? Grapics Card Running Shaders in Shader Cores #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(); }
How Do You Make Shader Programs? GLSL OpenGL Shader Language Shader Languages HLSL High Level Shader Language (Direct X) GLSL ~ GLSL != C++ C++ HLSL HLSL
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 glslshader 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 Data Clip Space Vertex Data Rasterized Fragment Clip Space Geometry Pixel Color Uniform Buffers
Programming Shader I/O #version 430 structFragInput { 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 register layout(binding = 1) uniform Light light; //Identifies shader’s IO with bound global variables layout(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 structFragInput { 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 register cbuffer light : register(b1) { float4 diffuse; float4 specular; } //Identifies shader’s IO as parameter and return type float4 fragShader(FragInput fin) : SV_Target { float4 color = float4(1,0,0,1); … return color; }
Extra Shader Code 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; 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;
Loading Shader File (C++) 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); 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);
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)