How to save backbuffer to file in DirectX 10?

后端 未结 1 507
别那么骄傲
别那么骄傲 2020-12-08 12:03

I want to render a video frame-by-frame using DirectX 10. The frames would be processed later by some other tool like mencoder or ffmpeg.

I had no problems doing so

1条回答
  •  醉梦人生
    2020-12-08 12:52

    All the credit goes to sepul of gamedev.net.

    Now, the problems:

    • texDesc.CPUAccessFlags should be 0
    • texDesc.Format should be DXGI_FORMAT_R8G8B8A8_UNORM
    • texDesc.SampleDesc.Count should be 1
    • texDesc.SampleDesc.Quality should be 0
    • texDesc.Usage should be D3D10_USAGE_DEFAULT

    This way D3DX10SaveTextureToFile will save to BMP even to PNG.

    The complete code is:

    HRESULT hr;
    ID3D10Resource *backbufferRes;
    _defaultRenderTargetView->GetResource(&backbufferRes);
    
    D3D10_TEXTURE2D_DESC texDesc;
    texDesc.ArraySize = 1;
    texDesc.BindFlags = 0;
    texDesc.CPUAccessFlags = 0;
    texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    texDesc.Width = 640;  // must be same as backbuffer
    texDesc.Height = 480; // must be same as backbuffer
    texDesc.MipLevels = 1;
    texDesc.MiscFlags = 0;
    texDesc.SampleDesc.Count = 1;
    texDesc.SampleDesc.Quality = 0;
    texDesc.Usage = D3D10_USAGE_DEFAULT;
    
    ID3D10Texture2D *texture;
    V( _device->CreateTexture2D(&texDesc, 0, &texture) );
    _device->CopyResource(texture, backbufferRes);
    
    V( D3DX10SaveTextureToFile(texture, D3DX10_IFF_PNG, L"test.png") );
    texture->Release();
    backbufferRes->Release();
    

    0 讨论(0)
提交回复
热议问题