Sei sulla pagina 1di 30

The Direct3D Graphics Pipeline

Richard Atwater Thomson


August 13, 2006

ii

Contents
Preface

xxvii

Preliminaries

1 Introduction
1.1 Overview . . . . . . . . . . . . .
1.2 Display Technology . . . . . . .
1.3 Gamma Correction . . . . . . .
1.4 Color . . . . . . . . . . . . . . .
1.5 Basic Mathematics . . . . . . .
1.6 Homogeneous Coordinates . . .
1.7 Vectors . . . . . . . . . . . . . .
1.8 Coordinate Systems . . . . . . .
1.9 Matrices . . . . . . . . . . . . .
1.10 Aliasing . . . . . . . . . . . . . .
1.11 Style Conventions . . . . . . . .
1.12 COM Objects . . . . . . . . . .
1.13 Code Techniques . . . . . . . . .
1.14 Previous Versions of Direct3D .
1.14.1 DirectX 1-2 . . . . . . .
1.14.2 DirectX 3 . . . . . . . .
1.14.3 DirectX 5 . . . . . . . .
1.14.4 DirectX 6 . . . . . . . .
1.14.5 DirectX 7 . . . . . . . .
1.14.6 DirectX 8.0 . . . . . . .
1.14.7 DirectX 8.1 . . . . . . .
1.14.8 DirectX 9.0c (June 2006)
1.15 Further Reading . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

3
3
6
8
10
12
14
14
15
17
18
20
22
27
37
37
38
38
38
38
39
39
39
40

2 Direct3D
2.1 Overview . . . . . . . . . . . . . .
2.2 A Minimal Direct3D Application
2.3 Direct3D Architecture . . . . . . .
2.4 Direct3D HRESULTs . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

43
43
44
50
51

iii

iv

CONTENTS
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.14
2.15

Windowed and Exclusive Mode . . . . .


Device Types . . . . . . . . . . . . . . .
Resources . . . . . . . . . . . . . . . . . .
IDirect3D9 . . . . . . . . . . . . . . . .
Selecting a Device . . . . . . . . . . . . .
Determining Available Resource Memory
Device Capabilities . . . . . . . . . . . .
Identifying a Particular Device . . . . . .
Creating the Device . . . . . . . . . . . .
Multiple Monitors . . . . . . . . . . . . .
Adapter Group Devices . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

52
52
53
57
59
61
61
61
63
66
67

3 Direct3D Devices
3.1 Overview . . . . . . . . . . . . . . . . . . . .
3.2 IDirect3DDevice9 . . . . . . . . . . . . . .
3.3 Capabilities . . . . . . . . . . . . . . . . . .
3.4 Resources . . . . . . . . . . . . . . . . . . . .
3.5 IDirect3DResource9 . . . . . . . . . . . . .
3.6 Destroying a Device . . . . . . . . . . . . . .
3.7 Miscellaneous Properties . . . . . . . . . . .
3.8 Device Queries . . . . . . . . . . . . . . . . .
3.8.1 Resource Manager Statistics Queries
3.8.2 Vertex Statistics Queries . . . . . . .
3.8.3 Vertex Cache Queries . . . . . . . . .
3.8.4 PIX Related Queries . . . . . . . . .
3.9 Device States . . . . . . . . . . . . . . . . .
3.9.1 Miscellaneous Render States . . . . .
3.9.2 Setting Groups of State . . . . . . . .
3.10 Device State Blocks . . . . . . . . . . . . . .
3.11 Pure Devices . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

69
69
70
76
82
82
86
86
87
90
91
91
92
93
101
101
104
109

4 2D Applications
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 Pixel Surfaces . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Accessing Surface Pixel Data . . . . . . . . . . . . . . . .
4.4 Using GDI On A Surface . . . . . . . . . . . . . . . . . .
4.5 Swap Chains . . . . . . . . . . . . . . . . . . . . . . . . .
4.6 Presentation . . . . . . . . . . . . . . . . . . . . . . . . .
4.7 Lost Devices and Reset . . . . . . . . . . . . . . . . . . .
4.8 Video Scan Out . . . . . . . . . . . . . . . . . . . . . . .
4.8.1 Cursor . . . . . . . . . . . . . . . . . . . . . . . .
4.8.2 Gamma Ramp . . . . . . . . . . . . . . . . . . . .
4.9 2D Pixel Copies . . . . . . . . . . . . . . . . . . . . . . .
4.9.1 Pixel Copies Within Device Memory . . . . . . .
4.9.2 Copies From System Memory To Device Memory
4.9.3 Copies From Device Memory To System Memory

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

111
111
112
114
119
119
122
125
125
126
127
128
129
131
133

CONTENTS

4.10 Filling Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . 133


4.11 Window Messages . . . . . . . . . . . . . . . . . . . . . . . . . . 133
4.12 rt 2DApp Sample Application . . . . . . . . . . . . . . . . . . . 135

II

Geometry Processing

5 Modeling
5.1 Overview . . . . . . . . . . . . . . .
5.2 Modeling Scenes . . . . . . . . . . .
5.3 Visibility . . . . . . . . . . . . . . .
5.4 Render Targets . . . . . . . . . . .
5.5 Primitive Types . . . . . . . . . . .
5.6 Vertex Data . . . . . . . . . . . . .
5.7 Flexible Vertex Formats . . . . . .
5.8 Vertex Declarations . . . . . . . . .
5.8.1 Fixed-Function Declarations
5.9 Vertex Buffers . . . . . . . . . . . .
5.10 Indexed Primitives . . . . . . . . .
5.10.1 Index Buffers . . . . . . . .
5.11 The Vertex Shader . . . . . . . . .
5.12 Drawing Primitives . . . . . . . . .
5.13 Vertex Data Streams . . . . . . . .
5.14 Capabilities for Vertex Assembly . .
5.15 Enhanced Primitives . . . . . . . .
5.15.1 Point Sprites . . . . . . . . .
5.15.2 N -Patches . . . . . . . . . .
5.16 Higher Order Surfaces . . . . . . . .
5.16.1 Triangular Patches . . . . .
5.16.2 Rectangular Patches . . . .
5.17 Object Approximations . . . . . . .
5.18 Further Reading . . . . . . . . . . .

157
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

159
159
160
161
163
164
167
167
172
177
178
183
184
185
186
188
190
191
191
192
193
195
196
197
198

