I need to read back a GPU texture via a staging ID3D11Texture, running in a Microsoft Store application.
I am reading from a D3D11ShaderResourceView defined in the cResource variable below. Code follows:
ID3D11Texture2D* pTextureInterface = 0;
ID3D11Resource* res;
HRESULT hr = 0;
ID3D11Texture2D* tx = 0;
ID3D11Device* dv;
deviceContext->GetDevice(&dv);
cResource->GetResource(&res);
hr = res->QueryInterface<ID3D11Texture2D>(&pTextureInterface);
D3D11_TEXTURE2D_DESC desc;
pTextureInterface->GetDesc(&desc);
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
hr = dv->CreateTexture2D(&desc, nullptr, &tx);
deviceContext->CopyResource(tx, pTextureInterface);
D3D11_MAPPED_SUBRESOURCE mapped = { 0 };
hr = deviceContext->Map(tx, 0, D3D11_MAP_READ, 0, &mapped);
...
The Map() function fails with an E_INVALIDARG.
I turned on Debug Layers and there are no specific Warnings nor Errors in the VS Window for all these code.
If I also try to save this texture using the DirectX::SaveWICTextureToFile function in the https://github.com/microsoft/DirectXTex/blob/5f22c7cc39241515f026269014678c157dc3dd9b/ScreenGrab/ScreenGrab11.cpp sample, inside this function the Map() function fails as well without any warnings nor Errors.
The GetDesc() function gives me a valid D3D11_TEXTURE2D_DESC for that texture.
What could be wrong?
Thanks!