slide - KUCG :::: Korea University Computer Graphics

pumpedlessΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

103 εμφανίσεις

Graphics


Graphics Lab @ Korea University

cgvr.korea.ac.kr

1

Using Vertex Shader
in DirectX 8.1








2002. 02. 05

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

2

Overview


What is Vertex Programming?


Vertex Programming Assembly Language


Sample Programs


Instruction Set




Graphics


Graphics Lab @ Korea University

cgvr.korea.ac.kr

3

What is Vertex
Programming?

Part 1

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

4

Traditional Rendering Pipeline


Traditional Graphics Pipeline

frame
-
buffer

anti
-
aliasing

texture

blending

setup

rasterizer

transform &

lighting

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

5

Vertex Shader Rendering
Pipeline

SetVertexShader()

frame
-
buffer

anti
-
aliasing

texture

blending

setup

rasterizer

transform &

lighting

Vertex

Program

Switch from standard T&L mode

to

Vertex Program mode

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

6

What is Possible?


Complete control of transform and lighting HW


Complex vertex operations accelerated in HW


Custom vertex lighting


Custom skinning and blending


Custom texture coordinate generation


Custom texture matrix operations


Custom vertex computations of your choice


Offloading vertex computations frees up CPU

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

7

What is Possible?


Custom transform, lighting, and skinning

Directional Light

Bump Point Lighting

Keyframe Interpolation

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

8



Vertex
Attributes

Vertex
Program

Vertex
Output

Program
Parameters

Temporary
Registers

Read/Write
-
able

16
x4 registers

128
instructions

96
x4 registers

12
x4 registers

Read
-
only

Vertex Programming

Conceptual Overview

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

9

Vertex Programming

Conceptual Overview

Vertex Attribute
Registers

Vertex
Program

Vertex Result
Registers

Program
Parameter
Registers

Temporary
Registers

v[0] v[1] … v[15]

c[0] c[1] … c[95]

R0 R1 … R10 R11

oPos, oD0

r

r

r/w

w

Address Register

A0.x

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

10

Sample Code


Position & Constant Color




reg c0


= (0,0.5,1.0,2.0)


reg c4
-
7


= WorldViewProj matrix


reg c8


= constant color


reg v0


= position ( 4x1 vector )


reg v5


= diffuse color

const char SimpleVertexShader0[] =“

vs.1.0

//Shader version 1.0

m4x4 oPos , v0, c4

//emit projected position



mov oD0, c8

//Diffuse color = c8”

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

11

What is Vertex Programming?


Vertex Program


Assembly language interface to T&L unit


GPU instruction set to perform all vertex math


Reads an untransformed, unlit vertex


Creates a transformed vertex


Optionally creates


Lights a vertex


Creates texture coordinates


Creates fog coordinates


Creates point sizes

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

12

What is Vertex Programming?


Vertex Program


Does not create or delete vertices


1 vertex in and 1 vertex out


No topological information provided


No edge, face, nor neighboring vertex info


Dynamically loadable






Graphics


Graphics Lab @ Korea University

cgvr.korea.ac.kr

13

Vertex Programming

Assembly Language

Part 2

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

14



Instruction Format:



Opcode

dst, [
-
]s0 [,[
-
]s1 [,[
-
]s2]];

#comment



Instruction
name

Destination
Register

Source0
Register

Source1
Register

Source2
Register

Assembly Language Format

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

15



Instruction Format:



Opcode

dst, [
-
]s0 [,[
-
]s1 [,[
-
]s2]];

#comment



Instruction
name

Destination
Register

Source0
Register

Source1
Register

Source2
Register

Example:



MOV

r1, r2

R1

x

y

z

w

R2

x

y

z

w

Assembly Language Format

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

16



Simple Example:




MOV

R1, R2;



R1

x

y

z

w

7.0

3.0

6.0

2.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

R1

x

y

z

w

0.0

0.0

0.0

0.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

before

after

Assembly Example

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

17



Source registers can be negated:




MOV

R1,
-
R2;



before

after

R1

x

y

z

w

0.0

0.0

0.0

0.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

R1

x

y

z

w

-
7.0

-
3.0

-
6.0

-
2.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

Assembly Example

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

18



Destination register can mask which components
are written to…



R1




w物瑥r慬a 捯浰潮敮瑳


R1.x




w物瑥r潮汹⁸ 捯浰潮敮e


刱⹸R




w物瑥r潮汹⁸ ⁷ 捯浰潮敮瑳



Masking

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

19



Destination register masking:




MOV

R1.xw,
-
R2;



before

after

R1

x

y

z

w

0.0

0.0

0.0

0.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

R1

x

y

z

w

-
7.0

0.0

0.0

-
2.0

R2

x

y

z

w

7.0

3.0

6.0

2.0

Masking

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

20



There are 17 instructions in total



ARL


MOV


MUL


ADD


MAD


RCP


RSQ


DP3


DP4


DST


MIN


MAX


SLT


SGE


EXP


LOG


LIT

All Instructions

Graphics


Graphics Lab @ Korea University

cgvr.korea.ac.kr

21

Sample Program

Part 3

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

