Programmable Graphics Hardware and Shaders - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Programmable Graphics Hardware and Shaders

Description:

example functionality: vertex displacements for mesh deformation (e.g. for fish-eye lens effect) ... http://www.toymaker.info/Games/html/pixel_shaders.html#6) ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 24
Provided by: claudecch
Category:

less

Transcript and Presenter's Notes

Title: Programmable Graphics Hardware and Shaders


1
Fac. of Comp., Eng. Tech. Staffordshire
University
3D Computer Graphics
Programmable Graphics Hardware and Shaders
Dr. Claude C. Chibelushi
2
Outline
  • Introduction
  • Programmable graphics hardware
  • Graphics processing unit (GPU)
  • GPU programming model
  • Shaders
  • Vertex shader
  • Pixel shader
  • Shader languages
  • Summary

3
Introduction
  • Shader custom program segment inserted into
    graphics pipeline and executed on GPU
  • replaces specific parts of fixed-function
    pipeline
  • comprises instructions that operate on
  • vertex data (vertex shader) vertex
    transformation and lighting calculations
  • or pixel data (pixel shader) e.g. pixel lighting
    calculations

4
Introduction
  • Shaders offer code development flexibility and
    processing speed
  • flexibility shader implements customised vertex
    or pixel processing
  • speed shader runs on fast specialised chips
  • many current graphics cards support shaders
  • Shaders often used for better visual quality in
    real time

5
Introduction
  • Recent feature of DirectX Graphics and OpenGL
  • addition of programmable pipeline
  • API provides high-level or assembly language
    interface to processing hardware
  • specialised language for vertex and pixel
    processing
  • developer can program special code for real-time
    graphics applications
  • e.g. rich graphical effects such as simulation of
    cloth, hair or fur

6
Programmable Graphics Hardware
  • Graphics processing unit (GPU)
  • Graphics cards used to be fixed-function
  • graphics processors hard-wired for specific
    functions
  • graphics limited by implementation of device
    driver and underlying hardware
  • fixed-function constraints overcome by running
    code on
  • general-purpose CPU (has limited support for
    graphics, hence slow)
  • programmable graphics processor(s) known as GPU
    (fast)
  • code downloaded onto GPU during rendering

7
Programmable Graphics Hardware
  • Graphics processing unit (GPU)
  • Special purpose microprocessor(s)
  • multiple programmable processing units for
    concurrent processing on single chip
  • optimised for graphics processing, e.g. vector /
    matrix manipulations
  • used in computer graphics (accelerator) card
  • for manipulation and display of graphics data
  • can perform some tasks of pipeline
  • evolving hardware capability improving with time

8
Programmable Graphics Hardware
  • DirectX 9 graphics pipeline

9
Programmable Graphics Hardware
CPU
GPU
VertexProcessor
FragmentProcessor
Frame bufferOperations
Assembly Rasterization
Application
Frame buffer
Textures
GPU programming model (Source Bill Mark,
NVIDIA Programmable Graphics Technology,
SIGGRAPH 2002 course)
10
Shaders
  • Definition fragment corresponds to pixel
  • has attributes such as colour, depth, texture
    coordinates
  • fragments are generated by rasterisation

11
Shaders
  • Vertex shader
  • Vertex shader bypasses fixed-function
    transformation and lighting pipeline segment
  • processes vertex
  • runs on programmable vertex processor
  • example functionality vertex displacements for
    mesh deformation (e.g. for fish-eye lens effect)

12
Shaders
  • Vertex shader
  • Vertex shader outputs attributes that can be
    interpolated during fragment processing
  • fragment processing done in subsequent stages of
    graphics pipeline
  • e.g. light reflected at vertex can be calculated
    in vertex shader for use in pixel shader

13
Shaders
  • Pixel shader
  • Also known as fragment shader
  • processes pixel / fragment
  • runs on pixel / fragment processor during
    rasterisation
  • example functionality shading / texturing
    effects, per-pixel lighting
  • e.g. accurate lighting models, simulation of
    complex surface properties or natural phenomena

14
Shader Languages
  • Shader programming requires language specialised
    for graphics
  • needs graphics-specific data types (e.g. vector,
    matrix) and operations (e.g. dot product, matrix
    multiply)
  • language often tied to specific graphics API
  • Actual shader code is assembly code
  • but high-level language can be compiled to
    assembly
  • compilers available

15
Shader Languages
  • High-level languages
  • historically preceded by various low-level
    languages such as Microsofts ASM
  • low-level programming has low productivity,
    limited portability and low code readability /
    maintainability
  • variety of languages, e.g.
  • OpenGL shading language (GLSL or glslang)
  • for use with OpenGL.
  • C-like languages jointly developed by Microsoft
    and NVidia (essentially same language but
    different brand names)
  • High-Level Shader Language (HLSL) (Microsoft)
  • for use with DirectX HLSL compiler is part of
    D3DX library
  • Cg (NVIDIA)
  • support for DirectX Graphics and OpenGL

