48
Real-Time Shading Using Programmable Graphics Hardware Introduction, Setup and Examples Wan-Chun Ma National Taiwan University

Real-Time Shading Using Programmable Graphics Hardware

  • Upload
    reuben

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

Real-Time Shading Using Programmable Graphics Hardware. Introduction, Setup and Examples Wan-Chun Ma National Taiwan University. Course Infomation. Instructor Wan-Chun Ma, Alex Dept. of Computer Science and Information Engineering, National Taiwan University - PowerPoint PPT Presentation

Citation preview

Page 1: Real-Time Shading Using Programmable Graphics Hardware

Real-Time Shading Using Programmable Graphics Hardware

Introduction, Setup and Examples

Wan-Chun MaNational Taiwan University

Page 2: Real-Time Shading Using Programmable Graphics Hardware

Course Infomation Instructor

Wan-Chun Ma, Alex Dept. of Computer Science and Information Engineering, Nati

onal Taiwan University http://graphics.csie.ntu.edu.tw/~firebird

Course 4/28, 5/5, 5/12, 5/14 Suggested readings

R. Fernando and M. J. Kilgard. The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics, Addison-Wesley, 2003 (beginners only!)

R. J. Rost. OpenGL Shading Language, Addison-Wesley, 2004 http://graphics.csie.ntu.edu.tw/~firebird/dokuwiki/doku.php?

id=tech:courses:dci_rts:home

Page 3: Real-Time Shading Using Programmable Graphics Hardware

The Student... The student should be familiar with

C/C++ programmingGraphics basics

Transformations in 3D (translation, rotation, modelview, projection)

Rasterization Texturing

OpenGL GLUT, GLUI Use texturing in OpenGL

Page 4: Real-Time Shading Using Programmable Graphics Hardware

Today’s Schedule Introduction Setup of Programming Environment Real-Time Shading Examples

Page 5: Real-Time Shading Using Programmable Graphics Hardware

Introduction

Page 6: Real-Time Shading Using Programmable Graphics Hardware

Evolution of GPUs

Virtual FighterSEGA

Dead or Alive 3Temco

Dawn DemoNVIDIA

NV1 Xbox (NV2A) GeForce FX (NV30)

50K triangles/sec1M pixel ops/sec1M transistors

100M triangles/sec1G pixel ops/sec20M transistors

200M triangles/sec2G pixel ops/sec120M transistors

1995 2001 2003

Page 7: Real-Time Shading Using Programmable Graphics Hardware

The 5 Generations of GPU 1st generation (up to 1998)

NVIDIA TNT2, ATI Rage, 3dfx Voodoo3 Lack of transform vertices of 3D objects. Vertex

transformation are done by CPU Limited math operations for combining textures to

compute the color of pixels

2nd generation (1999-2000) NVIDIA GeForce 256, GeForce 2, ATI Radeon 7500 GPU has the ability to do transformation and lighting. Bot

h OpenGL and DirectX 7 support vertex transformation by hardware

Configurable (in driver level) but not programmable

Page 8: Real-Time Shading Using Programmable Graphics Hardware

The 5 Generations of GPU 3rd generation (2001)

NVIDIA GeForce 3, GeForce 4 Ti, Xbox, ATI Radeon 8500 Vertex programmability: DirectX 8 vertex shader and OpenG

L ARB vertex program Pixel-level configurable

4th generation (2002) NVIDIA GeForce FX, ATI Radeon 9700 Vertex and pixel programmability High-level shading language (NVIDIA Cg, Microsoft HLSL, Ope

nGL GLSL)

5th generation (2004) NVIDIA GeForce 6, ATI Radeon X Infinite length shader program Dynamic flow control

Page 9: Real-Time Shading Using Programmable Graphics Hardware

GPU Model (Old) Fixed function pipeline

Page 10: Real-Time Shading Using Programmable Graphics Hardware

GPU Model (Current) Programmability!

Page 11: Real-Time Shading Using Programmable Graphics Hardware

GPU Process

VertexProcessin

g

FragmentProcessing

Page 12: Real-Time Shading Using Programmable Graphics Hardware

Programming GPU However, programming in assembly

