如何在 DirectX 10 中将后台缓冲区保存到文件?
我想使用 DirectX 10 逐帧渲染视频。这些帧稍后将由 mencoder 或 ffmpeg 等其他工具进行处理。
我在 DX9 中使用 D3DXSaveSurfaceToFile
这样做没有任何问题。
现在,在 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 视图/编辑器打开。
我的代码有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有功劳都归功于 gamedev.net 的 sepul。
现在,问题是:
texDesc.CPUAccessFlags
应该是 0texDesc.Format
应该是DXGI_FORMAT_R8G8B8A8_UNORM
texDesc.SampleDesc.Count
应该是 1这样
D3DX10SaveTextureToFile
将保存BMP 甚至 PNG。完整的代码是:
All the credit goes to sepul of gamedev.net.
Now, the problems:
texDesc.CPUAccessFlags
should be 0texDesc.Format
should beDXGI_FORMAT_R8G8B8A8_UNORM
texDesc.SampleDesc.Count
should be 1texDesc.SampleDesc.Quality
should be 0texDesc.Usage
should beD3D10_USAGE_DEFAULT
This way
D3DX10SaveTextureToFile
will save to BMP even to PNG.The complete code is: