如何在DirectX 10中保存backbuffer文件?

时间:2010-11-07 22:45:07

标签: screenshot directx-10

我想使用DirectX 10逐帧渲染视频。稍后将通过mencoder或ffmpeg等其他工具处理这些帧。

在使用D3DXSaveSurfaceToFile的DX9中我没有遇到任何问题。

现在,在DX10中我找到了D3DX10SaveTextureToFile,但没有运气用它来保存我的后备缓冲。

我使用以下代码:

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();

这会创建一个无法通过我所知的任何DDS视图/编辑器打开的.dds图像。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:12)

所有功劳都归功于gamedev.net的sepul。

现在,问题是:

  • texDesc.CPUAccessFlags应为0
  • texDesc.Format应为DXGI_FORMAT_R8G8B8A8_UNORM
  • texDesc.SampleDesc.Count应为1
  • texDesc.SampleDesc.Quality应为0
  • texDesc.Usage应为D3D10_USAGE_DEFAULT

这样D3DX10SaveTextureToFile将保存到BMP甚至PNG。

完整的代码是:

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();