gdv 1 - part ii - cgl.ethz.ch · 5 hw and programmable ge 17 gpu architecture (geforce 6800) hw and...

14
1 GDV 1 - Part I Classical Computer Graphics Graphics Pipeline: Colors – color spaces Transformations – affine / projection Lighting – Gouraud / Phong Texture mapping / Anti aliasing • Clipping • Rasterization GDV 1 - Part II Real-Time Graphics + Animation 14. Jan Programmable graphics hardware 21. Jan Real-time rendering techniques 28. Jan Terrain Visualization Flytastic 4. Feb Introduction to Animation Hardware Architectures and Programmable Graphics Engines HW and Programmable GE 4 Today Graphics Hardware – Evolution Architecture Example Graphics Programming Vertex Programs Fragment Programs Cg – C for Graphics

Upload: others

Post on 24-Oct-2019

14 views

Category:

Documents


0 download

TRANSCRIPT

1

GD

V 1 -

Pa

rt IC

lassica

l Co

mp

uter G

rap

hics

Graphics Pipeline:

•C

olo

rs –co

lor sp

aces

•T

ran

sform

atio

ns –

affin

e / pro

jectio

n

•Lig

htin

g –

Go

ura

ud

/ Ph

on

g

•T

extu

re m

ap

pin

g / A

nti a

liasin

g

•C

lipp

ing

•R

asteriza

tion

GD

V 1 -

Pa

rt IIR

eal-T

ime G

rap

hics +

An

ima

tion

14. Ja

nP

rog

ram

ma

ble

gra

ph

ics ha

rdw

are

21. Jan

Rea

l-time

rend

ering

tech

niq

ue

s

28. Ja

nT

errain

Visu

aliza

tion

Flytastic

4. Feb

Intro

du

ction

toA

nim

atio

n

Ha

rdw

are A

rchitectu

res an

d

Pro

gra

mm

ab

le Gra

ph

ics Eng

ines

HW

an

d P

rogra

mm

ab

le GE 4

To

da

y

•G

rap

hics H

ard

wa

re

–E

volu

tion

–A

rchite

cture

Ex

am

ple

•G

rap

hics P

rog

ram

min

g

–V

ertex

Pro

gra

ms

–Fra

gm

en

t Pro

gra

ms

–C

g –

C fo

r Gra

ph

ics

2

HW

an

d P

rogra

mm

ab

le GE 5

Evolu

tion

of G

rap

hics

Ha

rdw

are (1)

First successful3D

accelerator for PCs

HW

an

d P

rogra

mm

ab

le GE 6

Evolu

tion

of G

rap

hics

Ha

rdw

are (2)

Two survivors

HW

an

d P

rogra

mm

ab

le GE 7

Evolu

tion

of G

rap

hics

Ha

rdw

are (3)

•N

Vid

ia(w

ww

.nvid

ia.co

m)

NV1 (1995)

GeForce

6800 (2004)

= 1 huge GPU

+ Vent

HW

an

d P

rogra

mm

ab

le GE 8

Evolu

tion

of G

rap

hics

Ha

rdw

are (4

)

•A

TI (w

ww

.ati.co

m)

3D Rage (1995)

Radeonx850 (2004)

= 1 huge GPU

+ Vent

3

HW

an

d P

rogra

mm

ab

le GE 9

Wh

ere to evo

lve?

•P

erfo

rma

nce

–T

rian

gles / sec.

–P

ixe

l frag

men

ts / sec.

•Fe

atu

res

–P

rog

ram

ma

ble

sha

din

g (ve

rtex, fra

gm

en

t)

–T

ex

ture m

ap

pin

g (sh

ad

ow

s, bu

mp

s, disp

lacem

en

ts)

–Fu

ll-scene a

nti-a

liasin

g

•Q

ua

lity–

Nu

me

rical p

recision

(8-b

it integ

er →6

4 b

it floa

t)

–Im

ag

e Filters

HW

an

d P

rogra

mm

ab

le GE 10

Mo

ore’s La

w

•E

xp

on

en

tial g

row

th in

nu

mb

er o

f tran

sistors

(Go

rdo

n M

oo

re, 196

5)

•D

ou

ble

s eve

