Game Development Reference
In-Depth Information
Of course, several of the values needed in this calculation could be com-
puted in the vertex shader, and we could use the interpolated results in the
pixel shader. This is usually a performance win because we assume that
most of our triangles fill more than a pixel or two, so that number of pixels
to fill is significantly more than the number of vertices to shade. However,
a precise analysis can be complicated because the number of vertices and
pixels is not the only factor; the number of execution units available for
vertex and pixel shading is also important. Furthermore, on some hard-
ware, a pool of generic execution units are shared between vertex and pixel
shading. There can also be performance implications for increasing the
number of interpolated values. Still, dividing up the labor to do more cal-
culations per vertex is a speedup on most platforms and in most situations.
Listings 10.15 and 10.16 show one way we could shift work up to the vertex
shader.
/ /
Mesh
i n p u t s .
s t r u c t
I n p u t
{
f l o a t 4
p o s
:
POSITION ;
/ /
p o s i t i o n
i n
model
space
f l o a t 3
n o r m a l
: NORMAL ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
} ;
/ /
V e r t e x
s h a d e r
o u t p u t
s t r u c t
O u t p u t
{
f l o a t 4
c l i p P o s
:
POSITION ;
/ /
c l i p space
p o s i t i o n
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
f l o a t 3
n o r m a l
:
TEXCOORD1 ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 3
L
:
TEXCOORD2 ;
/ /
v e c t o r
t o
l i g h t
f l o a t 3 H
:
TEXCOORD3 ;
/ /
h a l f w a y
v e c t o r
f l o a t 3
l i g h t C o l o r
:
TEXCOORD4 ;
/ /
l i g h t
c o l o r
+
a t t e n u a t i o n
f a c t o r
} ;
/ /
Model −> c l i p
t r a n s f o r m
m a t r i x .
uniform
f l o a t 4 x 4
m o d e l T o C l i p ;
/ /
Omni
l i g h t
p o s i t i o n ,
i n MODEL space ,
uniform
f l o a t 3
omniPos ;
/ /
R e c i p r o c a l
o f
omni
l i g h t
r a d i u s .
( The
l i g h t
w i l l
f a l l o f f
/ /
l i n e a r l y
t o
z e r o
a t
t h i s
r a d i u s ) .
Note
t h a t
i t ' s
common
t o
t u c k
/ /
t h i s
i n t o
t h e w component
o f
t h e
p o s i t i o n ,
t o
reduce
t h e
number
o f
/ /
c o n s t a n t s ,
s i n c e
each
c o n s t a n t
u s u a l l y
t a k e s
a
f u l l
4D v e c t o r
s l o t .
uniform
f l o a t
invOmniRad ;
/ /
U n a t t e n u a t e d
omni
l i g h t
c o l o r
uniform
f l o a t 3
o m n i C o l o r ;
/ /
View
p o s i t i o n ,
i n MODEL space
uniform
f l o a t 3
v i e w P o s ;
/ /
The
body
o f
our
v e r t e x
s h a d e r
O u t p u t
main ( I n p u t
i n p u t )
{
O u t p u t
o u t p u t ;
/ /
T r a n s f o r m
v e r t e x
p o s i t i o n
t o
c l i p
space .
o u t p u t . c l i p P o s
= mul ( i n p u t . pos ,
m o d e l T o C l i p ) ;
Search WWH ::




Custom Search