Thomas Papamihos
Thomas Papamihos

Reputation: 47

Create 1D Texture in DirectX 11

I try to create the 1D Texture in DirectX 11 wih this code:

PARAMETER: ID3D11Device* pDevice

D3D11_TEXTURE1D_DESC text1_desc;

::ZeroMemory(&text1_desc, sizeof(D3D11_TEXTURE1D_DESC));

text1_desc.Width = 258

text1_desc.MipLevels = 2;

text1_desc.ArraySize = 2;

text1_desc.Usage = D3D11_USAGE_IMMUTABLE;

text1_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

text1_desc.Format = R8G8B8A8_UNORM;

FLOAT* pData = new FLOAT[text1_desc.MipLevels * text1_desc.ArraySize * text1_desc.Width];

D3D11_SUBRESOURCE_DATA sr_data;

::ZeroMemory(&sr_data, sizeof(D3D11_SUBRESOURCE_DATA));

sr_data.pSysMem = pData;

ID3D11Texture1D* pTexture1D = nullptr;

auto hr = pDevice->CreateTexture1D(&text1_desc, &sr_data, &pTexture1D);

When text1_desc.MipLevels = 1 and text1_desc.ArraySize = 1 everything is good.

When text1_desc.MipLevels = 0 or text1_desc.MipLevels > 1 it raises Unhandled exception at 0x000007FEE6D14CC0 (nvwgf2umx.dll): 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Can anyone help me to solve this problem?

Upvotes: 1

Views: 921

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41057

Mip-levels of '0' is a problem since it results in an allocation size of '0'. You need to figure out the number of mip-levels that will get generated for the given input width. So for 0, you need something like:

size_t mipLevels = 1;
size_t width = 258;
while ( width > 1 )
{
    if ( width > 1 )
        width >>= 1;
    ++mipLevels;
}

The second thing to note is that you have to pass an array of D3D11_SUBRESOURE_DATA instances, not just one, if you are creating a complex resource. There is one D3D11_SUBRESOURE_DATA per sub-resource, which needs to be mipLevels * text1_desc.ArraySize in length. You only ever allocate 1, which is why you get a runtime fault.

You should look at DirectXTex for code that works with a all kinds of Direct3D 11 textures.

Upvotes: 2

Related Questions