ry 18 m

on

ths (h

old

s for C

PU

’s)

yea

rly gro

wth

fa

ctor: 1.6

HW

an

d P

rogra

mm

ab

le GE 11

Nvid

iad

evelop

men

t

75M6

25M30

06

3MG

eFo

rce4

1H0

2

Pe

r-Pix

. Sh

ad

.25M

200

M20

025M

Ge

Force

2 GT

S1H

00

Ve

rtex

Sha

d.

25M4

17M20

057M

Ge

Force

31H

01

30M

500

M24

057M

Ge

Force

3 Ti50

02H

01

31M250

M250

25MG

eFo

rce2 U

ltra2H

00

HW

T&

L15M

120M

12023M

Ge

Force

256

2H9

9

200

M

9M

6M

3M 3M

po

ly ra

te

Floa

ting

Po

int

1500

M50

0125M

Ge

Force

FX1H

03

AG

P 4

x75M

1509

MT

NT

21H

99

32-bit, T

ri50

M10

07M

Riva

TN

T2H

98

AG

P 2x

31M?

5MR

iva Z

X1H

98

2D/3D

20M

?3M

Riva

1282H

97

ne

w fea

ture

sfill

rate

MH

z#

tran

sp

rod

uct

seaso

n

HW

an

d P

rogra

mm

ab

le GE 12

Nvid

iah

ard

wa

re

2.620

0M

2.4150

0M

Ge

Force

FX1H

03

2.2 1.6

1.4

2.5

1.6

2.6

2.6

2.3

2.6

2.4 -

yea

rra

te

6.3

75M6

25MG

eFo

rce4

1H0

2

2.825M

200

MG

eFo

rce2 G

TS

1H0

0

0.6

25M4

17MG

eFo

rce3

1H0

1

1.430

M50

0M

Ge

Force

3 Ti50

02H

01

1.531M

250M

Ge

Force

2 Ultra

2H0

0

2.815M

120M

Ge

Force

2562H

99

9M

6M

3M 3M

po

ly ra

te

2.5A

vera

ge

rate

2.375M

TN

T 2

1H9

9

4.0

50M

Riva

TN

T2H

98

1.031M

Riva

ZX

1H9

8

-20

MR

iva 128

2H9

7

yea

rra

tefill

rate

pro

du

ctsea

son

Esse

ntia

lly Mo

ore

’s law

squ

are

d!

4

HW

an

d P

rogra

mm

ab

le GE 13

Futu

re?

•D

eve

lop

me

nt d

riven

by g

am

es!

•C

ine

ma

:

–“T

oy S

tory”

(199

5) 7 h

ou

rs/fram

e to ren

der (m

ax

. 90

ho

urs)

–“S

hrek”

(200

1) m

ain

cha

racters: ~

80

0K

po

lygo

ns ea

ch

•N

ice: 80

M p

olyg

on

/fram

e @6

0H

z

–4

.8 b

illion

po

lygo

ns/se

c

HW

an

d P

rogra

mm

ab

le GE 14

Futu

re? (2)

•W

e ha

ve…–

Ve

ry hig

h fill ra

tes

–H

igh

po

lygo

n ra

tes

–Lo

ts of te

xtu

res

–“S

om

e”

pro

gra

mm

ab

ility

•W

e wa

nt…

–“Fu

ll”p

rog

ram

ma

bility

–Flex

ible g

eo

metry sp

ecificatio

n

HW

an

d P

rogra

mm

ab

le GE 15

Pro

gra

mm

ab

le G

rap

hics A

rchitectu

res

•Fro

m fix

ed

to p

rog

ram

ma

ble a

rchite

cture

–D

X7/N

V1x

/R10

0

con

figu

rab

le p

ixe

l stag

es.

–D

X8

/NV

2x/R

200

vertex

sha

de

rsa

nd

limite

d p

ixel sh

ad

ers.

–D

X9

/NV

3x/R

300

verte

x sh

ad

ers

with

flow

con

trol

pix

el sha

ders

with

arb

itrarily p

ositio

ne

dco

mm

an

ds.

•N

ea

r futu

re:–

DX

10?

gen

era

tion

of n

ew

verticesin

sha

ders

ne

