Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Introduction to Shaders

Similar presentations


Presentation on theme: "Computer Graphics Introduction to Shaders"— Presentation transcript:

1 Computer Graphics Introduction to Shaders
CO2409 Computer Graphics Week 10

2 Lecture Contents The Programmable Pipeline Shader Languages
Inputs / Outputs Vertex Shaders Pixel Shaders Geometry Shaders

3 DirectX so far… So far in DirectX we have:
Learnt how to initialise a window to use DirectX Use input data Vertices, indices, triangles Render simple shapes This is the beginning of the Rendering Pipeline Up to the Input-Assember Stage The next stage is the first of the programmable shaders Shaders are somewhat different to the C++ code we’re familiar with They use a different language

4 Rationale for Shaders Historically, the rendering pipeline was fixed
The programmer could only control a selection of states All rendering processes were similar This was inflexible: Difficult to do advanced or original rendering approaches Often needed to use CPU to do things the GPU pipeline couldn’t do Shaders provide programmable control over pipeline sections Other parts still controlled with states

5 The Challenge of Shaders
The massive flexibility given by shaders can be daunting There is a “traditional” way of using the shaders to render Similar to the original fixed pipeline We will cover this But there is no requirement to follow this or any other approach: Can be incredibly imaginative in the use of shaders: Render in non-standard ways Performance optimisations Non-graphics related programming Etc.

6 Shader Overview (DX10) The 3 rounded parts are shaders:
The Vertex Shader Process each individual vertex, uses matrices to transform from 3D to 2D May also do other vertex work, such as deformation or animation The Geometry Shader Process each primitive (usu. triangles) Has no specific task, but can be used to create, manipulate or delete triangles prior to rendering The Pixel Shader Processes each 2D pixel within the 2D polygons to render Combines colours to give final colour for pixel. Used for lighting / texturing

7 Preview: Lighting & Texturing
We have not yet covered lighting and texturing But I need to mention them in this topic We will cover them in full in later lectures, but briefly: Lighting is calculated during vertex/pixel processing Two colours calculated: diffuse and specular colour Diffuse is the general light level, specular is a shiny highlight Each vertex will need a vertex normal for these calculations Textures are bitmaps mapped onto mesh polygons Each vertex has a texture coordinate (called a UV) to indicate which part of the texture applies there Lighting and texturing create colours, that are combined together with any model colour during the pixel processing stage

8 Programming Shaders Shaders can be written in assembly language
For exact control of the underlying hardware More common to use a high level language: HLSL – for DirectX (High Level Shader Language) GLSL - for OpenGL Can write separate HLSL files, one per shader Compile in Visual Studio Or use “Effect” files (.fx) Compile at runtime This is a legacy approach, but you may see it in tutorials

9 Shader Languages We will look at HLSL in some detail
OpenGL’s GLSL is very similar Similar in style to C++ Using much of the same syntax and keywords But lacks many OO features and is limited in other ways These high-level programs are compiled into shader machine code At run-time or in advance We write shaders for each and every effect needed Different shaders are loaded in and out of the GPU as needed (at run-time)

10 Writing Shaders: Input / Outputs
Each stage of the pipeline takes input from the previous stage and outputs to next stage So define shaders in terms of their inputs and outputs. E.g. for a vertex shader: struct VS_Input { float3 ModelPosition : Position; // Input float3 ModelNormal : Normal; // data float2 UV : UV; }; struct VS_Output { float4 Position2D : SV_Position; // Output float2 UV : UV; // data float3 Colour : Colour; }; VS_Output MyVertexShader(VS_Input vIn) // Vertex shader function { /* Shader code... */ } This shader takes 3D model position, normal and UVs and outputs a 2D position, UVs and colour

11 Inputs / Outputs Inputs/outputs are flexible
More inputs and outputs can be added as required, e.g A vertex colour More UVs for a second texture Whilst others can be omitted The exact operation of the shader to produce the output is not constrained There are standard shader approaches based on traditional graphics theory But we are able to adjust or adapt these at will

12 Vertex Shaders The vertex shader operates one-by-one through each vertex in the original 3D geometry Its standard usage is to transform and project the vertex into 2D viewport space The input for a vertex shader is the mesh 3D vertex data We can provide any vertex data we like (from C++), so this is very flexible Vertex must always have a 3D position Can optionally have normals, UVs, vertex colours etc. A single rule: A vertex shader must at least output the 2D projected position for the vertex

13 Pixel Shaders A pixel shader operates on each pixel in each 2D polygon: Its standard usage is to: Sample any texture colours on that pixel (if using textures) Combine texture, lighting and polygon colours Pixel shader input is usually vertex shader output A pixel shader must always output a pixel colour To be drawn or blended with the viewport pixels It can also alter the depth value for the depth buffer - for special fx Pixel shaders must be efficient Run millions of times per frame

14 A Simple Pixel Shader // Pixel shader to blend a texture with the geometry colours Texture2D Texture; // A texture // Input data to pixel shader is output from vertex shader struct PS_Input { float4 Position2D : SV_Position; // Position of pixel float2 UV : TEXCOORD0; // Texture coordinate for pixel float3 Colour : COLOR0; // Colour for pixel, from mesh }; // or from lighting calculation // Pixel shader function (SV_Target = output goes to render target) float4 MyPixelShader( PS_Input input) : SV_Target // Return a colour // Sample texture colour for pixel float4 texColour = Texture.Sample( Bilinear, input.UV ); // Blend with geometry colour and output to next stage return input.Colour * texColour; }

15 Geometry Shaders The geometry shader was introduced in DirectX 10
Operates on each primitive Triangles, lines, points Can alter, remove or add primitives Tessellation, procedural geometry, particle systems The geometry shader is not required for “standard” pipeline It can be left out More and more advanced algorithms use it though


Download ppt "Computer Graphics Introduction to Shaders"

Similar presentations


Ads by Google