6 Vertex Transformations
6.1 Overview . . . . . . . . . . . . . . . . . . . .
6.2 Vertex Processing . . . . . . . . . . . . . . .
6.3 Transformation Matrices . . . . . . . . . . .
6.4 Order of Transformations . . . . . . . . . . .
6.5 World Transformation . . . . . . . . . . . . .
6.6 Transformation Hierarchy . . . . . . . . . . .
6.7 Vertex Blending . . . . . . . . . . . . . . . .
6.7.1 Basic Vertex Blending . . . . . . . .
6.7.2 Indexed Vertex Blending . . . . . . .
6.7.3 Vertex Tweening . . . . . . . . . . .
6.7.4 Blending Within Device Limitations .
6.8 Vertex Fog . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

199
199
200
201
203
205
206
207
212
213
214
215
216

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

vi

CONTENTS
6.9 Face Culling . . . . . . . . . . . . . .
6.10 Clipping . . . . . . . . . . . . . . . .
6.10.1 User Clip Planes . . . . . . . .
6.10.2 View Frustum Clipping . . . .
6.10.3 Guard Band Clipping . . . . .
6.11 Screen Space and the Viewport . . .
6.12 rt VertexBlend Sample Application
6.13 Further Reading . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

218
220
221
222
223
224
225
237

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

239
239
240
242
242
244
244
245
246
246

8 Lighting and Materials


8.1 Overview . . . . . . . . . . . . . . . . .
8.2 Transformed Vertices . . . . . . . . . .
8.3 Lighting Calculations . . . . . . . . . .
8.4 Surface Material Properties . . . . . . .
8.5 Light Sources . . . . . . . . . . . . . .
8.5.1 Directional Lights . . . . . . . .
8.5.2 Point Lights . . . . . . . . . . .
8.5.3 Spot Lights . . . . . . . . . . .
8.5.4 Light Attenuation . . . . . . . .
8.6 The Illumination Model . . . . . . . . .
8.6.1 Transforming to Camera Space
8.6.2 Ambient Reflection . . . . . . .
8.6.3 Diffuse Reflection . . . . . . . .
8.6.4 Specular Reflection . . . . . . .
8.6.5 Emission . . . . . . . . . . . . .
8.6.6 Summary . . . . . . . . . . . . .
8.7 rt Lighting Sample Application . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

255
255
256
257
260
262
264
264
264
265
266
267
267
267
268
269
269
270

. . . . .
. . . . .
. . . . .
Register
. . . . .
. . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

301
301
302
303
304
305
305

7 Viewing and Projection


7.1 Overview . . . . . . . . . . . . . . .
7.2 Viewing Through a Virtual Camera
7.3 Planar Geometric Projections . . .
7.3.1 Parallel Projections . . . . .
7.3.2 Perspective Projections . . .
7.4 Projection Transformation . . . . .
7.4.1 Projection Matrices . . . . .
7.5 Further Reading . . . . . . . . . . .
7.6 rt Views Sample Application . . .

9 Vertex Shaders
9.1 Overview . . . . . . . . . . . .
9.2 Vertex Shader Architecture . .
9.2.1 Input Registers . . . .
9.2.2 Constant Registers and
9.2.3 Output Registers . . .
9.2.4 Temporary Registers .

. .
. .
. .
the
. .
. .

.
.
.
.
.
.
.
.
.

. . . . .
. . . . .
. . . . .
Address
. . . . .
. . . . .

CONTENTS

9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10

9.11
9.12
9.13
9.14
9.15
9.16
9.17

9.18

9.2.5 The Loop Counter Register . . . . . . . . .


9.2.6 The Predicate Register . . . . . . . . . . . .
9.2.7 Sampler Registers . . . . . . . . . . . . . . .
9.2.8 Register Modifiers . . . . . . . . . . . . . . .
Vertex Shader 1.1 Architecture . . . . . . . . . . . .
Vertex Shader 2.0 Architecture . . . . . . . . . . . .
Vertex Shader 2.x Architecture . . . . . . . . . . . .
Vertex Shader 3.0 Architecture . . . . . . . . . . . .
Shader Instruction Syntax . . . . . . . . . . . . . .
Execution Model . . . . . . . . . . . . . . . . . . . .
Software Vertex Processing . . . . . . . . . . . . . .
Vertex Shader 1.1 Instructions . . . . . . . . . . . .
9.10.1 Declaration Instructions . . . . . . . . . . .
9.10.2 Basic Arithmetic Instructions . . . . . . . .
9.10.3 Matrix Instructions . . . . . . . . . . . . . .
9.10.4 Comparison Instructions . . . . . . . . . . .
9.10.5 Lighting Instructions . . . . . . . . . . . . .
Vertex Shader 2.0 Instructions . . . . . . . . . . . .
9.11.1 Version 2.0 Flow Control Nesting Limits . .
Vertex Shader 2.x Instructions . . . . . . . . . . . .
9.12.1 Version 2.x Flow Control Nesting Limits . .
Vertex Shader 3.0 Instructions . . . . . . . . . . . .
9.13.1 Version 3.0 Flow Control Nesting Limits . .
Manipulating Shaders . . . . . . . . . . . . . . . . .
Drawing Multiple Instances . . . . . . . . . . . . . .
Shader Design . . . . . . . . . . . . . . . . . . . . .
Common Computations . . . . . . . . . . . . . . . .
9.17.1 Constant Generation . . . . . . . . . . . . .
9.17.2 Fractional Part . . . . . . . . . . . . . . . .
9.17.3 Absolute Value . . . . . . . . . . . . . . . .
9.17.4 Division . . . . . . . . . . . . . . . . . . . .
9.17.5 Square Root . . . . . . . . . . . . . . . . . .
9.17.6 Conditional Selection . . . . . . . . . . . . .
9.17.7 Clamping To An Interval . . . . . . . . . . .
9.17.8 Floor and Ceiling . . . . . . . . . . . . . . .
9.17.9 Vector Cross Product . . . . . . . . . . . . .
9.17.10 Vector Normalization . . . . . . . . . . . . .
9.17.11 Transposed Matrix Multiplication . . . . . .
9.17.12 Signum Function . . . . . . . . . . . . . . .
9.17.13 Minimum and Maximum Vector Component
9.17.14 Trigonometric Functions . . . . . . . . . . .
9.17.15 Exponential and Logarithmic Functions . . .
Fixed-Function Processing . . . . . . . . . . . . . .
9.18.1 Register Layout . . . . . . . . . . . . . . . .
9.18.2 Constant Register Layout . . . . . . . . . .
9.18.3 Coordinate Transformation . . . . . . . . . .

vii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

305
306
306
306
307
308
309
310
310
312
312
313
315
316
318
319
320
321
325
326
329
330
332
332
334
335
336
337
337
337
338
338
338
339
339
340
340
340
341
341
342
344
346
346
346
348

viii