w in

structio

ns.

•Fin

al g

oa

l?–

GP

U =

arra

y of fu

ll-fea

ture

d sh

ad

er

pro

cesso

rs w

itho

ut d

ivision

into

pix

el a

nd

vertex

sha

ders.

HW

an

d P

rogra

mm

ab

le GE 16

GP

U A

rchitectu

re (G

eForce

68

00

)•

Specifica

tion

s

–T

ran

sistors:

222M

–M

em

ory in

terfa

ce:

256-b

it

–M

emo

ry ba

nd

wid

th:

35.2GB

/sec

–V

ertex p

rocesso

rs:6

–V

ertices/sec.6

00

M

–P

ixel p

rocesso

rs 16

–P

ixels p

er C

lock (p

eak):

16

–Fill R

ate:

6.4

GP

ixe

ls/sec

–T

extu

res pe

r Pix

el:

16

–2 R

AM

DA

Cs

40

0M

Hz

•Se

e also

AT

I Ra

deo

nX

850

with

simila

r specs

5

HW

an

d P

rogra

mm

ab

le GE 17

GP

U A

rchitectu

re (G

eForce

68

00

)

HW

an

d P

rogra

mm

ab

le GE 18

GP

U A

rchitectu

re (G

eForce

68

00

)

•A

dva

nce

d ve

rtex

/frag

me

nt sh

ad

erfu

nctio

na

lity–

dyn

am

ic, con

ditio

na

l execu

tion

an

d flo

w co

ntro

l

–128

bit flo

atin

g-p

oin

t precisio

n co

mp

uta

tion

th

rou

gh

the

entire ren

de

ring

pip

eline

•Lo

ssless co

mp

ressio

na

lgo

rithm

s–

for co

lor, tex

ture a

nd

z da

ta

•A

niso

trop

ic filterin

g–

qu

ality u

p to

8x

of th

e u

sua

l bilin

ea

r tex

ture

–u

p to

128 d

iscrete

sam

ple

s pe

r on

e te

xtu

re fetch

•N

ew h

ybrid

An

tiaA

liasin

gm

od

es

•A

lot m

ore…

(see N

Vid

iasp

ecificatio

ns)

Gra

ph

ics Pro

gra

mm

ing

HW

an

d P

rogra

mm

ab

le GE

20

Tra

ditio

na

lG

rap

hics P

ipelin

e

fram

e-b

uffe

ro

ps

tex

ture

fetch

,fra

gm

en

t sha

din

g

setup

tran

sform

,lig

htin

g

raste

rizer

Each unit has a specific function(usually with configurable “m

odes”of operation)

6

HW

an

d P

rogra

mm

ab

le GE 21

Pro

gra

mm

ab

leSh

ad

ers

User-d

efined

Ve

rtex

Pro

cessing

User-d

efined

Frag

me

nt

Pro

cessing

fram

e-b

uffe

ro

ps

tex

ture

fetch

,fra

gm

en

t sha

din

g

setup

tran

sform

,lig

htin

g

raste

rizer

User-defined shadersbypass the conventional functions

Vertex

Pro

gra

mm

ing

HW

an

d P

rogra

mm

ab

le GE 23

Vertex

Pro

gra

mm

ing

Co

ncep

tua

l Overview

Ve

rtex

A

ttribu

tes

Ve

rtex P

rog

ram

Vertex

Resu

lt R

eg

isters

Tem

po

rary

Va

riab

les

Nx

4 reg

isters (N≥

16)

M in

structio

ns (M

≥128

)

Pro

gra

mLo

cal

Pa

ram

ete

rs

Lx4

registers (L≥

96

)

Tx

4 va

riab

les (T≥12)

Pro

gra

m

En

viron

men

t P

ara

me

ters

Ex4

registers (E≥

96

)

Rx

4 reg

isters (R≥8

)

Ad

dre

ss V

aria

ble

sA

x4

varia

bles (A

≥1)

HW

an

d P

rogra

mm

ab

le GE 24

Vertex

Pro

gra

mm

ing

•V

ertexp

rog

ram

–A

ssem

bly

lan

gu

ag

ein

terface

to T

&L u

nit

–G

PU

instru

ction

set

to p