is painfulDP3 R0, c[11].xyzx, c[11].xyzx;RSQ R0, R0.x;MUL R0, R0.x, c[11].xyzx;MOV R1, c[3];MUL R1, R1.x, c[0].xyzx;DP3 R2, R1.xyzx, R1.xyzx;RSQ R2, R2.x;MUL R1, R2.x, R1.xyzx;ADD R2, R0.xyzx, R1.xyzx;DP3 R3, R2.xyzx, R2.xyzx;RSQ R3, R3.x;MUL R2, R3.x, R2.xyzx;DP3 R2, R1.xyzx, R2.xyzx;MAX R2, c[3].z, R2.x;MOV R2.z, c[3].y;MOV R2.w, c[3].y;LIT R2, R2;

DX8 shader instructions Basic: mov, add, mul, mad, rsq… Vector operation: dp3, dp4… Miscellaneous: lit, exp, log, min, max, te

x…

Oh my god!

Page 13: Real-Time Shading Using Programmable Graphics Hardware

Programming GPU The need of high level shading

languageCompile

DP3 R0, c[11].xyzx, c[11].xyzx;RSQ R0, R0.x;MUL R0, R0.x, c[11].xyzx;MOV R1, c[3];MUL R1, R1.x, c[0].xyzx;DP3 R2, R1.xyzx, R1.xyzx;RSQ R2, R2.x;MUL R1, R2.x, R1.xyzx;ADD R2, R0.xyzx, R1.xyzx;DP3 R3, R2.xyzx, R2.xyzx;RSQ R3, R3.x;MUL R2, R3.x, R2.xyzx;DP3 R2, R1.xyzx, R2.xyzx;MAX R2, c[3].z, R2.x;MOV R2.z, c[3].y;MOV R2.w, c[3].y;LIT R2, R2;

// A Phong model shaderCOLOR c = k_a + k_d * dot(N, L) + k_s * pow(max(0, dot(N, H)), k_exp);

High level shading language Easier to read and modify Cross-platform Code reuse

Page 14: Real-Time Shading Using Programmable Graphics Hardware

Cg: A Shading Language Cg is a high level language from NVIDIA for pro

gramming GPUs, developed in close collaboration with Microsoft

Cg stands for “C for Graphics”

Cg enables a dramatic productivity increase for graphics development developers of: Games CAD tools Scientific visualizations

Page 15: Real-Time Shading Using Programmable Graphics Hardware

Cg: A C-like Language Syntax, operators, functions from C Conditionals and flow control (for, if) Particularly suitable for GPUs:

Express data flow of pipeline/stream architecture of GPUs (e.g. vertex-to-pixel)

Vector and matrix operations Support hardware data types for maximum performance Exposes GPU functions for convenience and speed:

Intrinsic: (mul, dot, sqrt, exp, pow) Built-in: extremely useful and GPU optimized math, utility and g

eometric functions (noise, ddx, ddy, reflect) Compiler uses hardware profiles to subset Cg as requi

red for particular hardware feature sets

Page 16: Real-Time Shading Using Programmable Graphics Hardware

Cg Workflow Architecture

Page 17: Real-Time Shading Using Programmable Graphics Hardware

Cg WorkflowShader Development Application

Cg program source code

// Diffuse lightingfloat d = dot(normalize(N), normalize(L));if (d < 0) d = 0;c = d*tex2D(texture, T)*diffuse;

1. Load/bind program

2. Specify program parameter

3. Specify vertex inputs

4. Render

Cg Compiler

Shader program assembly code

DP3 r0.x, f[TEX0], f[TEX0];RSQ r0.x, r0.x;MUL r0, r0.x, f[TEX0];DP3 r1.x, f[TEX1], f[TEX1];RSQ r1.x, r1.x;MUL r1, r1.x, f[TEX1];DP3 r0, r0, r1;MAX r0.x, r0.x, 1.0;MUL r0, r0.x, DIFFUSE;TEX r1, f[TEX1], 0, 2D;MUL r0, r0, r1;

Shader Compiler

