From b495e484055d37106a1d137b371acc3b9a44e6bd Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 18 Sep 2019 04:32:52 +0000 Subject: [PATCH] Support compilation of Dawn on iOS. BUG=dawn:225 Change-Id: I618552c55c8472adc25625733b59d972d7297f57 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/11040 Reviewed-by: Kai Ninomiya Reviewed-by: Austin Eng Commit-Queue: Corentin Wallez --- src/common/Platform.h | 12 +++++++++ src/dawn_native/metal/BackendMTL.mm | 40 ++++++++++++++++++++++++----- src/dawn_native/metal/DeviceMTL.h | 3 ++- src/dawn_native/metal/DeviceMTL.mm | 9 ++++--- src/dawn_native/metal/TextureMTL.h | 1 + src/dawn_native/metal/TextureMTL.mm | 13 +++++++--- 6 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/common/Platform.h b/src/common/Platform.h index 3951362f44..f6d7fc5d05 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -17,15 +17,27 @@ #if defined(_WIN32) || defined(_WIN64) # define DAWN_PLATFORM_WINDOWS 1 + #elif defined(__linux__) # define DAWN_PLATFORM_LINUX 1 # define DAWN_PLATFORM_POSIX 1 + #elif defined(__APPLE__) # define DAWN_PLATFORM_APPLE 1 # define DAWN_PLATFORM_POSIX 1 +# include +# if TARGET_OS_IPHONE +# define DAWN_PLATFORM_IOS +# elif TARGET_OS_MAC +# define DAWN_PLATFORM_MACOS +# else +# error "Unsupported Apple platform." +# endif + #elif defined(__Fuchsia__) # define DAWN_PLATFORM_FUCHSIA 1 # define DAWN_PLATFORM_POSIX 1 + #else # error "Unsupported platform." #endif diff --git a/src/dawn_native/metal/BackendMTL.mm b/src/dawn_native/metal/BackendMTL.mm index 80287ddbb3..28565dd809 100644 --- a/src/dawn_native/metal/BackendMTL.mm +++ b/src/dawn_native/metal/BackendMTL.mm @@ -15,15 +15,20 @@ #include "dawn_native/metal/BackendMTL.h" #include "common/Constants.h" +#include "common/Platform.h" #include "dawn_native/Instance.h" #include "dawn_native/MetalBackend.h" #include "dawn_native/metal/DeviceMTL.h" -#import +#if defined(DAWN_PLATFORM_MACOS) +# import +#endif namespace dawn_native { namespace metal { namespace { + +#if defined(DAWN_PLATFORM_MACOS) struct PCIIDs { uint32_t vendorId; uint32_t deviceId; @@ -150,6 +155,18 @@ namespace dawn_native { namespace metal { NSOperatingSystemVersion macOS10_11 = {10, 11, 0}; return [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:macOS10_11]; } +#elif defined(DAWN_PLATFORM_IOS) + MaybeError GetDevicePCIInfo(id device, PCIIDs* ids) { + DAWN_UNUSED(device); + *ids = PCIIDs(0, 0); + } + + bool IsMetalSupported() { + return true; + } +#else +# error "Unsupported Apple platform." +#endif } // anonymous namespace // The Metal backend's Adapter. @@ -201,14 +218,23 @@ namespace dawn_native { namespace metal { } std::vector> Backend::DiscoverDefaultAdapters() { - NSArray>* devices = MTLCopyAllDevices(); - std::vector> adapters; - for (id device in devices) { - adapters.push_back(std::make_unique(GetInstance(), device)); - } - [devices release]; + if (@available(macOS 10.12, *)) { + NSArray>* devices = MTLCopyAllDevices(); + + for (id device in devices) { + adapters.push_back(std::make_unique(GetInstance(), device)); + } + + [devices release]; + } else if (@available(iOS 8.0, *)) { + // iOS only has a single device so MTLCopyAllDevices doesn't exist there. + adapters.push_back( + std::make_unique(GetInstance(), MTLCreateSystemDefaultDevice())); + } else { + UNREACHABLE(); + } return adapters; } diff --git a/src/dawn_native/metal/DeviceMTL.h b/src/dawn_native/metal/DeviceMTL.h index 20ab9e9637..0174e4af9c 100644 --- a/src/dawn_native/metal/DeviceMTL.h +++ b/src/dawn_native/metal/DeviceMTL.h @@ -21,8 +21,9 @@ #include "dawn_native/Device.h" #include "dawn_native/metal/Forward.h" +#import #import -#import +#import #include #include diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index dda06915be..52fd864c1a 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -76,10 +76,13 @@ namespace dawn_native { namespace metal { } void Device::InitTogglesFromDriver() { + if (@available(macOS 10.12, *)) { + bool emulateStoreAndMSAAResolve = + ![mMtlDevice supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v2]; + SetToggle(Toggle::EmulateStoreAndMSAAResolve, emulateStoreAndMSAAResolve); + } + // TODO(jiawei.shao@intel.com): check iOS feature sets - bool emulateStoreAndMSAAResolve = - ![mMtlDevice supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v2]; - SetToggle(Toggle::EmulateStoreAndMSAAResolve, emulateStoreAndMSAAResolve); // TODO(jiawei.shao@intel.com): tighten this workaround when the driver bug is fixed. SetToggle(Toggle::AlwaysResolveIntoZeroLevelAndLayer, true); diff --git a/src/dawn_native/metal/TextureMTL.h b/src/dawn_native/metal/TextureMTL.h index d4feb5bfc4..13c30f8761 100644 --- a/src/dawn_native/metal/TextureMTL.h +++ b/src/dawn_native/metal/TextureMTL.h @@ -17,6 +17,7 @@ #include "dawn_native/Texture.h" +#include #import namespace dawn_native { namespace metal { diff --git a/src/dawn_native/metal/TextureMTL.mm b/src/dawn_native/metal/TextureMTL.mm index 8bbd28be82..ffe56a8185 100644 --- a/src/dawn_native/metal/TextureMTL.mm +++ b/src/dawn_native/metal/TextureMTL.mm @@ -14,10 +14,9 @@ #include "dawn_native/metal/TextureMTL.h" +#include "common/Platform.h" #include "dawn_native/metal/DeviceMTL.h" -#include - namespace dawn_native { namespace metal { namespace { @@ -121,6 +120,14 @@ namespace dawn_native { namespace metal { return DAWN_VALIDATION_ERROR("Unsupported IOSurface format"); } } + +#if defined(DAWN_PLATFORM_MACOS) + MTLStorageMode kIOSurfaceStorageMode = MTLStorageModeManaged; +#elif defined(DAWN_PLATFORM_IOS) + MTLStorageMode kIOSurfaceStorageMode = MTLStorageModePrivate; +#else +# error "Unsupported Apple platform." +#endif } MTLPixelFormat MetalPixelFormat(dawn::TextureFormat format) { @@ -322,7 +329,7 @@ namespace dawn_native { namespace metal { uint32_t plane) : TextureBase(device, descriptor, TextureState::OwnedInternal) { MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(descriptor); - mtlDesc.storageMode = MTLStorageModeManaged; + mtlDesc.storageMode = kIOSurfaceStorageMode; mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc iosurface:ioSurface plane:plane];