CONTENTS
9.18.4 Vertex Blending . . . . . . . . .
9.18.5 Position Output . . . . . . . . .
9.18.6 Normalize Eye Normal . . . . .
9.18.7 Non-Homogeneous Eye Position
9.18.8 Eye Space Vectors . . . . . . . .
9.18.9 Fog Output . . . . . . . . . . .
9.18.10 Point Parameters . . . . . . . .
9.18.11 Lighting . . . . . . . . . . . . .
9.18.12 Texture Coordinate Generation
9.19 Beyond Fixed-Function Processing . . .
9.20 rt VertexShader Sample Application .
9.21 Vertex Shader SDK Samples . . . . . .
9.21.1 BumpSelfShadow . . . . . . . .
9.21.2 BumpWaves . . . . . . . . . . .
9.21.3 DolphinVS . . . . . . . . . . . .
9.21.4 SkinnedMesh . . . . . . . . . . .
9.21.5 SphereMap . . . . . . . . . . . .
9.21.6 VertexBlend . . . . . . . . . . .
9.21.7 VertexShader . . . . . . . . . .

III

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

Pixel Processing

10 Rasterization and Shading


10.1 Overview . . . . . . . . . . . . . .
10.2 Shading and Filling . . . . . . . .
10.3 Scanline Rendering . . . . . . . .
10.4 Source Pixel Generation . . . . .
10.5 Source Pixel Data . . . . . . . . .
10.6 rt Rasterize Sample Application

348
349
349
349
349
350
350
350
352
353
354
354
354
355
355
356
357
357
357

377
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

379
379
380
382
383
385
386

11 Basic Texturing
11.1 Overview . . . . . . . . . . . . . . . . . . .
11.2 Direct3D Multitexture Architecture . . . .
11.3 Texture Resources . . . . . . . . . . . . . .
11.3.1 IDirect3DBaseTexture9 . . . . . .
11.3.2 IDirect3DTexture9 . . . . . . . .
11.3.3 IDirect3DCubeTexture9 . . . . . .
11.3.4 IDirect3DVolume9 . . . . . . . . .
11.3.5 IDirect3DVolumeTexture9 . . . .
11.4 Texture Formats . . . . . . . . . . . . . . .
11.4.1 Palette Based Textures . . . . . . .
11.4.2 Compressed Texture Formats . . .
11.5 Texture Coordinate Processing . . . . . . .
11.5.1 Texture Coordinate Generation . .
11.5.2 Texture Coordinate Transformation

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

403
403
404
407
409
411
414
416
420
422
422
423
427
430
430

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

CONTENTS

ix
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

432
434
435
439
445
446
447
448
449
451
451
452
452
453

12 Advanced Texturing
12.1 Overview . . . . . . . . . . . . . . . . . . . . .
12.2 Multiple Texture Stages . . . . . . . . . . . . .
12.3 Environment Mapping . . . . . . . . . . . . .
12.3.1 Cubic Environment Mapping . . . . . .
12.3.2 Spherical Environment Mapping . . . .
12.4 Bump Mapping Techniques . . . . . . . . . . .
12.4.1 Dot3 Bump Mapping . . . . . . . . . .
12.4.2 Emboss Mapping . . . . . . . . . . . .
12.4.3 Environment Mapped Bump Mapping
12.4.4 Tangent Basis Bump Mapping . . . . .
12.5 Texturing Samples . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

469
469
469
470
470
470
470
470
470
470
470
470

13 Pixel Shaders
13.1 Overview . . . . . . . . . . . . . . . .
13.2 Pixel Shader Architecture . . . . . .
13.3 Pixel Shader Architecture Versions .
13.3.1 Pixel Shader 1.0 . . . . . . . .
13.3.2 Pixel Shader 1.1 . . . . . . . .
13.3.3 Pixel Shader 1.2 . . . . . . . .
13.3.4 Pixel Shader 1.3 . . . . . . . .
13.3.5 Pixel Shader 1.4 . . . . . . . .
13.3.6 Pixel Shader 2.0 . . . . . . . .
13.3.7 Pixel Shader 2.x . . . . . . . .
13.3.8 Pixel Shader 3.0 . . . . . . . .
13.4 Instruction Set . . . . . . . . . . . . .
13.4.1 Declaration Instructions . . .
13.4.2 Basic Arithmetic Instructions
13.4.3 Comparison Instructions . . .
13.4.4 Basic Texture Addressing . . .
13.4.5 Bump Mapping Instructions .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

471
471
472
472
472
482
482
482
482
483
483
483
483
483
484
484
485
487

11.6
11.7
11.8

11.9
11.10
11.11
11.12

11.5.3 Texture Addressing . . . . . . .


11.5.4 Texture Wrapping . . . . . . . .
Texture Sampling . . . . . . . . . . . .
Texture Stage Processing . . . . . . . .
Examples . . . . . . . . . . . . . . . . .
11.8.1 Single Stage Light Maps . . . .
11.8.2 Decals . . . . . . . . . . . . . .
Specular Addition . . . . . . . . . . . .
Textured Primitives . . . . . . . . . . .
Further Reading . . . . . . . . . . . . .
Sample Applications . . . . . . . . . . .
11.12.1 Book Samples . . . . . . . . . .
11.12.2 SDK Samples . . . . . . . . . .
11.12.3 rt Texture Sample Application

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

CONTENTS
13.5
13.6
13.7
13.8
13.9
13.10
13.11

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

487
488
488
489
490
490
490

14 The Frame Buffer


14.1 Overview . . . . . . . . . . . . . . . .
14.2 Fog Blending . . . . . . . . . . . . . .
14.3 Alpha Test . . . . . . . . . . . . . . .
14.4 The Z Buffer and Visibility . . . . . .
14.5 Stencil Test . . . . . . . . . . . . . .
14.6 Alpha Blending . . . . . . . . . . . .
14.7 Dithering . . . . . . . . . . . . . . . .
14.8 Multisampling . . . . . . . . . . . . .
14.9 Writing to the Render Target . . . .
14.10 rt FrameBuffer Sample Application

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

491
491
492
492
494
497
501
505
506
508
508

IV

PS 2.0 Instructions . . . . .
PS 2.x Instructions . . . . .
PS 3.0 Instructions . . . . .
Manipulating Pixel Shaders
Texture Stage States . . . .
Examples . . . . . . . . . . .
Debugging a Pixel Shader .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

D3DX Library

15 D3DX Utility Library


