The Road to Filling the Pit DirectX11 (II) Solution to Errors When Using DirectXTex to Read DDS Files

Recently, learning Direct3D requires loading a texture map into memory, but since the new Windows SDK does not include d3dx11. lib, development can only rely on third-party libraries.

After understanding that DirectXTex could handle this requirement, without hesitation, dependencies were introduced into the project. However, under the premise of normal code operation, texture maps could not be loaded into memory, which led to this article.

Purpose of this article:

(1) Learn to add additional references to your Direct3D project; .

(2) Analyze the reasons for errors when reading DDS files and resolve them .

Referencing the DirectXTex library

Follow the steps in the following diagram step by step, and there will be no problem:




Then introduce the header file where you need to use this library:

#include<DirectXTex.h>

Then you can happily write code and use the things inside. How to use it is not detailed here. I will provide an example code on GitHub here. In fact, this repository already has very detailed documentation that teaches us how to use it: DirectXTex GitHub address.

Example

Before using this library, I still used the d3dx11. lib library, but it requires the installation of the DirectX SDK and configuration path to be used, which is very inconvenient. The code for importing resources before was as follows:

D3DX11CreateShaderResourceViewFromFile(d3dDevice, filePath, 0, 0, &carBodyColorMap, 0);

After using DirectXTex, I need to read DDS resources into memory in the project, so after reading GitHub's documentation, the code looks like this:

wchar_t const* filename = L"filePath";
wchar_t ext[_MAX_EXT] = {};
_wsplitpath_s(filename, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT);
ScratchImage image;
LoadFromDDSFile(filename, DDS_FLAGS_NONE, nullptr, image);
HRESULT res = CreateShaderResourceView(d3dDevice, image.GetImages(), image.GetImageCount(), image.GetMetadata(), &carBodyColorMap);

The tutorial on GitHub will be very detailed, and it is highly recommended to read the documentation!

Reason for error

After successfully reading the DDS file, there were no rendering texture resources on the screen, and strangely, a black spot appeared


After checking the online information, there were no results, and suddenly I found that the console output an error message:

D3D11 ERROR: ID3D11Device::CreateTexture2D: A Texture2D created with the following Format (0x47, BC1_UNORM) experiences aligment restrictions on the dimensions of the Resource. The dimensions, which are (Width: 827, Height: 964), must be multiples of (Width: 4, Height: 4). [ STATE_CREATION ERROR #101: CREATETEXTURE2D_INVALIDDIMENSIONS]

The key lies in this sentence:

 which are (Width: 827, Height: 964), must be multiples of (Width: 4, Height: 4)

The width and height of the DDS image must be multiples of 4, so we only need to adjust the width and height of the image to meet the requirements! .