PowerPoint Template
Download
Report
Transcript PowerPoint Template
Graphics
Using Vertex Shader in
DirectX 8.1
강신진
2002. 02. 05
cgvr.korea.ac.kr
1
Graphics Lab @ Korea University
Overview
CGVR
What is Vertex Programming?
Vertex Programming Assembly Language
Sample Programs
Instruction Set
cgvr.korea.ac.kr
2
Graphics Lab @ Korea University
Graphics
What is Vertex
Programming?
Part 1
cgvr.korea.ac.kr
3
Graphics Lab @ Korea University
Traditional Rendering Pipeline
CGVR
Traditional Graphics Pipeline
transform &
lighting
setup
rasterizer
texture
blending
frame-buffer
anti-aliasing
cgvr.korea.ac.kr
4
Graphics Lab @ Korea University
Vertex Shader Rendering
Pipeline
transform &
lighting
CGVR
Vertex
Program
setup
rasterizer
texture
blending
SetVertexShader()
frame-buffer
anti-aliasing
cgvr.korea.ac.kr
5
Switch from standard T&L mode
to
Vertex Program mode
Graphics Lab @ Korea University
What is Possible?
CGVR
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.korea.ac.kr
6
Graphics Lab @ Korea University
What is Possible?
CGVR
Custom transform, lighting, and skinning
Directional Light
Keyframe Interpolation
cgvr.korea.ac.kr
7
Bump Point Lighting
Graphics Lab @ Korea University
Vertex Programming
Conceptual Overview
Vertex
Attributes
CGVR
Program
Parameters
16x4 registers
Vertex
Program
128 instructions
Read-only
96x4 registers
Temporary
Registers
Read/Write-able
12x4 registers
Vertex
Output
cgvr.korea.ac.kr
8
Graphics Lab @ Korea University
Vertex Programming
Conceptual Overview
Vertex Attribute
Registers
CGVR
Program
Parameter
Registers
v[0] v[1] … v[15]
r
Vertex
Program
w
Vertex Result
Registers
oPos, oD0
cgvr.korea.ac.kr
9
r
c[0] c[1] … c[95]
r/w
Temporary
Registers
R0 R1 … R10 R11
A0.x
Address Register
Graphics Lab @ Korea University
Sample Code
CGVR
Position & Constant Color
reg c0
reg c4-7
reg c8
reg v0
reg v5
= (0,0.5,1.0,2.0)
= WorldViewProj matrix
= constant color
= position ( 4x1 vector )
= 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.korea.ac.kr
10
Graphics Lab @ Korea University
What is Vertex Programming?
CGVR
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
cgvr.korea.ac.kr
Lights a vertex
Creates texture coordinates
Creates fog coordinates
Creates point sizes
11
Graphics Lab @ Korea University
What is Vertex Programming?
CGVR
Vertex Program
Does not create or delete vertices
No topological information provided
1 vertex in and 1 vertex out
No edge, face, nor neighboring vertex info
Dynamically loadable
cgvr.korea.ac.kr
12
Graphics Lab @ Korea University
Graphics
Vertex Programming
Assembly Language
Part 2
cgvr.korea.ac.kr
13
Graphics Lab @ Korea University
Assembly Language Format
CGVR
Instruction Format:
Opcode
Instruction
name
cgvr.korea.ac.kr
dst, [-]s0 [,[-]s1 [,[-]s2]]; #comment
Destination Source0
Register
Register
14
Source1
Register
Source2
Register
Graphics Lab @ Korea University
Assembly Language Format
CGVR
Instruction Format:
Opcode
Instruction
name
dst, [-]s0 [,[-]s1 [,[-]s2]]; #comment
Destination Source0
Register
Register
Source2
Register
R1
Example:
MOV
cgvr.korea.ac.kr
Source1
Register
15
r1, r2
R2
x
y
z
w
x
y
z
w
Graphics Lab @ Korea University
Assembly Example
CGVR
Simple Example:
MOV
R1, R2;
before
R1
0.0
0.0
0.0
0.0
after
R2
x
y
7.0
z
3.0
6.0
w
2.0
cgvr.korea.ac.kr
16
R1
x
y
7.0
z
3.0
6.0
w
2.0
R2
x
y
7.0
z
3.0
6.0
w
2.0
x
y
z
w
Graphics Lab @ Korea University
Assembly Example
CGVR
Source registers can be negated:
MOV
R1, -R2;
before
R1
0.0
0.0
0.0
0.0
after
R2
x
y
7.0
z
3.0
6.0
w
2.0
cgvr.korea.ac.kr
17
R1
x
y
-7.0
z
-3.0
-6.0
w
-2.0
R2
x
y
7.0
z
3.0
6.0
w
2.0
x
y
z
w
Graphics Lab @ Korea University
Masking
CGVR
Destination register can mask which components
are written to…
R1
write all components
R1.x
write only x component
R1.xw
write only x, w components
cgvr.korea.ac.kr
18
Graphics Lab @ Korea University
Masking
CGVR
Destination register masking:
MOV
R1.xw, -R2;
before
R1
0.0
0.0
0.0
0.0
after
R2
x
y
7.0
z
3.0
6.0
w
2.0
cgvr.korea.ac.kr
19
R1
x
y
-7.0
z
0.0
0.0
w
-2.0
R2
x
y
7.0
z
3.0
6.0
w
2.0
x
y
z
w
Graphics Lab @ Korea University
All Instructions
CGVR
There are 17 instructions in total
• ARL
• MOV
• MUL
• ADD
• MAD
• RCP
cgvr.korea.ac.kr
• RSQ
• DP3
• DP4
• DST
• MIN
• MAX
20
• SLT
• SGE
• EXP
• LOG
• LIT
Graphics Lab @ Korea University
Graphics
Sample Program
Part 3
cgvr.korea.ac.kr
21
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
22
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
23
Graphics Lab @ Korea University
Step 1
CGVR
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.korea.ac.kr
24
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
25
Graphics Lab @ Korea University
Basic Instruction (Step 2)
CGVR
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
cgvr.korea.ac.kr
dest, src0, src1;
26
Graphics Lab @ Korea University
Basic Instruction (Step 2)
CGVR
DP3 Example:
DP3
R1, R6, R6;
before
after
R1
R6
R1
R6
0.0
3.0
14.0
3.0
z
0.0
0.0
2.0
1.0
z
14.0
14.0
2.0
1.0
w
0.0
1.0
w
14.0
1.0
x
y
cgvr.korea.ac.kr
27
x
y
Graphics Lab @ Korea University
Sample Code – 1 (Step 2)
CGVR
Position & Diffuse Color
reg c0
reg c4-7
reg c8
reg v0
reg v5
= (0,0.5,1.0,2.0)
= WorldViewProj matrix
= constant color
= position ( 4x1 vector )
= 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.korea.ac.kr
28
Graphics Lab @ Korea University
Sample Code – 2 (Step 2)
CGVR
Position & Texture
reg c0
reg c4-7
reg c8
reg v0
reg v5
reg v7
= (0,0.5,1.0,2.0)
= WorldViewProj matrix
= constant color
= position ( 4x1 vector )
= diffuse color
= 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.korea.ac.kr
29
Graphics Lab @ Korea University
Sample Code – 3 (Step 2)
CGVR
Position & Lighting
reg c0
reg c4-7
reg c8
reg v0
reg v5
reg v7
= (0,0.5,1.0,2.0)
= WorldViewProj matrix
= constant color
= position ( 4x1 vector )
= diffuse color
= 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.korea.ac.kr
30
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
31
Graphics Lab @ Korea University
Step 3
CGVR
Check for vertex shader support
D3DCAPS8 caps;
m_pd3dDevice->GetDeviceCaps(&caps);
if( D3DSHADER_VERSION_MAJOR( caps.VertexShaderVersio
n ) < 1 ) return E_FAIL;
cgvr.korea.ac.kr
32
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
33
Graphics Lab @ Korea University
Step 4
CGVR
Declare the shader registers
DWORD dwDecl[] = { D3DVSD_STREAM(0),
D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ),
D3DVSD_END() };
cgvr.korea.ac.kr
34
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
35
Graphics Lab @ Korea University
Step 5
CGVR
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.korea.ac.kr
36
Graphics Lab @ Korea University
Vertex Shader Frame Work in
DX 8.1
CGVR
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.korea.ac.kr
37
Graphics Lab @ Korea University
Step 6
CGVR
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.korea.ac.kr
38
Graphics Lab @ Korea University
Demo Program
cgvr.korea.ac.kr
39
CGVR
Graphics Lab @ Korea University
Performance
CGVR
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.korea.ac.kr
40
Graphics Lab @ Korea University
Summary – Vertex Programs
Increased programmability
CGVR
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.
cgvr.korea.ac.kr
41
Graphics Lab @ Korea University
Graphics
The Instruction Set
Appendix
cgvr.korea.ac.kr
42
Graphics Lab @ Korea University
All Instructions
CGVR
There are 17 instructions in total
• ARL
• MOV
• MUL
• ADD
• MAD
• RCP
cgvr.korea.ac.kr
• RSQ
• DP3
• DP4
• DST
• MIN
• MAX
43
• SLT
• SGE
• EXP
• LOG
• LIT
Graphics Lab @ Korea University
RCP
CGVR
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.korea.ac.kr
44
Graphics Lab @ Korea University
RCP
CGVR
RCP Example:
RCP
R1, R2.w;
before
after
R1
R2
R1
R2
0.0
7.0
0.5
7.0
z
0.0
0.0
3.0
6.0
z
0.5
0.5
3.0
6.0
w
0.0
2.0
w
0.5
2.0
x
y
cgvr.korea.ac.kr
45
x
y
Graphics Lab @ Korea University
RSQ
CGVR
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.korea.ac.kr
46
Graphics Lab @ Korea University
RSQ
CGVR
RSQ Example:
RSQ
R1.x, R5.x;
before
after
R1
R5
R1
R5
0.0
-4.0
0.5
-4.0
z
0.0
0.0
3.0
7.0
z
0.0
0.0
3.0
7.0
w
0.0
9.0
w
0.0
9.0
x
y
cgvr.korea.ac.kr
47
x
y
Graphics Lab @ Korea University
SLT
CGVR
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
cgvr.korea.ac.kr
dest, src0, src1;
48
Graphics Lab @ Korea University
SLT
CGVR
SLT Example:
SLT
R1, R2, R3;
before
after
R1
R2
R3
0.0
7.0
2.0
z
0.0
0.0
3.0
6.0
2.1
5.0
w
0.0
2.0
7.0
x
y
cgvr.korea.ac.kr
49
R1
R2
R3
0.0
7.0
2.0
z
0.0
0.0
3.0
6.0
2.1
5.0
w
1.0
2.0
7.0
x
y
Graphics Lab @ Korea University
LIT
CGVR
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
src0.y = specular dot product
src0.w = power
cgvr.korea.ac.kr
50
(N • L)
(N • H)
(m)
Graphics Lab @ Korea University
LIT
CGVR
LIT: Light Coefficients
Syntax:
LIT dest, src0
Result:
dest.x = 1.0
dest.y = CLAMP(src0.x, 0, 1)
= CLAMP(N • L, 0, 1)
dest.z = (see next slide…)
dest.w = 1.0
cgvr.korea.ac.kr
51
(ambient coeff.)
(diffuse coeff.)
(specular coeff.)
Graphics Lab @ Korea University
LIT
CGVR
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.korea.ac.kr
52
Graphics Lab @ Korea University
LIT
CGVR
LIT Example:
LIT
R1, R7;
before
after
R1
R7
R1
R7
0.0
0.3
1.0
0.3
z
0.0
0.0
0.8
0.0
(specular) z
0.3
0.64
0.8
0.0
w
0.0
2.0
w
1.0
2.0
x
y
(ambient) x
(diffuse) y
(Good to 8+ bits)
cgvr.korea.ac.kr
53
Graphics Lab @ Korea University