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.
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:
And the code which brings the problem:
It generates the following error when calling PSSetShaderResources(0, 2, pShaderResView);
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.