【问题标题】:Direct3D11: Sharing a texture between devices: black textureDirect3D11:在设备之间共享纹理:黑色纹理
【发布时间】:2017-01-13 00:06:27
【问题描述】:

我有两个 D3D11 设备,每个设备都有自己的上下文,但在同一个适配器上。

我试图在两者之间共享一个纹理,但我在另一侧收到的纹理始终是黑色的。

HRESULT hr;

// Make a shared texture on device_A / context_A
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = 1024;
desc.Height = 1024;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
ID3D11Texture2D* copy_tex;
hr = device_A->CreateTexture2D(&desc, NULL, &copy_tex);

// Test the texture by filling it with some color
D3D11_RENDER_TARGET_VIEW_DESC rtvd = {};
rtvd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
rtvd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvd.Texture2D.MipSlice = 0;
ID3D11RenderTargetView* copy_tex_view = 0;
hr = device_A->CreateRenderTargetView(copy_tex, &rtvd, &copy_tex_view);
FLOAT clear_color[4] = {1, 0, 0, 1};
context_A->ClearRenderTargetView(copy_tex_view, clear_color);

// Now try to share it to device_B:
IDXGIResource* copy_tex_resource = 0;
hr = copy_tex->QueryInterface( __uuidof(IDXGIResource), (void**)&copy_tex_resource );
HANDLE copy_tex_shared_handle = 0;
hr = copy_tex_resource->GetSharedHandle(&copy_tex_shared_handle);
IDXGIResource* copy_tex_resource_mirror = 0;
hr = device_B->OpenSharedResource(copy_tex_shared_handle, __uuidof(ID3D11Texture2D), (void**)&copy_tex_resource_mirror);
ID3D11Texture2D* copy_tex_mirror = 0;
hr = copy_tex_resource_mirror->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&copy_tex_mirror));

但是:copy_tex_mirror 纹理始终是黑色的。

我没有收到任何 HRESULT 错误代码,甚至可以在 device_B / context_B 上正常使用 copy_tex_mirror,但我无法在 device_A 上获取我放入其中的像素数据。

我错过了什么吗?

提前致谢!

【问题讨论】:

    标签: c++ direct3d direct3d11


    【解决方案1】:

    你怎么知道纹理总是黑色的? :-)

    GPU 操作由 Direct3D 排队,因此当您在 device_B 上打开共享资源时,device_A 上的 ClearRenderTargetView() 可能尚未执行。根据ID3D11Device::OpenSharedResource Method上的MSDN库文档:

    如果在一个设备上更新共享纹理ID3D11DeviceContext::Flush,则必须在该设备上调用。

    当我们在工作的设备之间实现共享纹理时,我们遇到了很多这样的问题。如果您将 D3D9 或 OpenGL 添加到混合中,则陷阱会成倍增加..

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多