简体   繁体   English

Direct3D11屏幕截图崩溃

[英]Direct3D11 Screenshot crash

I'm trying to get, basically, screenshot (each 1 second, without saving) of Direct3D11 application. 我基本上想获取Direct3D11应用程序的屏幕截图(每秒钟1秒,无需保存)。 Code works fine on my PC(Intel CPU, Radeon GPU) but crashes after few iterations on 2 others (Intel CPU + Intel integrated GPU, Intel CPU + Nvidia GPU). 代码在我的PC(Intel CPU,Radeon GPU)上运行良好,但是在另外2个迭代(Intel CPU + Intel集成GPU,Intel 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();

Crash log: 崩溃日志:

CreateTexture2D FAILED:887a0005
DXGI_ERROR_DEVICE_REMOVED -- 887a0020

Once I comment out D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); 一旦我注释掉D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); code works fine on all 3 PC's. 代码在所有3台PC上都能正常工作。

Got it wokring. 知道了。 I was running my code in separate thread. 我在单独的线程中运行代码。 After I moved it to hooked Present(), application did not crash. 在将其移到挂钩的Present()之后,应用程序没有崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM