如何在 DirectX 10 中将后台缓冲区保存到文件?

发布于 2024-10-01 05:36:56 字数 1053 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-10-08 05:36:57

所有功劳都归功于 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文