DirectX 11 framebuffer capture (C++, no Win32 or D3DX)

前端 未结 3 1866
一个人的身影
一个人的身影 2020-12-08 12:30

I would like to capture the contents of my front or back buffer using DirectX 11 into an array of bytes which I can then use as a texture or as a source for creating a file.

3条回答
  •  孤城傲影
    2020-12-08 12:51

    Swap chain buffers can be easily saved with D3D11 as shown below.

    1. Create a Texture2D as same as the swap chain's back buffer you are trying to save
    2. Call CopyResource on the device context to copy from back buffer to the newly created texture
    3. Call D3DX11SaveTextureToFile(...) with file name

    contrived code fragment:

    ID3D11Texture2D* pBuffer;
    
    swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBuffer);
    
    if(texture_to_save == NULL)
    {
        D3D11_TEXTURE2D_DESC td;
        pBuffer->GetDesc(&td);
        device->CreateTexture2D(&td, NULL, &texture_to_save);
    }
    
    deviceContext->CopyResource(texture_to_save, pBuffer);
    
    D3DX11SaveTextureToFile(deviceContext,texture_to_save,D3DX11_IFF_PNG,filename);
    

提交回复
热议问题