D3DXCreateTextureFromFile 不创建纹理

D3DXCreateTextureFromFile does not create texture

D3DXCreateTextureFromFile 对我不起作用,当我使用 XorString 时出现 D3DXERR_INVALIDDATA 错误,而当我不使用 XorString 时出现 D3DERR_INVALIDCALL。

if (Globals::User::g_pAvatar == nullptr)
{
    auto s_avatarFile = Globals::g_sAppdataFolder + "\avatar.jpg";
    auto avatarFile = s_avatarFile.c_str();

    if (!fileExists(avatarFile))
    {
        download(Globals::User::g_sAvatarUrl.c_str(), avatarFile);
        D3DXCreateTextureFromFile(Globals::g_pD3DDevice, XorStr(avatarFile), Globals::User::g_pAvatar);
    }
    else if (fileExists(avatarFile)) {
        HRESULT hr = D3DXCreateTextureFromFile(Globals::g_pD3DDevice, XorStr(avatarFile), Globals::User::g_pAvatar);
        switch (hr) {
        case D3D_OK:                            
            break;
        case D3DERR_NOTAVAILABLE:
            MessageBox(0, "D3DERR_NOTAVAILABLE", "dx9", MB_OKCANCEL | MB_ICONHAND);
            break;
        case D3DERR_OUTOFVIDEOMEMORY:
            MessageBox(0, "D3DERR_OUTOFVIDEOMEMORY", "dx9", MB_OKCANCEL | MB_ICONHAND);
            break;
        case D3DERR_INVALIDCALL:
            MessageBox(0, "D3DERR_INVALIDCALL", "dx9", MB_OKCANCEL | MB_ICONHAND);
            break;
        case D3DXERR_INVALIDDATA:
            MessageBox(0, "D3DXERR_INVALIDDATA", "dx9", MB_OKCANCEL | MB_ICONHAND);
            break;
        case E_OUTOFMEMORY:
            MessageBox(0, "E_OUTOFMEMORY", "dx9", MB_OKCANCEL | MB_ICONHAND);
            break;
        }
    }
}

D3DX9 库非常古老,它使用文件扩展名来确定使用哪个编解码器来加载图像。既然你告诉它 jpg 它只会尝试使用 jpeglib 加载它。

D3DX10 和 D3DX11 库使用 Windows 成像组件(WIC) which relies on a file inspection technique, so it wouldn't care what the physical file-name is. You can find a modern DDSTextureLoader, WICTextureLoader, and ScreenGrab for legacy DX9 here 根本不需要 D3DX9。

D3DX9 包括 BMP、JPG、PNG、TGA、HDR 和 PPM/PFM 的编解码器。 D3DX10/D3DX11 通过 WIC 支持 BMP、GIF、JPG、PNG、TIFF 和 HDPhoto (JPEG-XR)。它们都包括对 DDS 的支持。

DirectXTex includes a codec for TGA and HDR because they are common source texture formats, but are not supported by any built-in WIC codec. The DirectXTex implementation of HDR recognizes both ?RADIANCE and ?RGBE headers, but D3DX9 only supported ?RADIANCE.

Note that D3DX9, D3DX10, and D3DX11 are all deprecated (see Microsoft Docs) along with the DirectX SDK itself.