我想使用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图像。
我的代码出了什么问题?
答案 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();