漫反射纹理CreateTexture2D失败了吗?

时间:2014-07-19 18:17:36

标签: c++ directx directx-11

我有一个2d漫反射纹理加载到内存中,并希望从中创建一个dx11纹理。它不需要改变,所以我把它变成了不可变的。

DX11Texture::DX11Texture(ID3D11Device* device, const std::vector<uint8_t>& textureData, uint32_t textureWidth, uint32_t textureHeight, TextureType textureType, Logger& logger) :
        mTexture(nullptr), mTextureID(gNextTextureID++)
    {
        D3D11_TEXTURE2D_DESC textureDesc;
        ZeroMemory(&textureDesc, sizeof(D3D11_TEXTURE2D_DESC));
        textureDesc.Width = textureWidth;
        textureDesc.Height = textureHeight;
        textureDesc.MipLevels = 1;
        textureDesc.ArraySize = 1;
        textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        textureDesc.SampleDesc.Count = 1;
        textureDesc.Usage = D3D11_USAGE_IMMUTABLE;
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

        D3D11_SUBRESOURCE_DATA initData;
        ZeroMemory(&initData, sizeof(D3D11_SUBRESOURCE_DATA));
        initData.pSysMem = &textureData.at(0);

        DXCALL(device->CreateTexture2D(&textureDesc, &initData, &mTexture));     // throws E_INVALIDARG result
    }

我不明白为什么它会导致CreateTexture2D调用出错。例如,我有一个纹理128x128 R8B8G8A8纹理,它会导致糟糕的结果。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可能还应设置SysMemPitch initData成员的值,如此处msdn: d3d11_subresource_data所述。另请注意,CreateTexture2D需要一个指向D3D11_SUBRESOURCE_DATA数组的指针(每个mip级别一个)。