Also use ExternalImageDescriptor in D3D12 and Metal Backends

Bug: chromium:1036080
Change-Id: I358b0441c168ca075b09ab028c2f6d9df2a44d59
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/16240
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Natasha Lee <natlee@microsoft.com>
This commit is contained in:
Natasha Lee 2020-03-02 22:27:46 +00:00 committed by Commit Bot service account
parent cb90835dd9
commit 6f92b9160b
20 changed files with 121 additions and 56 deletions

View File

@ -171,4 +171,8 @@ namespace dawn_native {
return GetProcMapNamesForTestingInternal(); return GetProcMapNamesForTestingInternal();
} }
ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageDescriptorType type)
: type(type) {
}
} // namespace dawn_native } // namespace dawn_native

View File

@ -46,15 +46,27 @@ namespace dawn_native { namespace d3d12 {
return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat()); return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat());
} }
ExternalImageDescriptorDXGISharedHandle::ExternalImageDescriptorDXGISharedHandle()
: ExternalImageDescriptor(ExternalImageDescriptorType::DXGISharedHandle) {
}
WGPUTexture WrapSharedHandle(WGPUDevice device,
const ExternalImageDescriptorDXGISharedHandle* descriptor) {
Device* backendDevice = reinterpret_cast<Device*>(device);
TextureBase* texture = backendDevice->WrapSharedHandle(descriptor, descriptor->sharedHandle,
descriptor->acquireMutexKey);
return reinterpret_cast<WGPUTexture>(texture);
}
WGPUTexture WrapSharedHandle(WGPUDevice device, WGPUTexture WrapSharedHandle(WGPUDevice device,
const WGPUTextureDescriptor* descriptor, const WGPUTextureDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
uint64_t acquireMutexKey) { uint64_t acquireMutexKey) {
Device* backendDevice = reinterpret_cast<Device*>(device); Device* backendDevice = reinterpret_cast<Device*>(device);
const TextureDescriptor* backendDescriptor = ExternalImageDescriptorDXGISharedHandle externalDescriptor = {};
reinterpret_cast<const TextureDescriptor*>(descriptor); externalDescriptor.cTextureDescriptor = descriptor;
TextureBase* texture = TextureBase* texture =
backendDevice->WrapSharedHandle(backendDescriptor, sharedHandle, acquireMutexKey); backendDevice->WrapSharedHandle(&externalDescriptor, sharedHandle, acquireMutexKey);
return reinterpret_cast<WGPUTexture>(texture); return reinterpret_cast<WGPUTexture>(texture);
} }
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -312,7 +312,7 @@ namespace dawn_native { namespace d3d12 {
initialUsage); initialUsage);
} }
TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor, TextureBase* Device::WrapSharedHandle(const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
uint64_t acquireMutexKey) { uint64_t acquireMutexKey) {
TextureBase* dawnTexture; TextureBase* dawnTexture;

View File

@ -98,7 +98,7 @@ namespace dawn_native { namespace d3d12 {
ShaderVisibleDescriptorAllocator* GetShaderVisibleDescriptorAllocator() const; ShaderVisibleDescriptorAllocator* GetShaderVisibleDescriptorAllocator() const;
TextureBase* WrapSharedHandle(const TextureDescriptor* descriptor, TextureBase* WrapSharedHandle(const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
uint64_t acquireMutexKey); uint64_t acquireMutexKey);
ResultOrError<ComPtr<IDXGIKeyedMutex>> CreateKeyedMutexForTexture( ResultOrError<ComPtr<IDXGIKeyedMutex>> CreateKeyedMutexForTexture(

View File

@ -280,13 +280,16 @@ namespace dawn_native { namespace d3d12 {
} }
ResultOrError<TextureBase*> Texture::Create(Device* device, ResultOrError<TextureBase*> Texture::Create(Device* device,
const TextureDescriptor* descriptor, const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
uint64_t acquireMutexKey) { uint64_t acquireMutexKey) {
const TextureDescriptor* textureDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor);
Ref<Texture> dawnTexture = Ref<Texture> dawnTexture =
AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal)); AcquireRef(new Texture(device, textureDescriptor, TextureState::OwnedExternal));
DAWN_TRY( DAWN_TRY(dawnTexture->InitializeAsExternalTexture(textureDescriptor, sharedHandle,
dawnTexture->InitializeAsExternalTexture(descriptor, sharedHandle, acquireMutexKey)); acquireMutexKey));
return dawnTexture.Detach(); return dawnTexture.Detach();
} }

View File

@ -18,6 +18,7 @@
#include "common/Serial.h" #include "common/Serial.h"
#include "dawn_native/Texture.h" #include "dawn_native/Texture.h"
#include "dawn_native/DawnNative.h"
#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h" #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
#include "dawn_native/d3d12/d3d12_platform.h" #include "dawn_native/d3d12/d3d12_platform.h"
@ -36,7 +37,7 @@ namespace dawn_native { namespace d3d12 {
static ResultOrError<TextureBase*> Create(Device* device, static ResultOrError<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor); const TextureDescriptor* descriptor);
static ResultOrError<TextureBase*> Create(Device* device, static ResultOrError<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor, const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
uint64_t acquireMutexKey); uint64_t acquireMutexKey);
Texture(Device* device, Texture(Device* device,

View File

@ -55,7 +55,7 @@ namespace dawn_native { namespace metal {
MapRequestTracker* GetMapTracker() const; MapRequestTracker* GetMapTracker() const;
TextureBase* CreateTextureWrappingIOSurface(const TextureDescriptor* descriptor, TextureBase* CreateTextureWrappingIOSurface(const ExternalImageDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane); uint32_t plane);
void WaitForCommandsToBeScheduled(); void WaitForCommandsToBeScheduled();

View File

@ -269,13 +269,16 @@ namespace dawn_native { namespace metal {
return {}; return {};
} }
TextureBase* Device::CreateTextureWrappingIOSurface(const TextureDescriptor* descriptor, TextureBase* Device::CreateTextureWrappingIOSurface(const ExternalImageDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane) { uint32_t plane) {
if (ConsumedError(ValidateTextureDescriptor(this, descriptor))) { const TextureDescriptor* textureDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor);
if (ConsumedError(ValidateTextureDescriptor(this, textureDescriptor))) {
return nullptr; return nullptr;
} }
if (ConsumedError(ValidateIOSurfaceCanBeWrapped(this, descriptor, ioSurface, plane))) { if (ConsumedError(
ValidateIOSurfaceCanBeWrapped(this, textureDescriptor, ioSurface, plane))) {
return nullptr; return nullptr;
} }

View File

@ -27,14 +27,27 @@ namespace dawn_native { namespace metal {
return device->GetMTLDevice(); return device->GetMTLDevice();
} }
ExternalImageDescriptorIOSurface::ExternalImageDescriptorIOSurface()
: ExternalImageDescriptor(ExternalImageDescriptorType::IOSurface) {
}
WGPUTexture WrapIOSurface(WGPUDevice cDevice,
const ExternalImageDescriptorIOSurface* cDescriptor) {
Device* device = reinterpret_cast<Device*>(cDevice);
TextureBase* texture = device->CreateTextureWrappingIOSurface(
cDescriptor, cDescriptor->ioSurface, cDescriptor->plane);
return reinterpret_cast<WGPUTexture>(texture);
}
WGPUTexture WrapIOSurface(WGPUDevice cDevice, WGPUTexture WrapIOSurface(WGPUDevice cDevice,
const WGPUTextureDescriptor* cDescriptor, const WGPUTextureDescriptor* cDescriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane) { uint32_t plane) {
Device* device = reinterpret_cast<Device*>(cDevice); Device* device = reinterpret_cast<Device*>(cDevice);
const TextureDescriptor* descriptor = ExternalImageDescriptorIOSurface descriptor = {};
reinterpret_cast<const TextureDescriptor*>(cDescriptor); descriptor.cTextureDescriptor = cDescriptor;
TextureBase* texture = device->CreateTextureWrappingIOSurface(descriptor, ioSurface, plane); TextureBase* texture =
device->CreateTextureWrappingIOSurface(&descriptor, ioSurface, plane);
return reinterpret_cast<WGPUTexture>(texture); return reinterpret_cast<WGPUTexture>(texture);
} }

View File

@ -19,6 +19,7 @@
#include <IOSurface/IOSurfaceRef.h> #include <IOSurface/IOSurfaceRef.h>
#import <Metal/Metal.h> #import <Metal/Metal.h>
#include "dawn_native/DawnNative.h"
namespace dawn_native { namespace metal { 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);
Texture(Device* device, const TextureDescriptor* descriptor, id<MTLTexture> mtlTexture); Texture(Device* device, const TextureDescriptor* descriptor, id<MTLTexture> mtlTexture);
Texture(Device* device, Texture(Device* device,
const TextureDescriptor* descriptor, const ExternalImageDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane); uint32_t plane);
~Texture(); ~Texture();

View File

@ -335,11 +335,14 @@ namespace dawn_native { namespace metal {
} }
Texture::Texture(Device* device, Texture::Texture(Device* device,
const TextureDescriptor* descriptor, const ExternalImageDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane) uint32_t plane)
: TextureBase(device, descriptor, TextureState::OwnedInternal) { : TextureBase(device,
MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(descriptor); reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor),
TextureState::OwnedInternal) {
MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor));
mtlDesc.storageMode = kIOSurfaceStorageMode; mtlDesc.storageMode = kIOSurfaceStorageMode;
mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc
iosurface:ioSurface iosurface:ioSurface

View File

@ -36,7 +36,6 @@ namespace dawn_native { namespace vulkan {
class Adapter; class Adapter;
class BufferUploader; class BufferUploader;
class DescriptorSetService; class DescriptorSetService;
struct ExternalImageDescriptor;
class FencedDeleter; class FencedDeleter;
class MapRequestTracker; class MapRequestTracker;
class RenderPassCache; class RenderPassCache;

View File

@ -26,7 +26,6 @@ namespace dawn_native { namespace vulkan {
struct CommandRecordingContext; struct CommandRecordingContext;
class Device; class Device;
struct ExternalImageDescriptor;
VkFormat VulkanImageFormat(const Device* device, wgpu::TextureFormat format); VkFormat VulkanImageFormat(const Device* device, wgpu::TextureFormat format);
VkImageUsageFlags VulkanImageUsage(wgpu::TextureUsage usage, const Format& format); VkImageUsageFlags VulkanImageUsage(wgpu::TextureUsage usage, const Format& format);

View File

@ -60,10 +60,6 @@ namespace dawn_native { namespace vulkan {
} }
#ifdef DAWN_PLATFORM_LINUX #ifdef DAWN_PLATFORM_LINUX
ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageDescriptorType type)
: type(type) {
}
ExternalImageDescriptorFD::ExternalImageDescriptorFD(ExternalImageDescriptorType type) ExternalImageDescriptorFD::ExternalImageDescriptorFD(ExternalImageDescriptorType type)
: ExternalImageDescriptor(type) { : ExternalImageDescriptor(type) {
} }

View File

@ -30,6 +30,18 @@ namespace dawn_native { namespace d3d12 {
DAWN_NATIVE_EXPORT WGPUTextureFormat DAWN_NATIVE_EXPORT WGPUTextureFormat
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain); 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. // Note: SharedHandle must be a handle to a texture object.
DAWN_NATIVE_EXPORT WGPUTexture WrapSharedHandle(WGPUDevice device, DAWN_NATIVE_EXPORT WGPUTexture WrapSharedHandle(WGPUDevice device,
const WGPUTextureDescriptor* descriptor, const WGPUTextureDescriptor* descriptor,

View File

@ -185,6 +185,24 @@ namespace dawn_native {
DAWN_NATIVE_EXPORT uint64_t AcquireErrorInjectorCallCount(); DAWN_NATIVE_EXPORT uint64_t AcquireErrorInjectorCallCount();
DAWN_NATIVE_EXPORT void InjectErrorAt(uint64_t index); 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 } // namespace dawn_native
#endif // DAWNNATIVE_DAWNNATIVE_H_ #endif // DAWNNATIVE_DAWNNATIVE_H_

View File

@ -33,6 +33,17 @@ typedef __IOSurface* IOSurfaceRef;
#endif //__OBJC__ #endif //__OBJC__
namespace dawn_native { namespace metal { 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, DAWN_NATIVE_EXPORT WGPUTexture WrapIOSurface(WGPUDevice device,
const WGPUTextureDescriptor* descriptor, const WGPUTextureDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,

View File

@ -23,26 +23,6 @@
#include <vector> #include <vector>
namespace dawn_native { namespace vulkan { 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 VkInstance GetInstance(WGPUDevice device);
DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName); DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName);

View File

@ -102,9 +102,13 @@ namespace {
&sharedHandle); &sharedHandle);
ASSERT_EQ(hr, S_OK); ASSERT_EQ(hr, S_OK);
WGPUTexture texture = dawn_native::d3d12::WrapSharedHandle( dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc;
device.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(dawnDescriptor), externDesc.cTextureDescriptor =
sharedHandle, 0); reinterpret_cast<const WGPUTextureDescriptor*>(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 // Now that we've created all of our resources, we can close the handle
// since we no longer need it. // since we no longer need it.
::CloseHandle(sharedHandle); ::CloseHandle(sharedHandle);
@ -329,9 +333,12 @@ class D3D12SharedHandleUsageTests : public D3D12ResourceTestBase {
hr = dxgiKeyedMutex->ReleaseSync(1); hr = dxgiKeyedMutex->ReleaseSync(1);
ASSERT_EQ(hr, S_OK); ASSERT_EQ(hr, S_OK);
WGPUTexture dawnTexture = dawn_native::d3d12::WrapSharedHandle( dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc;
device.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(dawnDescriptor), externDesc.cTextureDescriptor =
sharedHandle, 1); reinterpret_cast<const WGPUTextureDescriptor*>(dawnDescriptor);
externDesc.sharedHandle = sharedHandle;
externDesc.acquireMutexKey = 1;
WGPUTexture dawnTexture = dawn_native::d3d12::WrapSharedHandle(device.Get(), &externDesc);
*dawnTextureOut = wgpu::Texture::Acquire(dawnTexture); *dawnTextureOut = wgpu::Texture::Acquire(dawnTexture);
*d3d11TextureOut = d3d11Texture.Detach(); *d3d11TextureOut = d3d11Texture.Detach();

View File

@ -96,9 +96,12 @@ namespace {
wgpu::Texture WrapIOSurface(const wgpu::TextureDescriptor* descriptor, wgpu::Texture WrapIOSurface(const wgpu::TextureDescriptor* descriptor,
IOSurfaceRef ioSurface, IOSurfaceRef ioSurface,
uint32_t plane) { uint32_t plane) {
WGPUTexture texture = dawn_native::metal::WrapIOSurface( dawn_native::metal::ExternalImageDescriptorIOSurface externDesc;
device.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(descriptor), ioSurface, externDesc.cTextureDescriptor =
plane); reinterpret_cast<const WGPUTextureDescriptor*>(descriptor);
externDesc.ioSurface = ioSurface;
externDesc.plane = plane;
WGPUTexture texture = dawn_native::metal::WrapIOSurface(device.Get(), &externDesc);
return wgpu::Texture::Acquire(texture); return wgpu::Texture::Acquire(texture);
} }
}; };