In my project, I have a hook to the DirectX12 Present
function, and I’m trying to take a screenshot using from SaveWICTextureToFile
ScreenGrab12.h. However, my game freezes, and the screenshot is not taken.
PS: DirectX::SaveWICTextureToFile
is not returning an error.
DEBUG_COUT << "[D3D12-SCREENSHOT] - Saving screenshot..." << "n";
// Get the device and command queue
ID3D12Device* pDevice = nullptr;
HRESULT hr = pSwapChain->GetDevice(__uuidof(ID3D12Device), (void**)&pDevice);
if (FAILED(hr)) {
std::cout << "[D3D12-SCREENSHOT] - Failed to get device" << std::endl;
return;
}
ID3D12CommandQueue* pCommandQueue = nullptr;
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
hr = pDevice->CreateCommandQueue(&queueDesc, __uuidof(ID3D12CommandQueue), (void**)&pCommandQueue);
if (FAILED(hr)) {
std::cout << "[D3D12-SCREENSHOT] - Failed to create command queue" << std::endl;
return;
}
// Obtain the back buffer
ID3D12Resource* pBackBuffer = nullptr;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D12Resource), (void**)&pBackBuffer);
if (FAILED(hr)) {
std::cout << "[D3D12-SCREENSHOT] - Failed to get back buffer" << std::endl;
pDevice->Release();
pCommandQueue->Release();
return;
}
HRESULT hSaveWICTextureToFile = DirectX::SaveWICTextureToFile(pCommandQueue, pBackBuffer, GUID_ContainerFormatPng, L"C:\FiveFPS-Screenshots\test.png");
if (FAILED(hr)) {
std::cout << "[D3D12-SCREENSHOT] - Failed to save texture to file" << std::hex << hSaveWICTextureToFile << std::endl;
pDevice->Release();
pCommandQueue->Release();
pBackBuffer->Release();
return;
}
pDevice->Release();
pCommandQueue->Release();
pBackBuffer->Release();
DEBUG_COUT << "[D3D12-SCREENSHOT] - Screenshot saved successfully" << "n";