erfo

rma

ll verte

xm

ath

•In

pu

t:

–A

rbitra

ry vertex

attrib

ute

s

–P

rog

ram

pa

ram

eters,

–O

pen

GL sta

tes

•O

utp

ut: tra

nsfo

rme

d ve

rtex

resu

lts

–H

om

og

en

eo

us clip

spa

ce p

ositio

n (re

qu

ired)

–C

olo

rs (fron

t/ba

ck, prim

ary/seco

nd

ary)

–Fo

g co

ord

ina

tes

–T

ex

ture

coo

rdin

ate

s

–G

en

eric ve

rtex

attrib

ute

s

7

HW

an

d P

rogra

mm

ab

le GE 25

Vertex

Pro

gra

mm

ing

(2)

•P

rog

ram

pro

pe

rties

–D

oe

s no

t ge

ne

rate

or d

estro

y vertice

s

Alw

ays 1 ve

rtex

in a

nd

1 verte

x o

ut

–N

o to

po

log

ical in

form

atio

n p

rovid

ed

No

ed

ge

, face, n

or n

eig

hb

orin

g ve

rtex

info

rma

tion

ava

ilab

le

–D

yna

mica

lly loa

da

ble

•E

xp

ose

d th

rou

gh

the

Op

en

GL A

RB

ex

tensio

n:

ARB_vertex_program

HW

an

d P

rogra

mm

ab

le GE

26

Vertex

Pro

gra

mm

ing

Assem

bly La

ng

ua

ge

•P

ow

erful S

IMD

instru

ction

set (27 in

structio

ns):

ABS, ADD, ARL, DP3, DP4, DPH, DST, EX2,

EXP, FLR, FRC, LG2, LIT, LOG, MAD, MAX,

MIN, MOV, MUL, POW, RCP, RSQ, SGE, SLT,

SUB, SWZ, XPD.

•In

structio

n fo

rma

t:Opcode

dest, src0

[,src1[,src2]];

•R

eg

isters: 4

-vecto

r or re

plica

ted

scala

r

–So

urce

reg

isters ca

n b

e n

eg

ated

:MAD R1, -R2, R3, R4;

–So

urce

reg

isters ca

n b

e “sw

izzled":

MOV R1, R2.yzwx;

–D

estin

atio

n re

giste

r can

be

ma

sked

:DP3 R1.zw, R2, R3;

HW

an

d P

rogra

mm

ab

le GE 27

Vertex

Pro

gra

mm

ing

Reg

ister Set

Ve

rtex

A

ttribu

tes

Ve

rtex P

rog

ram

Vertex

Resu

lt R

eg

isters

Tem

po

rary

Va

riab

les

vertex.*

Pro

gra

mLo

cal

Pa

ram

ete

rs

program.local[0]

…program.local[N-1]

user defined

Pro

gra

m

En

viron

men

t P

ara

me

ters

program.env[0]

…program.env[N-1]

result.*

Ad

dre

ss V

aria

ble

s

user defined

HW

an

d P

rogra

mm

ab

le GE 28

Vertex

Pro

gra

mm

ing

Attrib

utes &

Resu

lts

Attribute RegisterCom

ponentsUnderlying State

vertex

.po

sition

(x,y,z,w

)o

bject p

ositio

nverte

x.n

orm

al

(x,y,z,1)

no

rma

lverte

x.co

lor

(r,g,b

,a)

prim

ary co

lor

vertex

.tex

coo

rd[n

](s,t,r,q

)te

xtu

re co

ord

ina

te, un

it n…

……

vertex

.attrib

[n]

(x,y,z,w

)g

ene

ric vertex

attrib

ute

n

Result Register Com

ponentsDescription

result.p

ositio

n(x

,y,z,w)

po

sition

in clip

coo

rdin

ates

resu

lt.colo

r(r,g

,b,a

)fro

nt-fa

cing

, prim

ary co

lor

resu

lt.tex

coo

rd[n

](s,t,r,q

)te

xtu

re co

ord

ina

te, un

it n…

……

8

HW

an

d P

rogra

mm

ab

le GE

29

Vertex

Pro

gra

mm

ing

Pa

ram

eters & Sta

tes•

Pro

