Skip to content

Commit

Permalink
Add usdview macos/ios metal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed Feb 13, 2025
1 parent fd92cdc commit b202fb0
Show file tree
Hide file tree
Showing 21 changed files with 971 additions and 309 deletions.
20 changes: 1 addition & 19 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"originHash" : "065c817dbe12581c3a40f494993c105d94719cc228270d3b0d2b745586455451",
"originHash" : "f5e02745871b95617e6f30f7baca9016ed665f2d27342bc87f9d5f6d804b536a",
"pins" : [
{
"identity" : "galah",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/galah.git",
"state" : {
"revision" : "49cd69c9af968c034b345e39956ad458ce9bd27d",
"version" : "1.0.2"
}
},
{
"identity" : "icu",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -100,15 +91,6 @@
"version" : "1.6.1"
}
},
{
"identity" : "swift-macro-toolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-macro-toolkit",
"state" : {
"revision" : "e706aa98bc28f82677923f7b8f560bba6f90fac2",
"version" : "0.6.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
66 changes: 11 additions & 55 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
// swift-tools-version: 5.10
// swift-tools-version: 6.0
import CompilerPluginSupport
import Foundation
import PackageDescription

/// previous versions of swift before 6.0 have issues
/// compiling swift macros, so galah is only supported
/// for swift 6.0+.
var galahDeps: [Package.Dependency] = []
var galahSettings: [SwiftSetting] = [.interoperabilityMode(.Cxx)]
var galahTargetDeps: [Target.Dependency] = [.target(name: "PixarUSD")]
_ = Galah()

let package = Package(
name: "SwiftUSD",
platforms: [
Expand Down Expand Up @@ -295,7 +287,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.4.0"),
.package(url: "https://github.com/onevcat/Rainbow.git", from: "3.0.0"),
.package(url: "https://github.com/mxcl/Version.git", from: "2.0.0"),
] + galahDeps,
],
targets: [
.target(
name: "pxr",
Expand Down Expand Up @@ -1728,9 +1720,9 @@ let package = Package(
dependencies: [
.target(name: "PixarUSD"),
],
// resources: [
// .process("Resources")
// ],
resources: [
.process("Resources")
],
cxxSettings: [
.define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])),
.define("_ALLOW_KEYWORD_MACROS", to: "1", .when(platforms: [.windows])),
Expand Down Expand Up @@ -1926,13 +1918,17 @@ let package = Package(

.executableTarget(
name: "Examples",
dependencies: galahTargetDeps,
dependencies: [
.target(name: "PixarUSD"),
],
cxxSettings: [
.define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])),
.define("_ALLOW_KEYWORD_MACROS", to: "1", .when(platforms: [.windows])),
.define("static_assert(_conditional, ...)", to: "", .when(platforms: [.windows])),
],
swiftSettings: galahSettings
swiftSettings: [
.interoperabilityMode(.Cxx),
]
),

.testTarget(
Expand Down Expand Up @@ -2903,43 +2899,3 @@ enum Arch
}
}
}

struct Galah
{
/**
* hacky way to detect if swift 6 might be available on your platform,
* sadly there is no other way since swiftpm got even more restrictive
* by not allowing executables to be called via Process() inside swift
* package manifests anymore, as this now errors with a lovely output
* of: ["error: permissionDenied\n"], so we cannot just simply call
* 'swift --version' either. */
init(supported: Bool = false)
{
var withGalah = supported

// if we are on these platforms, we are on swift 6, enable galah.
#if os(macOS)
if #available(macOS 15, visionOS 2, iOS 18, tvOS 18, watchOS 18, *)
{
withGalah = true
}
#endif

// if swift is version 6 or later, enable galah. need to check how
// this preprocesses if linux has swift 6.0 installed (we want to
// continue to allow a minimum swift-tools-version of 5.10, while
// opening up features for later swift versions, if available).
#if swift(>=6.0)
withGalah = true
#endif /* swift(>=6.0) */

if withGalah { Galah.enableGalah() }
}

static func enableGalah()
{
galahDeps = [.package(url: "https://github.com/wabiverse/galah.git", from: "1.0.2")]
galahSettings = [.interoperabilityMode(.Cxx), .define("WITH_GALAH")]
galahTargetDeps = [.target(name: "PixarUSD"), .product(name: "GalahInterpreter", package: "galah")]
}
}
5 changes: 0 additions & 5 deletions Sources/Examples/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ enum USDExamples
// custom ar resolver examples.
ArResolverExamples.run()

// galah interpreter embedding examples.
#if WITH_GALAH
GalahInterpreterExamples.run()
#endif /* WITH_GALAH */

// python interpreter embedding examples.
PythonInterpreterExamples.run()

Expand Down
62 changes: 0 additions & 62 deletions Sources/Examples/GalahInterpreter.swift

This file was deleted.

27 changes: 10 additions & 17 deletions Sources/PixarUSD/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ import Rainbow
Msg.logger.log(level: .info, "Bundle path: \(resourcePath ?? "")")
}

private let fileManager = FileManager.default

public var resourcePath: String?