16
Shader Languages
  • HLSL
  • Sample intrinsic data types
  • vector types contain from 1 to 4 components,
    e.g.
  • int1 vector containing 1 int float3 vector
    containing 3 floats double4 vector containing 4
    doubles
  • matrix types contain rows and columns of
    same-type data, e.g.
  • int2x1 integer matrix with 2 rows and 1 column
    float4x4 float matrix with 4 rows and 4 columns

17
Shader Languages
  • HLSL
  • Sample intrinsic functions
  • dot(a, b) returns dot product of vectors a and b
  • mul(A, B) returns product of matrices A and B
  • reflect(i, n) returns reflection vector given
    incident ray direction i and surface normal n
  • round(x) returns x rounded to nearest integer

18
Shader Languages
  • Sample code simple HLSL shaders
  • Example 1 (from DirectX Graphics documentation
    (http//msdn.microsoft.com/library/default.asp?url
    /library/en-us/directx9_c/HLSL_Shaders.asp))
  • / Vertex shader
  • Functionality
  • transforms vertex position from object space to
    projection space
  • assigns per-vertex colour from vertex input /
  • // Note POSITION and COLOR0 are
    semantics that tell compiler usage of variable
  • float4x4 mWorldViewProj // World View
    Projection transformation
  • // (global variable set by application)
  • // shader output structure
  • struct VS_OUTPUT
  • float4 Position POSITION // vertex
    position
  • float4 Diffuse COLOR0 // vertex
    diffuse color

19
Shader Languages
  • Sample code simple HLSL shaders
  • Example 1 (ctd.)
  • VS_OUTPUT Vertex_Shader_Transform( in float4
    vPosition POSITION, in float4 vColor COLOR0)
  • VS_OUTPUT Output
  • // Transform the vertex into projection
    space.
  • Output.Position mul( vPosition,
    mWorldViewProj )
  • // Set the diffuse color for output
  • Output.Diffuse vColor
  • return Output

20
Shader Languages
  • Sample code simple HLSL shaders
  • Example 1 (ctd.)
  • / Pixel shader /
  • Functionality copies interpolated vertex colour
    data, and outputs pixel colour
  • float4 Pixel_Shader( VS_OUTPUT in ) COLOR0
  • float4 color in. Diffuse // copy input
    colour
  • / float4 color in.Color / // note error in
    original example code
  • return color

Output semantic
Input parameter
21
Shader Languages
  • Sample code simple HLSL shaders
  • Example 2 Ambient and diffuse reflection models
    to simulate matte surface (adapted from
    http//www.toymaker.info/Games/html/pixel_shaders.
    html6)
  • / Vertex Shader
  • Functionality
  • transform vertex position, normalise light vector
    and vertex normal,
  • transform vertex normal /
  • // shader output structure
  • struct VS_OUTPUT
  • float4 Pos POSITION // vertex position
  • float3 Light TEXCOORD0 // normalised light
    vector
  • float3 Norm TEXCOORD1 // normalised vertex
    normal

22
Shader Languages
  • Sample code simple HLSL shaders
  • Example 2 (ctd.)
  • // Vertex shader
  • // Note matWorldViewProj to be passed in from
    application, given semantic WORLDVIEWPROJECTION
  • VS_OUTPUT vertShader(float4 vertPos POSITION,
    float3 vertNorm NORMAL)
  • VS_OUTPUT Out (VS_OUTPUT)0
  • Out.Pos mul(vertPos, matWorldViewProj) //
    transform position from model to screen space
  • Out.Light normalize(vecLightDir) //
    normalise light vector
  • Out.Norm -normalize(mul(vertNorm,
    matWorld)) // transform normal from model to
    world space, and normalize
  • return Out

23
Shader Languages
  • Sample code simple HLSL shaders
  • Example 2 (ctd.)
  • / Pixel Shader
  • Functionality
  • output pixel colour calculated using ambient and
    diffuse reflection models
  • (from light and normal vector input from
    pipeline) /
  • // Note some values set by application, e.g.
    global variables Dintensity, Dcolour, Aintensity,
    Acolour.
  • float4 pixShader(float3 Light TEXCOORD0, float3
    Norm TEXCOORD1) COLOR
  • // calculate diffuse reflection
  • float4 difsRefDintensityDcolour(dot(Norm,Light
    ))
  • // combine diffuse reflection ambient light
    reflection
  • return AintensityAcolourdifsRef

24
Suggested Reading
  • http//www.microsoft.com/directx/default.asp
    (Microsoft DirectX homepage)
  • http//developer.nvidia.com/page/cg_main.html (Cg
    homepage)

25
Summary
  • Shaders offer code development flexibility and
    processing speed
  • Programmable hardware pipeline on GPU
  • concurrent processing on single chip
  • Shader program segment inserted into graphics
    pipeline and executed on GPU
  • replaces section of fixed-function pipeline
  • vertex shader or pixel shader
  • Variety of shader languages
Write a Comment
User Comments (0)
About PowerShow.com