gra

mlo

calp

ara

meters

–Sto

re pro

gra

m-sp

ecific pa

ram

eters re

qu

ired

by th

e pro

gra

mPARAM foo= program.local[12];

•P

rog

ram

enviro

nm

ent

pa

ram

eters

–S

tore p

ara

mete

r com

mo

n to

a se

t of p

rog

ram

s (i.e

. mo

de

lviewm

atrix

, mo

del-vie

w-p

rojectio

n (m

vp) m

atrix

) PARAM time = program.env[0];

•O

pen

GL

state

pa

ram

eters

–A

ccess to

curre

nt O

pe

nG

L pro

pe

rties

PARAM ambient = state.material.ambient;

PARAM lightDir= state.light[0].position;

PARAM mat[4] = { state.matrix.modelview

};

HW

an

d P

rogra

mm

ab

le GE

30

Vertex

Pro

gra

mm

ing

Va

riab

les & C

on

stan

ts

•T

emp

ora

ryva

riab

les

–Fo

ur-co

mp

on

ent flo

atin

g-p

oin

t vectors u

sed to

store

interm

edia

te com

pu

tatio

ns

–D

eclared

befo

re first use:

TEMP tmp;

MOV tmp, vertex.position;

•C

on

stan

ts

–Ex

plicit co

nsta

nt b

ind

ing

PARAM c = {1.0, 2.0};

(1.0, 2.0, 0.0, 1.0)

–Im

plicit co

nsta

nt b

ind

ing

ADD a, b,

{7.0, -2.0, 0.33};

(7.0, -2.0, 0.33, 1.0)

HW

an

d P

rogra

mm

ab

le GE 31

Exa

mp

le Pro

gra

m #

1Sim

ple transform to clip space

!!ARBvp1.0

ATTRIB pos = vertex.position;

PARAM

mat[4] = { state.matrix.mvp

};

# Transform by concatenation of the

# MODELVIEW and PROJECTION matrices.

DP4 result.position.x, mat[0], pos;

DP4 result.position.y, mat[1], pos;

DP4 result.position.z, mat[2], pos;

DP4 result.position.w, mat[3], pos;

# Pass the primary color through w/o lighting.

MOV result.color, vertex.color;

END

HW

an

d P

rogra

mm

ab

le GE 32

Exa

mp

le Pro

gra

m #

2Sim

ple ambient, specular, and diffuse lighting

!!ARBvp1.0

ATTRIB iPos

= vertex.position;

ATTRIB iNormal= vertex.normal;

PARAM mvinv[4] = { state.matrix.modelview.invtrans

};

PARAM mvp[4] = { state.matrix.mvp

};

PARAM lightDir

= state.light[0].position;

PARAM halfDir

= state.light[0].half;

PARAM specExp

= state.material.shininess;

PARAM ambientCol

= state.lightprod[0].ambient;

PARAM diffuseCol

= state.lightprod[0].diffuse;

PARAM specularCol

= state.lightprod[0].specular;

TEMP eyeNormal, temp, dots, lightcoefs;

OUTPUT oPos

= result.position;

OUTPUT oColor

= result.color;

9

HW

an

d P

rogra

mm

ab

le GE 33

Exa

mp

le Pro

gra

m #

2 (2)# Transform the vertex to clip coordinates.

DP4 oPos.x, mvp[0], iPos;

DP4 oPos.y, mvp[1], iPos;

DP4 oPos.z, mvp[2], iPos;

DP4 oPos.w, mvp[3], iPos;

# Transform the normal into eye space.

DP3 eyeNormal.x, mvinv[0], iNormal;

DP3 eyeNormal.y, mvinv[1], iNormal;

DP3 eyeNormal.z, mvinv[2], iNormal;

# Compute diffuse and specular

dot products

# and use LIT to compute lighting coefficients.

DP3 dots.x, eyeNormal, lightDir;

DP3 dots.y, eyeNormal, halfDir;

MOV dots.w, specExp.x;

LIT lightcoefs, dots;

# Accumulate color contributions.

MAD temp, lightcoefs.y, diffuseCol, ambientCol;

MAD oColor.xyz, lightcoefs.z, specularCol, temp;