15.1 Overview . . . . . . . . . . . . . . . . . . . . . .
15.2 Headers and Libraries . . . . . . . . . . . . . . .
15.3 Data Types . . . . . . . . . . . . . . . . . . . .
15.4 Helper Objects . . . . . . . . . . . . . . . . . . .
15.5 Meshes . . . . . . . . . . . . . . . . . . . . . . .
15.6 Resources . . . . . . . . . . . . . . . . . . . . . .
15.7 Resource Image Information . . . . . . . . . . .
15.8 Resource Requirements . . . . . . . . . . . . . .
15.9 Format Conversion . . . . . . . . . . . . . . . .
15.10 Creating Texture Resources . . . . . . . . . . .
15.10.1 Creating Uninitialized Textures . . . . .
15.10.2 Creating Textures From Files . . . . . .
15.10.3 Creating Textures From Files In Memory
15.10.4 Creating Textures From Resources . . .
15.11 Loading Resources . . . . . . . . . . . . . . . . .
15.12 Saving Resources . . . . . . . . . . . . . . . . .
15.13 Filling Textures . . . . . . . . . . . . . . . . . .
15.14 Filtering Textures . . . . . . . . . . . . . . . . .
15.15 Creating Normal Maps . . . . . . . . . . . . . .
15.16 Miscellaneous . . . . . . . . . . . . . . . . . . .
15.16.1 Macros . . . . . . . . . . . . . . . . . . .
15.16.2 Fresnels Formulas . . . . . . . . . . . . .

573
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

575
575
576
576
576
577
577
578
579
580
582
582
583
585
586
588
591
591
592
593
594
594
594

CONTENTS
15.17
15.18
15.19
15.20
15.21
15.22

Error Handling . .
ID3DXBuffer . . . .
Vertex Declarations
Intersection Testing
Shader Assembly .
Further Reading . .

xi
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

595
596
597
597
598
599

16 D3DX Concrete Types


16.1 Overview . . . . . . . . . . . . . . . . . . . . . . .
16.2 Colors . . . . . . . . . . . . . . . . . . . . . . . .
16.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . .
16.4 Matrices . . . . . . . . . . . . . . . . . . . . . . .
16.4.1 Arithmetic Functions . . . . . . . . . . . .
16.4.2 Identity, Translation, and Scaling Matrices
16.4.3 Rotation Matrices . . . . . . . . . . . . . .
16.4.4 Composite Transformation Matrices . . . .
16.4.5 Viewing Matrices . . . . . . . . . . . . . .
16.4.6 Orthographic Projection Matrices . . . . .
16.4.7 Perspective Projection Matrices . . . . . .
16.4.8 Shadow and Reflection Matrices . . . . . .
16.4.9 Matrix Helper Classes . . . . . . . . . . .
16.5 Planes . . . . . . . . . . . . . . . . . . . . . . . .
16.6 Quaternions . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

601
601
601
604
614
617
618
618
619
619
620
621
622
622
623
625

17 D3DX Helper Objects


17.1 Overview . . . . . . . . . . .
17.2 Matrix Stacks . . . . . . . .
17.3 Fonts . . . . . . . . . . . . .
17.4 Sprites . . . . . . . . . . . .
17.5 Surface Rendering . . . . . .
17.6 Environment Map Rendering

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

631
631
631
634
638
640
642

18 High Level Shaders and Effects


18.1 Overview . . . . . . . . . . . .
18.2 Visual Effects . . . . . . . . .
18.2.1 Text Syntax . . . . . .
18.2.2 Effect Objects . . . . .
18.3 Sample Applications . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

645
645
645
645
652
657

19 D3DX Mesh Objects


19.1 Overview . . . . . . . . . . . . .
19.2 Use of const Qualifiers . . . . .
19.3 Triangle Adjacencies . . . . . .
19.4 Vertex and Triangle Remapping
19.5 Base Mesh . . . . . . . . . . . .
19.6 Mesh . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

671
671
672
674
675
676
683

xii

CONTENTS

19.7
19.8
19.9
19.10
19.11
19.12

19.6.1 Creating Meshes From Scratch


19.6.2 Primitive Construction . . . .
19.6.3 Reducing Mesh Data . . . . .
19.6.4 Adding Mesh Data . . . . . .
19.6.5 Mesh Intersection Tests . . . .
19.6.6 Mesh Conversion . . . . . . .
19.6.7 Meshes in X Files . . . . . . .
Progressive Mesh . . . . . . . . . . .
Simplification Mesh . . . . . . . . . .
Skin Mesh . . . . . . . . . . . . . . .
19.9.1 Skin Meshes in X Files . . . .
rt D3DXSphere Sample Application .
SDK Samples . . . . . . . . . . . . .
Further Reading . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

685
687
689
693
694
695
698
700
704
707
713
714
714
715

20 D3DX Animation Objects


717
20.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717

The Application

21 X Files
21.1 Overview . . . . . . . . . .
21.2 X File Structure . . . . . .
21.2.1 X File Header . . .
21.2.2 Base Data Types .
21.2.3 Templates . . . . .
21.2.4 Data Nodes . . . .
21.2.5 Identifiers . . . . .
21.3 Text Encoding . . . . . . .
21.3.1 Reserved Words . .
21.3.2 Templates . . . . .
21.3.3 Data Nodes . . . .
21.3.4 Nested Data . . . .
21.4 Binary Encoding . . . . . .
21.4.1 Token Records . . .
21.5 Predefined Templates . . .
21.5.1 Animation . . . . .
21.5.2 Animation Key . .
21.5.3 Animation Options
21.5.4 Animation Set . . .
21.5.5 Boolean . . . . . .
21.5.6 Boolean 2D . . . .
21.5.7 Color RGB . . . . .
21.5.8 Color RGBA . . . .
21.5.9 Coords 2D . . . . .

719
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

721
721
722
722
723
723
723
724
724
725
725
726
728
729
730
731
732
732
733
734
734
735
735
735
736

CONTENTS

21.6
21.7
21.8
21.9
21.10
21.11
21.12
21.13
21.14
21.15
21.16

21.5.10 External Visual . . . . . . . . . . . .


