Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
The Direct3D Graphics Pipeline
Richard Atwater Thomson
August 13, 2006
ii
Contents
Preface xxvii
I Preliminaries 1
1 Introduction 31.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Display Technology . . . . . . . . . . . . . . . . . . . . . . . . . 61.3 Gamma Correction . . . . . . . . . . . . . . . . . . . . . . . . . 81.4 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.5 Basic Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . 121.6 Homogeneous Coordinates . . . . . . . . . . . . . . . . . . . . . 141.7 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.8 Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . 151.9 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.10 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.11 Style Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 201.12 COM Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221.13 Code Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . 271.14 Previous Versions of Direct3D . . . . . . . . . . . . . . . . . . . 37
1.14.1 DirectX 1-2 . . . . . . . . . . . . . . . . . . . . . . . . . 371.14.2 DirectX 3 . . . . . . . . . . . . . . . . . . . . . . . . . . 381.14.3 DirectX 5 . . . . . . . . . . . . . . . . . . . . . . . . . . 381.14.4 DirectX 6 . . . . . . . . . . . . . . . . . . . . . . . . . . 381.14.5 DirectX 7 . . . . . . . . . . . . . . . . . . . . . . . . . . 381.14.6 DirectX 8.0 . . . . . . . . . . . . . . . . . . . . . . . . . 391.14.7 DirectX 8.1 . . . . . . . . . . . . . . . . . . . . . . . . . 391.14.8 DirectX 9.0c (June 2006) . . . . . . . . . . . . . . . . . . 39
1.15 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2 Direct3D 432.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432.2 A Minimal Direct3D Application . . . . . . . . . . . . . . . . . 442.3 Direct3D Architecture . . . . . . . . . . . . . . . . . . . . . . . . 502.4 Direct3D HRESULTs . . . . . . . . . . . . . . . . . . . . . . . . . 51
iii
iv CONTENTS
2.5 Windowed and Exclusive Mode . . . . . . . . . . . . . . . . . . 522.6 Device Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532.8 IDirect3D9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572.9 Selecting a Device . . . . . . . . . . . . . . . . . . . . . . . . . . 592.10 Determining Available Resource Memory . . . . . . . . . . . . . 612.11 Device Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . 612.12 Identifying a Particular Device . . . . . . . . . . . . . . . . . . . 612.13 Creating the Device . . . . . . . . . . . . . . . . . . . . . . . . . 632.14 Multiple Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . 662.15 Adapter Group Devices . . . . . . . . . . . . . . . . . . . . . . . 67
3 Direct3D Devices 693.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.2 IDirect3DDevice9 . . . . . . . . . . . . . . . . . . . . . . . . . 703.3 Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763.4 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823.5 IDirect3DResource9 . . . . . . . . . . . . . . . . . . . . . . . . 823.6 Destroying a Device . . . . . . . . . . . . . . . . . . . . . . . . . 863.7 Miscellaneous Properties . . . . . . . . . . . . . . . . . . . . . . 863.8 Device Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.8.1 Resource Manager Statistics Queries . . . . . . . . . . . 903.8.2 Vertex Statistics Queries . . . . . . . . . . . . . . . . . . 913.8.3 Vertex Cache Queries . . . . . . . . . . . . . . . . . . . . 913.8.4 PIX Related Queries . . . . . . . . . . . . . . . . . . . . 92
3.9 Device States . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933.9.1 Miscellaneous Render States . . . . . . . . . . . . . . . . 1013.9.2 Setting Groups of State . . . . . . . . . . . . . . . . . . . 101
3.10 Device State Blocks . . . . . . . . . . . . . . . . . . . . . . . . . 1043.11 Pure Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4 2D Applications 1114.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1114.2 Pixel Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124.3 Accessing Surface Pixel Data . . . . . . . . . . . . . . . . . . . . 1144.4 Using GDI On A Surface . . . . . . . . . . . . . . . . . . . . . . 1194.5 Swap Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194.6 Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1224.7 Lost Devices and Reset . . . . . . . . . . . . . . . . . . . . . . . 1254.8 Video Scan Out . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4.8.1 Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1264.8.2 Gamma Ramp . . . . . . . . . . . . . . . . . . . . . . . . 127
4.9 2D Pixel Copies . . . . . . . . . . . . . . . . . . . . . . . . . . . 1284.9.1 Pixel Copies Within Device Memory . . . . . . . . . . . 1294.9.2 Copies From System Memory To Device Memory . . . . 1314.9.3 Copies From Device Memory To System Memory . . . . 133
CONTENTS v
4.10 Filling Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . 1334.11 Window Messages . . . . . . . . . . . . . . . . . . . . . . . . . . 1334.12 rt 2DApp Sample Application . . . . . . . . . . . . . . . . . . . 135
II Geometry Processing 157
5 Modeling 1595.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1595.2 Modeling Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . 1605.3 Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1615.4 Render Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . 1635.5 Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 1645.6 Vertex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1675.7 Flexible Vertex Formats . . . . . . . . . . . . . . . . . . . . . . 1675.8 Vertex Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.8.1 Fixed-Function Declarations . . . . . . . . . . . . . . . . 1775.9 Vertex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1785.10 Indexed Primitives . . . . . . . . . . . . . . . . . . . . . . . . . 183
5.10.1 Index Buffers . . . . . . . . . . . . . . . . . . . . . . . . 1845.11 The Vertex Shader . . . . . . . . . . . . . . . . . . . . . . . . . 1855.12 Drawing Primitives . . . . . . . . . . . . . . . . . . . . . . . . . 1865.13 Vertex Data Streams . . . . . . . . . . . . . . . . . . . . . . . . 1885.14 Capabilities for Vertex Assembly . . . . . . . . . . . . . . . . . . 1905.15 Enhanced Primitives . . . . . . . . . . . . . . . . . . . . . . . . 191
5.15.1 Point Sprites . . . . . . . . . . . . . . . . . . . . . . . . . 1915.15.2 N -Patches . . . . . . . . . . . . . . . . . . . . . . . . . . 192
5.16 Higher Order Surfaces . . . . . . . . . . . . . . . . . . . . . . . . 1935.16.1 Triangular Patches . . . . . . . . . . . . . . . . . . . . . 1955.16.2 Rectangular Patches . . . . . . . . . . . . . . . . . . . . 196
5.17 Object Approximations . . . . . . . . . . . . . . . . . . . . . . . 1975.18 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
6 Vertex Transformations 1996.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1996.2 Vertex Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 2006.3 Transformation Matrices . . . . . . . . . . . . . . . . . . . . . . 2016.4 Order of Transformations . . . . . . . . . . . . . . . . . . . . . . 2036.5 World Transformation . . . . . . . . . . . . . . . . . . . . . . . . 2056.6 Transformation Hierarchy . . . . . . . . . . . . . . . . . . . . . . 2066.7 Vertex Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.7.1 Basic Vertex Blending . . . . . . . . . . . . . . . . . . . 2126.7.2 Indexed Vertex Blending . . . . . . . . . . . . . . . . . . 2136.7.3 Vertex Tweening . . . . . . . . . . . . . . . . . . . . . . 2146.7.4 Blending Within Device Limitations . . . . . . . . . . . . 215
6.8 Vertex Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
vi CONTENTS
6.9 Face Culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2186.10 Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
6.10.1 User Clip Planes . . . . . . . . . . . . . . . . . . . . . . . 2216.10.2 View Frustum Clipping . . . . . . . . . . . . . . . . . . . 2226.10.3 Guard Band Clipping . . . . . . . . . . . . . . . . . . . . 223
6.11 Screen Space and the Viewport . . . . . . . . . . . . . . . . . . 2246.12 rt VertexBlend Sample Application . . . . . . . . . . . . . . . 2256.13 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7 Viewing and Projection 2397.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2397.2 Viewing Through a Virtual Camera . . . . . . . . . . . . . . . . 2407.3 Planar Geometric Projections . . . . . . . . . . . . . . . . . . . 242
7.3.1 Parallel Projections . . . . . . . . . . . . . . . . . . . . . 2427.3.2 Perspective Projections . . . . . . . . . . . . . . . . . . . 244
7.4 Projection Transformation . . . . . . . . . . . . . . . . . . . . . 2447.4.1 Projection Matrices . . . . . . . . . . . . . . . . . . . . . 245
7.5 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 2467.6 rt Views Sample Application . . . . . . . . . . . . . . . . . . . 246
8 Lighting and Materials 2558.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2558.2 Transformed Vertices . . . . . . . . . . . . . . . . . . . . . . . . 2568.3 Lighting Calculations . . . . . . . . . . . . . . . . . . . . . . . . 2578.4 Surface Material Properties . . . . . . . . . . . . . . . . . . . . . 2608.5 Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
8.5.1 Directional Lights . . . . . . . . . . . . . . . . . . . . . . 2648.5.2 Point Lights . . . . . . . . . . . . . . . . . . . . . . . . . 2648.5.3 Spot Lights . . . . . . . . . . . . . . . . . . . . . . . . . 2648.5.4 Light Attenuation . . . . . . . . . . . . . . . . . . . . . . 265
8.6 The Illumination Model . . . . . . . . . . . . . . . . . . . . . . . 2668.6.1 Transforming to Camera Space . . . . . . . . . . . . . . 2678.6.2 Ambient Reflection . . . . . . . . . . . . . . . . . . . . . 2678.6.3 Diffuse Reflection . . . . . . . . . . . . . . . . . . . . . . 2678.6.4 Specular Reflection . . . . . . . . . . . . . . . . . . . . . 2688.6.5 Emission . . . . . . . . . . . . . . . . . . . . . . . . . . . 2698.6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
8.7 rt Lighting Sample Application . . . . . . . . . . . . . . . . . 270
9 Vertex Shaders 3019.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3019.2 Vertex Shader Architecture . . . . . . . . . . . . . . . . . . . . . 302
9.2.1 Input Registers . . . . . . . . . . . . . . . . . . . . . . . 3039.2.2 Constant Registers and the Address Register . . . . . . . 3049.2.3 Output Registers . . . . . . . . . . . . . . . . . . . . . . 3059.2.4 Temporary Registers . . . . . . . . . . . . . . . . . . . . 305
CONTENTS vii
9.2.5 The Loop Counter Register . . . . . . . . . . . . . . . . 3059.2.6 The Predicate Register . . . . . . . . . . . . . . . . . . . 3069.2.7 Sampler Registers . . . . . . . . . . . . . . . . . . . . . . 3069.2.8 Register Modifiers . . . . . . . . . . . . . . . . . . . . . . 306
9.3 Vertex Shader 1.1 Architecture . . . . . . . . . . . . . . . . . . . 3079.4 Vertex Shader 2.0 Architecture . . . . . . . . . . . . . . . . . . . 3089.5 Vertex Shader 2.x Architecture . . . . . . . . . . . . . . . . . . . 3099.6 Vertex Shader 3.0 Architecture . . . . . . . . . . . . . . . . . . . 3109.7 Shader Instruction Syntax . . . . . . . . . . . . . . . . . . . . . 3109.8 Execution Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 3129.9 Software Vertex Processing . . . . . . . . . . . . . . . . . . . . . 3129.10 Vertex Shader 1.1 Instructions . . . . . . . . . . . . . . . . . . . 313
9.10.1 Declaration Instructions . . . . . . . . . . . . . . . . . . 3159.10.2 Basic Arithmetic Instructions . . . . . . . . . . . . . . . 3169.10.3 Matrix Instructions . . . . . . . . . . . . . . . . . . . . . 3189.10.4 Comparison Instructions . . . . . . . . . . . . . . . . . . 3199.10.5 Lighting Instructions . . . . . . . . . . . . . . . . . . . . 320
9.11 Vertex Shader 2.0 Instructions . . . . . . . . . . . . . . . . . . . 3219.11.1 Version 2.0 Flow Control Nesting Limits . . . . . . . . . 325
9.12 Vertex Shader 2.x Instructions . . . . . . . . . . . . . . . . . . . 3269.12.1 Version 2.x Flow Control Nesting Limits . . . . . . . . . 329
9.13 Vertex Shader 3.0 Instructions . . . . . . . . . . . . . . . . . . . 3309.13.1 Version 3.0 Flow Control Nesting Limits . . . . . . . . . 332
9.14 Manipulating Shaders . . . . . . . . . . . . . . . . . . . . . . . . 3329.15 Drawing Multiple Instances . . . . . . . . . . . . . . . . . . . . . 3349.16 Shader Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3359.17 Common Computations . . . . . . . . . . . . . . . . . . . . . . . 336
9.17.1 Constant Generation . . . . . . . . . . . . . . . . . . . . 3379.17.2 Fractional Part . . . . . . . . . . . . . . . . . . . . . . . 3379.17.3 Absolute Value . . . . . . . . . . . . . . . . . . . . . . . 3379.17.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . 3389.17.5 Square Root . . . . . . . . . . . . . . . . . . . . . . . . . 3389.17.6 Conditional Selection . . . . . . . . . . . . . . . . . . . . 3389.17.7 Clamping To An Interval . . . . . . . . . . . . . . . . . . 3399.17.8 Floor and Ceiling . . . . . . . . . . . . . . . . . . . . . . 3399.17.9 Vector Cross Product . . . . . . . . . . . . . . . . . . . . 3409.17.10Vector Normalization . . . . . . . . . . . . . . . . . . . . 3409.17.11Transposed Matrix Multiplication . . . . . . . . . . . . . 3409.17.12Signum Function . . . . . . . . . . . . . . . . . . . . . . 3419.17.13Minimum and Maximum Vector Component . . . . . . . 3419.17.14Trigonometric Functions . . . . . . . . . . . . . . . . . . 3429.17.15Exponential and Logarithmic Functions . . . . . . . . . . 344
9.18 Fixed-Function Processing . . . . . . . . . . . . . . . . . . . . . 3469.18.1 Register Layout . . . . . . . . . . . . . . . . . . . . . . . 3469.18.2 Constant Register Layout . . . . . . . . . . . . . . . . . 3469.18.3 Coordinate Transformation . . . . . . . . . . . . . . . . . 348
viii CONTENTS
9.18.4 Vertex Blending . . . . . . . . . . . . . . . . . . . . . . . 3489.18.5 Position Output . . . . . . . . . . . . . . . . . . . . . . . 3499.18.6 Normalize Eye Normal . . . . . . . . . . . . . . . . . . . 3499.18.7 Non-Homogeneous Eye Position . . . . . . . . . . . . . . 3499.18.8 Eye Space Vectors . . . . . . . . . . . . . . . . . . . . . . 3499.18.9 Fog Output . . . . . . . . . . . . . . . . . . . . . . . . . 3509.18.10Point Parameters . . . . . . . . . . . . . . . . . . . . . . 3509.18.11Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . 3509.18.12Texture Coordinate Generation . . . . . . . . . . . . . . 352
9.19 Beyond Fixed-Function Processing . . . . . . . . . . . . . . . . . 3539.20 rt VertexShader Sample Application . . . . . . . . . . . . . . . 3549.21 Vertex Shader SDK Samples . . . . . . . . . . . . . . . . . . . . 354
9.21.1 BumpSelfShadow . . . . . . . . . . . . . . . . . . . . . . 3549.21.2 BumpWaves . . . . . . . . . . . . . . . . . . . . . . . . . 3559.21.3 DolphinVS . . . . . . . . . . . . . . . . . . . . . . . . . . 3559.21.4 SkinnedMesh . . . . . . . . . . . . . . . . . . . . . . . . . 3569.21.5 SphereMap . . . . . . . . . . . . . . . . . . . . . . . . . . 3579.21.6 VertexBlend . . . . . . . . . . . . . . . . . . . . . . . . . 3579.21.7 VertexShader . . . . . . . . . . . . . . . . . . . . . . . . 357
III Pixel Processing 377
10 Rasterization and Shading 37910.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37910.2 Shading and Filling . . . . . . . . . . . . . . . . . . . . . . . . . 38010.3 Scanline Rendering . . . . . . . . . . . . . . . . . . . . . . . . . 38210.4 Source Pixel Generation . . . . . . . . . . . . . . . . . . . . . . 38310.5 Source Pixel Data . . . . . . . . . . . . . . . . . . . . . . . . . . 38510.6 rt Rasterize Sample Application . . . . . . . . . . . . . . . . . 386
11 Basic Texturing 40311.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40311.2 Direct3D Multitexture Architecture . . . . . . . . . . . . . . . . 40411.3 Texture Resources . . . . . . . . . . . . . . . . . . . . . . . . . . 407
11.3.1 IDirect3DBaseTexture9 . . . . . . . . . . . . . . . . . . 40911.3.2 IDirect3DTexture9 . . . . . . . . . . . . . . . . . . . . 41111.3.3 IDirect3DCubeTexture9 . . . . . . . . . . . . . . . . . . 41411.3.4 IDirect3DVolume9 . . . . . . . . . . . . . . . . . . . . . 41611.3.5 IDirect3DVolumeTexture9 . . . . . . . . . . . . . . . . 420
11.4 Texture Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . 42211.4.1 Palette Based Textures . . . . . . . . . . . . . . . . . . . 42211.4.2 Compressed Texture Formats . . . . . . . . . . . . . . . 423
11.5 Texture Coordinate Processing . . . . . . . . . . . . . . . . . . . 42711.5.1 Texture Coordinate Generation . . . . . . . . . . . . . . 43011.5.2 Texture Coordinate Transformation . . . . . . . . . . . . 430
CONTENTS ix
11.5.3 Texture Addressing . . . . . . . . . . . . . . . . . . . . . 43211.5.4 Texture Wrapping . . . . . . . . . . . . . . . . . . . . . . 434
11.6 Texture Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . 43511.7 Texture Stage Processing . . . . . . . . . . . . . . . . . . . . . . 43911.8 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
11.8.1 Single Stage Light Maps . . . . . . . . . . . . . . . . . . 44611.8.2 Decals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
11.9 Specular Addition . . . . . . . . . . . . . . . . . . . . . . . . . . 44811.10 Textured Primitives . . . . . . . . . . . . . . . . . . . . . . . . . 44911.11 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 45111.12 Sample Applications . . . . . . . . . . . . . . . . . . . . . . . . . 451
11.12.1Book Samples . . . . . . . . . . . . . . . . . . . . . . . . 45211.12.2SDK Samples . . . . . . . . . . . . . . . . . . . . . . . . 45211.12.3rt Texture Sample Application . . . . . . . . . . . . . . 453
12 Advanced Texturing 46912.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46912.2 Multiple Texture Stages . . . . . . . . . . . . . . . . . . . . . . . 46912.3 Environment Mapping . . . . . . . . . . . . . . . . . . . . . . . 470
12.3.1 Cubic Environment Mapping . . . . . . . . . . . . . . . . 47012.3.2 Spherical Environment Mapping . . . . . . . . . . . . . . 470
12.4 Bump Mapping Techniques . . . . . . . . . . . . . . . . . . . . . 47012.4.1 Dot3 Bump Mapping . . . . . . . . . . . . . . . . . . . . 47012.4.2 Emboss Mapping . . . . . . . . . . . . . . . . . . . . . . 47012.4.3 Environment Mapped Bump Mapping . . . . . . . . . . 47012.4.4 Tangent Basis Bump Mapping . . . . . . . . . . . . . . . 470
12.5 Texturing Samples . . . . . . . . . . . . . . . . . . . . . . . . . . 470
13 Pixel Shaders 47113.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47113.2 Pixel Shader Architecture . . . . . . . . . . . . . . . . . . . . . 47213.3 Pixel Shader Architecture Versions . . . . . . . . . . . . . . . . 472
13.3.1 Pixel Shader 1.0 . . . . . . . . . . . . . . . . . . . . . . . 47213.3.2 Pixel Shader 1.1 . . . . . . . . . . . . . . . . . . . . . . . 48213.3.3 Pixel Shader 1.2 . . . . . . . . . . . . . . . . . . . . . . . 48213.3.4 Pixel Shader 1.3 . . . . . . . . . . . . . . . . . . . . . . . 48213.3.5 Pixel Shader 1.4 . . . . . . . . . . . . . . . . . . . . . . . 48213.3.6 Pixel Shader 2.0 . . . . . . . . . . . . . . . . . . . . . . . 48313.3.7 Pixel Shader 2.x . . . . . . . . . . . . . . . . . . . . . . . 48313.3.8 Pixel Shader 3.0 . . . . . . . . . . . . . . . . . . . . . . . 483
13.4 Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48313.4.1 Declaration Instructions . . . . . . . . . . . . . . . . . . 48313.4.2 Basic Arithmetic Instructions . . . . . . . . . . . . . . . 48413.4.3 Comparison Instructions . . . . . . . . . . . . . . . . . . 48413.4.4 Basic Texture Addressing . . . . . . . . . . . . . . . . . . 48513.4.5 Bump Mapping Instructions . . . . . . . . . . . . . . . . 487
x CONTENTS
13.5 PS 2.0 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . 48713.6 PS 2.x Instructions . . . . . . . . . . . . . . . . . . . . . . . . . 48813.7 PS 3.0 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . 48813.8 Manipulating Pixel Shaders . . . . . . . . . . . . . . . . . . . . 48913.9 Texture Stage States . . . . . . . . . . . . . . . . . . . . . . . . 49013.10 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49013.11 Debugging a Pixel Shader . . . . . . . . . . . . . . . . . . . . . 490
14 The Frame Buffer 49114.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49114.2 Fog Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49214.3 Alpha Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49214.4 The Z Buffer and Visibility . . . . . . . . . . . . . . . . . . . . . 49414.5 Stencil Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49714.6 Alpha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . 50114.7 Dithering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50514.8 Multisampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50614.9 Writing to the Render Target . . . . . . . . . . . . . . . . . . . 50814.10 rt FrameBuffer Sample Application . . . . . . . . . . . . . . . 508
IV D3DX Library 573
15 D3DX Utility Library 57515.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57515.2 Headers and Libraries . . . . . . . . . . . . . . . . . . . . . . . . 57615.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57615.4 Helper Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57615.5 Meshes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57715.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57715.7 Resource Image Information . . . . . . . . . . . . . . . . . . . . 57815.8 Resource Requirements . . . . . . . . . . . . . . . . . . . . . . . 57915.9 Format Conversion . . . . . . . . . . . . . . . . . . . . . . . . . 58015.10 Creating Texture Resources . . . . . . . . . . . . . . . . . . . . 582
15.10.1Creating Uninitialized Textures . . . . . . . . . . . . . . 58215.10.2Creating Textures From Files . . . . . . . . . . . . . . . 58315.10.3Creating Textures From Files In Memory . . . . . . . . . 58515.10.4Creating Textures From Resources . . . . . . . . . . . . 586
15.11 Loading Resources . . . . . . . . . . . . . . . . . . . . . . . . . . 58815.12 Saving Resources . . . . . . . . . . . . . . . . . . . . . . . . . . 59115.13 Filling Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . 59115.14 Filtering Textures . . . . . . . . . . . . . . . . . . . . . . . . . . 59215.15 Creating Normal Maps . . . . . . . . . . . . . . . . . . . . . . . 59315.16 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
15.16.1Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59415.16.2Fresnel’s Formulas . . . . . . . . . . . . . . . . . . . . . . 594
CONTENTS xi
15.17 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 59515.18 ID3DXBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59615.19 Vertex Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 59715.20 Intersection Testing . . . . . . . . . . . . . . . . . . . . . . . . . 59715.21 Shader Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . 59815.22 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
16 D3DX Concrete Types 60116.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60116.2 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60116.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60416.4 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
16.4.1 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . 61716.4.2 Identity, Translation, and Scaling Matrices . . . . . . . . 61816.4.3 Rotation Matrices . . . . . . . . . . . . . . . . . . . . . . 61816.4.4 Composite Transformation Matrices . . . . . . . . . . . . 61916.4.5 Viewing Matrices . . . . . . . . . . . . . . . . . . . . . . 61916.4.6 Orthographic Projection Matrices . . . . . . . . . . . . . 62016.4.7 Perspective Projection Matrices . . . . . . . . . . . . . . 62116.4.8 Shadow and Reflection Matrices . . . . . . . . . . . . . . 62216.4.9 Matrix Helper Classes . . . . . . . . . . . . . . . . . . . 622
16.5 Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62316.6 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
17 D3DX Helper Objects 63117.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63117.2 Matrix Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63117.3 Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63417.4 Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63817.5 Surface Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . 64017.6 Environment Map Rendering . . . . . . . . . . . . . . . . . . . . 642
18 High Level Shaders and Effects 64518.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64518.2 Visual Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
18.2.1 Text Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 64518.2.2 Effect Objects . . . . . . . . . . . . . . . . . . . . . . . . 652
18.3 Sample Applications . . . . . . . . . . . . . . . . . . . . . . . . . 657
19 D3DX Mesh Objects 67119.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67119.2 Use of const Qualifiers . . . . . . . . . . . . . . . . . . . . . . . 67219.3 Triangle Adjacencies . . . . . . . . . . . . . . . . . . . . . . . . 67419.4 Vertex and Triangle Remapping . . . . . . . . . . . . . . . . . . 67519.5 Base Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67619.6 Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
xii CONTENTS
19.6.1 Creating Meshes From Scratch . . . . . . . . . . . . . . . 68519.6.2 Primitive Construction . . . . . . . . . . . . . . . . . . . 68719.6.3 Reducing Mesh Data . . . . . . . . . . . . . . . . . . . . 68919.6.4 Adding Mesh Data . . . . . . . . . . . . . . . . . . . . . 69319.6.5 Mesh Intersection Tests . . . . . . . . . . . . . . . . . . . 69419.6.6 Mesh Conversion . . . . . . . . . . . . . . . . . . . . . . 69519.6.7 Meshes in X Files . . . . . . . . . . . . . . . . . . . . . . 698
19.7 Progressive Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . 70019.8 Simplification Mesh . . . . . . . . . . . . . . . . . . . . . . . . . 70419.9 Skin Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
19.9.1 Skin Meshes in X Files . . . . . . . . . . . . . . . . . . . 71319.10 rt D3DXSphere Sample Application . . . . . . . . . . . . . . . . 71419.11 SDK Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71419.12 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
20 D3DX Animation Objects 71720.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
V The Application 719
21 X Files 72121.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72121.2 X File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
21.2.1 X File Header . . . . . . . . . . . . . . . . . . . . . . . . 72221.2.2 Base Data Types . . . . . . . . . . . . . . . . . . . . . . 72321.2.3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 72321.2.4 Data Nodes . . . . . . . . . . . . . . . . . . . . . . . . . 72321.2.5 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . 724
21.3 Text Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72421.3.1 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . 72521.3.2 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 72521.3.3 Data Nodes . . . . . . . . . . . . . . . . . . . . . . . . . 72621.3.4 Nested Data . . . . . . . . . . . . . . . . . . . . . . . . . 728
21.4 Binary Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . 72921.4.1 Token Records . . . . . . . . . . . . . . . . . . . . . . . . 730
21.5 Predefined Templates . . . . . . . . . . . . . . . . . . . . . . . . 73121.5.1 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . 73221.5.2 Animation Key . . . . . . . . . . . . . . . . . . . . . . . 73221.5.3 Animation Options . . . . . . . . . . . . . . . . . . . . . 73321.5.4 Animation Set . . . . . . . . . . . . . . . . . . . . . . . . 73421.5.5 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . 73421.5.6 Boolean 2D . . . . . . . . . . . . . . . . . . . . . . . . . 73521.5.7 Color RGB . . . . . . . . . . . . . . . . . . . . . . . . . . 73521.5.8 Color RGBA . . . . . . . . . . . . . . . . . . . . . . . . . 73521.5.9 Coords 2D . . . . . . . . . . . . . . . . . . . . . . . . . . 736
CONTENTS xiii
21.5.10External Visual . . . . . . . . . . . . . . . . . . . . . . . 73621.5.11Float Keys . . . . . . . . . . . . . . . . . . . . . . . . . . 73621.5.12Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73721.5.13Frame Transform Matrix . . . . . . . . . . . . . . . . . . 73721.5.14FVF Data . . . . . . . . . . . . . . . . . . . . . . . . . . 73721.5.15Guid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73821.5.16Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73921.5.17 Indexed Color . . . . . . . . . . . . . . . . . . . . . . . . 73921.5.18 Inline Data . . . . . . . . . . . . . . . . . . . . . . . . . . 74021.5.19Material . . . . . . . . . . . . . . . . . . . . . . . . . . . 74021.5.20Material Wrap . . . . . . . . . . . . . . . . . . . . . . . . 74121.5.21Matrix 4x4 . . . . . . . . . . . . . . . . . . . . . . . . . . 74121.5.22Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74121.5.23Mesh Face . . . . . . . . . . . . . . . . . . . . . . . . . . 74221.5.24Mesh Face Wraps . . . . . . . . . . . . . . . . . . . . . . 74321.5.25Mesh Material List . . . . . . . . . . . . . . . . . . . . . 74321.5.26Mesh Normals . . . . . . . . . . . . . . . . . . . . . . . . 74421.5.27Mesh Texture Coords . . . . . . . . . . . . . . . . . . . . 74421.5.28Mesh Vertex Colors . . . . . . . . . . . . . . . . . . . . . 74521.5.29Patch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74521.5.30Patch Mesh . . . . . . . . . . . . . . . . . . . . . . . . . 74721.5.31Progressive Mesh Attribute Range . . . . . . . . . . . . . 74721.5.32Progressive Mesh Info . . . . . . . . . . . . . . . . . . . . 74721.5.33Progressive Mesh Vertex Split Record . . . . . . . . . . . 74821.5.34Progressive Mesh . . . . . . . . . . . . . . . . . . . . . . 74821.5.35Property Bag . . . . . . . . . . . . . . . . . . . . . . . . 74921.5.36Right Handed . . . . . . . . . . . . . . . . . . . . . . . . 74921.5.37Skin Weights . . . . . . . . . . . . . . . . . . . . . . . . . 74921.5.38String Property . . . . . . . . . . . . . . . . . . . . . . . 75021.5.39Texture Filename . . . . . . . . . . . . . . . . . . . . . . 75021.5.40Timed Float Keys . . . . . . . . . . . . . . . . . . . . . . 75121.5.41Url . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75121.5.42Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75121.5.43Vertex Duplication Indices . . . . . . . . . . . . . . . . . 75221.5.44X Skin Mesh Header . . . . . . . . . . . . . . . . . . . . 752
21.6 Headers, Libraries and Interfaces . . . . . . . . . . . . . . . . . . 75321.7 IDirectXFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75321.8 IDirectXFileEnumObject . . . . . . . . . . . . . . . . . . . . . 75621.9 IDirectXFileObject . . . . . . . . . . . . . . . . . . . . . . . . 75621.10 IDirectXFileData . . . . . . . . . . . . . . . . . . . . . . . . . 75721.11 IDirectXFileBinary . . . . . . . . . . . . . . . . . . . . . . . . 76021.12 IDirectXFileDataReference . . . . . . . . . . . . . . . . . . . 76121.13 IDirectXFileSaveObject . . . . . . . . . . . . . . . . . . . . . 76221.14 Reading Scene Data . . . . . . . . . . . . . . . . . . . . . . . . . 76321.15 Writing Scene Data . . . . . . . . . . . . . . . . . . . . . . . . . 76421.16 Exporting and Conversion . . . . . . . . . . . . . . . . . . . . . 764
xiv CONTENTS
21.17 Example X Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 76521.18 rt MakeScene Sample Program . . . . . . . . . . . . . . . . . . 767
22 Debugging 78922.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78922.2 Version Control Systems . . . . . . . . . . . . . . . . . . . . . . 79022.3 C++ Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
22.3.1 Dynamic Memory Allocation . . . . . . . . . . . . . . . . 79022.3.2 Accidental Assignments . . . . . . . . . . . . . . . . . . . 79122.3.3 String Handling . . . . . . . . . . . . . . . . . . . . . . . 79222.3.4 Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79222.3.5 C++ Exceptions . . . . . . . . . . . . . . . . . . . . . . . 792
22.4 Debugging Windows Applications . . . . . . . . . . . . . . . . . 79322.4.1 ANSI vs. Unicode . . . . . . . . . . . . . . . . . . . . . . 79422.4.2 Security Environment . . . . . . . . . . . . . . . . . . . . 79422.4.3 Special Directories . . . . . . . . . . . . . . . . . . . . . 79422.4.4 Output Debug Stream . . . . . . . . . . . . . . . . . . . 79622.4.5 Structured Exception Handling . . . . . . . . . . . . . . 79722.4.6 Stack Traces . . . . . . . . . . . . . . . . . . . . . . . . . 79922.4.7 Mini Dumps . . . . . . . . . . . . . . . . . . . . . . . . . 79922.4.8 Debugging Buffer Overruns and Underruns . . . . . . . . 80022.4.9 Fast User Switching in XP . . . . . . . . . . . . . . . . . 800
22.5 Debugging With Visual C++ 6 . . . . . . . . . . . . . . . . . . . 80122.5.1 Decoding Direct3D HRESULTs . . . . . . . . . . . . . . . . 80122.5.2 Highlighting Direct3D Identifiers . . . . . . . . . . . . . 80122.5.3 Expanding Direct3D Structures . . . . . . . . . . . . . . 80122.5.4 Limiting Step-Into . . . . . . . . . . . . . . . . . . . . . 80222.5.5 Remote Debugging . . . . . . . . . . . . . . . . . . . . . 80322.5.6 Multiple Monitor Debugging . . . . . . . . . . . . . . . . 803
22.6 Debugging With Direct3D . . . . . . . . . . . . . . . . . . . . . 80422.6.1 Debug and Retail Runtimes . . . . . . . . . . . . . . . . 80422.6.2 Checking HRESULTs . . . . . . . . . . . . . . . . . . . . . 80522.6.3 Reference Rasterizer . . . . . . . . . . . . . . . . . . . . 80522.6.4 DirectX Error Routines . . . . . . . . . . . . . . . . . . . 80522.6.5 Reproducing Problems . . . . . . . . . . . . . . . . . . . 80622.6.6 Exclusive Mode Problems . . . . . . . . . . . . . . . . . 80622.6.7 Driver Problems . . . . . . . . . . . . . . . . . . . . . . . 80622.6.8 Shader Problems . . . . . . . . . . . . . . . . . . . . . . 80722.6.9 Texturing Problems . . . . . . . . . . . . . . . . . . . . . 80822.6.10Processor Specific Graphics Processing . . . . . . . . . . 80822.6.11 Interfering Applications . . . . . . . . . . . . . . . . . . . 809
22.7 DirectX Diagnostic Tool . . . . . . . . . . . . . . . . . . . . . . 80922.8 DirectX Control Panel Applet . . . . . . . . . . . . . . . . . . . 81022.9 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
22.9.1 Asking Smart Questions . . . . . . . . . . . . . . . . . . 81322.9.2 IHV Developer Relations . . . . . . . . . . . . . . . . . . 814
CONTENTS xv
22.10 Filing Bug Reports . . . . . . . . . . . . . . . . . . . . . . . . . 814
23 Pipeline Performance 81723.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81723.2 Pipeline Bottlenecks . . . . . . . . . . . . . . . . . . . . . . . . . 81923.3 Optimization Techniques . . . . . . . . . . . . . . . . . . . . . . 819
23.3.1 Reordering Data . . . . . . . . . . . . . . . . . . . . . . . 81923.3.2 Reducing Data . . . . . . . . . . . . . . . . . . . . . . . . 82023.3.3 Redesigning . . . . . . . . . . . . . . . . . . . . . . . . . 821
23.4 Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82123.4.1 Vertex Processing . . . . . . . . . . . . . . . . . . . . . . 82223.4.2 Pixel Processing . . . . . . . . . . . . . . . . . . . . . . . 82223.4.3 Frame Buffer Processing . . . . . . . . . . . . . . . . . . 822
23.5 Image Compositing . . . . . . . . . . . . . . . . . . . . . . . . . 82223.6 Vertex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82223.7 Texture Map Size . . . . . . . . . . . . . . . . . . . . . . . . . . 82223.8 Scalable Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . 82223.9 Model Simplification . . . . . . . . . . . . . . . . . . . . . . . . 82223.10 Alpha Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82323.11 Stencil Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82323.12 Z Buffer Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82323.13 Guard Band clip . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
24 Installation and Setup 82524.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82524.2 Determining the DirectX Version . . . . . . . . . . . . . . . . . 82624.3 DirectSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
24.3.1 Determining the DirectX Version . . . . . . . . . . . . . 82724.3.2 Installing the DirectX Runtime . . . . . . . . . . . . . . 82724.3.3 Using a Callback Procedure . . . . . . . . . . . . . . . . 82924.3.4 Registering With DirectPlay Lobby . . . . . . . . . . . . 83224.3.5 Preparing the Redistributables . . . . . . . . . . . . . . . 833
24.4 Windows Installer . . . . . . . . . . . . . . . . . . . . . . . . . . 83324.4.1 Using a Launch Sequencer . . . . . . . . . . . . . . . . . 83424.4.2 Requiring the Proper Runtime Version . . . . . . . . . . 83424.4.3 Using a Custom Action to Invoke DirectSetup . . . . . . 835
24.5 Logo Requirements . . . . . . . . . . . . . . . . . . . . . . . . . 83524.6 AutoRun Enabled Installations . . . . . . . . . . . . . . . . . . . 83624.7 Testing Installation Programs . . . . . . . . . . . . . . . . . . . 83624.8 Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
VI Appendices 839
A A Guided Tour of the SDK 841A.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
xvi CONTENTS
A.2 A Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841A.3 Installing the SDK . . . . . . . . . . . . . . . . . . . . . . . . . 842A.4 Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842A.5 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 845A.6 Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845A.7 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846A.8 Tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846A.9 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847A.10 SDK Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
B D3DFrame 849B.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849B.2 AppWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850B.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850B.4 CD3DApplication . . . . . . . . . . . . . . . . . . . . . . . . . . 850B.5 CD3DScreensaver . . . . . . . . . . . . . . . . . . . . . . . . . . 854B.6 CD3DFont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854B.7 CD3DFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855B.8 CD3DFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856B.9 CD3DMesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857B.10 CD3DScreensaver . . . . . . . . . . . . . . . . . . . . . . . . . . 858B.11 CD3DArcBall . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860B.12 CD3DCamera . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861B.13 D3DUtil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862B.14 DXUtil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
C Shader Tokens 867C.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867C.2 Instruction Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . 867
D Sample Code 873D.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873D.2 C++ Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . 873
D.2.1 rt Library Code . . . . . . . . . . . . . . . . . . . . . . . 875D.3 Visual Basic Sample Code . . . . . . . . . . . . . . . . . . . . . 876
Glossary 877
Index 877
Source Code Listings
1.1 dshow.cpp: A simple AVI file player. . . . . . . . . . . . . . . . . 241.2 <rt/hr.h>: mapping HRESULTs to exceptions. . . . . . . . . . . . 291.3 dshow2.cpp: An improved simple AVI player. . . . . . . . . . . . 36
2.1 minimal.cpp: A minimal well-behaved Direct3D application. . . 44
3.1 <rt/states.h>: Setting aggregate state . . . . . . . . . . . . . . 101
4.1 <rt/surface.h>: A surface lock helper class. . . . . . . . . . . . 1164.2 rt 2DApp.cpp: A simple 2D application using StretchRect. . . . 136
5.1 <rt/vertexbuf.h>: A vertex buffer lock helper class. . . . . . . 182
6.1 rt VertexBlend.cpp: Vertex blending. . . . . . . . . . . . . . . 225
7.1 rt Views.cpp: Drawing multiple views . . . . . . . . . . . . . . . 246
8.1 rt Lighting.cpp: Lighting and material parameters. . . . . . . 2709.1 BumpShader.vsh: BumpSelfShadow shader 1. . . . . . . . . . . . 3599.2 BumpShader2.vsh: BumpSelfShadow shader 2. . . . . . . . . . . 3609.3 BumpShader3.vsh: BumpSelfShadow shader 3. . . . . . . . . . . 3619.4 BumpShader4.vsh: BumpSelfShadow shader 4. . . . . . . . . . . 3629.5 BumpWaves vertex shader. . . . . . . . . . . . . . . . . . . . . . 3639.6 DolphinTween.vsh: DolphinVS shader 1. . . . . . . . . . . . . . 3649.7 DolphinTween2.vsh: DolphinVS shader 2. . . . . . . . . . . . . . 3669.8 SkinnedMesh4.vsh: SkinnedMesh shader 4. . . . . . . . . . . . . 3689.9 SphereMap shader . . . . . . . . . . . . . . . . . . . . . . . . . . 3719.10 Blend.vsh: VertexBlend shader . . . . . . . . . . . . . . . . . . . 3729.11 Ripple.vsh: VertexShader shader . . . . . . . . . . . . . . . . . 3749.12 FogShader.vsh: VolumeFog shader 1 . . . . . . . . . . . . . . . . 37610.1 rt Rasterize.cpp: Rasterization. . . . . . . . . . . . . . . . . . 38611.1 rt Texture.cpp: Texturing. . . . . . . . . . . . . . . . . . . . . . 453
14.1 rt FrameBuffer.cpp: Frame buffer processing. . . . . . . . . . . 51018.1 water.fx: Water sample effect file. . . . . . . . . . . . . . . . . . 65818.2 cubemap.fx: CubeMap sample effect file. . . . . . . . . . . . . . 66721.1 rt MakeScene.cpp: X file output. . . . . . . . . . . . . . . . . . 76921.2 frame.h: X file output. . . . . . . . . . . . . . . . . . . . . . . . 77221.3 frame.cpp: X file output. . . . . . . . . . . . . . . . . . . . . . . 776
xvii
Figures
1.1 Subsystems of a typical 2D graphics adapter. . . . . . . . . . . . 71.2 Left-handed, right-handed and screen coordinate systems . . . . 161.3 Illustration of Aliasing. . . . . . . . . . . . . . . . . . . . . . . . . 181.4 Antialiasing During Resampling. . . . . . . . . . . . . . . . . . . 20
2.1 Relationship of Direct3D to other Windows subsystems. . . . . . 512.2 Direct3D device architecture . . . . . . . . . . . . . . . . . . . . 51
3.1 Resource interface hierarchy. . . . . . . . . . . . . . . . . . . . . . 823.2 Query object state transitions. . . . . . . . . . . . . . . . . . . . 88
4.1 Semantics of D3DSWAPEFFECT on a swap chain . . . . . . . . . . . 123
5.1 Illustration of a cycle of occlusion with four rectangles. . . . . . . 1625.2 Illustration of primitive types . . . . . . . . . . . . . . . . . . . . 1655.3 Modeling with primitive types . . . . . . . . . . . . . . . . . . . . 1665.4 Relationship between arguments to DrawIndexedPrimitive . . . 1905.5 Point sprite structure in screen space. . . . . . . . . . . . . . . . 1925.6 Bezier triangular patch vertex order. . . . . . . . . . . . . . . . . 195
6.1 Order of transformations . . . . . . . . . . . . . . . . . . . . . . . 2046.2 Illustration of a composite transformation. . . . . . . . . . . . . . 2056.3 Jointed linkage transformation hierarchy . . . . . . . . . . . . . . 2076.4 Visualization of vertex blending. . . . . . . . . . . . . . . . . . . 2086.5 Sample blend weight functions. . . . . . . . . . . . . . . . . . . . 2096.6 The effect of blend weight functions on vertex blending. . . . . . 2106.7 The effect of the world transformation on vertex blending. . . . . 2116.8 Fog factor functions. . . . . . . . . . . . . . . . . . . . . . . . . . 2176.9 Computing a triangle’s plane normal . . . . . . . . . . . . . . . . 2186.10 Relationship of triangles to the line of sight in culling. . . . . . . 2196.11 Illustration of clipping . . . . . . . . . . . . . . . . . . . . . . . . 221
7.1 Viewing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2407.2 Taxonomy of planar geometric projections. . . . . . . . . . . . . 242
8.1 Lighting and materials control flow . . . . . . . . . . . . . . . . . 2588.2 Computing vertex surface normals for triangles. . . . . . . . . . . 259
xviii
FIGURES xix
8.3 Light source geometry. . . . . . . . . . . . . . . . . . . . . . . . . 2648.4 Lambert’s law of diffuse reflection. . . . . . . . . . . . . . . . . . 2688.5 The geometry of specular reflection. . . . . . . . . . . . . . . . . 269
9.1 Vertex shader architecture . . . . . . . . . . . . . . . . . . . . . . 3029.2 Shader assembly syntax . . . . . . . . . . . . . . . . . . . . . . . 311
10.1 Scanline rendering of a triangle . . . . . . . . . . . . . . . . . . . 38210.2 Rasterization of a triangle . . . . . . . . . . . . . . . . . . . . . . 384
11.1 Multitexture cascade. . . . . . . . . . . . . . . . . . . . . . . . . 40511.2 Organization of mipmapped textures. . . . . . . . . . . . . . . . 40811.3 Cube texture organization. . . . . . . . . . . . . . . . . . . . . . 41511.4 Volume organization. . . . . . . . . . . . . . . . . . . . . . . . . . 41711.5 Organization of mipmapped volume textures. . . . . . . . . . . . 42111.6 Texel organization within a block for compressed textures. . . . . 42311.7 Sample block of compressed texels. . . . . . . . . . . . . . . . . . 42411.8 Texture address space. . . . . . . . . . . . . . . . . . . . . . . . . 42811.9 Illustration of the reflection vector. . . . . . . . . . . . . . . . . . 43011.10Texture coordinate addressing modes . . . . . . . . . . . . . . . . 43211.11Texture coordinate wrapping . . . . . . . . . . . . . . . . . . . . 43511.12Texture minification filtering . . . . . . . . . . . . . . . . . . . . 43711.13Texture stage processing . . . . . . . . . . . . . . . . . . . . . . . 439
13.1 Pixel shader architecture overview. . . . . . . . . . . . . . . . . . 47213.2 Pixel shader instruction execution. . . . . . . . . . . . . . . . . . 473
18.1 Structure of an effect file . . . . . . . . . . . . . . . . . . . . . . . 64618.2 Effect file syntax summary . . . . . . . . . . . . . . . . . . . . . . 646
19.1 D3DX Mesh Interface Hierarchy. . . . . . . . . . . . . . . . . . . 67219.2 Triangle Adjacency . . . . . . . . . . . . . . . . . . . . . . . . . . 67419.3 Bowtie arrangement of vertices in a triangle mesh . . . . . . . . . 68919.4 Mesh simplification by collapsing edges. . . . . . . . . . . . . . . 691
21.1 X file grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72421.2 Template grammar . . . . . . . . . . . . . . . . . . . . . . . . . . 72521.3 Data node grammar . . . . . . . . . . . . . . . . . . . . . . . . . 72721.4 Order of indices for Bezier patches. . . . . . . . . . . . . . . . . . 74621.5 .x File Interface Hierarchy . . . . . . . . . . . . . . . . . . . . . . 753
22.1 DirectX Diagnostic Tool . . . . . . . . . . . . . . . . . . . . . . . 81022.2 DirectX Control Panel Applet . . . . . . . . . . . . . . . . . . . . 811
23.1 The performance optimization process. . . . . . . . . . . . . . . . 817
A.1 SDK Installation Wizard . . . . . . . . . . . . . . . . . . . . . . . 843
xx FIGURES
A.2 DirectX 9.0c SDK Directory Structure. . . . . . . . . . . . . . . . 844A.3 HTML Help Viewer displaying the DirectX SDK documentation. 845
B.1 D3DFrame Class Hierarchy . . . . . . . . . . . . . . . . . . . . . 849
D.1 Samples Directory Structure. . . . . . . . . . . . . . . . . . . . . 874
Tables
1.1 Vector operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.2 Matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . . 171.3 Macros for HRESULT return values. . . . . . . . . . . . . . . . . . 24
3.1 Device Methods by Pipeline Section . . . . . . . . . . . . . . . . 763.2 Device Capabilities by Pipeline Section . . . . . . . . . . . . . . . 813.3 Implicit Query Beginnings . . . . . . . . . . . . . . . . . . . . . . 893.4 Query Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 893.5 Device States by Pipeline Section . . . . . . . . . . . . . . . . . . 943.6 Device State Data Types . . . . . . . . . . . . . . . . . . . . . . 1003.7 Methods captured by BeginStateBlock . . . . . . . . . . . . . . 1063.8 Device Properties by State Block Type . . . . . . . . . . . . . . . 1093.9 Device property support on a pure HAL device. . . . . . . . . . . 110
4.1 Presentation intervals supported in windowed and exclusive mode.1244.2 Combinations of source and destination surfaces supported with
UpdateSurface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
5.1 Flexible Vertex Format . . . . . . . . . . . . . . . . . . . . . . . . 1705.2 Vertex usage for the fixed-function pipeline. . . . . . . . . . . . . 1785.3 Relationship of primitive count to vertex count . . . . . . . . . . 1875.4 Vertex count for triangular Bezier patches. . . . . . . . . . . . . . 1965.5 Rectangular patch vertex grid sizes. . . . . . . . . . . . . . . . . 197
8.1 Material parameter defaults. . . . . . . . . . . . . . . . . . . . . . 2608.2 The D3DLIGHT9 members used by each light type. . . . . . . . . . 2638.3 Light attenuation coefficient examples. . . . . . . . . . . . . . . . 2658.4 Symbols used for D3DLIGHT9 and D3DMATERIAL9. . . . . . . . . . 266
9.1 Vertex shader version instruction counts. . . . . . . . . . . . . . . 3039.2 Vertex shader version register counts. . . . . . . . . . . . . . . . 3049.3 Vertex shader register modifiers . . . . . . . . . . . . . . . . . . . 3069.4 Vertex Shader 1.1 Instruction Summary . . . . . . . . . . . . . . 3149.5 Vertex Shader 2.0 Instruction Summary . . . . . . . . . . . . . . 322
xxi
xxii TABLES
9.6 sincos results based on write mask. ? indicates a componentthat has an undefined value after the instruction executes. ∗indicates a component that is not changed by the instruction. . . 323
9.7 Vertex shader version nesting limits. . . . . . . . . . . . . . . . . 3269.8 Vertex Shader 2.x Instruction Summary . . . . . . . . . . . . . . 3269.9 Vertex shader instructions supporting predication . . . . . . . . . 3279.10 Vertex Shader 3.0 Instruction Summary . . . . . . . . . . . . . . 3309.11 Register layout for fixed-function emulation. . . . . . . . . . . . . 3469.13 BumpSelfShadow vertex shader constants. . . . . . . . . . . . . . 3559.14 Input registers used by the DolphinVS sample . . . . . . . . . . . 356
11.1 Summary of texture operations. . . . . . . . . . . . . . . . . . . . 442
13.1 Pixel shader architecture versions. . . . . . . . . . . . . . . . . . 47313.2 Pixel shader instruction availability. . . . . . . . . . . . . . . . . 47413.3 Pixel shader instruction set summary. . . . . . . . . . . . . . . . 47513.4 Pixel shader 1.0 register restrictions. . . . . . . . . . . . . . . . . 47613.5 Pixel shader 1.1 register restrictions. . . . . . . . . . . . . . . . . 47713.6 Pixel shader 1.2 register restrictions. . . . . . . . . . . . . . . . . 47813.7 Pixel shader 1.3 register restrictions. . . . . . . . . . . . . . . . . 47913.8 Pixel shader 1.4 register restrictions in phase 1. . . . . . . . . . . 48013.9 Pixel shader 1.4 register restrictions in phase 2. . . . . . . . . . . 48113.10Pixel shader 2.0 instructions in assembly syntax. . . . . . . . . . 48713.11Pixel shader 2.x instructions in assembly syntax. . . . . . . . . . 48813.12Pixel shader 3.0 instructions in assembly syntax. . . . . . . . . . 48813.13Texture stage states honored by pixel shaders. . . . . . . . . . . . 490
14.1 Summary of stencil operations . . . . . . . . . . . . . . . . . . . 49914.2 Blend factors described by D3DBLEND . . . . . . . . . . . . . . . . 50314.3 Blending functions described by D3DBLENDOP . . . . . . . . . . . 50314.4 Blend factors for common compositing operators. . . . . . . . . . 50414.5 Command-line switches supported by rt FrameBuffer. . . . . . 510
16.1 D3DXCOLOR class members. . . . . . . . . . . . . . . . . . . . . . . 60316.2 D3DXCOLOR global functions. . . . . . . . . . . . . . . . . . . . . . 60416.3 D3DX vector class members . . . . . . . . . . . . . . . . . . . . . 60516.4 D3DX vector class function summary. . . . . . . . . . . . . . . . 60816.5 Vector function arithmetic operation summary . . . . . . . . . . 60916.6 D3DXMATRIX class members . . . . . . . . . . . . . . . . . . . . . 61516.7 D3DXQUATERNION class members. . . . . . . . . . . . . . . . . . . 627
18.1 Effect value syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 648
19.1 Vertex data for a flat shaded cube. . . . . . . . . . . . . . . . . . 68119.2 Face data for a flat shaded cube. . . . . . . . . . . . . . . . . . . 681
21.1 X File Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
TABLES xxiii
21.2 Meaning of terminal X file grammar tokens . . . . . . . . . . . . 72421.3 X file tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72921.4 X file record tokens . . . . . . . . . . . . . . . . . . . . . . . . . . 73021.5 Animation key types . . . . . . . . . . . . . . . . . . . . . . . . . 733
22.1 Common CSIDL values. . . . . . . . . . . . . . . . . . . . . . . . . 79522.2 Format specifiers for debugger structure expansion formatting. . 80222.3 Registry keys to disable CPU specific software processing. . . . . 80922.4 Community resources . . . . . . . . . . . . . . . . . . . . . . . . 81322.5 IHV Contact Information . . . . . . . . . . . . . . . . . . . . . . 815
24.1 GetDXVersion return values. . . . . . . . . . . . . . . . . . . . . 82624.2 DirectXSetupGetVersion return values. . . . . . . . . . . . . . . 828
A.1 Direct3D related include files. . . . . . . . . . . . . . . . . . . . . 846A.2 Summary of SDK libraries for Direct3D applications. . . . . . . . 847A.3 Summary of SDK tools. . . . . . . . . . . . . . . . . . . . . . . . 847
xxiv TABLES
COM Interfaces
1.1 IUnknown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1 IDirect3D9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.1 IDirect3DDevice9 . . . . . . . . . . . . . . . . . . . . . . . . . . 713.2 IDirect3DResource9 . . . . . . . . . . . . . . . . . . . . . . . . 833.3 IDirect3DQuery9 . . . . . . . . . . . . . . . . . . . . . . . . . . . 873.4 IDirect3DStateBlock9 . . . . . . . . . . . . . . . . . . . . . . . 105
4.1 IDirect3DSurface9 . . . . . . . . . . . . . . . . . . . . . . . . . 1124.2 IDirect3DSwapChain9 . . . . . . . . . . . . . . . . . . . . . . . . 120
5.1 IDirect3DVertexDeclaration9 . . . . . . . . . . . . . . . . . . 1765.2 IDirect3DVertexBuffer9 . . . . . . . . . . . . . . . . . . . . . . 1795.3 IDirect3DIndexBuffer9 . . . . . . . . . . . . . . . . . . . . . . 1845.4 IDirect3DVertexShader9 . . . . . . . . . . . . . . . . . . . . . . 186
11.1 IDirect3DBaseTexture9 . . . . . . . . . . . . . . . . . . . . . . 41011.2 IDirect3DTexture9 . . . . . . . . . . . . . . . . . . . . . . . . . 41311.3 IDirect3DCubeTexture9 . . . . . . . . . . . . . . . . . . . . . . 41511.4 IDirect3DVolume9 . . . . . . . . . . . . . . . . . . . . . . . . . 41711.5 IDirect3DVolumeTexture9 . . . . . . . . . . . . . . . . . . . . 420
15.1 ID3DXBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
17.1 ID3DXMatrixStack . . . . . . . . . . . . . . . . . . . . . . . . . 63217.2 ID3DXFont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63417.3 ID3DXSprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63917.4 ID3DXRenderToSurface . . . . . . . . . . . . . . . . . . . . . . 64017.5 ID3DXRenderToEnvMap . . . . . . . . . . . . . . . . . . . . . . . 64218.1 ID3DXEffect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
19.1 ID3DXBaseMesh . . . . . . . . . . . . . . . . . . . . . . . . . . . 67619.2 ID3DXMesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68319.3 ID3DXPMesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70119.4 ID3DXSPMesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70519.5 ID3DXSkinInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
21.1 IDirectXFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
xxv
xxvi COM INTERFACES
21.2 IDirectXFileEnumObject . . . . . . . . . . . . . . . . . . . . . 75621.3 IDirectXFileObject . . . . . . . . . . . . . . . . . . . . . . . . 75721.4 IDirectXFileData . . . . . . . . . . . . . . . . . . . . . . . . . 75721.5 IDirectXFileBinary . . . . . . . . . . . . . . . . . . . . . . . . 76021.6 IDirectXFileDataReference . . . . . . . . . . . . . . . . . . . 76121.7 IDirectXFileSaveObject . . . . . . . . . . . . . . . . . . . . . 762
Preface
Who Should Read This Book
This book is for C++ programmers who want to understand how to create im-ages rendered from scenes using Microsoft’s Direct3D application programminginterface.
The reader is assumed to be familiar with basic Win32 programming: win-dows, messages, keyboard and mouse input, device contexts, GDI, and so-on. Ifthese areas are new to the reader, they are encouraged to start first with a basictext on Windows. The author recommends Win32 Programming by Brent E.Rector and Joseph M. Newcomer.
Direct3D is provided by the system as a set of interfaces provided by COMobjects. No existing knowledge of COM is assumed and the basics of COM asused with Direct3D are reviewed.
COM objects can be used from a variety of languages. This book will useC++ for its programming examples and the reader should be comfortable withC++ concepts such as classes, inheritance, abstract interfaces, and generic pro-gramming with template classes such as those in the ANSI C++ standard library.The author believes in the KISS (Keep It Simple, Stupid) maxim – C++ featuresare used only when they provide a clear benefit.
Three dimensional scenes are described to the computer with the assistanceof some mathematical concepts. Readers should be comfortable with the math-ematics of geometry, trigonometry, vectors and matrices. A summary review ofthe mathematics common in computer graphics is given for reference.
What’s Not In This Book
This book provides a complete explanation of the Direct3D graphics pipelineprovided in release 9.0c of Microsoft’s DirectX technology. A sophisticated 3Dmultimedia application may use many other technologies such as DirectSoundand DirectMusic for sound and music, DirectInput for input device processing,DirectPlay or TCP/IP for networking, physically-based models and dynam-ics simulations for realistic animation, persistent storage for scenes, interactivemodelling of scenes, and Win32 for GUI presentation.
xxvii
xxviii PREFACE
In this book we will touch on input processing with the core Win32 mes-sages, but other areas of DirectX and application processing will not be covered.References are provided for other books that cover these areas when they arisein our discussion of the pipeline.
Thanks
List of people to thank.
This book is dedicated to:
Sylvia Mead Atwater1901-1990
who shared her joy of knowledge,
Anne and Bill Thomsonfor making all things possible,
and
Pete Townshendfor over 20 years of musical ecstasy.
xxix
xxx PREFACE