MOV oColor.w, diffuseCol.w;

END

Frag

men

t Pro

gra

mm

ing

HW

an

d P

rogra

mm

ab

le GE 35

Frag

men

t Pro

gra

mm

ing

Co

ncep

tua

l Overview

Frag

men

t A

ttribu

tes

Frag

men

t P

rog

ram

Frag

men

t Resu

lt R

eg

isters

Tem

po

rary

Va

riab

les

Nx

4 reg

isters (N≥

10)

M in

structio

ns (M

≥72)

Pro

gra

mLo

cal

Pa

ram

ete

rs

Lx4

registers (L≥

24)

Tx

4 va

riab

les (T≥16

)

Pro

gra

m

En

viron

men

t P

ara

me

ters

Ex4

registers (E≥

24)

1x4

colo

r reg

ister

1x1 d

epth

register

HW

an

d P

rogra

mm

ab

le GE

36

Frag

men

t Pro

gra

mm

ing

•Fra

gm

ent p

rog

ram

–A

ssemb

lyla

ng

ua

ge

inte

rface

to tex

ture, co

lor

sum

an

d fo

gu

nit

–G

PU

instru

ction

set

to p

erfo

rma

ll frag

men

tm

ath

(verysim

ilar

to ve

rtexp

rog

ram

s)

•In

pu

t: arb

itrary fra

gm

ent a

ttribu

tes, p

rog

ram

pa

ram

ete

rs,

Op

en

GL sta

tes

•O

utp

ut: fin

al co

lor a

nd

dep

th o

f frag

men

t

10

HW

an

d P

rogra

mm

ab

le GE 37

Frag

men

t Pro

gra

mm

ing

(2)

•P

rog

ram

pro

pe

rties

–D

oe

s no

t ge

ne

rate

frag

me

nts b

ut m

ay d

estro

y the

mK

IL instru

ction

–N

o to

po

log

ical in

form

atio

n p

rovid

ed

No

verte

x, n

or n

eig

hb

orin

g fra

gm

en

t info

rma

tion

ava

ilab

le

–D

yna

mica

lly loa

da

ble

•E

xp

ose

d th

rou

gh

the

Op

en

GL A

RB

ex

tensio

n:

ARB_fragment_program

HW

an

d P

rogra

mm

ab

le GE 38

Frag

men

t Pro

gra

mm

ing

Assem

bly La

ng

ua

ge

•P

ow

erful S

IMD

instru

ction

set (33 in

structio

ns):

ABS, ADD, CMP, COS, DP3, DP4, DPH, DST,

EX2, FLR, FRC, KIL, LG2, LIT, LRP, MAD,

MAX, MIN, MOV, MUL, POW, RCP, RSQ, SCS,

SGE, SIN, SLT, SUB, SWZ, TEX, TXB, TXP,

XPD.

•Like in

verte

x p

rog

ram

s:

•In

structio

n fo

rma

t

•R

egister “sw

izzling

”, ma

sking

•P

rog

ram

pa

ram

eter a

nd

Op

en

GL sta

te a

ccess

•T

em

po

rary va

riab

le an

d co

nsta

nt h

an

dlin

g

HW

an

d P

rogra

mm

ab

le GE

39

Frag

men

t Pro

gra

mm

ing

Attrib

utes &

Resu

lts

Attribute RegisterCom

ponentsUnderlying State

frag

men

t.po

sition

(x,y,z,1/w

)w

ind

ow

po

sition

frag

men

t.colo

r(r,g

,b,a

)p

rima

ry colo

rfra

gm

ent.te

xco

ord

[n]

(s,t,r,q)

tex

ture

coo

rdin

ate, u

nit n

……

Result Register Com

ponentsDescription

resu

lt.colo

r(r,g

,b,a

)co

lor

resu

lt.de

pth

(*,*,d,*)

de

pth

coo

rdin

ate

HW

an

d P

rogra

mm

ab

le GE

40

Exa

mp

le Pro

gra

m #

3Sim

ple color modulation with texture lookup

!!ARBfp1.0

TEMP temp;

ATTRIB tex0 = fragment.texcoord[0];

ATTRIB col0 = fragment.color;

# Fetch the texture value.

