wgpu icon indicating copy to clipboard operation
wgpu copied to clipboard

Vulkan: create texture from d3d11 shared handle

Open xiaopengli89 opened this issue 1 year ago • 5 comments

Description VK_KHR_external_memory_win32 extension enables application to import Vulkan memory objects from Windows handles.

Testing

  1. Create D3D11 texture with D3D11_RESOURCE_MISC_SHARED_NTHANDLE and D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX flags
  2. Create shared handle with IDXGIResource1::CreateSharedHandle
  3. Create vulkan texture with texture_from_d3d11_shared_handle

Checklist

  • [x] Run cargo fmt.
  • [x] Run cargo clippy. If applicable, add:
    • [ ] --target wasm32-unknown-unknown
    • [ ] --target wasm32-unknown-emscripten
  • [x] Run cargo xtask test to run tests.
  • [ ] Add change to CHANGELOG.md. See simple instructions inside file.

xiaopengli89 avatar Aug 26 '24 07:08 xiaopengli89

@cwfitzgerald Does this PR need any other improvements? This feature is already working properly in our product.

xiaopengli89 avatar Oct 16 '24 13:10 xiaopengli89

@cwfitzgerald ☝🏻

ErichDonGubler avatar Oct 17 '24 16:10 ErichDonGubler

Hello. I've been working on sharing textures with wgpu using both Dx12 and Vulkan but without doing any changes to wgpu itself. My memory allocation code ended up looking very similar with important difference: vk::ExternalMemoryHandleTypeFlags::D3D12_RESOURCE instead of vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE. Other types could potentially be also usable for storing textures. If possible I would encourage to add it as a parameter next to d3d11_shared_handle.

In fact this new method is called texture_from_d3d11_shared_handle which makes my comment a little misplaced. Still, adding this parameter would make this code more usable.

Change to adapter to add external_memory_win32 capabilities is very nice - otherwise one has to extract raw adapter from wgpu, copy code from here, add required enabled_extensions to the list and then inject it back with adapter .create_device_from_hal which is a pain.

Other considerations: If I'm not wrong, NT handle holds the resource alive and it won't be cleaned up unless you call CloseHandle. I may be wrong here but I think Vulkan will NOT close the handle itself so user should do that manually. In my specific situation (sending textures between processes) I'm managing handles outside of wgpu - I remove injected shared textures from wgpu and it gets sent to different process. I think that this ownership situation should be reflected in documentation of this method (but please don't quote me on that, I'm not 100% sure how exactly this mechanism works after reading documentation and my limited tests).

Tsuguri avatar Oct 18 '24 12:10 Tsuguri

I too have the interop code here if that's helpful to anyone. One thing I had to do is copy the texture format (DXGI_FORMAT) conversion functions, it would be nice if they were exposed from wgpu

AdrianEddy avatar Oct 18 '24 17:10 AdrianEddy

Sorry my time basically disappeared suddenly - looking now.

cwfitzgerald avatar Oct 20 '24 23:10 cwfitzgerald

@Tsuguri VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT owns a reference to the memory used by the Direct3D resource, after importing the handle can be released safely.

xiaopengli89 avatar Oct 21 '24 06:10 xiaopengli89

@MarijnS95 Any other suggestions?

xiaopengli89 avatar Oct 21 '24 07:10 xiaopengli89

Can this PR be merged before #6465?

xiaopengli89 avatar Oct 29 '24 03:10 xiaopengli89