Table Of ContentLearn Vertex and
Pixel Shader
Programming
with DirectX® 9
James C. Leiterman
This page intentionally left blank.
Learn Vertex and
Pixel Shader
Programming
with DirectX® 9
James C. Leiterman
Wordware Publishing, Inc.
LibraryofCongressCataloging-in-PublicationData
Leiterman,JamesC.
LearnVertexandPixelShaderprogrammingwithDirectX9/byJamesLeiterman.
p.cm.
ISBN1-55622-287-4(pbk.)
1. Vectorprocessing(Computerscience) 2. Computer
games--Programming. 3. DirectX. I. Title.
QA76.5.L4442003
794.8'1666--dc22 2003020944
CIP
©2004,WordwarePublishing,Inc.
AllRightsReserved
2320LosRiosBoulevard
Plano,Texas75074
Nopartofthisbookmaybereproducedinanyformorbyanymeans
withoutpermissioninwritingfromWordwarePublishing,Inc.
PrintedintheUnitedStatesofAmerica
ISBN1-55622-287-4
10 9 8 7 6 5 4 3 2 1
0310
Microsoft,DirectX,andWindowsareeitherregisteredtrademarksortrademarksofMicrosoftCorporationinthe
UnitedStatesand/orothercountries
Allbrandnamesandproductnamesmentionedinthisbookaretrademarksorservicemarksoftheirrespective
companies.Anyomissionormisuse(ofanykind)ofservicemarksortrademarksshouldnotberegardedasintent
to infringe on the property of others. The publisher recognizes and respects all marks used by companies,
manufacturers,anddevelopersasameanstodistinguishtheirproducts.
Thisbookissoldasis,withoutwarrantyofanykind,eitherexpressorimplied,respectingthecontentsofthisbook
andanydisksorprogramsthatmayaccompanyit,includingbutnotlimitedtoimpliedwarrantiesforthebook’s
quality,performance,merchantability,orfitnessforanyparticularpurpose.NeitherWordwarePublishing,Inc.
noritsdealersordistributorsshallbeliabletothepurchaseroranyotherpersonorentitywithrespecttoany
liability,loss,ordamagecausedorallegedtohavebeencauseddirectlyorindirectlybythisbook.
All inquiries for volume purchases of this book should be addressed to Wordware
Publishing,Inc.,attheaboveaddress.Telephoneinquiriesmaybemadebycalling:
(972)423-0090
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Chapter 1 Introduction . . . . . . . . . . . . . . . . . 1
Chapter 2 Getting Started . . . . . . . . . . . . . . . 7
DirectXVersionChecking. . . . . . . . . . . . . . . . . . 9
DataAlignmentIssues . . . . . . . . . . . . . . . . . . . 12
VideoCards. . . . . . . . . . . . . . . . . . . . . . . . . 12
Version(s)Determination:VertexandPixelShader . . . . 15
Plumbing:TheRenderingPipeline. . . . . . . . . . . . . 24
TheVector. . . . . . . . . . . . . . . . . . . . . . . . . . 26
GraphicsProcessorUnit(GPU)SwizzlingData. . . . . . 30
Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Part I Vertex Shaders . . . . . . . . . . . . . . . . . 33
Chapter 3 Vertex Shaders. . . . . . . . . . . . . . . 35
VertexShaderRegisters. . . . . . . . . . . . . . . . . . . 35
InstructionModifiers . . . . . . . . . . . . . . . . . . . . 39
VertexInputStreams . . . . . . . . . . . . . . . . . . . . 40
VertexShaderInstructions . . . . . . . . . . . . . . . . . 41
Assembly(Scripting)Commands . . . . . . . . . . . . . 44
VertexShaderAssembly . . . . . . . . . . . . . . . . . . 52
VertexShaderInstructions(DataConversions) . . . . . . 55
VertexShaderInstructions(Mathematics) . . . . . . . . . 58
VectortoVectorSummation v+w . . . . . . . . . 58
3DCartesianCoordinateSystem. . . . . . . . . . . . . . 59
CrossProduct v×w. . . . . . . . . . . . . . . . . 61
DotProduct . . . . . . . . . . . . . . . . . . . . . . 64
Reciprocals . . . . . . . . . . . . . . . . . . . . . . . . . 68
Division . . . . . . . . . . . . . . . . . . . . . . . . 70
SquareRoots . . . . . . . . . . . . . . . . . . . . . 71
VectorMagnitude. . . . . . . . . . . . . . . . . . . . . . 73
2DDistance . . . . . . . . . . . . . . . . . . . . . . 73
v
Contents
3DDistance . . . . . . . . . . . . . . . . . . . . . . 74
SpecialFunctions . . . . . . . . . . . . . . . . . . . . . . 75
Chapter 4 Flow Control . . . . . . . . . . . . . . . . 83
BranchlessCoding . . . . . . . . . . . . . . . . . . . . . 83
BranchingCode. . . . . . . . . . . . . . . . . . . . . . . 91
Chapter 5 Matrix Math. . . . . . . . . . . . . . . . 101
Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . 102
VectortoVectorSummation v+w . . . . . . . . 103
TheMatrix. . . . . . . . . . . . . . . . . . . . . . . . . 103
MatrixCopy D=A . . . . . . . . . . . . . . . . 107
MatrixSummation D=A+B. . . . . . . . . . . 107
ScalarMatrixProduct rA . . . . . . . . . . . . . 109
ApplyMatrixtoVector(Multiplication) vA. . . . 110
MatrixMultiplication D=AB. . . . . . . . . . . 115
MatrixSetIdentity . . . . . . . . . . . . . . . . . . . 118
ScalingTriangles . . . . . . . . . . . . . . . . . . . . . 120
MatrixSetScale . . . . . . . . . . . . . . . . . . . . 121
MatrixSetTranslation . . . . . . . . . . . . . . . . . 122
MatrixTranspose . . . . . . . . . . . . . . . . . . . . 123
MatrixInverse mD=mA–1. . . . . . . . . . . . . . 124
Chapter 6 Matrix Deux: A Wee Bit o’ Trig. . . . . . 129
SineandCosineFunctions . . . . . . . . . . . . . . . . 131
vmp_SinCos(X86) . . . . . . . . . . . . . . . . . . . 133
MatrixRotations. . . . . . . . . . . . . . . . . . . . . . 134
MatrixSetXRotation . . . . . . . . . . . . . . . . . 135
MatrixSetYRotation . . . . . . . . . . . . . . . . . 136
MatrixSetZRotation . . . . . . . . . . . . . . . . . 137
Chapter 7 Quaternions. . . . . . . . . . . . . . . . 139
QuaternionOperations . . . . . . . . . . . . . . . . . . 143
QuaternionAddition . . . . . . . . . . . . . . . . . . 143
QuaternionSubtraction. . . . . . . . . . . . . . . . . 144
QuaternionDotProduct(InnerProduct). . . . . . . . 144
QuaternionMagnitude(LengthofVector). . . . . . . 145
QuaternionNormalization . . . . . . . . . . . . . . . 145
QuaternionConjugation D=A . . . . . . . . . . . . 147
QuaternionInversion D=A–1 . . . . . . . . . . . . . 147
QuaternionMultiplication D=AB . . . . . . . . . . 148
QuaternionDivision . . . . . . . . . . . . . . . . . . 149
vi
Contents
Chapter 8 Vertex Play . . . . . . . . . . . . . . . . 151
D3DX9Sample:VertexShader.cpp . . . . . . . . . . . . 151
AWeeBito’SimpleParticlePhysics. . . . . . . . . . . 158
Chapter 9 Texture 101 . . . . . . . . . . . . . . . . 165
WhatIsaPolygon? . . . . . . . . . . . . . . . . . . . . 166
WhatIsShading? . . . . . . . . . . . . . . . . . . . . . 166
WhatIsColorMixing? . . . . . . . . . . . . . . . . . . 167
WhatIsaTexture?. . . . . . . . . . . . . . . . . . . . . 169
WhatIsTextureFiltering?. . . . . . . . . . . . . . . . . 171
TextureDimensional . . . . . . . . . . . . . . . . . . . 171
BumpMapping . . . . . . . . . . . . . . . . . . . . . . 174
Part II Pixel Shaders. . . . . . . . . . . . . . . . . 181
Chapter 10 Pixel Shaders . . . . . . . . . . . . . . 183
PixelShaderVersionChecking . . . . . . . . . . . . . . 184
PixelShaderRegisters. . . . . . . . . . . . . . . . . . . 185
PixelShaderInstructions . . . . . . . . . . . . . . . . . 187
Assembly(Scripting)Commands. . . . . . . . . . . . . 192
PixelShaderInstructions(DataConversions) . . . . . . 197
PixelShaderAssembly . . . . . . . . . . . . . . . . . . 198
InstructionModifiers . . . . . . . . . . . . . . . . . . . 200
Co-IssuedInstruction. . . . . . . . . . . . . . . . . . 201
PixelShaderInstructions(Mathematics) . . . . . . . . . 201
SpecialFunctions . . . . . . . . . . . . . . . . . . . . . 206
BranchlessCode. . . . . . . . . . . . . . . . . . . . . . 209
BranchingCode . . . . . . . . . . . . . . . . . . . . . . 213
Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 11 Textures . . . . . . . . . . . . . . . . . 223
TextureRegisters . . . . . . . . . . . . . . . . . . . . . 223
PixelShaderInstructions(TextureMatrices) . . . . . 238
Chapter 12 Rendering Up, Up ’n Away . . . . . . . 245
PixelShadingforFunandProfit . . . . . . . . . . . . . 245
WhereDoYouGoFromHere? . . . . . . . . . . . . . . 246
vii
Contents
Epilogue. . . . . . . . . . . . . . . . . . . . . . . . . 249
Appendix A Shaders — Opcode Ordered . . . . . . 251
Appendix B Shaders — Mnemonic Ordered. . . . . 255
Appendix C Instruction Dissection. . . . . . . . . . 259
TheOpcodeDword. . . . . . . . . . . . . . . . . . . 261
TheParameter(Argument)Dword. . . . . . . . . . . 261
RegisterIdentifierCode . . . . . . . . . . . . . . . 262
Destination{XYZW}Elements. . . . . . . . . . . 263
SourceSwizzled{XYZW}Elements . . . . . . . . 263
Appendix D Floating-Point 101 . . . . . . . . . . . 267
Floating-PointComparison . . . . . . . . . . . . . . . . 270
Glossary. . . . . . . . . . . . . . . . . . . . . . . . . 273
References . . . . . . . . . . . . . . . . . . . . . . . 277
Index . . . . . . . . . . . . . . . . . . . . . . . . . . 280
viii
Preface
(or, Why Did He Write Yet Another Book?)
Anotherbook?Herewegoagain…
Why,yes!Forthoseofyouwhohavefollowed
myeventfulcareer,youalreadyknowthatthisis
actuallymythirdbook.Thirdbook?Well,mysec-
ondbook,VectorGameMathProcessors(thefirst
tobepublished)wasmetinthemarketplacewith
enthusiasm.
Everyonewhoreaditandcontactedmeseemedtolikeit.Itwas
moreofa“niche”book,asitprimarilytaughthowtovectorize
codetotakeadvantageofthevectorinstructionsetsofprocessors
onlyrecentlyintroducedintotheofficeandhome.Thesecon-
tainedfunctionalitythatwasonceconfinedonlytothedomainof
the“supercomputer.”Thisbookdiscussedpseudovectorcode
thatactuallyemulatedvectorprocessorsusinggenericoptimized
C,aswellasheavyutilizationofthefollowinginstructionsets:
(cid:1) X86(MMX,SSE(2),3D-Now!Professional)
(cid:1) PowerPCandAltivec
(cid:1) MIPS(MIPS-3D,MIPSV,etc.)
YoumayaskwhyIshouldwriteaboutthreedifferentprocessors
inonebookinsteadofthreeseparatebooks.Byusingthetutorials,
youshouldbeabletocrossprogramtheseandfutureprocessors
(especiallythoseproprietaryones).
Soifyouhavenotalready,gobuyacopy!
DidInotmentionthatthiswasmythirdbook?Well,thefirst
bookhasalongstory,butyou’llhavetobuythevectorbookand
readtheprefaceinittogetthescooponthatone!
Theproblemwithwriting“yetanotherbook”isthatthefirst
onetendstohavethebestjokesortheauthor’sfavoritestories.
ix