Presentation is loading. Please wait.

Presentation is loading. Please wait.

GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.

Similar presentations


Presentation on theme: "GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders."— Presentation transcript:

1 GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders

2 The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output Vertex DataPixel Color

3 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)

4 Geometry Assembly / Processing Clip Space Vertices (Flattened into 2D screen space) Connect Associated Vertices (Winding order preserved) Construct Geometry

5 Rasterization Clip Space Geometry Clipping and Backface Culling (Removes pieces that will not be seen) Rasterize (Split the geometry into fragments, interpolating vertex values)

6 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

7 The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output Programmable ClosedProgrammableClosed

8 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

9 How Do You Make Shader Programs? Shader Languages GLSL HLSL OpenGL Shader Language High Level Shader Language (Direct X) GLSL HLSL ~ C++ GLSL != C++

10 Shader Data Types GLSLHLSL Booleanbool Signed 32 bit Integerint Unsigned 32 bit Integeruintuint / dword Signed 32 bit Floating Pointfloat Signed 64 bit Floating Pointdouble n Element Boolean Vectorbvecnbooln n Element Signed Integer Vectorivecnintn n Element Unsigned Integer Vectoruvecnuintn n Element Float (32bit) Vectorvecnfloatn n Element Double (64bit) Vectordvecndoublen n By n Element Float Matrixmatnfloatnxn n By m Element Float Matrixmatnxmfloatnxn

11 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++ }

12 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

13 Programming Shader I/O #version 430 struct 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 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 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 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; }

14 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;

15 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);

16 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);

17 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)


Download ppt "GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders."

Similar presentations


Ads by Google