public init?(path: String)
{
if fileManager.fileExists(atPath: path, isDirectory: nil)
if FileManager.default.fileExists(atPath: path, isDirectory: nil)
{
resourcePath = path
}
Expand Down Expand Up @@ -338,16 +336,12 @@ public enum BundleKind

public extension Pixar
{
struct Bundler
final class Bundler: Sendable
{
let fileManager: FileManager

public static let shared: Bundler = .init()
public static let shared = Bundler()

private init()
{
fileManager = FileManager.default
}
{}

public func setup(_ kind: BundleKind, installPlugins: Bool = false)
{
Expand All @@ -372,7 +366,7 @@ public extension Pixar
{ path in

#if os(macOS) || os(visionOS) || os(iOS) || os(tvOS) || os(watchOS)
let doInstallPlugs = installPlugins || (!path.contains(".app") && !fileManager.fileExists(atPath: path.replacingOccurrences(of: "/Contents/Resources", with: "") + "/Contents/Resources", isDirectory: nil))
let doInstallPlugs = installPlugins || (!path.contains(".app") && !FileManager.default.fileExists(atPath: path.replacingOccurrences(of: "/Contents/Resources", with: "") + "/Contents/Resources", isDirectory: nil))
#else /* os(Linux) */
let doInstallPlugs = true
#endif /* os(Linux) */
Expand Down Expand Up @@ -585,16 +579,16 @@ public extension Pixar.Bundler
}

let dest = strip(path) + "/Contents/Resources"
let srcEnum = fileManager.enumerator(atPath: path)
let srcEnum = FileManager.default.enumerator(atPath: path)

do
{
if fileManager.fileExists(atPath: strip(path) + "/Contents", isDirectory: nil)
if FileManager.default.fileExists(atPath: strip(path) + "/Contents", isDirectory: nil)
{
try fileManager.removeItem(atPath: strip(path) + "/Contents")
try FileManager.default.removeItem(atPath: strip(path) + "/Contents")
}

try fileManager.createDirectory(atPath: dest, withIntermediateDirectories: true)
try FileManager.default.createDirectory(atPath: dest, withIntermediateDirectories: true)

while let file = srcEnum?.nextObject() as? String
{
Expand All @@ -610,8 +604,7 @@ public extension Pixar.Bundler
Msg.logger.log(level: .info, "Moving resource: \(sourceFile) -> \(destFile)")
#endif /* DEBUG_PIXAR_BUNDLE */

try fileManager.moveItem(atPath: sourceFile,
toPath: destFile)
try FileManager.default.moveItem(atPath: sourceFile, toPath: destFile)
}
}
catch
Expand Down
6 changes: 1 addition & 5 deletions Sources/UsdView/Hydra/GL/Hydra+GLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import PixarUSD
*
* The Hydra Engine (``Hd``) OpenGL renderer for the ``UsdView``
* application. */
class GLRenderer: HdRenderEngine
class GLRenderer
{
let hgi: Pixar.HgiGLPtr
var device: Pixar.HgiGLDevice!
Expand All @@ -45,7 +45,6 @@ import PixarUSD
let driver = HdDriver(name: .renderDriver, driver: hgi.value)

#if canImport(UsdImagingGL)
// UsdImagingGL is not available on iOS.
engine = UsdImagingGL.Engine.createEngine(
rootPath: stage.getPseudoRoot().getPath(),
excludedPaths: Sdf.PathVector(),
Expand All @@ -63,9 +62,6 @@ import PixarUSD
{
Msg.logger.log(level: .info, "Created HGI -> OpenGL.")
}

public func draw()
{}
}
}
#endif // canImport(HgiGL)
84 changes: 84 additions & 0 deletions Sources/UsdView/Hydra/Hydra+Camera.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* ----------------------------------------------------------------
* :: : M E T A V E R S E : ::
* ----------------------------------------------------------------
* Licensed under the terms set forth in the LICENSE.txt file, this
* file is available at https://openusd.org/license.
*
* Copyright (C) 2016 Pixar.
* Copyright (C) 2024 Wabi Foundation. All Rights Reserved.
* ----------------------------------------------------------------
* . x x x . o o o . x x x . : : : . o x o . : : : .
* ---------------------------------------------------------------- */

import Foundation
import PixarUSD

public extension Hydra
{
class Camera
{
private let isZUp: Bool
public var params = Params()

public struct Params
{
public var rotation = Pixar.GfVec3d(0.0)
public var focus = Pixar.GfVec3d(0.0)
public var distance = 50.0
public var focalLength = 0.0
public var projection = Pixar.GfCamera.Projection(0)
public var leftBottomNear = Pixar.GfVec3d()
public var rightTopFar = Pixar.GfVec3d()
public var scaleViewport = 1.0
}

public var position = Pixar.GfVec3d()
public var standardFocalLength = Double()
public var scaleBias = Double()

public init(isZUp: Bool)
{
self.isZUp = isZUp
params.rotation = Pixar.GfVec3d(0.0)
params.focus = Pixar.GfVec3d(0.0)
params.distance = 50.0
params.scaleViewport = 1.0
}

public func getTransform() -> GfMatrix4d
{
let gfRotation = getRotation()
var cameraTransform = GfMatrix4d(1.0)

var gfMatrix1 = GfMatrix4d()
var gfMatrix2 = GfMatrix4d()
var gfMatrix3 = GfMatrix4d()

cameraTransform =
gfMatrix1.SetTranslate(Pixar.GfVec3d(0.0, 0.0, params.distance)).pointee *
gfMatrix2.SetRotate(gfRotation).pointee *
gfMatrix3.SetTranslate(params.focus).pointee

return cameraTransform
}

public func getRotation() -> Pixar.GfRotation
{
var gfRotation = Pixar.GfRotation(Pixar.GfVec3d.ZAxis(), params.rotation[2]) *
Pixar.GfRotation(Pixar.GfVec3d.XAxis(), params.rotation[0]) *
Pixar.GfRotation(Pixar.GfVec3d.YAxis(), params.rotation[1])

if isZUp
{
gfRotation = gfRotation * Pixar.GfRotation(Pixar.GfVec3d.XAxis(), 90.0)
}

return gfRotation
}

public func getShaderParams() -> Params
{
return params
}
}
}
Loading

0 comments on commit b202fb0

Please sign in to comment.