Shader binary0000h: 54 68 69 73 20 69 73 20 65 2D 54 65 58 2C 20 560010h: 65 72 73 69 6F 6E 20 33 2E 31 34 31 35 39 32 2D0020h: 32 2E 31 20 28 4D 69 4B 54 65 58 20 32 2E 34 290030h: 20 28 70 72 65 6C 6F 61 64 65 64 20 66 6F 72 6D0040h: 61 74 3D 6C 61 74 65 78 20 32 30 30 34 2E 36 2E

Page 18: Real-Time Shading Using Programmable Graphics Hardware

What Cg can do? Real-time visual effects

Page 19: Real-Time Shading Using Programmable Graphics Hardware

What Cg can do? Lots of effects…

Page 20: Real-Time Shading Using Programmable Graphics Hardware

Coffee Break Next section: Setup of Programming

Environment

Page 21: Real-Time Shading Using Programmable Graphics Hardware

Setup of Programming Environment

Page 22: Real-Time Shading Using Programmable Graphics Hardware

Requirement Hardware

The computer should be equipped with programmable graphics hardware

NVIDIA FX, NVIDIA 6, ATI 9x00, ATI X series

SoftwareMicrosoft Visual Studio .NET 2003GLUT, GLUI...

Page 23: Real-Time Shading Using Programmable Graphics Hardware

Installation Cg Toolkit 1.3 (10MB)

http://developer.nvidia.com/object/cg_toolkit.html

Check the “Cg Installer for Windows”

NVIDIA SDK 9.0 (340MB, not required) http://developer.nvidia.com/object/sdk_home.ht

ml

FX Composer 1.6 (60MB, not required) http://developer.nvidia.com/object/fx_composer_

home.html Check the “FX Composer 1.6 Installer”

Page 24: Real-Time Shading Using Programmable Graphics Hardware

Installation If default installation locations are

used, all the packages are installed in the folder of C:\Program Files\NVIDIA Corporation\

C:\Program Files\NVIDIA Corporation\Cg\NVIDIA FX Composer\SDK 9.0\

Page 25: Real-Time Shading Using Programmable Graphics Hardware

My Stuff Several useful codes I collect

http://graphics.csie.ntu.edu.tw/~firebird/download/dci_rts/class.zip

Download it and unpack it into a folder, sayD:\My Projects\Class\Any folder is ok, but remember where

you put it

Page 26: Real-Time Shading Using Programmable Graphics Hardware

VC++ Directories Execute visual studio

Tools, Options, Projects, VC++ DirectoriesShow the directories for: Include files

D:\My Project\Class (remember My Stuff?) C:\Program Files\NVIDIA Corporation\Cg\include

Library files C:\Program Files\NVIDIA Corporation\Cg\lib

Page 27: Real-Time Shading Using Programmable Graphics Hardware

Ready to Go A small engine

http://graphics.csie.ntu.edu.tw/~firebird/download/dci_rts/env.zip

I will use this engine for shader development during these courses

The first examplehttp://graphics.csie.ntu.edu.tw/~firebird/do

wnload/dci_rts/ex01.zip

Page 28: Real-Time Shading Using Programmable Graphics Hardware

Compilation cgc –profile profiles filename

profiles: graphics hardware profiles Vertex: arbvp1, vp20, vp30, vp40... Fragment: arbfp1, fp20, fp30, fp40...

filename: filename of the shader

Examplescgc –profile vp30 test_vtx.cxxcgc –profile fp30 test_frg.cxx

Page 29: Real-Time Shading Using Programmable Graphics Hardware

Debugging Debugging is very hard (it is GPU, not

CPU) However, you may still use

intermediate visualization to debug your programOutput intermediate data (e.g. position,

normal, textures…) as color

Page 30: Real-Time Shading Using Programmable Graphics Hardware

Coffee Break Next section: Real-time Shading

Examples

Page 31: Real-Time Shading Using Programmable Graphics Hardware

Real-Time Shading Examples

Page 32: Real-Time Shading Using Programmable Graphics Hardware

Progression Games push hardware, hardware

advances games

Page 33: Real-Time Shading Using Programmable Graphics Hardware

Effects in Games

Shadows

Level of detail

Reflection

Shading

Smoke

