DirectX - 在不同线程之间共享资源句柄

时间:2018-05-30 07:52:59

标签: c++ windows multithreading directx dxgi

我创建了一个带有标志D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX的纹理,如文档中所述,以便在不同设备之间共享它(我有多个线程同意在创建后读取该资源)

D3D11_TEXTURE2D_DESC textureDesc;
textureDesc.Width = Width;
textureDesc.Height = Height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;

ID3D11Texture2D *returnTexture;
d3d11Device->CreateTexture2D(&textureDesc, NULL, &returnTexture);

IDXGIResource1 *sharedResource;
returnTexture->QueryInterface(__uuidof(IDXGIResource1), reinterpret_cast<void**>(&sharedResource));

HANDLE shareHandle;
sharedResource->CreateSharedHandle(nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr, &shareHandle);

// acquire the mutex and copy data into the texture from another one
// release the mutex

填充此纹理后(我获取其IDXGIKeyedMutex,将数据复制到其中然后释放互斥锁)我将shareHandle传递给多个线程。

每个线程可能偶尔需要从该纹理

传输CPU上的内存
ID3D11Device1 *d3d11Device1;
d3d11Device->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&d3d11Device1));

HANDLE shareHandle = getSharedHandleSomewhere();
IDXGIResource1 *sharedResource;
d3d11Device1->OpenSharedResource1(shareHandle, __uuidof(IDXGIResource1), reinterpret_cast<void**>(&sharedResource));

// acquire the mutex and write the contents into another staging texture
// release the mutex

CloseHandle(shareHandle);

所有HRESULT错误代码(出于清晰原因从上面的代码中排除)不会返回任何错误,但是在其中一个线程上OpenSharedResource1之后DXGI_ERROR_DEVICE_REMOVED失败了DuplicateHandle(和我#39;我确定不会删除任何物理监视器。)

这可能是什么原因?我也尝试用每个线程make_iter = model.get_operation_by_name("YOURPREFIX/MakeIterator") sess.run(make_iter, feed_dict) model.infer(some_numpy_array) 复制一个句柄,但是这也没有解决问题(句柄都是不透明的&#39;引用&#39;到同一资源我想的)。 可能是我只能打开共享资源一次而不是多次这样做?

0 个答案:

没有答案