Using Compressed Textures
A version of this page is also available for
4/8/2010
Before your application creates a rendering device, it can determine if the device supports texturing from compressed texture surfaces by calling the IDirect3DMobile::CheckDeviceFormat method. This method determines whether a surface format can be used as a texture on a device representing the adapter. To test the adapter, specify any pixel format that uses the DXT1, DXT2, DXT3, DXT4, or DXT5 Four Character Codes (FOURCC) (see D3DMFORMAT). If CheckDeviceFormat returns D3D_OK, the device can create texture directly from a compressed texture surface that uses that format. If so, you can use compressed texture surfaces directly with Microsoft® Direct3D Mobile® by calling the IDirect3DMobileDevice::SetTextureStageState method. The following code example shows how to determine if the adapter supports a compressed texture format.
BOOL IsCompressedTextureFormatOk( D3DMFORMAT TextureFormat,
D3DMFORMAT AdapterFormat ) {
HRESULT hr = pD3DM->CheckDeviceFormat( D3DMADAPTER_DEFAULT,
D3DMDEVTYPE_DEFAULT,
AdapterFormat,
0,
D3DMRTYPEFLAG_TEXTURE,
TextureFormat);
return SUCCEEDED( hr );
}
If the device does not support texturing from compressed texture surfaces, you can still store texture data in a compressed format surface, but you must convert any compressed textures to a supported format before they can be used for texturing.
Creating Compressed Textures
After creating a device that supports a compressed texture format on the adapter, you can create a compressed texture resource. Call IDirect3DMobileDevice::CreateTexture and specify a compressed texture format for the Format parameter.
Before loading an image into a texture object, retrieve a pointer to the texture surface by calling the IDirect3DMobileTexture::GetSurfaceLevel method.
The advantage of this behavior is that an application can copy the contents of a compressed surface to a file without calculating how much storage is required for a surface of a particular width and height in the specific format.
The following table shows the five types of compressed textures. For more information on how the data is stored, see Compressed Texture Formats. You only need this information if you are writing your own compression routines.
FOURCC | Description | Alpha-premultiplied? |
---|---|---|
DXT1 |
Opaque/one-bit alpha |
N/A |
DXT2 |
Explicit alpha |
Yes |
DXT3 |
Explicit alpha |
No |
DXT4 |
Interpolated alpha |
Yes |
DXT5 |
Interpolated alpha |
No |
Note
When you transfer data from a non-premultiplied format to a premultiplied format, Direct3D scales the colors based on the alpha values. Transferring data from a premultiplied format to a non-premultiplied format is not supported. If you try to transfer data from a premultiplied-alpha source to a non-premultiplied-alpha destination, the method returns D3DMERR_INVALIDCALL (see D3DMERR Values). If you transfer data from a premultiplied-alpha source to a destination that has no alpha, the source color components, which have been scaled by alpha, are copied as is.
Decompressing Compressed Textures
As with compressing a texture surface, decompressing a compressed texture is performed through Direct3D Mobile copying services.
If the driver supports the creation of compressed video-memory surfaces, then the driver can also decompress copies from a compressed video-memory surface to an uncompressed video- or system-memory surface.
Copies from compressed system-memory surfaces to uncompressed video-memory surfaces are largely unsupported and should not be attempted, even when the driver supports compressed textures. This does not mean that it is impossible to decompress a compressed system-memory surface and move its contents into a video-memory surface; it merely requires an additional step.
To decompress a system-memory surface into video memory
Create an uncompressed, off-screen plain surface in system memory of the desired dimensions and pixel format.
Copy from the compressed system-memory surface to the uncompressed system-memory surface.
Copy the uncompressed surface to the uncompressed video-memory surface.