Creating and loading textures

There are a number of previous posts on the state of .DDS files which is the container format for Direct3D resources (see MSDN for some basics).

The DDS File Format Lives

DDS Update and 10:10:10:2 Problems

DDSWithoutD3DX Sample Update

With the retirement of the D3DX library, the recommended solutions for working with Direct3D 11 textures and .DDS files are now DirectXTex and DirectXTK.

  • The DirectXTex library provides the functionality in D3DX for loading bitmaps, resizing and converting them, generating mipmaps, compressing, and then writing them out as .DDS files. This is usually overkill for most applications to do at run-time, and not a particularly good use of end-user's time anyhow, but it's great for writing custom content tool pipelines for texture processing. The DirectXTex package includes a 'sample' which is the venerable texconv command-line tool written to use DirectXTex instead of D3DX.

 

  • The DDSTextureLoader module is intended to handle efficient loading of .DDS files and creating Direct3D 11 resources from them. It does not perform any runtime conversions, so some legacy files with pixel formats that do not directly map to a DXGI format will fail to load and in some cases the DXGI format of the file is not supported by the device and will also fail to load. For these cases, you will want to use DirectXTex to convert them offline to something that you can rely on being able to load on your target machine. This code supports the full range of Direct3D 11 resources including 1D, 2D, 3D, cubemaps, and texture arrays with mipmaps. The DDSTextureLoader module is included in both the DirectXTK library and in the DirectXTex package.

 

  • For very simple cases, there is also a WICTextureLoader module which can load standard bitmap files, does some runtime conversions and resizing, and then creates a Direct3D 11 texture 2D from it. It can optionally enable the 'auto-gen mipmaps' feature of Direct3D 11 to provide some basic mipmap support as well (standard bitmap files can't store mipmaps with the base image the way a .DDS file can). This makes use of the Windows Imaging Component (WIC), but is much more 'heavyweight' than DDSTextureLoader. This gives you less control over the quality of the filtering (particularly mipmaps), and does not support complex textures like volume maps, cubemaps, or texture arrays. The WICTextureLoader module is also included in both the DirectXTK library and in the DirectXTex package.

 

  • The ScreenGrab module is intended as a light-weight texture saver for creating 'screen shot' bitmap files from render target textures. The ScreenGrab module is included in the DirectXTK library and DirectXTex package.

DirectXTK and DirectXTex both support Windows Store apps (a.k.a. Metro style apps) on Windows 8 as well as traditional Win32 desktop applications on Windows 8, Windows 7, and Windows Vista SP2 with the DirectX 11.0 update. There is a version of DDSTextureLoader used by the Windows 8 DirectX samples as well (see the Direct3D resource loading sample).

Update: Both libraries also work on Windows phone and Xbox One.

Block Compression

One of the advantages of using .DDS files is the abiltiy to make use of Direct3D texture compression. The original Direct3D 9 texture compression schemes (DXT1, DXT2/3, DXT4/5) are still supported (now called BC1, BC2, and BC3) and support is required on all Direct3D 11 API compatible hardware including the 9.1, 9.2, and 9.3 feature levels. These schemes are designed for compressing standard three-channel color data with and without an alpha channel. DirectXTex provides the same software compressor that has been in D3DX for many years, with the addition of a few minor improvements through optional control flags.

Feature Level 10.0, 10.1, 11.0, and 11.1 devices also support BC4 and BC5 compression schemes. BC4 is also known as 3DC+ ("ATI1"), and BC5 is known as 3Dc ("ATI2") or "DXN", and both are designed for compressing single-channel or tow-channel textures such as normal maps. DirectXTex provides the same software compressor that was in D3DX10 and D3DX11.

Feature Level 11.0 and 11.1 devices support BC6H and BC7 compression schemes. BC6H is designed for high-dynamic range (HDR) float16 content, while BC7 is a more robust high-quality alternative to the standard BC1-3 schemes for three-channel color data images. Note that BC6H and BC7 decompression is extremely fast with hardware support, but can be expensive to compress. DirectXTex provides the same software compressor available in D3DX11, but a DirectCompute accelerated version is in the BC6HBC7EncoderDecoder sample.

Update: The latest DirectXTex supports both the CPU and DirectCompute compressors.

Porting Notes

Here's a handy table of equivalents for replacing legacy D3DX texture related functions.

D3DX11ComputeNormalMap

DirectXTex library, ComputeNormalMap

D3DX11CreateShaderResourceViewFromFile
D3DX11CreateTextureFromFile

DDSTextureLoader: CreateDDSTextureFromFile
WICTextureLoader: CreateWICTextureFromFile

DirectXTex library (tools), LoadFromXXXFile then CreateShaderResourceView or CreateTexture

D3DX11CreateShaderResourceViewFromMemory
D3DX11CreateTextureFromMemory

DDSTextureLoader: CreateDDSTextureFromMemory
WICTextureLoader: CreateWICTextureFromMemory

DirectXTex library (tools), LoadFromXXXMemory then CreateShaderResourceView or CreateTexture

D3DX11CreateShaderResourceViewFromResource
D3DX11CreateTextureFromResource

No direct equivalent, can use Win32 resource functions and then the ‘from memory’ APIs above.

D3DX11FilterTexture

DirectXTex library, GenerateMipMaps and GenerateMipMaps3D

D3DX11GetImageInfoFromFile

DirectXTex library, GetMetadataFromXXXFile

D3DX11GetImageInfoFromMemory

DirectXTex library, GetMetadataFromXXXMemory

D3DX11GetImageInfoFromResource

No direct equivalent, can use Win32 resource functions and then the ‘from memory’ APIs above.

D3DX11LoadTextureFromTexture

DirectXTex library, Resize, Convert, Compress, Decompress, and/or CopyRectangle

D3DX11SaveTextureToFile

ScreenGrab: SaveDDSTextureToFile or SaveWICTextureToFile

DirectXTex library, CaptureTexture then SaveToXXXFile

D3DX11SaveTextureToMemory

DirectXTex library, CaptureTexture then SaveToXXXMemory

D3DX11SHProjectCubeMap

Spherical Harmonics Math, SHProjectCubeMap

Viewing DDS files

The legacy DirectX SDK (see Where is the DirectX SDK?) includes the DirectX Texture Viewer tool. This only supports older Direct3D 9 .DDS files and will fail to load any file using the 'DX10' extension or BC4, BC5, BC6H, or BC7. Visual Studio 2012 can view modern .DDS files directly via File / Open / File... (the Image Files filter includes .dds). There is also a 'ddsview' sample available in the DirectXTex package.

Presentations

Modern Texture Content Pipelines (Gamefest 2011)

Tiled Resources for Xbox 360 and Direct3D 11 (Gamefest 2011)

Block Compression Smorgasbord (Gamefest 2010)

 

Related: Getting Started with Direct3D 11