Skip to content

Commit

Permalink
add visionOS metal implementation for usdview.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <devs@wabi.foundation>
  • Loading branch information
furby-tm committed Feb 14, 2025
1 parent a87847a commit b2b892a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
13 changes: 11 additions & 2 deletions Sources/UsdView/Hydra/Hydra+RenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
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
{
public class RenderEngine
{
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

Expand All @@ -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(),
Expand Down Expand Up @@ -224,6 +231,7 @@ public enum Hydra
return frustum
}

#if canImport(Metal)
public var hydraDevice: MTLDevice
{
hgi.device
Expand All @@ -233,6 +241,7 @@ public enum Hydra
{
hgi
}
#endif // canImport(Metal)

public func getEngine() -> UsdImagingGL.EngineSharedPtr
{
Expand Down
16 changes: 8 additions & 8 deletions Sources/UsdView/Hydra/Hydra+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 4 additions & 4 deletions Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import Foundation
import PixarUSD
#if canImport(Metal) && !os(visionOS)
#if canImport(Metal)
import Metal
import MetalKit

Expand All @@ -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
}

Expand Down Expand Up @@ -181,4 +181,4 @@ import PixarUSD
}
}
}
#endif // canImport(Metal) && !os(visionOS)
#endif // canImport(Metal)
2 changes: 1 addition & 1 deletion Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import PixarUSD
import Combine
import SwiftUI

#if os(iOS)
#if os(iOS) || os(visionOS)
import Metal
import MetalKit

Expand Down

0 comments on commit b2b892a

Please sign in to comment.