Search code examples
c++windowswinapitexturesdirectx-12

DX12 one process 2 devices can use the same texture created by one of the devices?


win32 app create 2 devices, device1 and device2(the same adapter). device1 creates a texture as "ComPtr<::ID3D12Resource> texture", then if device2 can use the texture created by device1? because the device1 and device2 are in the same process, so device2 has the way to access the "ComPtr<::ID3D12Resource> texture". ("ComPtr<::ID3D12Resource> texture" is not a shared texture) Thanks

I coded as above, i find it works. But i donot know if it works by using any other GPU.


Solution

  • You must create the resource using the flag D3D12_HEAP_FLAG_SHARED, otherwise, you cannot share the resource between different devices.

    You should not use the ID3D12Resource instance created by device 1 directly on device 2. Instead, use CreateSharedHandle on the resource created by device 1, and then call OpenSharedHandle on device 2 (using that handle) to create a second ID3D12Resource instance dedicated to device 2. After that, these two ID3D12Resource instances will point to the same resource on the GPU (this resource is now shared).

    If your devices are created on different adapters (different GPUs), you must use D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER instead of D3D12_HEAP_FLAG_SHARED.