22

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality


Step 3: Check for vertex shader support


Step 4: Declare the shader registers


Step 5: Create the shader


Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

23

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data



Step 2: Design the shader functionality


Step 3: Check for vertex shader support


Step 4: Declare the shader registers


Step 5: Create the shader


Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

24

Step 1



Declare the vertex data

struct CUSTOMVERTEX { FLOAT x, y, z; DWORD diffuseColor; };


#define D3DFVF_CUSTOMVERTEX

(D3DFVF_XYZ|D3DFVF_DIFFUSE)


CUSTOMVERTEX g_Vertices[]= {






{
-
1.0f,
-
1.0f, 0.0f, 0xffff0000 },






{ +1.0f,
-
1.0f, 0.0f, 0xff00ff00 },






{ +1.0f, +1.0f, 0.0f, 0xff0000ff },






{
-
1.0f, +1.0f, 0.0f, 0xffffff00 }, };

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

25

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality



Step 3: Check for vertex shader support


Step 4: Declare the shader registers


Step 5: Create the shader


Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

26

Basic Instruction (Step 2)




DP3: Three
-
Component Dot Product



Function:



Computes the three
-
component (x,y,z) dot
product of two source vectors and replicates the
result across the destination register.



Syntax:



DP3

dest, src0, src1;

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

27

Basic Instruction (Step 2)



before

after


DP3 Example:




DP3

R1, R6, R6;



x

y

z

w

R6

3.0

2.0

1.0

1.0

R1

0.0

0.0

0.0

0.0

x

y

z

w

R6

3.0

2.0

1.0

1.0

R1

14.0

14.0

14.0

14.0

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

28

Sample Code


1 (Step 2)


Position & Diffuse Color


reg c0


= (0,0.5,1.0,2.0)


reg c4
-
7


= WorldViewProj matrix


reg c8


= constant color


reg v0


= position ( 4x1 vector )


reg v5


= diffuse color

const char SimpleVertexShader1[] =“

vs.1.0 //Shader version 1.0



dp4 oPos.x , v0, c4 //emit projected x position


dp4 oPos.y , v0, c5 //emit projected y position


dp4 oPos.z , v0, c6 //emit projected z position


dp4 oPos.w , v0, c7 //emit projected w position


mov oD0, v5 //Diffuse color = vertex color ”

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

29

Sample Code


2 (Step 2)


Position & Texture

reg c0


= (0,0.5,1.0,2.0)

reg c4
-
7


= WorldViewProj matrix

reg c8


= constant color

reg v0


= position ( 4x1 vector )

reg v5


= diffuse color

reg v7


= texcoords ( 2x1 vector )

const char SimpleVertexShader2[] =“

vs.1.0 //Shader version 1.0



dp4 oPos.x , v0, c4 //emit projected x position

dp4 oPos.y , v0, c5 //emit projected y position


dp4 oPos.z , v0, c6 //emit projected z position


dp4 oPos.w , v0, c7 //emit projected w position


mov oT0.xy , v7 //copy texcoords”




CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

30

Sample Code


3 (Step 2)


Position & Lighting

reg c0


= (0,0.5,1.0,2.0)

reg c4
-
7


= WorldViewProj matrix

reg c8


= constant color

reg v0


= position ( 4x1 vector )

reg v5


= diffuse color

reg v7


= texcoords ( 2x1 vector )

const char SimpleVertexShader3[] =

vs.1.0 //Shader version 1.0



dp4 oPos.x, v0, c4 //emit projected x position


dp4 oPos.y, v0, c5 //emit projected y position


dp4 oPos.z, v0, c6 //emit projected z position


dp4 oPos.w, v0, c7 //emit projected w position


dp3 r0.x, v3, c12 //N dot L in world space

mul oD0, r0.x , v5 //Calculate color intensity

mov oT0.xy , v7 //copy texcoords




CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

31

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality


Step 3: Check for vertex shader support



Step 4: Declare the shader registers


Step 5: Create the shader


Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

32

Step 3


Check for vertex shader support

D3DCAPS8 caps;

m_pd3dDevice
-
>GetDeviceCaps(&caps);
if( D3DSHADER_VERSION_MAJOR( caps.VertexShaderVersio
n ) < 1 ) return E_FAIL;

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

33

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality


Step 3: Check for vertex shader support


Step 4: Declare the shader registers



Step 5: Create the shader


Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

34

Step 4


Declare the shader registers


DWORD dwDecl[] = { D3DVSD_STREAM(0),
D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ),
D3DVSD_END() };


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

35

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality


Step 3: Check for vertex shader support


Step 4: Declare the shader registers


Step 5: Create the shader



Step 6: Render the output pixels


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

36

Step 5


Create the shader

TCHAR strPath[512];

LPD3DXBUFFER pCode;

DXUtil_FindMediaFile( strPath, _T("VertexShader.vsh") );
D3DXAssembleShaderFromFile( strPath, 0, NULL, &pCode,
NULL );

m_pd3dDevice
-
>CreateVertexShader( dwDecl, (DWORD*)pCode
-
>GetBufferPointer(), &m_hVertexShader, 0 )))