21.5.11 Float Keys . . . . . . . . . . . . . . .
21.5.12 Frame . . . . . . . . . . . . . . . . .
21.5.13 Frame Transform Matrix . . . . . . .
21.5.14 FVF Data . . . . . . . . . . . . . . .
21.5.15 Guid . . . . . . . . . . . . . . . . . .
21.5.16 Header . . . . . . . . . . . . . . . . .
21.5.17 Indexed Color . . . . . . . . . . . . .
21.5.18 Inline Data . . . . . . . . . . . . . . .
21.5.19 Material . . . . . . . . . . . . . . . .
21.5.20 Material Wrap . . . . . . . . . . . . .
21.5.21 Matrix 4x4 . . . . . . . . . . . . . . .
21.5.22 Mesh . . . . . . . . . . . . . . . . . .
21.5.23 Mesh Face . . . . . . . . . . . . . . .
21.5.24 Mesh Face Wraps . . . . . . . . . . .
21.5.25 Mesh Material List . . . . . . . . . .
21.5.26 Mesh Normals . . . . . . . . . . . . .
21.5.27 Mesh Texture Coords . . . . . . . . .
21.5.28 Mesh Vertex Colors . . . . . . . . . .
21.5.29 Patch . . . . . . . . . . . . . . . . . .
21.5.30 Patch Mesh . . . . . . . . . . . . . .
21.5.31 Progressive Mesh Attribute Range . .
21.5.32 Progressive Mesh Info . . . . . . . . .
21.5.33 Progressive Mesh Vertex Split Record
21.5.34 Progressive Mesh . . . . . . . . . . .
21.5.35 Property Bag . . . . . . . . . . . . .
21.5.36 Right Handed . . . . . . . . . . . . .
21.5.37 Skin Weights . . . . . . . . . . . . . .
21.5.38 String Property . . . . . . . . . . . .
21.5.39 Texture Filename . . . . . . . . . . .
21.5.40 Timed Float Keys . . . . . . . . . . .
21.5.41 Url . . . . . . . . . . . . . . . . . . .
21.5.42 Vector . . . . . . . . . . . . . . . . .
21.5.43 Vertex Duplication Indices . . . . . .
21.5.44 X Skin Mesh Header . . . . . . . . .
Headers, Libraries and Interfaces . . . . . . .
IDirectXFile . . . . . . . . . . . . . . . . .
IDirectXFileEnumObject . . . . . . . . . .
IDirectXFileObject . . . . . . . . . . . . .
IDirectXFileData . . . . . . . . . . . . . .
IDirectXFileBinary . . . . . . . . . . . . .
IDirectXFileDataReference . . . . . . . .
IDirectXFileSaveObject . . . . . . . . . .
Reading Scene Data . . . . . . . . . . . . . .
Writing Scene Data . . . . . . . . . . . . . .
Exporting and Conversion . . . . . . . . . .

xiii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

736
736
737
737
737
738
739
739
740
740
741
741
741
742
743
743
744
744
745
745
747
747
747
748
748
749
749
749
750
750
751
751
751
752
752
753
753
756
756
757
760
761
762
763
764
764

xiv

CONTENTS
21.17 Example X Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
21.18 rt MakeScene Sample Program . . . . . . . . . . . . . . . . . . 767

22 Debugging
22.1 Overview . . . . . . . . . . . . . . . . . . . . . . .
22.2 Version Control Systems . . . . . . . . . . . . . .
22.3 C++ Debugging . . . . . . . . . . . . . . . . . . .
22.3.1 Dynamic Memory Allocation . . . . . . . .
22.3.2 Accidental Assignments . . . . . . . . . . .
22.3.3 String Handling . . . . . . . . . . . . . . .
22.3.4 Casts . . . . . . . . . . . . . . . . . . . . .
22.3.5 C++ Exceptions . . . . . . . . . . . . . . .
22.4 Debugging Windows Applications . . . . . . . . .
22.4.1 ANSI vs. Unicode . . . . . . . . . . . . . .
22.4.2 Security Environment . . . . . . . . . . . .
22.4.3 Special Directories . . . . . . . . . . . . .
22.4.4 Output Debug Stream . . . . . . . . . . .
22.4.5 Structured Exception Handling . . . . . .
22.4.6 Stack Traces . . . . . . . . . . . . . . . . .
22.4.7 Mini Dumps . . . . . . . . . . . . . . . . .
22.4.8 Debugging Buffer Overruns and Underruns
22.4.9 Fast User Switching in XP . . . . . . . . .
22.5 Debugging With Visual C++ 6 . . . . . . . . . . .
22.5.1 Decoding Direct3D HRESULTs . . . . . . . .
22.5.2 Highlighting Direct3D Identifiers . . . . .
22.5.3 Expanding Direct3D Structures . . . . . .
22.5.4 Limiting Step-Into . . . . . . . . . . . . .
22.5.5 Remote Debugging . . . . . . . . . . . . .
22.5.6 Multiple Monitor Debugging . . . . . . . .
22.6 Debugging With Direct3D . . . . . . . . . . . . .
22.6.1 Debug and Retail Runtimes . . . . . . . .
22.6.2 Checking HRESULTs . . . . . . . . . . . . .
22.6.3 Reference Rasterizer . . . . . . . . . . . .
22.6.4 DirectX Error Routines . . . . . . . . . . .
22.6.5 Reproducing Problems . . . . . . . . . . .
22.6.6 Exclusive Mode Problems . . . . . . . . .
22.6.7 Driver Problems . . . . . . . . . . . . . . .
22.6.8 Shader Problems . . . . . . . . . . . . . .
22.6.9 Texturing Problems . . . . . . . . . . . . .
22.6.10 Processor Specific Graphics Processing . .
22.6.11 Interfering Applications . . . . . . . . . . .
22.7 DirectX Diagnostic Tool . . . . . . . . . . . . . .
22.8 DirectX Control Panel Applet . . . . . . . . . . .
22.9 Getting Help . . . . . . . . . . . . . . . . . . . . .
22.9.1 Asking Smart Questions . . . . . . . . . .
22.9.2 IHV Developer Relations . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

789
789
790
790
790
791
792
792
792
793
794
794
794
796
797
799
799
800
800
801
801
801
801
802
803
803
804
804
805
805
805
806
806
806
807
808
808
809
809
810
812
813
814

CONTENTS

xv

22.10 Filing Bug Reports . . . . . . . . . . . . . . . . . . . . . . . . . 814


23 Pipeline Performance
23.1 Overview . . . . . . . . . . . . .
23.2 Pipeline Bottlenecks . . . . . . .
23.3 Optimization Techniques . . . .
23.3.1 Reordering Data . . . . .
23.3.2 Reducing Data . . . . . .
23.3.3 Redesigning . . . . . . .
23.4 Strategies . . . . . . . . . . . . .
23.4.1 Vertex Processing . . . .
23.4.2 Pixel Processing . . . . .
23.4.3 Frame Buffer Processing
23.5 Image Compositing . . . . . . .
23.6 Vertex Buffers . . . . . . . . . .
23.7 Texture Map Size . . . . . . . .
23.8 Scalable Shading . . . . . . . . .
23.9 Model Simplification . . . . . .
23.10 Alpha Test . . . . . . . . . . . .
23.11 Stencil Test . . . . . . . . . . .
23.12 Z Buffer Test . . . . . . . . . . .
23.13 Guard Band clip . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