Page 34: Real-Time Shading Using Programmable Graphics Hardware

Effects in Games

Bump mapping

Light mapping

Per-pixel lighting

Multi-texturing

Page 35: Real-Time Shading Using Programmable Graphics Hardware

Multi-pass Rendering The rendering pass is not fixed

anymore. A single rendering pass may consists of many functional programs

Page 36: Real-Time Shading Using Programmable Graphics Hardware

Multi-pass Rendering Each different program (effect) is

handled individually, and finally summed up to become rendering result

Page 37: Real-Time Shading Using Programmable Graphics Hardware

Cg Samples Check out the effect samples in

NVIDIA SDK Browser

Page 38: Real-Time Shading Using Programmable Graphics Hardware

The First Cg Example A Phong model shader with color textur

e

ShadersVertex: ex1_vtx.cxxFragment: ex1_frg.cxx

TextureDiffuse: wood.bmp

Page 39: Real-Time Shading Using Programmable Graphics Hardware

Vertex Shader

struct v2f{float4 P2D : POSITION; // projected 2D positionfloat4 C : COLOR0; // colorfloat4 T : TEXCOORD0; // texture coordfloat3 P3D : TEXCOORD1; // vertex 3D positionfloat3 N : TEXCOORD2; // normalfloat3 G : TEXCOORD3; // tangentfloat3 B : TEXCOORD4; // binormal};

Vertex-to-fragment data structure

Page 40: Real-Time Shading Using Programmable Graphics Hardware

Vertex Shader Main (application-to-vertex)

argumentsv2f main(float4 C : COLOR,float4 P : POSITION,float4 N : NORMAL,float4 T : TEXCOORD0,uniform float4x4 ModelViewProj,uniform float4x4 ModelView,uniform float4x4 ModelViewIT)

Page 41: Real-Time Shading Using Programmable Graphics Hardware

Vertex Shader Main body

{v2f OUT;OUT.P2D = mul(ModelViewProj, P);OUT.P3D = P.xyz;OUT.T = T;OUT.N = normalize(N.xyz); // normalOUT.G = normalize(2.0*C.xyz - 1.0); // tangentOUT.B = normalize(cross(OUT.G, OUT.N));return OUT;}

Page 42: Real-Time Shading Using Programmable Graphics Hardware

Fragment Shader Fragment-to-screen data structure

struct f2s{float4 C : COLOR0;};

Page 43: Real-Time Shading Using Programmable Graphics Hardware

Fragment Shader Main (vertex-to-fragment) arguments

f2s main(v2f IN,uniform sampler2D tex01, // texture 01uniform float3 L,uniform float3 V)

Page 44: Real-Time Shading Using Programmable Graphics Hardware

Fragment Shader Main body

{f2s OUT; OUT.rgb = 0;L = normalize(L); V = normalize(V);float3 H = normalize(L+V);float diff = dot(normalize(IN.N), L);if(diff > 0){

float spec = 2*pow(dot(IN.N, H), 128);OUT.C.rgb = diff*tex2D(tex01, IN.T.xy) + spec;

}return OUT;}

Page 45: Real-Time Shading Using Programmable Graphics Hardware

Result

Page 46: Real-Time Shading Using Programmable Graphics Hardware

Try This... Output red color for all fragments

{f2s OUT; OUT.rgb = 0;// L = normalize(L); V = normalize(V);// float3 H = normalize(L+V);// float diff = dot(normalize(IN.N), L);// if(diff > 0)// {

// float spec = 2*pow(dot(IN.N, H), 128);OUT.C.rgb = float3(1.0, 0.0, 0.0);

// }return OUT;}

Page 47: Real-Time Shading Using Programmable Graphics Hardware

Try This... Visualize normal vectors

{f2s OUT; OUT.rgb = 0;// L = normalize(L); V = normalize(V);// float3 H = normalize(L+V);// float diff = dot(normalize(IN.N), L);// if(diff > 0)// {

// float spec = 2*pow(dot(IN.N, H), 128);OUT.C.rgb = (IN.N+1)/2;

// }return OUT;}

Page 48: Real-Time Shading Using Programmable Graphics Hardware

End