I trying to take a screenshot using DirectX, and then save it to file. But when the below code executes, I get a totally black .png sized(width/height) as target DirectX app. What am I doing wrong?
HRESULT hr = S_OK;
ID3D11Texture2D* pBuffer;
ID3D11Texture2D* pBackBufferStaging = NULL;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBuffer);
D3D11_TEXTURE2D_DESC td;
pBuffer->GetDesc(&td);
td.Usage = D3D11_USAGE_STAGING;
td.BindFlags = 0;
td.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
td.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
td.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
pDevice->CreateTexture2D(&td, NULL, &pBackBufferStaging);
pContext->CopyResource(pBackBufferStaging, pBuffer);
D3DX11SaveTextureToFile(pContext, pBackBufferStaging, D3DX11_IFF_PNG, L"D:\\screen.png");
pBackBufferStaging->Release();
pBuffer->Release();
pSwapChain
is pointer to IDXGISwapChain
, pDevice
is pointer to ID3D11Device
, and pContext
is pointer to ID3D11DeviceContext
. They are all setup correctly but the resulting screenshot is still black.
Update #1
Error checking shows nothing. All functions are successfully executed. While in DirectX 11 "Hello world", everything works fine. What am I doing wrong? I am trying to screen shot a World of Tanks game.
Update #2
On DirectX 9, my approach works fine - Heartstone produces working screenshot.