Computer Graphics Introduction to Shaders - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Computer Graphics Introduction to Shaders

Description:

Have mainly focused on the vertex processing ... Two colours calculated: diffuse and specular colour. Diffuse is main light level, specular is a shiny highlight ... – PowerPoint PPT presentation

Number of Views:231
Avg rating:3.0/5.0
Slides: 16
Provided by: lauren80
Category:

less

Transcript and Presenter's Notes

Title: Computer Graphics Introduction to Shaders


1
Computer GraphicsIntroduction to Shaders
  • CO2409 Computer Graphics
  • Week 10

2
Lecture Contents
  • Limitations of the Fixed Pipeline
  • The Programmable Pipeline
  • Shader Languages
  • Inputs / Outputs
  • Vertex Shaders
  • Pixel Shaders
  • Geometry Shaders

3
The Fixed Pipeline
  • So far we used the fixed rendering pipeline
  • Have mainly focused on the vertex processing
    stage, performing the standard matrix transform
    sequence
  • Used geometry processing stage for culling
  • Pixel processing/rendering to render coloured
    triangles
  • Could go on to look at lighting texturing in
    the fixed pipeline
  • But it is rarely used nowadays, the programmable
    pipeline is now the method of choice

4
Limitations of the Fixed Pipeline
  • The functionality of the fixed pipeline cannot be
    changed much
  • Limited by the API (DirectX, OpenGL, etc.)
  • What if we want an unusual effect?
  • Non-standard matrix transforms
  • A specialist lighting model or texturing
    technique
  • E.g. Cell Shading, Fur, Motion blur etc.

5
Fixed Pipeline Workarounds
  • Maybe we can leverage the fixed functionality to
    support the effects we want
  • Likely to compromise our requirements
  • Or maybe we can do some of the special effect
    work in our C program
  • Rather than using the graphics API (DirectX)
  • This is a waste of CPU power if the video card is
    potentially capable of the effect
  • This is a key point the fixed pipeline is fixed
    because of the API, not the hardware

6
The Programmable Pipeline
  • Modern video cards have (partially) programmable
    pipelines
  • At least two pipeline stages can be programmed
  • The vertex and pixel processing stages
  • Fixed versions can be replaced by our own
    programming

7
Preview Lighting Texturing
  • Have not yet covered lighting and texturing
  • Will be mentioned in this topic for completeness
  • Will cover them in later lectures. So briefly
  • Lighting is calculated during vertex processing
  • Two colours calculated diffuse and specular
    colour
  • Diffuse is main 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 textures are combined with any mesh
    colour during the pixel processing stage

8
Programming Shaders
  • Programs in the pipeline are called shaders
  • Vertex shaders and pixel shaders
  • Shaders can be written in assembly language
  • For exact control of the underlying hardware
  • Can use a high level language instead
  • HLSL for DirectX (High Level Shader Language)
  • (O)GLSL - for OpenGL
  • Cg - A language from NVIDIA

9
Shader Languages
  • We will look at HLSL in some detail
  • GLSL is very similar
  • Both are similar in style to C
  • Using much of the same syntax and keywords
  • These high-level programs are compiled into
    shader machine code
  • At run-time or in advance
  • We write a shader program for each and every
    effect needed
  • Different shaders are loaded in and out of the
    video card as needed (as the application runs)

10
Writing Shaders Input / Outputs
  • Each stage of the pipeline has inputs from the
    previous stage and outputs to next stage
  • So define shaders in terms of their inputs and
    outputs, here is a vertex shader declaration

struct VS_Input float4 vPosition POSITION
// Input float3 vNormal
NORMAL // data float2
vTexCoord0 TEXCOORD0 struct VS_Output
float4 vPosition POSITION // Output
float4 vColor COLOR //
data void main(in VS_Input i, out VS_Output o)
// Shader fn
  • This vertex shader takes 3D position, normal and
    UVs and outputs a 2D position and colour

11
Inputs / Outputs
  • Inputs/outputs are flexible
  • More inputs and outputs can be added
  • E.g. A vertex colour or more UVs for a second
    texture
  • Whilst others can be omitted
  • The exact operation of the shader to produce the
    output is not constrained
  • We can just emulate the fixed pipeline processes
  • But in general we want to use shaders to enhance
    or optimise the fixed pipeline

12
Vertex Shaders
  • A vertex shader operates on a single vertex in
    the original 3D geometry. Its basic usage is to
  • Transform and project the vertex into viewport
    space
  • Apply lighting to the vertex
  • The input for a vertex shader is raw 3D vertex
    data
  • We can customise vertex data structures, so this
    is very flexible
  • Vertex must always have a 3D position
  • Also normals, UVs, vertex colours etc.
  • A vertex shader must at least output a viewport
    position for the current vertex

13
Pixel Shaders
  • A pixel shader operates on each pixel in each 2D
    polygon Its basic usage is to
  • Sample the texture colour for the pixel
  • Combine texture, lighting and polygon colours
  • Pixel shader input is usually vertex shader output
  • A pixel shader must at least output a pixel
    colour
  • To be drawn or blended with the viewport
  • Can also alter the depth value for the depth
    buffer - for special effects

14
A Simple Pixel Shader
  • // Pixel shader to blend a texture with the
    geometry colours
  • sampler2D Tex0 // A texture (sampler)
  • struct PS_Input // Input data
  • float2 TexCoord0 TEXCOORD0 // Texture
    coordinate for pixel
  • float4 Colour COLOR0 // Colour for
    pixel, from mesh
  • // or from
    lighting calculation
  • struct PS_Output // Output data
  • float4 Colour COLOR0 // Final pixel colour
    for viewport
  • // Main pixel shader function
  • void main( in PS_Input i, out PS_Output o )
  • float4 texColour tex2D(Tex0, i.TexCoord0)
    // Get tex colour

15
Geometry Shaders
  • Recent video cards add another programmable
    stage, the geometry shader
  • Supported under DirectX 10
  • The pipeline is revised
  • The geometry shader operates on each primitive
  • Triangles, lines, points
  • Can alter, remove or add primitives
  • Tessellation, procedural geometry, particle
    systems
Write a Comment
User Comments (0)
About PowerShow.com