Direct3D11截图崩溃

时间:2017-09-13 13:50:57

标签: c++ direct3d direct3d11

我试图获得Direct3D11应用程序的截图(每1秒,不保存)。代码在我的PC(英特尔CPU,Radeon GPU)上运行良好,但在其他2个(英特尔CPU +英特尔集成GPU,英特尔CPU + Nvidia GPU)上进行了几次迭代后崩溃。

void extractBitmap(void* texture) {

    if (texture) {
        ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture;
        ID3D11Texture2D* pNewTexture = NULL;

        D3D11_TEXTURE2D_DESC desc;
        d3dtex->GetDesc(&desc);

        desc.BindFlags = 0;
        desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
        desc.Usage = D3D11_USAGE_STAGING;
        desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;

        HRESULT hRes = D3D11Device->CreateTexture2D(&desc, NULL, &pNewTexture);

        if (FAILED(hRes)) {
            printCon(std::string("CreateTexture2D FAILED:" + format_error(hRes)).c_str());
            if (hRes == DXGI_ERROR_DEVICE_REMOVED)
                printCon(std::string("DXGI_ERROR_DEVICE_REMOVED -- " + format_error(D3D11Device->GetDeviceRemovedReason())).c_str());
        }
        else {
            if (pNewTexture) {
                D3D11DeviceContext->CopyResource(pNewTexture, d3dtex);

                // Wokring with texture

                pNewTexture->Release();
            }
        }
    }
    return;
}


D3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pBackBuffer));
extractBitmap(pBackBuffer);
pBackBuffer->Release();

崩溃日志:

CreateTexture2D FAILED:887a0005
DXGI_ERROR_DEVICE_REMOVED -- 887a0020

我发表评论后D3D11DeviceContext->CopyResource(pNewTexture, d3dtex);代码在所有3台PC上运行良好。

1 个答案:

答案 0 :(得分:0)

得到了它。我在单独的线程中运行我的代码。在我将它移到hooked Present()之后,应用程序没有崩溃。