From b2b892ae56a8cac1e4800661d7f652139b17be41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?furby=E2=84=A2?= Date: Fri, 14 Feb 2025 15:46:38 -0700 Subject: [PATCH] add visionOS metal implementation for usdview. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: furby™ --- Sources/UsdView/Hydra/Hydra+RenderEngine.swift | 13 +++++++++++-- Sources/UsdView/Hydra/Hydra+View.swift | 16 ++++++++-------- .../UsdView/Hydra/MTL/Hydra+MTLRenderer.swift | 8 ++++---- Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Sources/UsdView/Hydra/Hydra+RenderEngine.swift b/Sources/UsdView/Hydra/Hydra+RenderEngine.swift index 80dfb1bb9..9fe191cd6 100644 --- a/Sources/UsdView/Hydra/Hydra+RenderEngine.swift +++ b/Sources/UsdView/Hydra/Hydra+RenderEngine.swift @@ -13,10 +13,10 @@ import Foundation import PixarUSD -#if canImport(Metal) && !os(visionOS) +#if canImport(Metal) import Metal import MetalKit -#endif // canImport(Metal) && !os(visionOS) +#endif // canImport(Metal) public enum Hydra { @@ -24,7 +24,10 @@ public enum Hydra { public var stage: UsdStageRefPtr +#if canImport(Metal) private let hgi: Pixar.HgiMetalPtr +#endif // canImport(Metal) + private let engine: UsdImagingGL.EngineSharedPtr private var viewCamera: Hydra.Camera @@ -38,8 +41,12 @@ public enum Hydra { self.stage = stage +#if canImport(Metal) hgi = HgiMetal.createHgi() let driver = HdDriver(name: .renderDriver, driver: hgi.value) +#else // !canImport(Metal) + let driver = HdDriver() +#endif // canImport(Metal) engine = UsdImagingGL.Engine.createEngine( rootPath: stage.getPseudoRoot().getPath(), @@ -224,6 +231,7 @@ public enum Hydra return frustum } +#if canImport(Metal) public var hydraDevice: MTLDevice { hgi.device @@ -233,6 +241,7 @@ public enum Hydra { hgi } +#endif // canImport(Metal) public func getEngine() -> UsdImagingGL.EngineSharedPtr { diff --git a/Sources/UsdView/Hydra/Hydra+View.swift b/Sources/UsdView/Hydra/Hydra+View.swift index 7a7897aed..71b5d1c0a 100644 --- a/Sources/UsdView/Hydra/Hydra+View.swift +++ b/Sources/UsdView/Hydra/Hydra+View.swift @@ -13,29 +13,29 @@ import Foundation import PixarUSD -#if canImport(Metal) && !os(visionOS) +#if canImport(Metal) import Metal import MetalKit -#endif // canImport(Metal) && !os(visionOS) +#endif // canImport(Metal) public extension Hydra { - #if os(macOS) || os(iOS) + #if os(macOS) || os(iOS) || os(visionOS) typealias Viewport = Hydra.MTLView - #elseif os(visionOS) || os(tvOS) || os(watchOS) + #elseif os(tvOS) || os(watchOS) typealias Viewport = UIViewRepresentable #else struct Viewport {} - #endif // canImport(Metal) && !os(visionOS) + #endif // os(macOS) || os(iOS) || os(visionOS) } public extension Hydra.Viewport { init(engine: Hydra.RenderEngine) { - #if canImport(Metal) && !os(visionOS) - let renderer = Hydra.MTLRenderer(device: engine.hydraDevice, hydra: engine) + #if canImport(Metal) + let renderer = Hydra.MTLRenderer(hydra: engine) self.init(hydra: engine, renderer: renderer) - #endif // canImport(Metal) && !os(visionOS) + #endif // canImport(Metal) } } diff --git a/Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift b/Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift index 3e663ce9f..73551af5d 100644 --- a/Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift +++ b/Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift @@ -12,7 +12,7 @@ import Foundation import PixarUSD -#if canImport(Metal) && !os(visionOS) +#if canImport(Metal) import Metal import MetalKit @@ -26,9 +26,9 @@ import PixarUSD private var inFlightSemaphore = DispatchSemaphore(value: 1) - convenience init(device: MTLDevice, hydra: Hydra.RenderEngine) + convenience init(hydra: Hydra.RenderEngine) { - self.init(device: device)! + self.init(device: hydra.hydraDevice)! self.hydra = hydra } @@ -181,4 +181,4 @@ import PixarUSD } } } -#endif // canImport(Metal) && !os(visionOS) +#endif // canImport(Metal) diff --git a/Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift b/Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift index 25f87d799..543f3a304 100644 --- a/Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift +++ b/Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift @@ -17,7 +17,7 @@ import PixarUSD import Combine import SwiftUI - #if os(iOS) + #if os(iOS) || os(visionOS) import Metal import MetalKit