pCode
-
>Release();


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

37

Vertex Shader Frame Work in
DX 8.1


Step 1: Declare the vertex data


Step 2: Design the shader functionality


Step 3: Check for vertex shader support


Step 4: Declare the shader registers


Step 5: Create the shader


Step 6: Render the output pixels



CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

38

Step 6


Render the output pixels

m_pd3dDevice
-
>SetVertexShaderConstant( 0, &mat, 4 );

float color[4] = {0,1,0,0};

m_pd3dDevice
-
>SetVertexShaderConstant( 4, &color, 1 );

m_pd3dDevice
-
>SetStreamSource( 0, m_pQuadVB,
sizeof(CUSTOMVERTEX) );

m_pd3dDevice
-
>SetVertexShader( m_hVertexShader );

m_pd3dDevice
-
>DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );


CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

39

Demo Program

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

40

Performance




For Optimal performance


Be clever


Exploit vector parallelism


(Ex. 4 scalar adds with a vector add)


Swizzle and negate away


(no performance penalty for doing so)


Use LIT and DST effectively

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

41

Summary


Vertex Programs


Increased programmability


Customizable engine for transform, lighting,

texture coordinate generation, and more.


Facilitates setup for per
-
fragment shading.


Allows animation/deformation through key
-
frame

interpolation and skinning.



Accelerated in Future Generation GPUs


Offloads CPU tasks to GPU yielding higher
performance.


Graphics


Graphics Lab @ Korea University

cgvr.korea.ac.kr

42

The Instruction Set

Appendix



CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

43



There are 17 instructions in total



ARL


MOV


MUL


ADD


MAD


RCP


RSQ


DP3


DP4


DST


MIN


MAX


SLT


SGE


EXP


LOG


LIT

All Instructions

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

44

RCP




RCP: Reciprocal



Function:



Inverts the value of the source and replicates

the result across the destination register.



Syntax:



RCP

dest, src0.C;




where ‘C’
is x, y, z, or w

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

45

RCP



before

after


RCP Example:




RCP

R1, R2.w;



x

y

z

w

R2

7.0

3.0

6.0

2.0

R1

0.0

0.0

0.0

0.0

x

y

z

w

R2

7.0

3.0

6.0

2.0

R1

0.5

0.5

0.5

0.5

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

46

RSQ




RSQ: Reciprocal Square Root



Function:



Computes the inverse square root of the
absolute value of the source scalar and replicates
the result across the destination register.



Syntax:



RSQ

dest, src0.C;



where ‘C’
is x, y, z, or w

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

47

RSQ



before

after


RSQ Example:




RSQ

R1.x, R5.x;



x

y

z

w

R5

-
4.0

3.0

7.0

9.0

R1

0.0

0.0

0.0

0.0

x

y

z

w

R5

-
4.0

3.0

7.0

9.0

R1

0.5

0.0

0.0

0.0

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

48

SLT




SLT: Set On Less Than



Function:



Performs a component
-
wise assignment of

either 1.0 or 0.0. 1.0 is assigned if the value

of the first source is less than the value of the

second. Otherwise, 0.0 is assigned.



Syntax:



SLT

dest, src0, src1;

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

49

SLT



before

after


SLT Example:




SLT

R1, R2, R3;



x

y

z

w

R3

2.0

2.1

5.0

7.0

R2

7.0

3.0

6.0

2.0

R1

0.0

0.0

0.0

0.0

x

y

z

w

R3

2.0

2.1

5.0

7.0

R2

7.0

3.0

6.0

2.0

R1

0.0

0.0

0.0

1.0

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

50

LIT




LIT: Light Coefficients



Function:



Computes ambient, diffuse, and specular
lighting coefficients from a diffuse dot product, a
specular dot product, and a specular power.




Assumes:




src0.x = diffuse dot product

(N • L)


src0.y = specular dot product

(N • H)


src0.w = power



(m)



CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

51

LIT




LIT: Light Coefficients


Syntax:




LIT

dest, src0



Result:




dest.x = 1.0




(ambient coeff.)


dest.y = CLAMP(src0.x, 0, 1)






= CLAMP(N • L, 0, 1)

(diffuse coeff.)



dest.z = (see next slide…)

(specular coeff.)


dest.w = 1.0

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

52

LIT




LIT: Light Coefficients




Result:


(Recall: src0.x


N • L)




if ( src0.x >

0.0 )







dest.z = (MAX(src0.y,0))
(ECLAMP(src0.w,
-
128,128))



= (MAX(N • H,0))
m

where m in (
-
128,128)










otherwise,








dest.z = 0.0





(dest.z is specular coeff. as defined by OpenGL)

CGVR


Graphics Lab @ Korea University

cgvr.korea.ac.kr

53

LIT



before

after


LIT Example:




LIT

R1, R7;



x

y

z

w

R7

0.3

0.8

0.0

2.0

R1

0.0

0.0

0.0

0.0

R7

0.3

0.8

0.0

2.0

x

y

z

w

R1

1.0

0.3

0.64

1.0

(
ambient)

(
diffuse)

(
specular)

(
Good to 8+ bits)