GDC2003 OGL ARBVertexProgram PDF
GDC2003 OGL ARBVertexProgram PDF
Cass Everitt
[email protected]
1
Overview
ARB Vertex Programming Overview
Loading Vertex Programs
Register Set
Variables and Variable Binding
Assembly Instruction Set
Example Programs
Wrap-Up
2
ARB Vertex Programming
Overview
Traditional Graphics Pipeline
transform,
lighting
setup
rasterizer
texture fetch,
fragment shading Each unit has specific function
(usually with configurable modes
frame-buffer of operation)
ops
3
ARB Vertex Programming
Overview
Vertex Programming offers
programmable T&L unit
transform, User-defined
lighting Vertex
setup
Processing
rasterizer
texture fetch,
fragment shading Gives the programmer total control
of vertex processing.
frame-buffer
ops
4
What is Vertex Programming?
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
Vertex Program
Assembly language interface to T&L unit
GPU instruction set to perform all vertex
math
Input: arbitrary vertex attributes
Output: a transformed vertex attributes
homogeneous clip space position (required)
colors (front/back, primary/secondary)
fog coord
texture coordinates
point size 6
What is Vertex Programming?
Vertex Program
Does not generate or destroy vertexes
1 vertex in and 1 vertex out
No topological information provided
No edge, face, nor neighboring vertex info
Dynamically loadable
7
What is ARB_vertex_program?
ARB_vertex_program is similar to
NV_vertex_program with the addition
of:
variables
local parameters
access to GL state
some extra instructions
implementation-specific resource limits
8
What is Vertex Programming?
transform,
Vertex
lighting
Program
setup
rasterizer
glEnable( GL_VERTEX_PROGRAM_ARB );
texture fetch,
fragment shading
Switch from conventional T&L model
frame-buffer to
ops Programmable mode
9
Specifically, what gets
bypassed?
Modelview and projection vertex transformations
Vertex weighting/blending
Normal transformation, rescaling, normalization
Color material
Per-vertex lighting
Texture coordinate generation and texture matrix
transformations
Per-vertex point size and fog coordinate
computations
User-clip planes
10
What does NOT get bypassed?
Evaluators
Clipping to the view frustum
Perspective divide
Viewport transformation
Depth range transformation
Front and back color selection (for two-sided)
Clamping of primary and secondary colors to [0,1]
Primitive assembly, setup, rasterization, blending
11
Vertex Programming
Conceptual Overview
13
Creating a Vertex Program
Gluint progid;
// Generate a program object handle.
glGenProgramsARB( 1, &progid );
// Make the current program object progid.
glBindProgramARB( GL_VERTEX_PROGRAM_ARB, progid );
// Specify the program for the current object.
glProgramStringARB( GL_VERTEX_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(myString), myString );
// Check for errors and warnings...
14
Creating a Vertex Program
// Check for errors and warnings...
if ( GL_INVALID_OPERATION == glGetError() )
{
// Find the error position
GLint errPos;
glGetIntergv( GL_PROGRAM_ERROR_POSITION_ARB,
&errPos );
// Print implementation-dependent program
// errors and warnings string.
Glubyte *errString;
glGetString( GL_PROGRAM_ERROR_STRING_ARB,
&errString );
fprintf( stderr, error at position: %d\n%s\n,
errPos, errString );
}
15
Creating a Vertex Program
When finished with a program object, delete it
16
Specifying Program Parameters
Three types
17
Specifying Vertex Attributes
Up to Nx4 per-vertex generic attributes
Values specified with (several) new commands
glVertexAttrib4fARB( index, x, y, z, w )
glVertexAttribs4fvARB( index, values )
20
Specifying Vertex Attributes
Setting vertex attribute 0 provokes vertex
program execution
Setting any other vertex attribute updates the
current values of the attribute register
program.env[0] program.local[0]
program.env[N-1] Vertex program.local[N-1]
Program
Address Temporary
Variables Variables
User defined User defined
Vertex Result
Registers
result.*
24
Program Environment and
Program Local Registers
Program environment registers
access using: program.env[i]
i in [0,GL_MAX_PROGRAM_ENV_PARAMETERS_ARB-1]
25
Vertex Attribute Registers
Attribute Components Underlying State
Register
vertex.position (x,y,z,w) object position
vertex.weight (w,w,w,w) vertex weights 0-3
vertex.weight[n] (w,w,w,w) vertex weights n-n+3
vertex.normal (x,y,z,1) normal
vertex.color (r,g,b,a) primary color
vertex.color.primary (r,g,b,a) primary color
vertex.color.secondary (r,g,b,a) secondary color
vertex.fogcoord (f,0,0,1) fog coordinate
vertex.texcoord (s,t,r,q) texture coordinate, unit 0
vertex.texcoord[n] (s,t,r,q) texture coordinate, unit n
vertex.matrixindex (i,i,i,i) vertex matrix indices 0-3
vertex.matrixindex[n] (i,i,i,i) vertex matrix indices n-n+3
vertex.attrib[n] (x,y,z,w) generic vertex attribute n
TEMP flag;
TEMP tmp, ndotl, keenval;
31
Program Parameter Variables
Set of four-component floating point vectors used
as constants during program execution
Bound either
Explicitly (declaration of param variables)
Implicitly (inline usage of constants)
32
Program Parameter Variable
Bindings
Explicit Constant Binding
Single Declaration
PARAM a = {1.0, 2.0, 3.0, 4.0}; (1.0, 2.0, 3.0, 4.0)
PARAM b = {3.0}; (3.0, 0.0, 0.0, 1.0)
PARAM c = {1.0, 2.0}; (1.0, 2.0, 0.0, 1.0)
PARAM d = {1.0, 2.0, 3.0 }; (1.0, 2.0, 3.0, 1.0)
PARAM e = 3.0; (3.0, 3.0, 3.0, 3.0)
Array Declaration
PARAM arr[2] = { {1.0, 2.0, 3.0, 4.0},
{5.0, 6.0, 7.0, 8.0} };
33
Program Parameter Variable
Bindings
Implicit Constant Binding
34
Program Parameter Variable
Bindings
Program Environment/Local Parameter Binding
PARAM a = program.local[8];
PARAM b = program.env[9];
PARAM arr[2] = program.local[4..5];
PARAM mat[4] = program.env[0..3];
35
Program Parameter Variable
Bindings
Material Property Binding
Bind to current GL material properties
36
Program Parameter Variable
Bindings
Binding Components Underlying GL state
37
Program Parameter Variable
Bindings
Light Property Binding
PARAM ambient = state.light[0].ambient;
PARAM diffuse = state.light[0].diffuse;
38
Output Variables
Variables that are declared bound to any vertex
result register
39
Aliasing of Variables
Allows multiple variable names to refer to a single
underlying variable
40
Additional Notes on Variables
May be declared anywhere prior to first usage
42
Assembly Language
Instruction Format:
Examples:
MOV R1, R2;
MAD R1, R2, R3, -R4; 43
Assembly Language
Source registers can be negated:
before after
R1 R2 R1 R2
0.0 x 7.0 x -7.0 x 7.0 x
0.0 y 3.0 y -3.0 y 3.0 y
0.0 z 6.0 z -6.0 z 6.0 z
0.0 w 2.0 w -2.0 w 2.0 w
44
Assembly Language
Source registers can be swizzled":
before after
R1 R2 R1 R2
0.0 x 7.0 x 3.0 x 7.0 x
0.0 y 3.0 y 6.0 y 3.0 y
0.0 z 6.0 z 2.0 z 6.0 z
0.0 w 2.0 w 7.0 w 2.0 w
45
Note: MOV R1, R2.xxxx; MOV R1, R2.x;
Assembly Language
Destination register can mask which components
are written to
46
Vertex Programming
Assembly Language
Destination register masking:
before after
R1 R2 R1 R2
0.0 x 7.0 x -7.0 x 7.0 x
0.0 y 3.0 y 0.0 y 3.0 y
0.0 z 6.0 z 0.0 z 6.0 z
0.0 w 2.0 w -2.0 w 2.0 w
47
Vertex Programming
Assembly Language
There are 27 instructions in total
ABS EX2 MAD RSQ
ADD EXP MAX SGE
ARL FLR MIN SLT
DP3 FRC MOV SUB
DP4 LG2 MUL SWZ
DPH LIT POW XPD
DST LOG RCP
48
Example Program #1
Simple 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
49
Example Program #2
Simple ambient, specular, and diffuse lighting
(single, infinite light, local viewer)
!!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; 50
Example Program #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; 51
END
Program Options
OPTION mechanism for future extensibility
Only one option: ARB_position_invariant
Guarantees position of vertex is same as what
it would be if vertex program mode is disabled
User clipping also performed
Useful for mixed-mode multi-pass
At start of program
OPTION ARB_position_invariant
Error if program attempts to write to
result.position
52
Querying Implementation-
specific Limits
Max number of instructions
glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
GL_MAX_PROGRAM_INSTRUCTIONS, &maxInsts );
Max number of temporaries
glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
GL_MAX_PROGRAM_INSTRUCTIONS, &maxTemps );
Max number of program parameter bindings
glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
GL_MAX_PROGRAM_PARAMETERS, &maxParams );
Others (including native limits)
Error if it attempts to
55
Generic and Conventional
Attribute Mappings
Conventional Generic Attribute
Attribute
vertex.position vertex.attrib[0]
vertex.weight vertex.attrib[1]
vertex.weight[0] vertex.attrib[1]
vertex.normal vertex.attrib[2]
vertex.color vertex.attrib[3]
vertex.color.primary vertex.attrib[3]
vertex.color.secondary vertex.attrib[4]
vertex.fogcoord vertex.attrib[5]
vertex.texcoord vertex.attrib[8]
vertex.texcoord[0] vertex.attrib[8]
vertex.texcoord[1] vertex.attrib[9]
vertex.texcoord[2] vertex.attrib[10]
vertex.texcoord[3] vertex.attrib[11]
vertex.texcoord[4] vertex.attrib[12]
vertex.texcoord[5] vertex.attrib[13]
vertex.texcoord[6] vertex.attrib[14]
vertex.texcoord[7] vertex.attrib[15]
In practice, probably use either conventional or generic not both 56
Wrap-Up
Increased programmability
Customizable engine for transform, lighting,
texture coordinate generation, and more.
Widely available!
great, portable target for higher level
abstractions
Vendor extensions available for dynamic
branching
will roll those into an ARBvp2 spec soon.
57
Questions?
[email protected]
58