Skip to content

Commit

Permalink
begin usdview+metal implementation.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <[email protected]>
  • Loading branch information
furby-tm committed Dec 28, 2024
1 parent f276ed0 commit 6aa00cd
Show file tree
Hide file tree
Showing 20 changed files with 574 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.swiftpm
generated
target
.index-build
12 changes: 12 additions & 0 deletions Bundler.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
format_version = 2

[projects.UsdView]
source = 'git(https://github.com/wabiverse/SwiftUSD.git)'
revision = 'main'

[projects.UsdView.products.UsdView]
type = 'executable'

[projects.UsdView.builder]
name = 'Package.swift'
type = 'wholeProject'
api = 'revision(main)'

[apps.UsdView]
identifier = 'foundation.wabi.UsdView'
product = 'UsdView'
Expand Down
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,9 @@ let package = Package(
dependencies: [
.target(name: "PixarUSD"),
],
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
15 changes: 15 additions & 0 deletions Sources/PixarUSD/UsdImaging/UsdImagingGL/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

public extension UsdImagingGL.Engine
{
static func createEngine() -> UsdImagingGL.EngineSharedPtr
{
UsdImagingGL.Engine.CreateEngine()
}

static func createEngine(params: Parameters) -> UsdImagingGL.EngineSharedPtr
{
UsdImagingGL.Engine.CreateEngine(params)
Expand Down Expand Up @@ -68,6 +73,11 @@
{
SetRendererAov(aov.token)
}

func render(root: Usd.Prim, params: UsdImagingGL.RenderParams)
{
Render(root, params)
}
}

public extension UsdImagingGL.EngineSharedPtr
Expand All @@ -81,5 +91,10 @@
{
pointee.setRenderer(aov: token)
}

func render(root prim: Usd.Prim, params: UsdImagingGL.RenderParams)
{
pointee.render(root: prim, params: params)
}
}
#endif // canImport(UsdImagingGLEngine)
22 changes: 22 additions & 0 deletions Sources/PixarUSD/UsdImaging/UsdImagingGL/RenderParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* ----------------------------------------------------------------
* :: : 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 . : : : .
* ---------------------------------------------------------------- */

#if canImport(UsdImagingGL)
import UsdImagingGL

public typealias UsdImagingGLRenderParams = Pixar.UsdImagingGLRenderParams

public extension UsdImagingGL
{
typealias RenderParams = UsdImagingGLRenderParams
}
#endif // canImport(UsdImagingGL)
10 changes: 10 additions & 0 deletions Sources/UsdImagingGL/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ UsdImagingGLEngine::UsdImagingGLEngine(const SdfPath &rootPath,
}
}

// static.
UsdImagingGLEngineSharedPtr UsdImagingGLEngine::CreateEngine()
{
UsdImagingGLEngineSharedPtr engine = std::make_shared<UsdImagingGLEngine>();

engine.reset(new UsdImagingGLEngine());

return engine;
}

// static.
UsdImagingGLEngineSharedPtr UsdImagingGLEngine::CreateEngine(const Parameters &params)
{
Expand Down
3 changes: 3 additions & 0 deletions Sources/UsdImagingGL/include/UsdImagingGL/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class UsdImagingGLEngine {
/// @{
// ---------------------------------------------------------------------

USDIMAGINGGL_API
static UsdImagingGLEngineSharedPtr CreateEngine();

USDIMAGINGGL_API
static UsdImagingGLEngineSharedPtr CreateEngine(const Parameters &params);

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)
29 changes: 12 additions & 17 deletions Sources/UsdView/Hydra/Hydra+RenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,28 @@ import PixarUSD

public enum Hydra
{
public class RenderEngine: HdRenderEngine
public class RenderEngine
{
public var stage: UsdStageRefPtr

public var engine: any HdRenderEngine
public var engine: UsdImagingGL.EngineSharedPtr

public required init(stage: UsdStageRefPtr)
{
self.stage = stage

#if !os(Linux) && !os(Windows) && !os(Android) && canImport(HgiMetal) && canImport(UsdImagingGL)
engine = Hydra.MTLRenderer(stage: stage)
#elseif canImport(HgiGL) && canImport(UsdImagingGL)
engine = Hydra.GLRenderer(stage: stage)
#else
engine = Hydra.NORenderer(stage: stage)
#endif
engine = UsdImagingGL.Engine.createEngine()
}

public func info()
public func render()
{
engine.info()
}
var params = UsdImagingGL.RenderParams()
params.frame = Usd.TimeCode.Default()
params.clearColor = .init(0.1, 0.1, 0.1, 1.0)
params.enableIdRender = false
params.showGuides = false
params.showRender = true
params.showProxy = false

public func draw()
{
engine.draw()
engine.render(root: stage.getPseudoRoot(), params: params)
}
}
}
44 changes: 44 additions & 0 deletions Sources/UsdView/Hydra/Hydra+View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* ----------------------------------------------------------------
* :: : 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

#if canImport(Metal) && !os(visionOS)
import Metal
import MetalKit
#endif // canImport(Metal) && !os(visionOS)

public extension Hydra
{
#if canImport(Metal) && !os(visionOS)
typealias Viewport = Hydra.MTLView
#elseif os(macOS)
typealias Viewport = NSViewRepresentable
#elseif os(visionOS) || os(tvOS) || os(watchOS)
typealias Viewport = UIViewRepresentable
#else
struct Viewport {}
#endif // canImport(Metal) && !os(visionOS)
}

public extension Hydra.Viewport
{
init(engine: Hydra.RenderEngine)
{
#if canImport(Metal) && !os(visionOS)
let device = MTLCreateSystemDefaultDevice()!
let renderer = Hydra.MTLRenderer(device: device)!
self.init(hydra: engine, device: device, renderer: renderer)
#endif // canImport(Metal) && !os(visionOS)
}
}
Loading

0 comments on commit 6aa00cd

Please sign in to comment.