D3DX Utility Library
D3DX Utility Library
D3DX Utility Library
15.1 Overview
D3DX is a static library designed for production use in shipping applications.
D3DX provides operations that are common in 3D graphics applications, but are
not necessarily tied directly to the graphics pipeline. D3DX is a static library,
so you do not have any runtime dependencies when you use it. D3DX consists of
several areas of related operations: abstract data types, helper objects, triangle
meshes, resource functions, and miscellaneous functions.
The concrete classes providing data types for colors, materials, vectors, ma-
trices, planes and quaternions. They are implemented as C++ classes and associ-
ated global functions operating on those classes. The data types are summarized
in section 15.3 and described in detail in chapter 16.
The helper objects implement matrix stacks, text rendering, sprites, multi-
pass rendering effects, and render target assistance. These objects are imple-
mented as COM objects. They are summarized in section 15.4 and described
in detail in chapter 17.
Most of the higher level operations in D3DX operate on triangle meshes.
Meshes come in several varieties, all implemented as COM objects. Mesh sim-
plification is exposed through several COM interfaces, providing dynamic level
of detail selection. Skinned meshes are also provided as a COM object. Func-
tions are provided for operating on meshes and streaming them to and from
permanent storage. Meshes are summarized in section 15.5 and described in
detail in chapter 19.
The resource functions in D3DX handle some of the details of creating,
initializing, and streaming resources to and from files within the requirements
of a device. The resource funcions are described in section 15.6.
575
576 CHAPTER 15. D3DX UTILITY LIBRARY
D3DX also provides a few bits and pieces not easily categorized with the
portions described so far. They are described in section 15.16
15.5 Meshes
D3DX provides several mesh objects that encapsulate indexed triangle lists.
Mesh simplification and progressive mesh support are provided. ID3DXMesh in-
terface encapsulates a shape as a triangle mesh. Progressive mesh refinement
is available in the ID3DXPMesh interface. ID3DXBaseMesh is a base interface
used by the ID3DXMesh and ID3DXPMesh objects. Arbitrary mesh simplification
is encapsulated by ID3DXSPMesh. Finally, skinned meshes – meshes using ver-
tex blending – are encapsulated by the ID3DXSkinInfo. D3DX also provides
support for reading and writing mesh objects to and from .x files.
The details of the mesh objects and their associated functions are described
in chapter 19.
15.6 Resources
Images and textures are resources that are used by almost every Direct3D ap-
plication. D3DX provides a wide variety of functions for creating and initializ-
ing surfaces and textures from files, Win32 resources, and raw memory blocks.
D3DX can read .bmp, .dds, .dib, .jpg, .png, .ppm, and .tga file formats
when reading resource data. D3DX can write resource data to DirectDraw
surface (.dds) files and Windows bitmap format (.bmp) files.
File data on disk is referenced by its file name. A file in memory is referenced
by a pointer to a block of memory containing the file data and the size of the
memory block. A file in a Win32 resource is referenced by a handle to the
module containing the resource and the name of the resource. Win32 resources
can be of type RT BITMAP for .bmp data or RT RCDATA for other file formats. Be
careful not to confuse Win32 resources, data stored in an executable file such as
a .dll or .exe file, with Direct3D resources, such as textures, image surfaces,
vertex buffers and index buffers.
578 CHAPTER 15. D3DX UTILITY LIBRARY
HRESULT ::D3DXCheckCubeTextureRequirements(
IDirect3DDevice9 *device,
UINT *size,
UINT *mip_levels,
DWORD usage,
D3DFORMAT *format,
D3DPOOL pool);
HRESULT ::D3DXCheckVolumeTextureRequirements(
IDirect3DDevice9 *device,
UINT *width,
UINT *height,
UINT *depth,
UINT *mip_levels,
DWORD usage,
D3DFORMAT *format,
580 CHAPTER 15. D3DX UTILITY LIBRARY
D3DPOOL pool);
HRESULT ::D3DXLoadSurfaceFromSurface(
IDirect3DSurface9 *destination,
const PALETTEENTRY *dest_palette,
const RECT *dest_rect,
IDirect3DSurface9 *source,
const PALETTEENTRY *source_palette,
const RECT *source_rect,
DWORD filter,
D3DCOLOR color_key);
HRESULT ::D3DXLoadVolumeFromVolume(
IDirect3DVolume9 *destination,
const PALETTEENTRY *dest_palette,
const D3DBOX *dest_box,
IDirect3DVolume9 *source,
const PALETTEENTRY *source_palette,
const D3DBOX *source_box,
DWORD filter,
D3DCOLOR color_key);
The dest palette and source palette arguments are pointers to a block of
memory containing 256 PALETTEENTRY structures when the source or destination
are in D3DFMT P8 format. These arguments may be NULL when the source or
destination is not an indexed format.
The dest rect and source rect arguments allow the conversion operation
to be applied to subrectangles of the source and destination surfaces. The
dest box and source box arguments operate similarly for volumes. When these
15.9. FORMAT CONVERSION 581
arguments refer to regions of different size in the source and destination, D3DX
performs filtering on the regions to resize the source to fit the destination. The
filter kernel used is specified by the filter parameter, whose value can be
D3DX DEFAULT or one or more of the following flags:
D3DX FILTER—textbf
The value D3DX FILTER NONE results in pixels in the destination region that
lie outside the source region to be replaced with transparent black. With this
value, no scaling or filtering will be performed. The value D3DX DEFAULT is
equivalent to D3DX FILTER TRIANGLE combined with D3DX FILTER DITHER.
The values D3DX FILTER POINT, D3DX FILTER LINEAR, D3DX FILTER TRIANGLE
or D3DX FILTER BOX specify the filter kernel to be applied to the source region.
Point filtering is the fastest rescaling method, but also introduces the most alias-
ing. Linear filtering uses a 2 × 2 pixel neighbourhood for a surface, or a 2 × 2 × 2
pixel neighbourhood for a volume, and linearly interpolates between the source
pixels in the neighbourhood to compute the destination pixel value. Linear
filtering is more expensive than point filtering but results in a higher quality
image. The triangle filter uses a neighbourhood proportional to the relative
size of the source and destination areas, making this the most expensive filter
with the highest quality. The box filter uses a 2 × 2 surface neighbourhood, or
2 × 2 × 2 for volumes, and computes the destination pixel value as the average
of all pixels within the source neighbourhood. The box filter is typically used
for computing mipmap levels, but still suffers from some aliasing artifacts.
The mirror flags control repetition of the source image with respect to the x,
y, or z axes of the source image. The D3DX FILTER MIRROR flag is a shorthand
for mirroring about all axes of the source image. The D3DX FILTER DITHER
flag enables a 4 × 4 ordered dither of the source pixels to the destination pixels.
Dithering is most useful in preventing banding artifacts when reducing the color
depth of the source.
Finally, the color key argument allows the application to specify a pixel
value in the source that will be substituted with transparent black during pro-
cessing. While Direct3D itself does not support color keying transparency,
D3DX provides this limited form of color keying during surface loading.
582 CHAPTER 15. D3DX UTILITY LIBRARY
HRESULT ::D3DXCreateTextureFromFile(
IDirect3DDevice9 *device,
LPCTSTR filename,
IDirect3DTexture9 **result);
HRESULT ::D3DXCreateCubeTextureFromFile(
IDirect3DDevice9 *device,
LPCTSTR filename,
IDirect3DCubeTexture9 **result);%
HRESULT ::D3DXCreateVolumeTextureFromFile(
IDirect3DDevice9 *device,
LPCTSTR filename,
IDirect3DVolumeTexture9 **result);
Texture resources can be created from any of the supported file formats
enumerated in D3DXIMAGE FILEFORMAT. Cube textures can only be created from
.dds files. Volume textures can be created from .dds files which can contain an
entire volume, or from image files to create a volume containing a single slice.
The extended texture creation functions allow all the parameters of the
texture creation process to be controlled by the caller. The caller can specify
target resource properties and file processing arguments to obtain fine control
over the resource creation process. The resource dimensions, usage, format,
and memory pool can all be specified individually, or left to default arguements
with D3DX DEFAULT. Texture filtering parameters and a color to be treated as a
transparent color key during the loading process can be specified.
HRESULT ::D3DXCreateTextureFromFileEx(
IDirect3DDevice9 *device,
LPCTSTR filename,
UINT width,
UINT height,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
584 CHAPTER 15. D3DX UTILITY LIBRARY
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DTexture9 **result);
HRESULT ::D3DXCreateCubeTextureFromFileEx(
IDirect3DDevice9 *device,
LPCTSTR filename,
UINT Size,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DCubeTexture9 **result);
HRESULT ::D3DXCreateVolumeTextureFromFileEx(
IDirect3DDevice9 *device,
LPCTSTR filename,
UINT width,
UINT height,
UINT depth,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DVolumeTexture9 **result);
The filter parameter controls the filtering of the image into the most de-
tailed level of the texture resource while the mip filter argument controls the
creation of mipmap levels. When D3DX DEFAULT is used for the filter param-
eter, a triangle filter with dithering is used to resample the image in the file.
When D3DX DEFAULT is used for the mip filter, a box filter is used to generate
the mipmap levels. The info parameter returns information about the image
file that was used to construct the texture. If this information is not needed,
15.10. CREATING TEXTURE RESOURCES 585
an application can pass NULL for the info parameter. The palette parameter
is used to return the palette loaded with the resource. This parameter can be
NULL if no palette is needed.
HRESULT ::D3DXCreateTextureFromFileInMemory(
IDirect3DDevice9 *device,
const void *data,
UINT size,
IDirect3DTexture9 **result);
HRESULT ::D3DXCreateCubeTextureFromFileInMemory(
IDirect3DDevice9 *device,
const void *data,
UINT size,
IDirect3DCubeTexture9 **result);
HRESULT ::D3DXCreateVolumeTextureFromFileInMemory(
IDirect3DDevice9 *device,
const void *data,
UINT size,
IDirect3DVolumeTexture9 **result);
HRESULT ::D3DXCreateTextureFromFileInMemoryEx(
IDirect3DDevice9 *device,
const void *data,
UINT size,
UINT width,
UINT height,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DTexture9 **result);
586 CHAPTER 15. D3DX UTILITY LIBRARY
HRESULT ::D3DXCreateCubeTextureFromFileInMemoryEx(
IDirect3DDevice9 *device,
const void *data,
UINT size,
UINT Size,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DCubeTexture9 **result);
HRESULT ::D3DXCreateVolumeTextureFromFileInMemoryEx(
IDirect3DDevice9 *device,
const void *data,
UINT size,
UINT width,
UINT height,
UINT depth,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DVolumeTexture9 **result);
HRESULT ::D3DXCreateTextureFromResource(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
IDirect3DTexture9 **result);
HRESULT ::D3DXCreateCubeTextureFromResource(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
IDirect3DCubeTexture9 **result);
HRESULT ::D3DXCreateVolumeTextureFromResource(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
IDirect3DVolumeTexture9 **result);
HRESULT ::D3DXCreateTextureFromResourceEx(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
UINT width,
UINT height,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DTexture9 **result);
HRESULT ::D3DXCreateCubeTextureFromResourceEx(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
UINT Size,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
588 CHAPTER 15. D3DX UTILITY LIBRARY
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DCubeTexture9 **result);
HRESULT ::D3DXCreateVolumeTextureFromResourceEx(
IDirect3DDevice9 *device,
HMODULE module,
LPCTSTR resource,
UINT width,
UINT height,
UINT depth,
UINT mip_levels,
DWORD usage,
D3DFORMAT format,
D3DPOOL pool,
DWORD filter,
DWORD mip_filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info,
PALETTEENTRY *palette,
IDirect3DVolumeTexture9 **result);
For example, if the file “foo.png” is stored in a data resource with the integer
name IDR DATA1 in the module associated with the process’ executable image,
you can create a texture from that resource with the following code snippet:
IDirect3DTexture9 *texture = 0;
::D3DXCreateTextureFromResource(device, NULL,
MAKEINTRESOURCE(IDR_DATA1), &texture);
HRESULT ::D3DXLoadSurfaceFromMemory(
IDirect3DSurface9 *destination,
const PALETTEENTRY *dest_palette,
const RECT *dest_rect,
const void *source,
D3DFORMAT format,
UINT pitch,
const PALETTEENTRY *source_palette,
const RECT *source_rect,
DWORD filter,
D3DCOLOR color_key);
HRESULT ::D3DXLoadSurfaceFromFile(
IDirect3DSurface9 *destination,
const PALETTEENTRY *dest_palette,
const RECT *dest_rect,
LPCTSTR filename,
const RECT *source_rect,
DWORD filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info);
HRESULT ::D3DXLoadSurfaceFromFileInMemory(
IDirect3DSurface9 *destination,
const PALETTEENTRY *dest_palette,
const RECT *dest_rect,
const void *data,
UINT size,
const RECT *source_rect,
DWORD filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info);
HRESULT ::D3DXLoadSurfaceFromResource(
590 CHAPTER 15. D3DX UTILITY LIBRARY
IDirect3DSurface9 *destination,
const PALETTEENTRY *dest_palette,
const RECT *dest_rect,
HMODULE module,
LPCTSTR resource,
const RECT *source_rect,
DWORD filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info);
HRESULT ::D3DXLoadVolumeFromMemory(
IDirect3DVolume9 *destination,
const PALETTEENTRY *dest_palette,
const D3DBOX *dest_box,
const void *source,
D3DFORMAT format,
UINT row_pitch,
UINT slice_pitch,
const PALETTEENTRY *source_palette,
const D3DBOX *source_box,
DWORD filter,
D3DCOLOR color_key);
HRESULT ::D3DXLoadVolumeFromFile(
IDirect3DVolume9 *destination,
const PALETTEENTRY *dest_palette,
const D3DBOX *dest_box,
LPCTSTR filename,
const D3DBOX *source_box,
DWORD filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info);
HRESULT ::D3DXLoadVolumeFromFileInMemory(
IDirect3DVolume9 *destination,
const PALETTEENTRY *dest_palette,
const D3DBOX *dest_box,
const void *data,
UINT size,
const D3DBOX *source_box,
DWORD filter,
D3DCOLOR color_key,
D3DXIMAGE_INFO *info);
HRESULT ::D3DXLoadVolumeFromResource(
IDirect3DVolume9 *destination,
15.12. SAVING RESOURCES 591
The context argument allows the fill function to obtain information from the
application.
HRESULT ::D3DXFillTexture(IDirect3DTexture9 *texture,
LPD3DXFILL2D fill_proc,
void *context);
HRESULT ::D3DXFillCubeTexture(
IDirect3DCubeTexture9 *texture,
LPD3DXFILL3D fill_proc,
void *context);
HRESULT ::D3DXFillVolumeTexture(
IDirect3DVolumeTexture9 *texture,
LPD3DXFILL3D fill_proc,
void *context);
D3DXFILL2D is the fill procedure used for filling texture resources. Cube
textures and volume textures are filled with a three dimensional fill procedure
D3DXFILL3D.
typedef void (*LPD3DXFILL2D)(D3DXVECTOR4 *result,
D3DXVECTOR2 *coord,
D3DXVECTOR2 *size,
void *context);
the other mipmap levels. If the filter parameter is D3DX DEFAULT, then a box
filter is used.
HRESULT ::D3DXFilterTexture(
IDirect3DBaseTexture9 *texture,
const PALETTEENTRY *palette,
UINT level,
DWORD filter);
The channel argument specifies which channel from the height field is used
as the elevation. It can be one of the following values.
15.16 Miscellaneous
Besides data types, helper objects, triangle meshes, and resource functions,
D3DX provides some other operations that don’t fit into any of the other cate-
gories.
Mathematical constant macros, unit conversion macros
15.16.1 Macros
D3DX supplies some trigonometric macros. The macros D3DX PI and D3DX 1-
BYPI define constants for the value of π and 1/π. The macros D3DXToDegree
and D3DXToRadian provide conversions between degrees and radians.
N1 η1 + iκ1 η1 η2 + κ1 κ2 η 1 η 2 − κ1 κ2
N= = = 2 2 +i
N2 η2 + iκ2 η 1 + κ1 η12 + κ21
a2 + b2 − 2a cos θ + cos2 θ
F⊥ =
a2 + b2 + 2a cos θ + cos2 θ
a2 + b2 − 2a sin θ tan θ + sin2 θ tan2 θ
Fk = F⊥ 2
a + b2 + 2a sin θ tan θ + sin2 θ tan2 θ
r
1
a = (c + d)
2
r
1
b = (c − d)
2
p
c = d2 + 4η 2 κ2
d = η 2 − κ2 − sin2 θ
For an interface between two simple materials, the simple relative index of
refraction is the ratio of the index of fraction of the two materials, η1 /η2 . For
instance, the relative index of refraction between air and glass is about 1.5. In
this simplified case, κ1 = κ2 = 0 and the reflectance terms are simplified to the
following formulas.
a2 − 2a cos θ + cos2 θ
F⊥ =
a2 + 2a cos θ + cos2 θ
a2 − 2a sin θ tan θ + sin2 θ tan2 θ
Fk = F⊥ 2
a + 2a sin θ tan θ + sin2 θ tan2 θ
q
a = η 2 − sin2 θ
The total reflectance F for polarized light is a weighted sum of the two
components, a0 F⊥ + a1 Fk , where the weights a0 and a1 sum to unity. For
unpolarized light, F = (F⊥ + Fk )/2.
The D3DXFresnelTerm function computes the Fresnel reflection term F for
unpolarized light, incident at an angle whose cosine is given by the cos angle
parameter, reflecting from an interface between two simple materials whose
relative index of refraction is given by the refraction index parameter.
enum _D3DXERR
{
D3DXERR_CANNOTATTRSORT = MAKE_DDHRESULT(2902),
D3DXERR_CANNOTMODIFYINDEXBUFFER = MAKE_DDHRESULT(2900),
D3DXERR_INVALIDDATA = MAKE_DDHRESULT(2905),
D3DXERR_INVALIDMESH = MAKE_DDHRESULT(2901),
D3DXERR_LOADEDMESHASNODATA = MAKE_DDHRESULT(2906),
D3DXERR_SKINNINGNOTSUPPORTED = MAKE_DDHRESULT(2903),
D3DXERR_TOOMANYINFLUENCES = MAKE_DDHRESULT(2904)
};
15.18 ID3DXBuffer
Several functions and object methods in D3DX, the mesh functions and methods
in particular, need a way to pass an arbitrarily sized and typed buffer between
the application and the function or object. The ID3DXBuffer encapsulates this
concept as a COM object that implements a simple wrapper around a variable-
sized chunk of memory. An application can create a buffer object with the
D3DXCreateBuffer function. The object allocates size bytes and manages the
lifetime of the allocated memory with the lifetime of the object.
The ID3DXBuffer interface includes only two methods: one for obtaining a
pointer to the allocated memory and one for obtaining the size of the allocated
memory.
15.19. VERTEX DECLARATIONS 597
ID3DXBuffer
Read-Only Properties
GetBufferPointer A pointer to the contained data
GetBufferSize The size of the contained data
The type of the data contained in the buffer is not exposed by the interface.
When we know the type of the encapsulated data, we can use a template-based
wrapper to safely expose the underlying type of the contained data. The class
rt::dx buffer in the include file rt/mesh.h in the sample code contains such
a wrapper.
by the p0, p1, and p2 parameters. The starting point of the ray is given by
the position parameter and the ray’s direction is given by the direction
parameter. The result of the function is TRUE when the ray intersects the triangle
and FALSE otherwise. If the ray intersects the triangle, then the barycentric
coordinates of the intersection point are returned in the u and v parameters.
The distance of the intersection point from the origin of the ray is returned in
the distance parameter.
The flags argument can be zero or more of the following flags to indicate
options to the assembly process. D3DXASM DEBUG inserts debugging information
as comments into the assembled shader. D3DXASM SKIPVALIDATION does not
perform any validation checks on the assembled shader.