Creating a Texture2DArray and populate it with solid values

2k views Asked by At

I have some problems in creating and filling a Texture2DArray in DirectX11.

I want to render many quads onto the screen, the drawing is done by instancing, but every quad should get his own texture. So I tried to create a Texture Array and added a index value to the instances data format.

The problem is, when I try to create two different textures into the array, it fails when I try to call the function CreateTexture2D with the following error:

D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]

Here is the code which I use to generate the texture:

//Create array for two textures
const int size = 256 * 256 * 4;
unsigned char color[size*2];

//First Texture white
for (int i = 0; i < size; i++) {
    color[i] = 255;
}

//Second Texture black
for (int i = size; i < size*2; i++) {
    color[i] = 0;
}


//Creating Texture2D description
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
desc.Width = 256;
desc.Height = 256;
desc.MipLevels = 1;
desc.ArraySize = 2;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;

ID3D11Texture2D *pTexture;
D3D11_SUBRESOURCE_DATA texture[2];
//ZeroMemory(&texture, sizeof(D3D11_SUBRESOURCE_DATA));

//Defining the texture start points
texture[0].pSysMem = color;
texture[0].SysMemPitch = sizeof(unsigned char) * 4;
texture[0].SysMemSlicePitch = 0;
texture[1].pSysMem = &color[size];
texture[1].SysMemPitch = sizeof(unsigned char) * 4;
texture[1].SysMemSlicePitch = 0;
result = device->CreateTexture2D(&desc, texture, &pTexture);

I have no idea why the error states at array positions I have at no point defined, I have only a two sized texture array. Maybe I'm doing something wrong with the initialization or filling of the data.

I hope someone can help me.

2

There are 2 answers

0
Strece On

Ok I fixed something now the call of the function CreateTexture2D returns S_OK, but as always another problem occur. The function CreateShaderResourceView returns also S_OK, but the result is that the first index of my ShaderRessourceView array is valid and the second is corrupt.

Here the fixed code:

//Defining the texture start points
texture[0].pSysMem = black;
texture[0].SysMemPitch = sizeof(unsigned char) * 4;
texture[0].SysMemSlicePitch = 256 * 256 * 4;
texture[1].pSysMem = &black[size];
texture[1].SysMemPitch = sizeof(unsigned char) * 4;
texture[1].SysMemSlicePitch = 256*256 *4;
result = device->CreateTexture2D(&desc, texture, &pTexture);

And the code which brings the problem:

D3D11_SHADER_RESOURCE_VIEW_DESC srDesc;
srDesc.Format = desc.Format;
srDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
srDesc.Texture2DArray.MostDetailedMip = 0;
srDesc.Texture2DArray.MipLevels = 1;
srDesc.Texture2DArray.ArraySize = 2;
srDesc.Texture2DArray.FirstArraySlice = 0;

ID3D11ShaderResourceView *pShaderResView[2];
result = device->CreateShaderResourceView(pTexture, &srDesc, pShaderResView);

It generates the following error when calling PSSetShaderResources(0, 2, pShaderResView);

D3D11 CORRUPTION: ID3D11DeviceContext::PSSetShaderResources: Third parameter, array index 1 corrupt or unexpectedly NULL. [ MISCELLANEOUS CORRUPTION #15: CORRUPTED_PARAMETER3]

I hope I initialized the Texture2D and Subressource_Data correctly, I had some problems understand the SysMemPitch and SysMemSlicePitch.

But for now I have no idea why the second index is corrupted.

0
Adam Miles On

CreateShaderResourceView only initialises one ID3D11ShaderResourceView*, you shouldn't be giving it an array of two pointers.

You've created one texture of dimensions 256x256x2 and have created one SRV that views both slices. When you come to call PSSetShaderResources, you only need to set one SRV.

Remove the second SRV and only set one since the one you have already views both slices.