817
817
819
819
819
820
821
821
822
822
822
822
822
822
822
822
823
823
823
823

24 Installation and Setup


24.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . .
24.2 Determining the DirectX Version . . . . . . . . . . .
24.3 DirectSetup . . . . . . . . . . . . . . . . . . . . . . .
24.3.1 Determining the DirectX Version . . . . . . .
24.3.2 Installing the DirectX Runtime . . . . . . . .
24.3.3 Using a Callback Procedure . . . . . . . . . .
24.3.4 Registering With DirectPlay Lobby . . . . . .
24.3.5 Preparing the Redistributables . . . . . . . . .
24.4 Windows Installer . . . . . . . . . . . . . . . . . . . .
24.4.1 Using a Launch Sequencer . . . . . . . . . . .
24.4.2 Requiring the Proper Runtime Version . . . .
24.4.3 Using a Custom Action to Invoke DirectSetup
24.5 Logo Requirements . . . . . . . . . . . . . . . . . . .
24.6 AutoRun Enabled Installations . . . . . . . . . . . . .
24.7 Testing Installation Programs . . . . . . . . . . . . .
24.8 Sample Program . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

825
825
826
827
827
827
829
832
833
833
834
834
835
835
836
836
836

VI

Appendices

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

839

A A Guided Tour of the SDK


841
A.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

xvi

CONTENTS
A.2
A.3
A.4
A.5
A.6
A.7
A.8
A.9
A.10

A Quick Start . . .
Installing the SDK
Organization . . . .
Documentation . .
Include Files . . . .
Libraries . . . . . .
Tutorials . . . . . .
Tools . . . . . . . .
SDK Samples . . .

B D3DFrame
B.1 Overview . . . . .
B.2 AppWizard . . . .
B.3 Files . . . . . . .
B.4 CD3DApplication
B.5 CD3DScreensaver
B.6 CD3DFont . . . .
B.7 CD3DFile . . . .
B.8 CD3DFrame . . .
B.9 CD3DMesh . . . .
B.10 CD3DScreensaver
B.11 CD3DArcBall . .
B.12 CD3DCamera . .
B.13 D3DUtil . . . . .
B.14 DXUtil . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

841
842
842
845
845
846
846
847
847

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

849
849
850
850
850
854
854
855
856
857
858
860
861
862
863

C Shader Tokens
867
C.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
C.2 Instruction Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . 867
D Sample Code
D.1 Overview . . . . . . . . . .
D.2 C++ Sample Code . . . .
D.2.1 rt Library Code . .
D.3 Visual Basic Sample Code

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

873
873
873
875
876

Glossary

877

Index

877

Source Code Listings


1.1
1.2
1.3

dshow.cpp: A simple AVI file player. . . . . . . . . . . . . . . . .


<rt/hr.h>: mapping HRESULTs to exceptions. . . . . . . . . . . .
dshow2.cpp: An improved simple AVI player. . . . . . . . . . . .

24
29
36

2.1

minimal.cpp: A minimal well-behaved Direct3D application. . .

44

3.1

<rt/states.h>: Setting aggregate state . . . . . . . . . . . . . . 101

4.1
4.2

<rt/surface.h>: A surface lock helper class. . . . . . . . . . . . 116


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.

7.1

rt Views.cpp: Drawing multiple views . . . . . . . . . . . . . . . 246

8.1
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11
9.12
10.1
11.1

rt Lighting.cpp: Lighting and material parameters.


BumpShader.vsh: BumpSelfShadow shader 1. . . . . .
BumpShader2.vsh: BumpSelfShadow shader 2. . . . .
BumpShader3.vsh: BumpSelfShadow shader 3. . . . .
BumpShader4.vsh: BumpSelfShadow shader 4. . . . .
BumpWaves vertex shader. . . . . . . . . . . . . . . .
DolphinTween.vsh: DolphinVS shader 1. . . . . . . .
DolphinTween2.vsh: DolphinVS shader 2. . . . . . . .
SkinnedMesh4.vsh: SkinnedMesh shader 4. . . . . . .
SphereMap shader . . . . . . . . . . . . . . . . . . . .
Blend.vsh: VertexBlend shader . . . . . . . . . . . . .
Ripple.vsh: VertexShader shader . . . . . . . . . . .
FogShader.vsh: VolumeFog shader 1 . . . . . . . . . .
rt Rasterize.cpp: Rasterization. . . . . . . . . . . .
rt Texture.cpp: Texturing. . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

270
359
360
361
362
363
364
366
368
371
372
374
376
386
453

14.1
18.1
18.2
21.1
21.2
21.3

rt FrameBuffer.cpp: Frame buffer processing.


water.fx: Water sample effect file. . . . . . . .
cubemap.fx: CubeMap sample effect file. . . .
rt MakeScene.cpp: X file output. . . . . . . .
frame.h: X file output. . . . . . . . . . . . . .
frame.cpp: X file output. . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

510
658
667
769
772
776

xvii

. . . . . . . . . . . . . . 225

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

Figures
1.1
1.2
1.3
1.4

Subsystems of a typical 2D graphics adapter. . . . . . . .


Left-handed, right-handed and screen coordinate systems
Illustration of Aliasing. . . . . . . . . . . . . . . . . . . . .
Antialiasing During Resampling. . . . . . . . . . . . . . .

.
.
.
.

7
16
18
20

2.1
2.2

Relationship of Direct3D to other Windows subsystems. . . . . .


Direct3D device architecture . . . . . . . . . . . . . . . . . . . .

51
51

3.1
3.2

Resource interface hierarchy. . . . . . . . . . . . . . . . . . . . . .


Query object state transitions. . . . . . . . . . . . . . . . . . . .

82
88

4.1

Semantics of D3DSWAPEFFECT on a swap chain . . . . . . . . . . . 123

5.1
5.2
5.3
5.4
5.5
5.6

Illustration of a cycle of occlusion with four rectangles. . . .


Illustration of primitive types . . . . . . . . . . . . . . . . .
Modeling with primitive types . . . . . . . . . . . . . . . . .
Relationship between arguments to DrawIndexedPrimitive
Point sprite structure in screen space. . . . . . . . . . . . .
Bezier triangular patch vertex order. . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

162
165
166
190
192
195

6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11