TEX temp, tex0, texture[0], 2D;

# Modulate the fragment color with the texture value.

MUL result.color, col0, temp;

END

11

Op

enG

L Integ

ratio

n

HW

an

d P

rogra

mm

ab

le GE 42

Op

enG

LIn

tegra

tion

// Generate a program object handle.

Gluint

progId;

glGenProgramsARB(1, &progId);

// Bind the program object as the “current”

program.

glBindProgramARB(GL_VERTEX_PROGRAM_ARB, progId);

// Specify the program for the current object.

// strProgram

contains the program source-code

glProgramStringARB(GL_VERTEX_PROGRAM_ARB,

GL_PROGRAM_FORMAT_ASCII_ARB,

strlen(strProgram), strProgram);

// Check for errors and warnings.

if(glGetError() == GL_INVALID_OPERATION) {

…}

HW

an

d P

rogra

mm

ab

le GE 43

Op

enG

LIn

tegra

tion

(2)

// Enable the vertex program.

glEnable(GL_VERTEX_PROGRAM_ARB);

// Set some local program parameter in local program

register 12.

glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 12,

0.0, 0.0, 0.0, 1.0 );

// Set per vertex attributes in attribute register 0

glVertexAttribPointerARB(0, 3, GL_FLOAT, false, 0,

vertexData

);

// Draw elements pointed to by nIndices

and indicies

glDrawElements(GL_TRIANGLES, nIndices, GL_UNSIGNED_INT,

indicies

);

// Disable the vertex program.

glDisable(GL_VERTEX_PROGRAM_ARB);

// When finished, delete the program object.

glDeleteProgramsARB(1, &progId);

HW

an

d P

rogra

mm

ab

le GE

44

Tren

ds

•H

igh

-levelsha

de

rla

ng

ua

ges

–C

g, H

LSL, ...

•C

on

ditio

na

lsa

nd

flow

con

trol

–If-else

–fo

ra

nd

wh

ile

–su

bro

utin

es

•O

vercom

elim

itatio

ns

–M

ax

. nu

mb

ero

f instru

ction

sa

nd

textu

relo

oku

ps

–N

o tex

ture

loo

kup

in vertex

sha

de

rp

ossib

leyet

•M

ore

da

tatyp

es

an

d h

igh

er

precisio

n–

Inte

ge

r da

tatyp

es

–Fu

ll floa

ting

-po

int d

ata

types

an

d b

uffe

rs

12

Cg

–C

for G

rap

hics

HW

an

d P

rogra

mm

ab

le GE

46

Cg

-C

for G

rap

hics

Graphics Program

Written in Cg

“C”for Graphics

Compiled & Optim

ized

Low Level, Graphics“Assem

bly Code”

HW

an

d P

rogra

mm

ab

le GE 47

Cg

Intro

du

ction

•Syn

tax

, op

era

tors, fu

nctio

ns fro

m C

lan

gu

ag

e•

Co

nd

ition

als a

nd

flow

con

trol

•P

articu

larly su

itab

le for G

PU

s:–

Exp

resses da

ta flo

w o

f the p

ipelin

e/stream

arch

itecture o

f GP

Us

(e.g. vertex

-to

-pix

el) –

Vecto

r an

d m

atrix

op

eratio

ns

–Su

pp

orts h

ard

wa

re d

ata

type

s for m

ax

imu

m p

erfo

rma

nce

–Ex

po

ses G

PU

fun

ction

s for co

nve

nie

nce

an

d sp

ee

d:

•In

trinsic: (m

ul, d

ot, sq

rt…)

•B

uilt-in

: extrem

ely usefu

l an

d G

PU

op

timized

ma

th, u

tility an

d

geo

metric fu

nctio

ns (n

oise, m

ix, refle

ct, sin…

)–

Lan

gu

ag

e re

serves keyw

ord

s to su

pp

ort fu

ture

ha

rdw

are

imp

lem

en

tatio

ns

(e.g. sw

itch, ca

se…)

–C

om

piler u

ses h

ard

wa

re p

rofiles to

sub

set C

g a

s req

uire

d fo

r pa

rticula

r h

ard

wa

re fea

ture

sets

HW

an

d P

rogra

mm

