Compress texture format
Why do we need to compress textures
A virtual scene may load hundreds of textures, which consume a significant amount of GPU memory (remember, we must make all textures reside in GPU memory so that shaders can quickly access them). The advantage of using these formats is that they can be compressed and stored in GPU memory, which can be decompressed in real-time by the GPU when in use. Another benefit of storing textures as DDS compressed files is that it can reduce the usage of disk space.
What are the compressed texture formats
To alleviate GPU memory pressure, Direct3D provides the following compressed texture formats: BC1, BC2, BC3, BC4, BC5, BC6, and BC7.
1BC1 (DXGI-FORMAT-BC1UNORM): This format supports 3 color channels, with only 1 bit (on/off) representing the alpha component.
2BC2 (DXGI-FORMAT-BC2UNORM): This format supports 3 color channels and only uses 4 bits to represent the alpha component.
3BC3 (DXGI-FORMAT-BC3_UNORM): This format supports 3 color channels, representing the alpha component with 8 bits.
4BC4 (DXGI-FORMAT-BC4_UNORM): This format supports 1 color channel (for example, grayscale images).
5BC5 (DXGI-FORMAT-BC5-UNORM): This format supports two color channels.
6BC6 (DXGI-FORMAT-BC6-UF16): This format is used for compressed HDR (high dynamic range) image data.
7BC7 (DXGI-FORMAT-BC7UNORM): This format is used for high-quality RGBA compression. Especially, this format can significantly reduce errors caused by compressing normal maps.
For more information about these formats, readers can search for "Block Compression" in the index of the SDK documentation.
Attention:
-
Compressed textures can only be passed as input data to the shader stage of the rendering pipeline.
-
Because the block compression algorithm uses 4 × 4 pixel blocks, so the texture size must be a multiple of 4.
How to create compressed textures
(1) D3DX11Create ShaderResourceViewFromFile
We can use the pLoadInfo parameter of the D3DX11Create ShaderResourceViewFromFile function when loading textures, allowing Direct3D to convert the texture to some compressed format. For example, the following code loads a BMP file:
D3DX11_IMAGE_LOAD_INFO loadInfo;
loadInfo.Format = DXGI_FORMAT_BC3_UNORM;
HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice,
L"Textures/darkbrick.bmp", &loadInfo, 0, &mDiffuseMapSRV, 0 ));
//get 2 from resource view D texture
ID3D11Texture2D* tex;
mDiffuseMapSRV->GetResource((ID3D11Resource**)&tex);
//from 2 D texture acquisition texture description
D3D11_TEXTURE2D_DESC texDesc;
tex->GetDesc(&texDesc);
(2) Using DDS (DirectDraw Surface) format
Additionally, you can use DDS (DirectDraw Surface) format to directly store compressed textures. The operation steps are to run the DirectX Texture Tool (DXTex.exe) in the SDK directory D: Microsoft DirectX SDK (June 2010) Utilities Bin x86, and open your image file. Then execute the menu command Menu> Format> Change Surface Format, select DXT1, DXT2, DXT3, DXT4, or DXT5, and save the DDS file. These formats are actually compressed texture formats for Direct3D 9, where DXT1 is equivalent to BC1, DXT2 and DXT3 are equivalent to BC2, and DXT4 and DXT5 are equivalent to BC3. For example, when we load a DDS file in DXT1 format using the D3DX11Create ShaderResourceViewFromFile function, its actual texture format is DXGI_ FORMAT_ BC1_ UNORM:
HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice,
L"Textures/darkbrick.dds", 0, 0, &mDiffuseMapSRV, 0 ));
//get 2 from resource view D texture
ID3D11Texture2D* tex;
mDiffuseMapSRV->GetResource((ID3D11Resource **)&tex);
//from 2 D texture acquisition texture description
D3D11_TEXTURE2D_DESC texDesc;
tex->GetDesc(&texDesc);
Note that if the DDS file uses a certain compression format, we can set the pLoadInfo parameter to a null value, and D3DX11Create ShaderResourceViewFromFile will automatically use the compression format specified by the file.
(3) Other methods For BC4 and BC5 formats, you can use NVIDIA Texture Tools( http://code.google.com/p/nvidia-texture-tools/ ). For the BC6 and BC7 formats, the DirectX SDK includes an example called "BC6HBC7EncoderDecoder11". This program can be used to convert textures to BC6 or BC7 formats. This example contains the complete source code, so you can integrate it into your own material pipeline. Moreover, if your graphics card supports computational shaders, this example will also use GPU for conversion work, which is much faster than conversion through CPU.
You can also use the DirectX texture tool to generate multi-level asymptotic texture layers (Menu> Format> Generate Mip Maps) and save them as DDS files. Through a method, multi-level asymptotic texture layers can be pre calculated and saved in files, saving computation time during loading (they only need to be loaded).
4. Debugging and checking the compressed texture format
The following figure a shows the texDesc value seen in the debugger; It includes our specified compressed texture format, which uses the compressed format DXGI_ FORMAT_ BC3_ UNORM creates textures. When the parameter pLoadInfo is set to a null value, Direct3D will use the source image format (as shown in Figure b), which is the uncompressed format DXGI_ FORMAT_ R8G8B8A8_ UNORM
.