diff --git a/src/dawn_native/DawnNative.cpp b/src/dawn_native/DawnNative.cpp index b39b9f27cf..6e3272b5cd 100644 --- a/src/dawn_native/DawnNative.cpp +++ b/src/dawn_native/DawnNative.cpp @@ -171,4 +171,8 @@ namespace dawn_native { return GetProcMapNamesForTestingInternal(); } + ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageDescriptorType type) + : type(type) { + } + } // namespace dawn_native diff --git a/src/dawn_native/d3d12/D3D12Backend.cpp b/src/dawn_native/d3d12/D3D12Backend.cpp index 2db62da461..4e57b541ef 100644 --- a/src/dawn_native/d3d12/D3D12Backend.cpp +++ b/src/dawn_native/d3d12/D3D12Backend.cpp @@ -46,15 +46,27 @@ namespace dawn_native { namespace d3d12 { return static_cast(impl->GetPreferredFormat()); } + ExternalImageDescriptorDXGISharedHandle::ExternalImageDescriptorDXGISharedHandle() + : ExternalImageDescriptor(ExternalImageDescriptorType::DXGISharedHandle) { + } + + WGPUTexture WrapSharedHandle(WGPUDevice device, + const ExternalImageDescriptorDXGISharedHandle* descriptor) { + Device* backendDevice = reinterpret_cast(device); + TextureBase* texture = backendDevice->WrapSharedHandle(descriptor, descriptor->sharedHandle, + descriptor->acquireMutexKey); + return reinterpret_cast(texture); + } + WGPUTexture WrapSharedHandle(WGPUDevice device, const WGPUTextureDescriptor* descriptor, HANDLE sharedHandle, uint64_t acquireMutexKey) { Device* backendDevice = reinterpret_cast(device); - const TextureDescriptor* backendDescriptor = - reinterpret_cast(descriptor); + ExternalImageDescriptorDXGISharedHandle externalDescriptor = {}; + externalDescriptor.cTextureDescriptor = descriptor; TextureBase* texture = - backendDevice->WrapSharedHandle(backendDescriptor, sharedHandle, acquireMutexKey); + backendDevice->WrapSharedHandle(&externalDescriptor, sharedHandle, acquireMutexKey); return reinterpret_cast(texture); } }} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 085dffd5e3..5ec7537487 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -312,7 +312,7 @@ namespace dawn_native { namespace d3d12 { initialUsage); } - TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor, + TextureBase* Device::WrapSharedHandle(const ExternalImageDescriptor* descriptor, HANDLE sharedHandle, uint64_t acquireMutexKey) { TextureBase* dawnTexture; diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 4817102a11..311f150727 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -98,7 +98,7 @@ namespace dawn_native { namespace d3d12 { ShaderVisibleDescriptorAllocator* GetShaderVisibleDescriptorAllocator() const; - TextureBase* WrapSharedHandle(const TextureDescriptor* descriptor, + TextureBase* WrapSharedHandle(const ExternalImageDescriptor* descriptor, HANDLE sharedHandle, uint64_t acquireMutexKey); ResultOrError> CreateKeyedMutexForTexture( diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index fa1cc2c93e..01652324d1 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -280,13 +280,16 @@ namespace dawn_native { namespace d3d12 { } ResultOrError Texture::Create(Device* device, - const TextureDescriptor* descriptor, + const ExternalImageDescriptor* descriptor, HANDLE sharedHandle, uint64_t acquireMutexKey) { + const TextureDescriptor* textureDescriptor = + reinterpret_cast(descriptor->cTextureDescriptor); + Ref dawnTexture = - AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal)); - DAWN_TRY( - dawnTexture->InitializeAsExternalTexture(descriptor, sharedHandle, acquireMutexKey)); + AcquireRef(new Texture(device, textureDescriptor, TextureState::OwnedExternal)); + DAWN_TRY(dawnTexture->InitializeAsExternalTexture(textureDescriptor, sharedHandle, + acquireMutexKey)); return dawnTexture.Detach(); } diff --git a/src/dawn_native/d3d12/TextureD3D12.h b/src/dawn_native/d3d12/TextureD3D12.h index 332ab5a610..3bbbeb420c 100644 --- a/src/dawn_native/d3d12/TextureD3D12.h +++ b/src/dawn_native/d3d12/TextureD3D12.h @@ -18,6 +18,7 @@ #include "common/Serial.h" #include "dawn_native/Texture.h" +#include "dawn_native/DawnNative.h" #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h" #include "dawn_native/d3d12/d3d12_platform.h" @@ -36,7 +37,7 @@ namespace dawn_native { namespace d3d12 { static ResultOrError Create(Device* device, const TextureDescriptor* descriptor); static ResultOrError Create(Device* device, - const TextureDescriptor* descriptor, + const ExternalImageDescriptor* descriptor, HANDLE sharedHandle, uint64_t acquireMutexKey); Texture(Device* device, diff --git a/src/dawn_native/metal/DeviceMTL.h b/src/dawn_native/metal/DeviceMTL.h index d881ea0401..6fa5b72c63 100644 --- a/src/dawn_native/metal/DeviceMTL.h +++ b/src/dawn_native/metal/DeviceMTL.h @@ -55,7 +55,7 @@ namespace dawn_native { namespace metal { MapRequestTracker* GetMapTracker() const; - TextureBase* CreateTextureWrappingIOSurface(const TextureDescriptor* descriptor, + TextureBase* CreateTextureWrappingIOSurface(const ExternalImageDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane); void WaitForCommandsToBeScheduled(); diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index d449d4832b..8869b68579 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -269,13 +269,16 @@ namespace dawn_native { namespace metal { return {}; } - TextureBase* Device::CreateTextureWrappingIOSurface(const TextureDescriptor* descriptor, + TextureBase* Device::CreateTextureWrappingIOSurface(const ExternalImageDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane) { - if (ConsumedError(ValidateTextureDescriptor(this, descriptor))) { + const TextureDescriptor* textureDescriptor = + reinterpret_cast(descriptor->cTextureDescriptor); + if (ConsumedError(ValidateTextureDescriptor(this, textureDescriptor))) { return nullptr; } - if (ConsumedError(ValidateIOSurfaceCanBeWrapped(this, descriptor, ioSurface, plane))) { + if (ConsumedError( + ValidateIOSurfaceCanBeWrapped(this, textureDescriptor, ioSurface, plane))) { return nullptr; } diff --git a/src/dawn_native/metal/MetalBackend.mm b/src/dawn_native/metal/MetalBackend.mm index 22b583af18..ac65399fd9 100644 --- a/src/dawn_native/metal/MetalBackend.mm +++ b/src/dawn_native/metal/MetalBackend.mm @@ -27,14 +27,27 @@ namespace dawn_native { namespace metal { return device->GetMTLDevice(); } + ExternalImageDescriptorIOSurface::ExternalImageDescriptorIOSurface() + : ExternalImageDescriptor(ExternalImageDescriptorType::IOSurface) { + } + + WGPUTexture WrapIOSurface(WGPUDevice cDevice, + const ExternalImageDescriptorIOSurface* cDescriptor) { + Device* device = reinterpret_cast(cDevice); + TextureBase* texture = device->CreateTextureWrappingIOSurface( + cDescriptor, cDescriptor->ioSurface, cDescriptor->plane); + return reinterpret_cast(texture); + } + WGPUTexture WrapIOSurface(WGPUDevice cDevice, const WGPUTextureDescriptor* cDescriptor, IOSurfaceRef ioSurface, uint32_t plane) { Device* device = reinterpret_cast(cDevice); - const TextureDescriptor* descriptor = - reinterpret_cast(cDescriptor); - TextureBase* texture = device->CreateTextureWrappingIOSurface(descriptor, ioSurface, plane); + ExternalImageDescriptorIOSurface descriptor = {}; + descriptor.cTextureDescriptor = cDescriptor; + TextureBase* texture = + device->CreateTextureWrappingIOSurface(&descriptor, ioSurface, plane); return reinterpret_cast(texture); } diff --git a/src/dawn_native/metal/TextureMTL.h b/src/dawn_native/metal/TextureMTL.h index 192945907e..0b18089f2a 100644 --- a/src/dawn_native/metal/TextureMTL.h +++ b/src/dawn_native/metal/TextureMTL.h @@ -19,6 +19,7 @@ #include #import +#include "dawn_native/DawnNative.h" namespace dawn_native { namespace metal { @@ -35,7 +36,7 @@ namespace dawn_native { namespace metal { Texture(Device* device, const TextureDescriptor* descriptor); Texture(Device* device, const TextureDescriptor* descriptor, id mtlTexture); Texture(Device* device, - const TextureDescriptor* descriptor, + const ExternalImageDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane); ~Texture(); diff --git a/src/dawn_native/metal/TextureMTL.mm b/src/dawn_native/metal/TextureMTL.mm index 23ca25fc5b..fbaf1fcb74 100644 --- a/src/dawn_native/metal/TextureMTL.mm +++ b/src/dawn_native/metal/TextureMTL.mm @@ -335,11 +335,14 @@ namespace dawn_native { namespace metal { } Texture::Texture(Device* device, - const TextureDescriptor* descriptor, + const ExternalImageDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane) - : TextureBase(device, descriptor, TextureState::OwnedInternal) { - MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(descriptor); + : TextureBase(device, + reinterpret_cast(descriptor->cTextureDescriptor), + TextureState::OwnedInternal) { + MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor( + reinterpret_cast(descriptor->cTextureDescriptor)); mtlDesc.storageMode = kIOSurfaceStorageMode; mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc iosurface:ioSurface diff --git a/src/dawn_native/vulkan/DeviceVk.h b/src/dawn_native/vulkan/DeviceVk.h index 7175710566..a4445f26f7 100644 --- a/src/dawn_native/vulkan/DeviceVk.h +++ b/src/dawn_native/vulkan/DeviceVk.h @@ -36,7 +36,6 @@ namespace dawn_native { namespace vulkan { class Adapter; class BufferUploader; class DescriptorSetService; - struct ExternalImageDescriptor; class FencedDeleter; class MapRequestTracker; class RenderPassCache; diff --git a/src/dawn_native/vulkan/TextureVk.h b/src/dawn_native/vulkan/TextureVk.h index f904452614..2c898ffcc1 100644 --- a/src/dawn_native/vulkan/TextureVk.h +++ b/src/dawn_native/vulkan/TextureVk.h @@ -26,7 +26,6 @@ namespace dawn_native { namespace vulkan { struct CommandRecordingContext; class Device; - struct ExternalImageDescriptor; VkFormat VulkanImageFormat(const Device* device, wgpu::TextureFormat format); VkImageUsageFlags VulkanImageUsage(wgpu::TextureUsage usage, const Format& format); diff --git a/src/dawn_native/vulkan/VulkanBackend.cpp b/src/dawn_native/vulkan/VulkanBackend.cpp index 2a9361f409..f24d8ca75f 100644 --- a/src/dawn_native/vulkan/VulkanBackend.cpp +++ b/src/dawn_native/vulkan/VulkanBackend.cpp @@ -60,10 +60,6 @@ namespace dawn_native { namespace vulkan { } #ifdef DAWN_PLATFORM_LINUX - ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageDescriptorType type) - : type(type) { - } - ExternalImageDescriptorFD::ExternalImageDescriptorFD(ExternalImageDescriptorType type) : ExternalImageDescriptor(type) { } diff --git a/src/include/dawn_native/D3D12Backend.h b/src/include/dawn_native/D3D12Backend.h index de12d640fc..1506e91fcf 100644 --- a/src/include/dawn_native/D3D12Backend.h +++ b/src/include/dawn_native/D3D12Backend.h @@ -30,6 +30,18 @@ namespace dawn_native { namespace d3d12 { DAWN_NATIVE_EXPORT WGPUTextureFormat GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain); + struct DAWN_NATIVE_EXPORT ExternalImageDescriptorDXGISharedHandle : ExternalImageDescriptor { + public: + ExternalImageDescriptorDXGISharedHandle(); + + HANDLE sharedHandle; + uint64_t acquireMutexKey; + }; + + // Note: SharedHandle must be a handle to a texture object. + DAWN_NATIVE_EXPORT WGPUTexture + WrapSharedHandle(WGPUDevice device, const ExternalImageDescriptorDXGISharedHandle* descriptor); + // Note: SharedHandle must be a handle to a texture object. DAWN_NATIVE_EXPORT WGPUTexture WrapSharedHandle(WGPUDevice device, const WGPUTextureDescriptor* descriptor, diff --git a/src/include/dawn_native/DawnNative.h b/src/include/dawn_native/DawnNative.h index 0230cca982..0a6a2866c1 100644 --- a/src/include/dawn_native/DawnNative.h +++ b/src/include/dawn_native/DawnNative.h @@ -185,6 +185,24 @@ namespace dawn_native { DAWN_NATIVE_EXPORT uint64_t AcquireErrorInjectorCallCount(); DAWN_NATIVE_EXPORT void InjectErrorAt(uint64_t index); + // The different types of ExternalImageDescriptors + enum ExternalImageDescriptorType { + OpaqueFD, + DmaBuf, + IOSurface, + DXGISharedHandle, + }; + + // Common properties of external images + struct DAWN_NATIVE_EXPORT ExternalImageDescriptor { + public: + const ExternalImageDescriptorType type; + const WGPUTextureDescriptor* cTextureDescriptor; // Must match image creation params + bool isCleared; // Sets whether the texture will be cleared before use + + protected: + ExternalImageDescriptor(ExternalImageDescriptorType type); + }; } // namespace dawn_native #endif // DAWNNATIVE_DAWNNATIVE_H_ diff --git a/src/include/dawn_native/MetalBackend.h b/src/include/dawn_native/MetalBackend.h index 6e07c05824..7ed458e983 100644 --- a/src/include/dawn_native/MetalBackend.h +++ b/src/include/dawn_native/MetalBackend.h @@ -33,6 +33,17 @@ typedef __IOSurface* IOSurfaceRef; #endif //__OBJC__ namespace dawn_native { namespace metal { + struct DAWN_NATIVE_EXPORT ExternalImageDescriptorIOSurface : ExternalImageDescriptor { + public: + ExternalImageDescriptorIOSurface(); + + IOSurfaceRef ioSurface; + uint32_t plane; + }; + + DAWN_NATIVE_EXPORT WGPUTexture + WrapIOSurface(WGPUDevice device, const ExternalImageDescriptorIOSurface* descriptor); + DAWN_NATIVE_EXPORT WGPUTexture WrapIOSurface(WGPUDevice device, const WGPUTextureDescriptor* descriptor, IOSurfaceRef ioSurface, diff --git a/src/include/dawn_native/VulkanBackend.h b/src/include/dawn_native/VulkanBackend.h index 005a655ce7..b144e4ece7 100644 --- a/src/include/dawn_native/VulkanBackend.h +++ b/src/include/dawn_native/VulkanBackend.h @@ -23,26 +23,6 @@ #include namespace dawn_native { namespace vulkan { - - // The different types of ExternalImageDescriptors - enum ExternalImageDescriptorType { -#ifdef __linux__ - OpaqueFD, - DmaBuf, -#endif // __linux__ - }; - - // Common properties of external images - struct DAWN_NATIVE_EXPORT ExternalImageDescriptor { - public: - const ExternalImageDescriptorType type; // Must match the subclass - const WGPUTextureDescriptor* cTextureDescriptor; // Must match image creation params - bool isCleared; // Sets whether the texture will be cleared before use - - protected: - ExternalImageDescriptor(ExternalImageDescriptorType type); - }; - DAWN_NATIVE_EXPORT VkInstance GetInstance(WGPUDevice device); DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName); diff --git a/src/tests/end2end/D3D12ResourceWrappingTests.cpp b/src/tests/end2end/D3D12ResourceWrappingTests.cpp index cbc74136b9..4aced27815 100644 --- a/src/tests/end2end/D3D12ResourceWrappingTests.cpp +++ b/src/tests/end2end/D3D12ResourceWrappingTests.cpp @@ -102,9 +102,13 @@ namespace { &sharedHandle); ASSERT_EQ(hr, S_OK); - WGPUTexture texture = dawn_native::d3d12::WrapSharedHandle( - device.Get(), reinterpret_cast(dawnDescriptor), - sharedHandle, 0); + dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc; + externDesc.cTextureDescriptor = + reinterpret_cast(dawnDescriptor); + externDesc.sharedHandle = sharedHandle; + externDesc.acquireMutexKey = 0; + WGPUTexture texture = dawn_native::d3d12::WrapSharedHandle(device.Get(), &externDesc); + // Now that we've created all of our resources, we can close the handle // since we no longer need it. ::CloseHandle(sharedHandle); @@ -329,9 +333,12 @@ class D3D12SharedHandleUsageTests : public D3D12ResourceTestBase { hr = dxgiKeyedMutex->ReleaseSync(1); ASSERT_EQ(hr, S_OK); - WGPUTexture dawnTexture = dawn_native::d3d12::WrapSharedHandle( - device.Get(), reinterpret_cast(dawnDescriptor), - sharedHandle, 1); + dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc; + externDesc.cTextureDescriptor = + reinterpret_cast(dawnDescriptor); + externDesc.sharedHandle = sharedHandle; + externDesc.acquireMutexKey = 1; + WGPUTexture dawnTexture = dawn_native::d3d12::WrapSharedHandle(device.Get(), &externDesc); *dawnTextureOut = wgpu::Texture::Acquire(dawnTexture); *d3d11TextureOut = d3d11Texture.Detach(); diff --git a/src/tests/end2end/IOSurfaceWrappingTests.cpp b/src/tests/end2end/IOSurfaceWrappingTests.cpp index cc0b2036c3..1119309862 100644 --- a/src/tests/end2end/IOSurfaceWrappingTests.cpp +++ b/src/tests/end2end/IOSurfaceWrappingTests.cpp @@ -96,9 +96,12 @@ namespace { wgpu::Texture WrapIOSurface(const wgpu::TextureDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane) { - WGPUTexture texture = dawn_native::metal::WrapIOSurface( - device.Get(), reinterpret_cast(descriptor), ioSurface, - plane); + dawn_native::metal::ExternalImageDescriptorIOSurface externDesc; + externDesc.cTextureDescriptor = + reinterpret_cast(descriptor); + externDesc.ioSurface = ioSurface; + externDesc.plane = plane; + WGPUTexture texture = dawn_native::metal::WrapIOSurface(device.Get(), &externDesc); return wgpu::Texture::Acquire(texture); } };