KaiserJohaan
KaiserJohaan

Reputation: 9240

Diffuse texture CreateTexture2D fails?

I have a 2d diffuse texture loaded into memory and want to create a dx11 texture from it. It does not need to be altered so I made it immutable.

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
    }

I don't see why it is causing a faulty result from the CreateTexture2D call. For example, I have a texture 128x128 R8B8G8A8 texture and it throws a bad result. Any ideas why?

Upvotes: 0

Views: 711

Answers (1)

rashmatash
rashmatash

Reputation: 1819

You should probably also set the value of SysMemPitch member of initData as explained here msdn: d3d11_subresource_data. Also keep in mind that CreateTexture2D expects a pointer to an array of D3D11_SUBRESOURCE_DATA (one for each mip level).

Upvotes: 1

Related Questions