Reputation: 127
I am facing problems creating DirectX texture from I420 data. My program crashes when I try to add texture. I am working on windows 8.0 on a WinRT metro app. Can you help me please? My code is as follows:
D3D11_TEXTURE2D_DESC texDesc = {};
texDesc.Width = 352;
texDesc.Height = 288;
texDesc.MipLevels = 1;
byte *bitmapData;
int datasize = read_whole_file_into_array(&bitmapData , "1.i420"); //bitmapData contains the I420 frame
D3D11_SUBRESOURCE_DATA frameData;
frameData.pSysMem = bitmapData;
frameData.SysMemSlicePitch = 0;
//frameData.SysMemPitch = texDesc.Width; //Unsure about it
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_420_OPAQUE;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.BindFlags = D3D11_BIND_DECODER;
texDesc.MiscFlags = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.CPUAccessFlags = 0;
m_d3dDevice->CreateTexture2D (&texDesc, &frameData, &m_background);
m_spriteBatch->AddTexture(m_background.Get());
Please help. Thanks in advance.
Additional Information: This MSDN link contains a similar problem, however in my case I already have a byte array containing the frame. I already asked a similar question to that forum.
Upvotes: 2
Views: 2581
Reputation: 8953
As per documentation here
Applications cannot use the CPU to map the resource and then access the data within the resource. You cannot use shaders with this format.
When you create your Texture, if you need to have access to it in shader, you also need to set the flag:
texDesc.BindFlags = D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE;
Then when you create textures, make sure you check result to see if texture is created:
HRESULT hr = m_d3dDevice->CreateTexture2D (&texDesc, &frameData, &m_background);
In that case it would warn you that texture can't be created (and with debug layer on you have this message):
D3D11 ERROR: ID3D11Device::CreateTexture2D: The format (0x6a, 420_OPAQUE) cannot be bound as a ShaderResource or cast to a format that could be bound as a ShaderResource. Therefore this format cannot support D3D11_BIND_SHADER_RESOURCE. [ STATE_CREATION ERROR #92: CREATETEXTURE2D_UNSUPPORTEDFORMAT]
D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]
Upvotes: 2