Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save backbuffer to file in DirectX 10?

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 in DX9 using D3DXSaveSurfaceToFile.

Now, in DX10 I've found D3DX10SaveTextureToFile, but had no luck using it to save my backbuffer.

I use the following code:

ID3D10Resource *backbufferRes;
_defaultRenderTargetView->GetResource(&backbufferRes);

D3D10_TEXTURE2D_DESC texDesc;
texDesc.ArraySize = 1;
texDesc.BindFlags = 0;
texDesc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
texDesc.Format = backbufferSurfDesc.Format;
texDesc.Height = backbufferSurfDesc.Height;
texDesc.Width = backbufferSurfDesc.Width;
texDesc.MipLevels = 1;
texDesc.MiscFlags = 0;
texDesc.SampleDesc = backbufferSurfDesc.SampleDesc;
texDesc.Usage = D3D10_USAGE_STAGING;

ID3D10Texture2D *texture;
HRESULT hr;
V( _device->CreateTexture2D(&texDesc, 0, &texture) );
_device->CopyResource(texture, backbufferRes);

V( D3DX10SaveTextureToFile(texture, D3DX10_IFF_DDS, filename) );
texture->Release();

This creates a .dds image that can not be opened by any sort of DDS view/editor I know of.

What's wrong with my code?

like image 731
shinjin Avatar asked Feb 03 '23 01:02

shinjin


1 Answers

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();
like image 134
shinjin Avatar answered Mar 29 '23 09:03

shinjin