Microsoft DirectX 8.1 (C++) |
Before your application creates a rendering device, it can determine if the device supports texturing from compressed texture surfaces by calling the IDirect3D8::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 (FOURCCs). 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® by calling the IDirect3DDevice8::SetTexture method. The following code example shows how to determine if the adapter supports a compressed texture format.
BOOL IsCompressedTextureFormatOk( D3DFORMAT TextureFormat, D3DFORMAT AdapterFormat ) { HRESULT hr = pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, AdapterFormat, 0, D3DRTYPE_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.
After creating a device that supports a compressed texture format on the adapter, you can create a compressed texture resource. Call IDirect3DDevice8::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 IDirect3DTexture8::GetSurfaceLevel method.
Now you can use any Direct3DX function that begins with D3DXLoadSurface to load an image into the surface that was retrieved by using GetSurfaceLevel. These functions handle conversion to and from compressed texture formats.
You can create and convert compressed texture (DDS) files using the DXTex Tool supplied with the SDK. You can also create your own DDS files.
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 D3DERR_INVALIDCALL. 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.
As with compressing a texture surface, decompressing a compressed texture is performed through Microsoft® Direct3D® copying services.
To copy a compressed texture surface to a uncompressed texture surface, use the function D3DXLoadSurfaceFromSurface. This functions handles compression to and from compressed and uncompressed surfaces.