From fc09bc28c19fcdfc781f9177399673c3f220476d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?furby=E2=84=A2?= Date: Mon, 9 Sep 2024 03:03:13 -0600 Subject: [PATCH] Add @dgovil support for iOS and visionOS imaging. * Also setup CI workflows to build and bundle on iOS and visionOS. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: furby™ --- .github/workflows/swift-ios.yml | 16 ++++++ .github/workflows/swift-visionos.yml | 16 ++++++ Package.swift | 20 +++++-- Sources/Garch/glPlatformContextDarwin.mm | 45 ++++++++++------ Sources/Garch/glPlatformDebugWindowDarwin.mm | 52 +++++++++++-------- Sources/HgiInterop/hgiInterop.mm | 18 ++++--- Sources/HgiMetal/blitCmds.mm | 21 +++++++- Sources/HgiMetal/capabilities.mm | 32 ++++++++++-- Sources/HgiMetal/computeCmds.mm | 2 + Sources/HgiMetal/conversions.mm | 9 ++++ Sources/HgiMetal/graphicsCmds.mm | 7 ++- Sources/HgiMetal/hgi.mm | 3 +- Sources/HgiMetal/include/HgiMetal/blitCmds.h | 1 + .../HgiMetal/include/HgiMetal/capabilities.h | 1 + .../include/HgiMetal/indirectCommandEncoder.h | 3 ++ .../include/HgiMetal/resourceBindings.h | 1 + Sources/HgiMetal/indirectCommandEncoder.mm | 17 +++++- Sources/HgiMetal/shaderFunction.mm | 2 +- Sources/HgiMetal/texture.mm | 8 +++ 19 files changed, 215 insertions(+), 59 deletions(-) create mode 100644 .github/workflows/swift-ios.yml create mode 100644 .github/workflows/swift-visionos.yml diff --git a/.github/workflows/swift-ios.yml b/.github/workflows/swift-ios.yml new file mode 100644 index 000000000..682a235de --- /dev/null +++ b/.github/workflows/swift-ios.yml @@ -0,0 +1,16 @@ +name: Swift (iOS 18) + +on: [push] + +jobs: + build: + name: Swift ${{ matrix.swift }} on iOS + strategy: + matrix: + os: [self-hosted] + swift: ["5.10"] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - name: 📦 Build + run: swift bundler bundle -c release -p iOSSimulator UsdView diff --git a/.github/workflows/swift-visionos.yml b/.github/workflows/swift-visionos.yml new file mode 100644 index 000000000..89018cb09 --- /dev/null +++ b/.github/workflows/swift-visionos.yml @@ -0,0 +1,16 @@ +name: Swift (visionOS 2) + +on: [push] + +jobs: + build: + name: Swift ${{ matrix.swift }} on visionOS + strategy: + matrix: + os: [self-hosted] + swift: ["5.10"] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - name: 📦 Build + run: swift bundler bundle -c release -p visionOSSimulator UsdView diff --git a/Package.swift b/Package.swift index e823e027a..b1e6e19eb 100644 --- a/Package.swift +++ b/Package.swift @@ -1158,6 +1158,7 @@ let package = Package( .target(name: "Tf"), ], cxxSettings: [ + .define("PXR_GL_SUPPORT_ENABLED", .when(platforms: Arch.OS.noembeddedapple.platform)), .define("MFB_PACKAGE_NAME", to: "Garch"), .define("MFB_ALT_PACKAGE_NAME", to: "Garch"), .define("MFB_PACKAGE_MODULE", to: "Garch"), @@ -1166,6 +1167,7 @@ let package = Package( ], linkerSettings: [ .linkedFramework("OpenGL", .when(platforms: [.macOS])), + .linkedFramework("UIKit", .when(platforms: Arch.OS.embeddedapple.platform)), .linkedLibrary("glut", .when(platforms: Arch.OS.linux.platform)), .linkedLibrary("GL", .when(platforms: Arch.OS.linux.platform)), .linkedLibrary("GLU", .when(platforms: Arch.OS.linux.platform)), @@ -1221,8 +1223,10 @@ let package = Package( .define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])), ], linkerSettings: [ + .linkedFramework("Foundation", .when(platforms: Arch.OS.apple.platform)), .linkedFramework("Metal", .when(platforms: Arch.OS.apple.platform)), .linkedFramework("AppKit", .when(platforms: [.macOS])), + .linkedFramework("UIKit", .when(platforms: Arch.OS.embeddedapple.platform)), ] ), @@ -1280,7 +1284,7 @@ let package = Package( .target(name: "Hgi"), .target(name: "HgiMetal", condition: .when(platforms: Arch.OS.apple.platform)), // .target(name: "HgiVulkan", condition: .when(platforms: Arch.OS.linux.platform)), - .target(name: "HgiGL"), + .target(name: "HgiGL", condition: .when(platforms: Arch.OS.noembeddedapple.platform)), ], cxxSettings: [ .define("MFB_PACKAGE_NAME", to: "HgiInterop"), @@ -1293,6 +1297,7 @@ let package = Package( .define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])), ], linkerSettings: [ + .linkedFramework("Foundation", .when(platforms: Arch.OS.apple.platform)), .linkedFramework("CoreVideo", .when(platforms: Arch.OS.apple.platform)) ] ), @@ -1345,6 +1350,9 @@ let package = Package( .define("MFB_PACKAGE_MODULE", to: "Glf"), .define("GLF_EXPORTS", to: "1"), .define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])), + ], + linkerSettings: [ + .linkedFramework("OpenGL", .when(platforms: [.macOS])), ] ), @@ -1421,7 +1429,7 @@ let package = Package( .target(name: "Glf"), .target(name: "Hd"), .target(name: "HdSi"), - .target(name: "HgiGL"), + .target(name: "HgiGL", condition: .when(platforms: Arch.OS.noembeddedapple.platform)), .target(name: "HgiInterop"), .target(name: "Sdr"), .target(name: "Arch"), @@ -1736,7 +1744,7 @@ let package = Package( .target(name: "Hgi"), .target(name: "HgiMetal", condition: .when(platforms: Arch.OS.apple.platform)), // .target(name: "HgiVulkan", condition: .when(platforms: Arch.OS.linux.platform)), - .target(name: "HgiGL"), + .target(name: "HgiGL", condition: .when(platforms: Arch.OS.noembeddedapple.platform)), .target(name: "HgiInterop"), .target(name: "Hio"), .target(name: "PxOsd"), @@ -1810,6 +1818,10 @@ enum Arch case applewindows /// everything not apple (linux + windows). case linwin + /// only embedded apple devices. + case embeddedapple + /// everything not embedded apple devices. + case noembeddedapple public var platform: [Platform] { @@ -1822,6 +1834,8 @@ enum Arch case .nix: [.macOS, .iOS, .visionOS, .tvOS, .watchOS, .linux, .android, .openbsd] case .applewindows: [.macOS, .iOS, .visionOS, .tvOS, .watchOS, .windows] case .linwin: [.linux, .android, .openbsd, .windows] + case .embeddedapple: [.iOS, .visionOS, .tvOS, .watchOS] + case .noembeddedapple: [.macOS, .linux, .android, .openbsd, .windows] } } diff --git a/Sources/Garch/glPlatformContextDarwin.mm b/Sources/Garch/glPlatformContextDarwin.mm index 5b7ed1a60..eb5d580b4 100644 --- a/Sources/Garch/glPlatformContextDarwin.mm +++ b/Sources/Garch/glPlatformContextDarwin.mm @@ -7,21 +7,20 @@ #include "Arch/defines.h" #if defined(__APPLE__) -# import -# if defined(ARCH_OS_IPHONE) -# import -# else // !defined(ARCH_OS_IPHONE) -# import -# endif // defined(ARCH_OS_IPHONE) - # include "pxr/pxrns.h" # include "Garch/GarchDarwin/glPlatformContextDarwin.h" - -# ifdef ARCH_OS_IPHONE -typedef EAGLContext NSGLContext; -# else +# import +# if defined(PXR_GL_SUPPORT_ENABLED) +# if defined(ARCH_OS_OSX) +# import typedef NSOpenGLContext NSGLContext; -# endif +# elif defined(ARCH_OS_IPHONE) +# import +typedef EAGLContext NSGLContext; +# endif // defined(ARCH_OS_IPHONE) +# else // !defined(PXR_GL_SUPPORT_ENABLED) +typedef void* NSGLContext; +# endif // defined(PXR_GL_SUPPORT_ENABLED) PXR_NAMESPACE_OPEN_SCOPE @@ -29,7 +28,11 @@ { public: Detail() { +#if defined(PXR_GL_SUPPORT_ENABLED) context = [NSGLContext currentContext]; +#else + context = nil; +#endif } Detail(NullState) { context = nil; @@ -80,18 +83,26 @@ void GarchNSGLContextState::MakeCurrent() { -#if ARCH_OS_IPHONE +#if defined(PXR_GL_SUPPORT_ENABLED) +# if defined(ARCH_OS_IPHONE) [EAGLContext setCurrentContext:_detail->context]; -#else +# else // !defined(ARCH_OS_IPHONE) [_detail->context makeCurrentContext]; -#endif +# endif // defined(ARCH_OS_IPHONE) +#endif // defined(PXR_GL_SUPPORT_ENABLED) } /// Make no context current. void GarchNSGLContextState::DoneCurrent() { +#if defined(PXR_GL_SUPPORT_ENABLED) +# if defined(ARCH_OS_IPHONE) + [EAGLContext setCurrentContext:nil]; +# else // !defined(ARCH_OS_IPHONE) [NSGLContext clearCurrentContext]; +# endif // defined(ARCH_OS_IPHONE) +#endif } GarchGLPlatformContextState @@ -103,6 +114,7 @@ void * GarchSelectCoreProfileMacVisual() { +#if defined(ARCH_OS_OSX) NSOpenGLPixelFormatAttribute attribs[10]; int c = 0; @@ -112,6 +124,9 @@ attribs[c++] = 0; return (__bridge void*)[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs]; +#else // !defined(ARCH_OS_OSX) + return NULL; +#endif // defined(ARCH_OS_OSX) } PXR_NAMESPACE_CLOSE_SCOPE diff --git a/Sources/Garch/glPlatformDebugWindowDarwin.mm b/Sources/Garch/glPlatformDebugWindowDarwin.mm index 1c212d3d3..5bc6610a2 100644 --- a/Sources/Garch/glPlatformDebugWindowDarwin.mm +++ b/Sources/Garch/glPlatformDebugWindowDarwin.mm @@ -1,29 +1,12 @@ // // Copyright 2016 Pixar // -// Licensed under the Apache License, Version 2.0 (the "Apache License") -// with the following modification; you may not use this file except in -// compliance with the Apache License and the following modification to it: -// Section 6. Trademarks. is deleted and replaced with: -// -// 6. Trademarks. This License does not grant permission to use the trade -// names, trademarks, service marks, or product names of the Licensor -// and its affiliates, except as required to comply with Section 4(c) of -// the License and to reproduce the content of the NOTICE file. -// -// You may obtain a copy of the Apache License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the Apache License with the above modification is -// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the Apache License for the specific -// language governing permissions and limitations under the Apache License. +// Licensed under the terms set forth in the LICENSE.txt file available at +// https://openusd.org/license. // #include "Arch/defines.h" -#if defined(__APPLE__) && defined(ARCH_OS_OSX) +#if defined(__APPLE__) /* ----------------------------------------------------------------- * to reduce the need to explicitly exclude platform specific source * from SwiftPM, we will just guard the compilation by its platform, @@ -34,12 +17,16 @@ # include "Garch/glDebugWindow.h" # include -# import -# import -# import +# if defined(ARCH_OS_OSX) +# import +# import +# import +# endif PXR_NAMESPACE_USING_DIRECTIVE +#if defined(ARCH_OS_OSX) + static int Garch_GetModifierKeys(NSUInteger flags) { @@ -249,6 +236,25 @@ - (void)keyDown:(NSEvent *)event [NSApp stop:nil]; } +#else // !defined(ARCH_OS_OSX) + +PXR_NAMESPACE_OPEN_SCOPE + +Garch_GLPlatformDebugWindow::Garch_GLPlatformDebugWindow(GarchGLDebugWindow *w) +{} + +void Garch_GLPlatformDebugWindow::Init(const char *title, int width, int height, int nSamples) +{} + +void +Garch_GLPlatformDebugWindow::Run() +{} + +void +Garch_GLPlatformDebugWindow::ExitApp() +{} +#endif // defined(ARCH_OS_OSX) + PXR_NAMESPACE_CLOSE_SCOPE #endif /* defined(__APPLE__) && defined(ARCH_OS_OSX) */ diff --git a/Sources/HgiInterop/hgiInterop.mm b/Sources/HgiInterop/hgiInterop.mm index 7ea52b139..334bdbd8e 100644 --- a/Sources/HgiInterop/hgiInterop.mm +++ b/Sources/HgiInterop/hgiInterop.mm @@ -24,22 +24,26 @@ #endif #if defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED -# include "HgiInterop/metal.h" # include "HgiMetal/hgi.h" -#endif +# if defined(ARCH_OS_OSX) +# include "HgiInterop/metal.h" +# endif // defined(ARCH_OS_OSX) +#endif // defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED PXR_NAMESPACE_OPEN_SCOPE struct HgiInteropImpl { #if defined(PXR_GL_SUPPORT_ENABLED) std::unique_ptr _openGLToOpenGL; -#endif +#endif // defined(PXR_GL_SUPPORT_ENABLED) #if defined(PXR_VULKAN_SUPPORT_ENABLED) && PXR_VULKAN_SUPPORT_ENABLED std::unique_ptr _vulkanToOpenGL; -#endif +#endif // defined(PXR_VULKAN_SUPPORT_ENABLED) && PXR_VULKAN_SUPPORT_ENABLED #if defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED +# if !defined(ARCH_OS_IPHONE) std::unique_ptr _metalToOpenGL; -#endif +# endif // !defined(ARCH_OS_IPHONE) +#endif // defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED }; HgiInterop::HgiInterop() : _hgiInteropImpl(std::make_unique()) {} @@ -60,7 +64,7 @@ return; } -#if defined(PXR_GL_SUPPORT_ENABLED) +#if defined(PXR_GL_SUPPORT_ENABLED) && !defined(ARCH_OS_IPHONE) if (srcApi == HgiTokens->OpenGL) { // Transfer OpenGL textures to OpenGL application if (!_hgiInteropImpl->_openGLToOpenGL) { @@ -86,7 +90,7 @@ } #endif -#if defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED +#if (defined(PXR_METAL_SUPPORT_ENABLED) && PXR_METAL_SUPPORT_ENABLED) && !defined(ARCH_OS_IPHONE) if (srcApi == HgiTokens->Metal) { // Transfer Metal textures to OpenGL application // XXX: It's possible that if we use the same HgiInterop with a diff --git a/Sources/HgiMetal/blitCmds.mm b/Sources/HgiMetal/blitCmds.mm index 8d2f3b268..bb289b173 100644 --- a/Sources/HgiMetal/blitCmds.mm +++ b/Sources/HgiMetal/blitCmds.mm @@ -209,7 +209,7 @@ mtlDesc.mipmapLevelCount = dstTexDesc.mipLevels; mtlDesc.arrayLength = dstTexDesc.layerCount; - mtlDesc.resourceOptions = MTLResourceStorageModeManaged; + mtlDesc.resourceOptions = _hgi->GetCapabilities()->preferredStorageMode; mtlDesc.sampleCount = 1; if (dstTexDesc.type == HgiTextureType3D) { mtlDesc.depth = depth; @@ -389,10 +389,12 @@ _CreateEncoder(); +#if defined(ARCH_OS_OSX) if ([metalBuffer->GetBufferId() storageMode] == MTLStorageModeManaged) { [_blitEncoder performSelector:@selector(synchronizeResource:) withObject:metalBuffer->GetBufferId()]; } +#endif // defined(ARCH_OS_OSX) // Offset into the dst buffer char* dst = ((char*) copyOp.cpuDestinationBuffer) + @@ -470,13 +472,28 @@ return false; } +static const std::set unfilterableIosFormats = +{ + {MTLPixelFormatRGBA32Float} +}; + +bool +IsFilterable(MTLPixelFormat format) +{ +#if defined(ARCH_OS_IPHONE) + return unfilterableIosFormats.find(format) == unfilterableIosFormats.end(); +#else // !defined(ARCH_OS_IPHONE) + return true; +#endif // defined(ARCH_OS_IPHONE) +} + void HgiMetalBlitCmds::GenerateMipMaps(HgiTextureHandle const& texture) { HgiMetalTexture* metalTex = static_cast(texture.Get()); if (metalTex) { - if (_HgiTextureCanBeFiltered(metalTex->GetDescriptor())) { + if (_HgiTextureCanBeFiltered(metalTex->GetDescriptor()) && IsFilterable(metalTex->GetTextureId().pixelFormat)) { _CreateEncoder(); [_blitEncoder generateMipmapsForTexture:metalTex->GetTextureId()]; } diff --git a/Sources/HgiMetal/capabilities.mm b/Sources/HgiMetal/capabilities.mm index a3c864719..737ceeb6a 100644 --- a/Sources/HgiMetal/capabilities.mm +++ b/Sources/HgiMetal/capabilities.mm @@ -23,13 +23,22 @@ _SetFlag(HgiDeviceCapabilitiesBitsConcurrentDispatch, true); } - bool const hasIntelGPU = [device isLowPower]; - + bool hasIntelGPU = false; +#if defined(ARCH_OS_OSX) + hasIntelGPU = [device isLowPower]; + if (![device hasUnifiedMemory]) { + preferredStorageMode = MTLResourceStorageModeManaged; + } else +#endif + { + preferredStorageMode = MTLResourceStorageModeShared; + } defaultStorageMode = MTLResourceStorageModeShared; bool unifiedMemory = false; bool barycentrics = false; bool hasAppleSilicon = false; bool icbSupported = false; + bool hasIPhone = false; if (@available(macOS 100.100, ios 12.0, *)) { unifiedMemory = true; } else if (@available(macOS 10.15, ios 13.0, *)) { @@ -44,11 +53,15 @@ barycentrics = ([device supportsShaderBarycentricCoordinates] || [device areBarycentricCoordsSupported]) && !hasIntelGPU; - +#if defined(ARCH_OS_OSX) hasAppleSilicon = [device hasUnifiedMemory] && ![device isLowPower]; - +#endif // defined(ARCH_OS_OSX) } +#if defined(ARCH_OS_IPHONE) + hasIPhone = true; +#endif + if (hasAppleSilicon) { // Indirect command buffers supported only on // Apple Silicon GPUs with macOS 12.3 or later. @@ -60,6 +73,9 @@ // Indirect command buffers not currently supported on Intel GPUs. icbSupported = false; } + if (hasIPhone) { + icbSupported = false; + } if (!TfGetEnvSetting(HGIMETAL_ENABLE_INDIRECT_COMMAND_BUFFER)) { icbSupported = false; @@ -89,7 +105,7 @@ // if we are on MacOS 14 or less //bool isMacOs13OrLess = NSProcessInfo.processInfo.operatingSystemVersion.majorVersion <= 13 //bool requireBasePrimitiveOffset = hasAppleSilicon && isMacOs13OrLess; - bool requiresBasePrimitiveOffset = hasAppleSilicon || hasIntelGPU; + bool requiresBasePrimitiveOffset = hasAppleSilicon || hasIntelGPU || hasIPhone; _SetFlag(HgiDeviceCapabilitiesBitsBasePrimitiveOffset, requiresBasePrimitiveOffset); @@ -98,9 +114,15 @@ _SetFlag(HgiDeviceCapabilitiesBitsPrimitiveIdEmulation, true); } + if (hasIPhone && !barycentrics) { + _SetFlag(HgiDeviceCapabilitiesBitsPrimitiveIdEmulation, true); + } + +#if defined(ARCH_OS_OSX) if (!unifiedMemory) { defaultStorageMode = MTLResourceStorageModeManaged; } +#endif // defined(ARCH_OS_OSX) _maxUniformBlockSize = 64 * 1024; _maxShaderStorageBlockSize = 1 * 1024 * 1024 * 1024; diff --git a/Sources/HgiMetal/computeCmds.mm b/Sources/HgiMetal/computeCmds.mm index d30c779cb..da3ae27a6 100644 --- a/Sources/HgiMetal/computeCmds.mm +++ b/Sources/HgiMetal/computeCmds.mm @@ -120,6 +120,7 @@ thread_height = maxTotalThreads / thread_width; } +#if defined(ARCH_OS_OSX) if (_argumentBuffer.storageMode != MTLStorageModeShared && [_argumentBuffer respondsToSelector:@selector(didModifyRange:)]) { NSRange range = NSMakeRange(0, _argumentBuffer.length); @@ -129,6 +130,7 @@ [_argumentBuffer didModifyRange:range]; ARCH_PRAGMA_POP } +#endif // defined(ARCH_OS_OSX) [_encoder dispatchThreads:MTLSizeMake(dimX, dimY, 1) threadsPerThreadgroup:MTLSizeMake(MIN(thread_width, dimX), diff --git a/Sources/HgiMetal/conversions.mm b/Sources/HgiMetal/conversions.mm index 16985e13c..d17a9246b 100644 --- a/Sources/HgiMetal/conversions.mm +++ b/Sources/HgiMetal/conversions.mm @@ -55,12 +55,21 @@ //MTLPixelFormatRGB8Unorm_sRGB, // Unsupported by HgiFormat MTLPixelFormatRGBA8Unorm_sRGB, // HgiFormatUNorm8Vec4srgb, +#if defined(ARCH_OS_IPHONE) + MTLPixelFormatInvalid, // Unsupported by iOS Metal + MTLPixelFormatInvalid, // Unsupported by iOS Metal + MTLPixelFormatInvalid, // Unsupported by iOS Metal + MTLPixelFormatInvalid, // Unsupported by iOS Metal + MTLPixelFormatInvalid, // Unsupported by iOS Metal + MTLPixelFormatInvalid, // Unsupported by iOS Metal +#else // !defined(ARCH_OS_IPHONE) MTLPixelFormatBC6H_RGBFloat, // HgiFormatBC6FloatVec3 MTLPixelFormatBC6H_RGBUfloat, // HgiFormatBC6UFloatVec3 MTLPixelFormatBC7_RGBAUnorm, // HgiFormatBC7UNorm8Vec4 MTLPixelFormatBC7_RGBAUnorm_sRGB, // HgiFormatBC7UNorm8Vec4srgb MTLPixelFormatBC1_RGBA, // HgiFormatBC1UNorm8Vec4 MTLPixelFormatBC3_RGBA, // HgiFormatBC3UNorm8Vec4 +#endif // defined(ARCH_OS_IPHONE) MTLPixelFormatDepth32Float_Stencil8, // HgiFormatFloat32UInt8 diff --git a/Sources/HgiMetal/graphicsCmds.mm b/Sources/HgiMetal/graphicsCmds.mm index 5c5374aa5..25d323467 100644 --- a/Sources/HgiMetal/graphicsCmds.mm +++ b/Sources/HgiMetal/graphicsCmds.mm @@ -396,6 +396,7 @@ HgiMetalGraphicsCmds::_SyncArgumentBuffer() { if (_argumentBuffer) { +#if defined(ARCH_OS_OSX) if (_argumentBuffer.storageMode != MTLStorageModeShared && [_argumentBuffer respondsToSelector:@selector(didModifyRange:)]) { @@ -404,6 +405,7 @@ [_argumentBuffer didModifyRange:{0, _argumentBuffer.length}]; ARCH_PRAGMA_POP } +#endif // defined(ARCH_OS_OSX) _argumentBuffer = nil; } } @@ -811,7 +813,9 @@ // Apple Silicon only support memory barriers between vertex stages after // macOS 12.3. - if (@available(macOS 12.3, *)) { + // For iOS we may want to introduce an alternative path. +#if defined(ARCH_OS_OSX) + if (@available(macOS 12.3, ios 16.0, *)) { MTLBarrierScope scope = MTLBarrierScopeBuffers; MTLRenderStages srcStages = MTLRenderStageVertex; MTLRenderStages dstStages = MTLRenderStageVertex; @@ -822,6 +826,7 @@ beforeStages:dstStages]; } } +#endif // defined(ARCH_OS_OSX) } static diff --git a/Sources/HgiMetal/hgi.mm b/Sources/HgiMetal/hgi.mm index 19d903a0b..18ee7f9a1 100644 --- a/Sources/HgiMetal/hgi.mm +++ b/Sources/HgiMetal/hgi.mm @@ -69,6 +69,7 @@ void Drain() {} , _pool(std::make_unique()) { if (!_device) { +#if defined(ARCH_OS_OSX) if( TfGetenvBool("HGIMETAL_USE_INTEGRATED_GPU", false)) { auto devices = MTLCopyAllDevices(); for (id d in devices) { @@ -78,7 +79,7 @@ void Drain() {} } } } - +#endif // defined(ARCH_OS_OSX) if (!_device) { _device = MTLCreateSystemDefaultDevice(); } diff --git a/Sources/HgiMetal/include/HgiMetal/blitCmds.h b/Sources/HgiMetal/include/HgiMetal/blitCmds.h index 2b8039cba..3a1cd0f86 100644 --- a/Sources/HgiMetal/include/HgiMetal/blitCmds.h +++ b/Sources/HgiMetal/include/HgiMetal/blitCmds.h @@ -11,6 +11,7 @@ #include "HgiMetal/api.h" #include "pxr/pxrns.h" +#include #include PXR_NAMESPACE_OPEN_SCOPE diff --git a/Sources/HgiMetal/include/HgiMetal/capabilities.h b/Sources/HgiMetal/include/HgiMetal/capabilities.h index 9782e5ba4..a87d91383 100644 --- a/Sources/HgiMetal/include/HgiMetal/capabilities.h +++ b/Sources/HgiMetal/include/HgiMetal/capabilities.h @@ -32,6 +32,7 @@ class HgiMetalCapabilities final : public HgiCapabilities { int GetShaderVersion() const override; MTLResourceOptions defaultStorageMode; + MTLResourceOptions preferredStorageMode; bool hasVertexMemoryBarrier; bool useParallelEncoder; bool requiresIndirectDrawFix; diff --git a/Sources/HgiMetal/include/HgiMetal/indirectCommandEncoder.h b/Sources/HgiMetal/include/HgiMetal/indirectCommandEncoder.h index 4b6566395..f5f9329f2 100644 --- a/Sources/HgiMetal/include/HgiMetal/indirectCommandEncoder.h +++ b/Sources/HgiMetal/include/HgiMetal/indirectCommandEncoder.h @@ -36,6 +36,9 @@ class HgiMetalIndirectCommandEncoder final : public HgiIndirectCommandEncoder { HGIMETAL_API HgiMetalIndirectCommandEncoder(Hgi *hgi); + HGIMETAL_API + ~HgiMetalIndirectCommandEncoder() override; + HGIMETAL_API HgiIndirectCommandsUniquePtr EncodeDraw(HgiComputeCmds *computeCmds, HgiGraphicsPipelineHandle const &pipeline, diff --git a/Sources/HgiMetal/include/HgiMetal/resourceBindings.h b/Sources/HgiMetal/include/HgiMetal/resourceBindings.h index f472caa42..60930d43b 100644 --- a/Sources/HgiMetal/include/HgiMetal/resourceBindings.h +++ b/Sources/HgiMetal/include/HgiMetal/resourceBindings.h @@ -11,6 +11,7 @@ #include "HgiMetal/api.h" #include "pxr/pxrns.h" +#include #include PXR_NAMESPACE_OPEN_SCOPE diff --git a/Sources/HgiMetal/indirectCommandEncoder.mm b/Sources/HgiMetal/indirectCommandEncoder.mm index 001f69aa4..85a0d88e3 100644 --- a/Sources/HgiMetal/indirectCommandEncoder.mm +++ b/Sources/HgiMetal/indirectCommandEncoder.mm @@ -89,7 +89,9 @@ options:_bufferStorageMode]; if (_bufferStorageMode != MTLStorageModeShared && [_triangleTessFactors respondsToSelector:@selector(didModifyRange:)]) { +#if defined(ARCH_OS_OSX) [_triangleTessFactors didModifyRange:{0, _triangleTessFactors.length}]; +#endif // defined(ARCH_OS_OSX) } MTLQuadTessellationFactorsHalf quadFactors; @@ -106,10 +108,20 @@ options:_bufferStorageMode]; if (_bufferStorageMode != MTLStorageModeShared && [_quadTessFactors respondsToSelector:@selector(didModifyRange:)]) { +#if defined(ARCH_OS_OSX) [_quadTessFactors didModifyRange:{0, _quadTessFactors.length}]; +#endif // defined(ARCH_OS_OSX) } } +HgiMetalIndirectCommandEncoder::~HgiMetalIndirectCommandEncoder() +{ +#if !__has_feature(objc_arc) + [_triangleTessFactors release]; + [_quadTessFactors release]; +#endif // !__has_feature(objc_arc) +} + std::string _ArgId(ArgIndex index) { @@ -670,8 +682,10 @@ void _SetArg( if (_bufferStorageMode != MTLStorageModeShared && [commands->indirectArgumentBuffer respondsToSelector:@selector(didModifyRange:)]) { +#if defined(ARCH_OS_OSX) [commands->indirectArgumentBuffer didModifyRange:{0, commands->indirectArgumentBuffer.length}]; +#endif // defined(ARCH_OS_OSX) } // Set pipeline state on the encoder and dispatch to populate the ICB @@ -717,8 +731,9 @@ void _SetArg( // Ensure the the main argument buffer is updated on managed hardware. if (mainArgumentBuffer.storageMode != MTLStorageModeShared && [mainArgumentBuffer respondsToSelector:@selector(didModifyRange:)]) { - +#if defined(ARCH_OS_OSX) [mainArgumentBuffer didModifyRange:{0, mainArgumentBuffer.length}]; +#endif // defined(ARCH_OS_OSX) } id indirectCommandBuffer = diff --git a/Sources/HgiMetal/shaderFunction.mm b/Sources/HgiMetal/shaderFunction.mm index f06002636..17299f434 100644 --- a/Sources/HgiMetal/shaderFunction.mm +++ b/Sources/HgiMetal/shaderFunction.mm @@ -32,7 +32,7 @@ options.fastMathEnabled = YES; if (@available(macOS 10.15, ios 13.0, *)) { - options.languageVersion = MTLLanguageVersion2_2; + options.languageVersion = MTLLanguageVersion2_3; } else { options.languageVersion = MTLLanguageVersion2_1; } diff --git a/Sources/HgiMetal/texture.mm b/Sources/HgiMetal/texture.mm index 798042657..abfac681e 100644 --- a/Sources/HgiMetal/texture.mm +++ b/Sources/HgiMetal/texture.mm @@ -23,6 +23,10 @@ MTLResourceOptions resourceOptions = MTLResourceStorageModePrivate; MTLTextureUsage usage = MTLTextureUsageShaderRead; + if (desc.initialData && desc.pixelsByteSize > 0) { + resourceOptions = hgi->GetCapabilities()->preferredStorageMode; + } + MTLPixelFormat mtlFormat = HgiMetalConversions::GetPixelFormat( desc.format, desc.usage); @@ -95,7 +99,11 @@ // to our original, private texture. // Modify texture descriptor to describe the temp texture. +#if defined(ARCH_OS_OSX) texDesc.resourceOptions = MTLResourceStorageModeManaged; +#else // !defined(ARCH_OS_OSX) + texDesc.resourceOptions = MTLResourceStorageModeShared; +#endif // defined(ARCH_OS_OSX) texDesc.sampleCount = 1; if (desc.type == HgiTextureType3D) { texDesc.textureType = MTLTextureType3D;