Order of transformations . . . . . . . . . . . . . . . . . . .
Illustration of a composite transformation. . . . . . . . . .
Jointed linkage transformation hierarchy . . . . . . . . . .
Visualization of vertex blending. . . . . . . . . . . . . . .
Sample blend weight functions. . . . . . . . . . . . . . . .
The effect of blend weight functions on vertex blending. .
The effect of the world transformation on vertex blending.
Fog factor functions. . . . . . . . . . . . . . . . . . . . . .
Computing a triangles plane normal . . . . . . . . . . . .
Relationship of triangles to the line of sight in culling. . .
Illustration of clipping . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

204
205
207
208
209
210
211
217
218
219
221

7.1
7.2

Viewing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Taxonomy of planar geometric projections. . . . . . . . . . . . . 242

8.1
8.2

Lighting and materials control flow . . . . . . . . . . . . . . . . . 258


Computing vertex surface normals for triangles. . . . . . . . . . . 259
xviii

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

FIGURES

xix

8.3
8.4
8.5

Light source geometry. . . . . . . . . . . . . . . . . . . . . . . . . 264


Lamberts law of diffuse reflection. . . . . . . . . . . . . . . . . . 268
The geometry of specular reflection. . . . . . . . . . . . . . . . . 269

9.1
9.2

Vertex shader architecture . . . . . . . . . . . . . . . . . . . . . . 302


Shader assembly syntax . . . . . . . . . . . . . . . . . . . . . . . 311

10.1 Scanline rendering of a triangle . . . . . . . . . . . . . . . . . . . 382


10.2 Rasterization of a triangle . . . . . . . . . . . . . . . . . . . . . . 384
11.1 Multitexture cascade. . . . . . . . . . . . . . . . . . . . .
11.2 Organization of mipmapped textures. . . . . . . . . . . .
11.3 Cube texture organization. . . . . . . . . . . . . . . . . .
11.4 Volume organization. . . . . . . . . . . . . . . . . . . . . .
11.5 Organization of mipmapped volume textures. . . . . . . .
11.6 Texel organization within a block for compressed textures.
11.7 Sample block of compressed texels. . . . . . . . . . . . . .
11.8 Texture address space. . . . . . . . . . . . . . . . . . . . .
11.9 Illustration of the reflection vector. . . . . . . . . . . . . .
11.10Texture coordinate addressing modes . . . . . . . . . . . .
11.11Texture coordinate wrapping . . . . . . . . . . . . . . . .
11.12Texture minification filtering . . . . . . . . . . . . . . . .
11.13Texture stage processing . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

405
408
415
417
421
423
424
428
430
432
435
437
439

13.1 Pixel shader architecture overview. . . . . . . . . . . . . . . . . . 472


13.2 Pixel shader instruction execution. . . . . . . . . . . . . . . . . . 473
18.1 Structure of an effect file . . . . . . . . . . . . . . . . . . . . . . . 646
18.2 Effect file syntax summary . . . . . . . . . . . . . . . . . . . . . . 646
19.1
19.2
19.3
19.4

D3DX Mesh Interface Hierarchy. . . . . . .


Triangle Adjacency . . . . . . . . . . . . . .
Bowtie arrangement of vertices in a triangle
Mesh simplification by collapsing edges. . .

. . .
. . .
mesh
. . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

672
674
689
691

21.1
21.2
21.3
21.4
21.5

X file grammar . . . . . .
Template grammar . . . .
Data node grammar . . .
Order of indices for Bezier
.x File Interface Hierarchy

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

724
725
727
746
753

. . . . .
. . . . .
. . . . .
patches.
. . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

22.1 DirectX Diagnostic Tool . . . . . . . . . . . . . . . . . . . . . . . 810


22.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. . . . . . . . . . . . . . . . 844
A.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
1.2
1.3

Vector operations . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . .
Macros for HRESULT return values. . . . . . . . . . . . . . . . . .

3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9

Device Methods by Pipeline Section . . . . . .


Device Capabilities by Pipeline Section . . . . .
Implicit Query Beginnings . . . . . . . . . . . .
Query Data Types . . . . . . . . . . . . . . . .
Device States by Pipeline Section . . . . . . . .
Device State Data Types . . . . . . . . . . . .
Methods captured by BeginStateBlock . . . .
Device Properties by State Block Type . . . . .
Device property support on a pure HAL device.

4.1
4.2

Presentation intervals supported in windowed and exclusive mode.124


Combinations of source and destination surfaces supported with
UpdateSurface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

5.1
5.2
5.3
5.4
5.5

Flexible Vertex Format . . . . . . . . . . . . . .


Vertex usage for the fixed-function pipeline. . .
Relationship of primitive count to vertex count
Vertex count for triangular Bezier patches. . . .
Rectangular patch vertex grid sizes. . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

170
178
187
196
197

8.1
8.2
8.3
8.4

Material parameter defaults. . . . . . . . . . . . .


The D3DLIGHT9 members used by each light type.
Light attenuation coefficient examples. . . . . . .
Symbols used for D3DLIGHT9 and D3DMATERIAL9.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

260
263
265
266

9.1
9.2
9.3
9.4
9.5

Vertex
Vertex
Vertex
Vertex
Vertex

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

303
304
306
314
322

shader version instruction counts.


shader version register counts. .
shader register modifiers . . . . .
Shader 1.1 Instruction Summary
Shader 2.0 Instruction Summary
xxi

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

15
17
24
76
81
89
89
94
100
106
109
110

xxii
9.6

9.7
9.8
9.9
9.10
9.11
9.13
9.14

TABLES
sincos results based on write mask. ? indicates a component
that has an undefined value after the instruction executes.
indicates a component that is not changed by the instruction. . .
Vertex shader version nesting limits. . . . . . . . . . . . . . . . .
Vertex Shader 2.x Instruction Summary . . . . . . . . . . . . . .
Vertex shader instructions supporting predication . . . . . . . . .
Vertex Shader 3.0 Instruction Summary . . . . . . . . . . . . . .
Register layout for fixed-function emulation. . . . . . . . . . . . .
BumpSelfShadow vertex shader constants. . . . . . . . . . . . . .
Input registers used by the DolphinVS sample . . . . . . . . . . .

323
326
326
327
330
346
355
356

11.1 Summary of texture operations. . . . . . . . . . . . . . . . . . . . 442


13.1 Pixel shader architecture versions. . . . . . . . .
13.2 Pixel shader instruction availability. . . . . . . .
13.3 Pixel shader instruction set summary. . . . . . .
13.4 Pixel shader 1.0 register restrictions. . . . . . . .
13.5 Pixel shader 1.1 register restrictions. . . . . . . .
13.6 Pixel shader 1.2 register restrictions. . . . . . . .
13.7 Pixel shader 1.3 register restrictions. . . . . . . .
13.8 Pixel shader 1.4 register restrictions in phase 1. .
13.9 Pixel shader 1.4 register restrictions in phase 2. .
13.10Pixel shader 2.0 instructions in assembly syntax.
13.11Pixel shader 2.x instructions in assembly syntax.
13.12Pixel shader 3.0 instructions in assembly syntax.
13.13Texture stage states honored by pixel shaders. . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