ab

le GE

48

Cg

Usa

ge

Th

ree wa

ys:•

Cg

Ru

ntim

e –a

thin

AP

I:–

ASC

II .Cg

sha

der

com

piled

at ru

ntim

e

–C

on

venien

t inte

rface fo

r setting

sha

der

pa

ram

eters a

nd

co

nsta

nts

•C

om

ma

nd

line co

mp

iler g

enera

tes text file o

utp

ut:

–D

X / O

pen

GL –

vertex a

nd

pix

el sh

ad

er

files

–T

we

ak th

e A

SM

you

rself

–G

enera

tes com

men

ts on

pro

gra

m p

ara

meters &

reg

isters

•C

gFX

–Effect fra

me

wo

rk with

rend

er states

13

HW

an

d P

rogra

mm

ab

le GE

49

Cg

Usa

ge (2)

Verte

x p

roce

ssing

Fixed

fun

ction

-or-

Ha

nd

-written

ASM

-or-

Co

mp

iled

Cg

-or-

Ha

nd

-op

timize

d C

g A

SM

Frag

me

nt p

roce

ssing

Fixed

fun

ction

-or-

Ha

nd

-written

ASM

-or-

Co

mp

iled

Cg

-or-

Ha

nd

-op

timize

d C

g A

SM

HW

an

d P

rogra

mm

ab

le GE

50

Cg

Ha

rdw

are P

rofiles

•N

VID

IA C

g C

om

pile

r pro

vides se

veral p

rofile

s:–

DX

Ve

rtex a

nd

Pix

el Sh

ad

ers

–O

pe

nG

L Ve

rtex a

nd

Frag

me

nt P

rog

ram

s

–N

V V

ertex

an

d Fra

gm

ent P

rog

ram

s

•E

ach

pro

file ha

s its ow

n lim

itatio

ns, e.g

.–

No

“ha

lf”o

r “fixed

”d

ata

type

–N

o tex

ture fu

nctio

ns in

vertex p

rog

ram

s

–N

o “fo

r”o

r “wh

ile”lo

op

s in fra

gm

ent p

rog

ram

s(u

nless th

ey’re u

nro

llab

le)

–etc.

HW

an

d P

rogra

mm

ab

le GE 51

Exa

mp

le Pro

gra

m #

4Sim

ple vertex lighting// Define inputs from application.

struct

appin

: application2vertex {

float4 Position, Normal, Color;

};

// Define outputs from vertex shader.

struct

vertout

: vertex2fragment{

float4 HPosition, Color0;

};

// Program entry point.

vertout

main(appin

IN,

uniform float4x4 ModelViewProj, uniform float4x4 ModelViewIT,

uniform float4 CamPos, uniform float4 LightVec)

{// Transform position/normal from model-space to view-space.

vertout

OUT;

OUT.HPosition

= mul(ModelViewProj, IN.Position);

float4 normal = normalize(mul(ModelViewIT,

IN.Normal).xyzz);

…H

W a

nd

Prog

ram

ma

ble G

E 52

Exa

mp

le Pro

gra

m #

4 (2)

// Normalize light vector.

float4 light = normalize(LightVec);

// Calculate half angle vector.

float4 eye = CamPos;

float4 half = normalize(light

+ eye);

// Calculate diffuse and specular

component.

float diffuse = dot(normal, light);

float specular

= dot(normal, half);

specular

= pow(specular, 32);

// Specify ambient/specular

color.

float4 ambientColor

= float4(0.2,0.2,0.2,0.2);

float4 specularMaterial

= float4(1.0, 1.0, 1.0, 1.0);

// Combine all components into final color.

OUT.Color0 = diffuse * IN.Color

+ specular

* specularMaterial

+ ambientColor;

return OUT;

}

14

HW

an

d P

rogra

mm

ab

le GE 53

Links

•N

Vid

iah

ttp://w

ww

.nvid

ia.co

m

•A

TI

http

://ww

w.a

ti.com

•O

pen

GL ex

tensio

n reg

istryh

ttp://o

ss.sgi.co

m/p

roje

cts/og

l-sam

ple/re

gistry

•C

gh

ttp://w

ww

.cgsh

ad

ers.o

rg