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();
}
ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageDescriptorType type)
: type(type) {
}
} // namespace dawn_native

View File

@ -46,15 +46,27 @@ namespace dawn_native { namespace d3d12 {
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,
const WGPUTextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey) {
Device* backendDevice = reinterpret_cast<Device*>(device);
const TextureDescriptor* backendDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor);
ExternalImageDescriptorDXGISharedHandle externalDescriptor = {};
externalDescriptor.cTextureDescriptor = descriptor;
TextureBase* texture =
backendDevice->WrapSharedHandle(backendDescriptor, sharedHandle, acquireMutexKey);
backendDevice->WrapSharedHandle(&externalDescriptor, sharedHandle, acquireMutexKey);
return reinterpret_cast<WGPUTexture>(texture);
}
}} // namespace dawn_native::d3d12

View File

@ -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;

View File

@ -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<ComPtr<IDXGIKeyedMutex>> CreateKeyedMutexForTexture(

View File

@ -280,13 +280,16 @@ namespace dawn_native { namespace d3d12 {
}
ResultOrError<TextureBase*> Texture::Create(Device* device,
const TextureDescriptor* descriptor,
const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey) {
const TextureDescriptor* textureDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor);
Ref<Texture> 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();
}

View File

@ -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<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor);
static ResultOrError<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor,
const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey);
Texture(Device* device,

View File

@ -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();

View File

@ -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<const TextureDescriptor*>(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;
}

View File

@ -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<Device*>(cDevice);
TextureBase* texture = device->CreateTextureWrappingIOSurface(
cDescriptor, cDescriptor->ioSurface, cDescriptor->plane);
return reinterpret_cast<WGPUTexture>(texture);
}
WGPUTexture WrapIOSurface(WGPUDevice cDevice,
const WGPUTextureDescriptor* cDescriptor,
IOSurfaceRef ioSurface,
uint32_t plane) {
Device* device = reinterpret_cast<Device*>(cDevice);
const TextureDescriptor* descriptor =
reinterpret_cast<const TextureDescriptor*>(cDescriptor);
TextureBase* texture = device->CreateTextureWrappingIOSurface(descriptor, ioSurface, plane);
ExternalImageDescriptorIOSurface descriptor = {};
descriptor.cTextureDescriptor = cDescriptor;
TextureBase* texture =
device->CreateTextureWrappingIOSurface(&descriptor, ioSurface, plane);
return reinterpret_cast<WGPUTexture>(texture);
}

View File

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

View File

@ -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<const TextureDescriptor*>(descriptor->cTextureDescriptor),
TextureState::OwnedInternal) {
MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor));
mtlDesc.storageMode = kIOSurfaceStorageMode;
mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc
iosurface:ioSurface

View File

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

View File

@ -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);

View File

@ -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) {
}

View File

@ -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,

View File

@ -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_

View File

@ -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,

View File

@ -23,26 +23,6 @@
#include <vector>
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);

View File

@ -102,9 +102,13 @@ namespace {
&sharedHandle);
ASSERT_EQ(hr, S_OK);
WGPUTexture texture = dawn_native::d3d12::WrapSharedHandle(
device.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(dawnDescriptor),
sharedHandle, 0);
dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc;
externDesc.cTextureDescriptor =
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
// 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<const WGPUTextureDescriptor*>(dawnDescriptor),
sharedHandle, 1);
dawn_native::d3d12::ExternalImageDescriptorDXGISharedHandle externDesc;
externDesc.cTextureDescriptor =
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);
*d3d11TextureOut = d3d11Texture.Detach();

View File

@ -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<const WGPUTextureDescriptor*>(descriptor), ioSurface,
plane);
dawn_native::metal::ExternalImageDescriptorIOSurface externDesc;
externDesc.cTextureDescriptor =
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);
}
};