473
474
475
476
477
478
479
480
481
487
488
488
490

14.1
14.2
14.3
14.4
14.5

Summary of stencil operations . . . . . . . . . . . . . .


Blend factors described by D3DBLEND . . . . . . . . . . .
Blending functions described by D3DBLENDOP . . . . . .
Blend factors for common compositing operators. . . . .
Command-line switches supported by rt FrameBuffer.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

499
503
503
504
510

16.1
16.2
16.3
16.4
16.5
16.6
16.7

D3DXCOLOR class members. . . . . . . . . . . . .


D3DXCOLOR global functions. . . . . . . . . . . .
D3DX vector class members . . . . . . . . . . .
D3DX vector class function summary. . . . . .
Vector function arithmetic operation summary
D3DXMATRIX class members . . . . . . . . . . .
D3DXQUATERNION class members. . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

603
604
605
608
609
615
627

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

18.1 Effect value syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 648


19.1 Vertex data for a flat shaded cube. . . . . . . . . . . . . . . . . . 681
19.2 Face data for a flat shaded cube. . . . . . . . . . . . . . . . . . . 681
21.1 X File Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722

TABLES

xxiii

21.2
21.3
21.4
21.5

Meaning of terminal X
X file tokens . . . . . .
X file record tokens . .
Animation key types .

file
. .
. .
. .

grammar tokens
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

724
729
730
733

22.1
22.2
22.3
22.4
22.5

Common CSIDL values. . . . . . . . . . . . . . . . . . . . . . . .


Format specifiers for debugger structure expansion formatting.
Registry keys to disable CPU specific software processing. . . .
Community resources . . . . . . . . . . . . . . . . . . . . . . .
IHV Contact Information . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.

795
802
809
813
815

24.1 GetDXVersion return values. . . . . . . . . . . . . . . . . . . . . 826


24.2 DirectXSetupGetVersion return values. . . . . . . . . . . . . . . 828
A.1 Direct3D related include files. . . . . . . . . . . . . . . . . . . . . 846
A.2 Summary of SDK libraries for Direct3D applications. . . . . . . . 847
A.3 Summary of SDK tools. . . . . . . . . . . . . . . . . . . . . . . . 847

xxiv

TABLES

COM Interfaces
1.1

IUnknown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

2.1

IDirect3D9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

57

3.1
3.2
3.3
3.4

IDirect3DDevice9 . . .
IDirect3DResource9 .
IDirect3DQuery9 . . . .
IDirect3DStateBlock9

4.1
4.2

IDirect3DSurface9 . . . . . . . . . . . . . . . . . . . . . . . . . 112
IDirect3DSwapChain9 . . . . . . . . . . . . . . . . . . . . . . . . 120

5.1
5.2
5.3
5.4

IDirect3DVertexDeclaration9
IDirect3DVertexBuffer9 . . . .
IDirect3DIndexBuffer9 . . . .
IDirect3DVertexShader9 . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

. 71
. 83
. 87
. 105

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

176
179
184
186

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

410
413
415
417
420

IDirect3DBaseTexture9 . .
IDirect3DTexture9 . . . . .
IDirect3DCubeTexture9 . .
IDirect3DVolume9 . . . . .
IDirect3DVolumeTexture9

15.1

ID3DXBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597

17.1
17.2
17.3
17.4
17.5
18.1

ID3DXMatrixStack . . .
ID3DXFont . . . . . . . .
ID3DXSprite . . . . . . .
ID3DXRenderToSurface
ID3DXRenderToEnvMap .
ID3DXEffect . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

632
634
639
640
642
652

19.1
19.2
19.3
19.4
19.5

ID3DXBaseMesh
ID3DXMesh . . .
ID3DXPMesh . .
ID3DXSPMesh . .
ID3DXSkinInfo

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

676
683
701
705
708

21.1

IDirectXFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

xxv

.
.
.
.
.

.
.
.
.

11.1
11.2
11.3
11.4
11.5

.
.
.
.
.

.
.
.
.
.

.
.
.
.

xxvi
21.2
21.3
21.4
21.5
21.6
21.7

COM INTERFACES
IDirectXFileEnumObject . .
IDirectXFileObject . . . . .
IDirectXFileData . . . . . .
IDirectXFileBinary . . . . .
IDirectXFileDataReference
IDirectXFileSaveObject . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

756
757
757
760
761
762

Preface
Who Should Read This Book
This book is for C++ programmers who want to understand how to create images rendered from scenes using Microsofts Direct3D application programming
interface.
The reader is assumed to be familiar with basic Win32 programming: windows, messages, keyboard and mouse input, device contexts, GDI, and so-on. If
these areas are new to the reader, they are encouraged to start first with a basic
text 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 COM
objects. No existing knowledge of COM is assumed and the basics of COM as
used with Direct3D are reviewed.
COM objects can be used from a variety of languages. This book will use
C++ for its programming examples and the reader should be comfortable with
C++ concepts such as classes, inheritance, abstract interfaces, and generic programming with template classes such as those in the ANSI C++ standard library.
The author believes in the KISS (Keep It Simple, Stupid) maxim C++ features
are used only when they provide a clear benefit.
Three dimensional scenes are described to the computer with the assistance
of some mathematical concepts. Readers should be comfortable with the mathematics of geometry, trigonometry, vectors and matrices. A summary review of
the mathematics common in computer graphics is given for reference.

Whats Not In This Book


This book provides a complete explanation of the Direct3D graphics pipeline
provided in release 9.0c of Microsofts DirectX technology. A sophisticated 3D
multimedia application may use many other technologies such as DirectSound
and DirectMusic for sound and music, DirectInput for input device processing,
DirectPlay or TCP/IP for networking, physically-based models and dynamics simulations for realistic animation, persistent storage for scenes, interactive
modelling of scenes, and Win32 for GUI presentation.
xxvii

xxviii

PREFACE

In this book we will touch on input processing with the core Win32 messages, but other areas of DirectX and application processing will not be covered.
References are provided for other books that cover these areas when they arise
in our discussion of the pipeline.

Thanks
List of people to thank.

This book is dedicated to:


Sylvia Mead Atwater
1901-1990
who shared her joy of knowledge,
Anne and Bill Thomson
for making all things possible,
and
Pete Townshend
for over 20 years of musical ecstasy.

xxix

xxx

PREFACE

Potrebbero piacerti anche