Skip to content

Commit

Permalink
Fix/simplify metal view update, add hdr texture to dome light.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <[email protected]>
  • Loading branch information
furby-tm committed Dec 29, 2024
1 parent 2887880 commit d396ec7
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 79 deletions.
3 changes: 2 additions & 1 deletion Sources/PixarMacros/MacrosPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SwiftSyntaxMacros
struct PixarMacrosPlugin: CompilerPlugin
{
let providingMacros: [Macro.Type] = [
PixarXformableMacro.self
PixarXformableMacro.self,
PixarPrimMacro.self
]
}
34 changes: 34 additions & 0 deletions Sources/PixarMacros/PrimMacros.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ----------------------------------------------------------------
* :: : 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 SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct PixarPrimMacro: MemberMacro
{
public static func expansion(of _: SwiftSyntax.AttributeSyntax,
providingMembersOf _: some SwiftSyntax.DeclGroupSyntax,
in _: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax]
{
let decl: DeclSyntax = """
public func getAttribute(_ name: Tf.Token) -> Usd.Attribute
{
GetAttribute(name)
}
"""

return [decl]
}
}
2 changes: 1 addition & 1 deletion Sources/PixarMacros/StringifyMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct StringifyMacro: ExpressionMacro
in _: some MacroExpansionContext
) -> ExprSyntax
{
guard let argument = node.argumentList.first?.expression
guard let argument = node.arguments.first?.expression
else
{
fatalError("compiler bug: the macro does not have any arguments")
Expand Down
30 changes: 30 additions & 0 deletions Sources/PixarUSD/Usd/Sdf/AssetPath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ----------------------------------------------------------------
* :: : 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 CxxStdlib
import Foundation
import Sdf

public typealias SdfAssetPath = Pixar.SdfAssetPath

public extension Sdf
{
typealias AssetPath = SdfAssetPath
}

public extension Sdf.AssetPath
{
init(_ path: String)
{
self.init(std.string(path))
}
}
10 changes: 10 additions & 0 deletions Sources/PixarUSD/Usd/Usd/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ public extension Usd.Attribute
{
SetDocumentation(std.string(doc))
}

func set(_ value: String, time: UsdTimeCode = UsdTimeCode.Default()) -> Bool
{
Set(std.string(value), time)
}

func set(_ value: Sdf.AssetPath, time: UsdTimeCode = UsdTimeCode.Default()) -> Bool
{
Set(value, time)
}
}
21 changes: 21 additions & 0 deletions Sources/PixarUSD/Usd/UsdGeom/PrimRepresentable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* ----------------------------------------------------------------
* :: : 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 Usd

@attached(member, names: arbitrary, conformances: PrimRepresentable)
public macro Prim() = #externalMacro(module: "PixarMacros", type: "PixarPrimMacro")

public protocol PrimRepresentable
{
func getAttribute(_ name: Tf.Token) -> Usd.Attribute
}
10 changes: 10 additions & 0 deletions Sources/PixarUSD/Usd/UsdLux/DomeLight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ extension UsdLux.DomeLight: GeomXformable
{
UsdLux.DomeLight.define(stage, path: .init(path))
}

public func getTextureFileAttr() -> Usd.Attribute
{
GetTextureFileAttr()
}

public func createTextureFileAttr(defaultValue: Vt.Value = Vt.Value(), writeSparsely: Bool = false) -> Usd.Attribute
{
CreateTextureFileAttr(defaultValue, writeSparsely)
}
}
4 changes: 2 additions & 2 deletions Sources/UsdView/Hydra/Hydra+RenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public enum Hydra
params.frame = Usd.TimeCode.Default()
params.clearColor = .init(Float(rgba.0), Float(rgba.1), Float(rgba.2), Float(rgba.3))
params.enableIdRender = false
params.showGuides = false
params.showGuides = true
params.showRender = true
params.showProxy = false
params.showProxy = true

engine.render(root: stage.getPseudoRoot(), params: params)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UsdView/Hydra/Hydra+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public extension Hydra.Viewport
init(engine: Hydra.RenderEngine, rgba: (Double, Double, Double, Double))
{
#if canImport(Metal) && !os(visionOS)
let renderer = Hydra.MTLRenderer(device: engine.hydraDevice)!
let renderer = Hydra.MTLRenderer(device: engine.hydraDevice, hydra: engine)
self.init(hydra: engine, renderer: renderer, rgba: rgba)
#endif // canImport(Metal) && !os(visionOS)
}
Expand Down
19 changes: 12 additions & 7 deletions Sources/UsdView/Hydra/MTL/Hydra+MTLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ import PixarUSD
class MTLRenderer: NSObject, MTKViewDelegate
{
private let device: MTLDevice
private var hydra: Hydra.RenderEngine?
//private let commandQueue: MTLCommandQueue
private var pipelineState: MTLRenderPipelineState?

convenience init(device: MTLDevice, hydra: Hydra.RenderEngine)
{
self.init(device: device)!
self.hydra = hydra
}

init?(device: MTLDevice)
{
self.device = device
Expand Down Expand Up @@ -57,15 +64,11 @@ import PixarUSD
public func draw(in view: MTKView)
{
guard let drawable = view.currentDrawable else { return }
let renderPassDescriptor = view.currentRenderPassDescriptor
guard let renderPassDescriptor = view.currentRenderPassDescriptor else { return }

let commandQueue = view.device?.makeCommandQueue()
let commandBuffer = commandQueue?.makeCommandBuffer()

var renderEncoder: (any MTLRenderCommandEncoder)? = nil
if let renderPassDescriptor {
renderEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
}
let renderEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor)

if let pipelineState {
renderEncoder?.setRenderPipelineState(pipelineState)
Expand All @@ -81,8 +84,10 @@ import PixarUSD
zfar: 1.0
)
)
renderEncoder?.endEncoding()

hydra?.render(rgba: (0.1, 0.1, 0.1, 1.0))

renderEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
Expand Down
3 changes: 0 additions & 3 deletions Sources/UsdView/Hydra/MTL/Hydra+MTLView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ import PixarUSD

public func updateNSView(_ view: MTKView, context: Context)
{
// view.clearColor = MTLClearColorMake(rgba.0, rgba.1, rgba.2, rgba.3)

renderer.draw(in: view)
hydra.render(rgba: rgba)
}

public class Coordinator
Expand Down
45 changes: 13 additions & 32 deletions Sources/UsdView/SceneDescription/CreateScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,23 @@ import PixarUSD
extension UsdView
{
/**
* Create a stage with a sphere, capsule, cylinder, cube
* and cone on a transform, create a different colored
* material for each geometry prim, iterate the stage and
* print out the path to each prim, and finally save the
* stage to disk. */
func createScene()
* Create a basic usd scene. */
static func createScene() -> UsdStageRefPtr
{
/* Create stage with a sphere, capsule, cylinder, cube, and cone on a transform. */
/* Create stage with a dome light & sphere on a transform. */

let xform = UsdGeom.Xform.define(stage, path: "/Geometry")
xform.addXformOp(type: .translate).set(GfVec3d(0.0, 5.0, 0.0))
xform.addXformOp(type: .scale, precision: .float).set(GfVec3f(5, 5, 5))
let stage = Usd.Stage.createNew("\(documentsDirPath())/HelloWorldExample", ext: .usd)

let sphere = UsdGeom.Sphere.define(stage, path: "/Geometry/Sphere")
sphere.addXformOp(type: .translate).set(GfVec3d(0.0, 0.0, -5.0))

let capsule = UsdGeom.Capsule.define(stage, path: "/Geometry/Capsule")
capsule.addXformOp(type: .translate).set(GfVec3d(0.0, 0.0, -10.0))
capsule.addXformOp(type: .rotateX, precision: .float).set(Float(-90))

let cylinder = UsdGeom.Cylinder.define(stage, path: "/Geometry/Cylinder")
cylinder.addXformOp(type: .rotateX, precision: .float).set(Float(-90))
let domeLight = UsdLux.DomeLight.define(stage, path: "/World/DefaultDomeLight")
let hdr = Sdf.AssetPath("\(Bundle.hdx!.resourcePath!)/textures/StinsonBeach.hdr")
domeLight.createTextureFileAttr().set(hdr)

let cube = UsdGeom.Cube.define(stage, path: "/Geometry/Cube")
cube.addXformOp(type: .translate).set(GfVec3d(0.0, 0.0, 5.0))

let cone = UsdGeom.Cone.define(stage, path: "/Geometry/Cone")
cone.addXformOp(type: .translate).set(GfVec3d(0.0, 0.0, 10.0))
cone.addXformOp(type: .rotateX, precision: .float).set(Float(-90))
UsdGeom.Xform.define(stage, path: "/Geometry")
let sphere = UsdGeom.Sphere.define(stage, path: "/Geometry/Sphere")

/* Create a different colored material for each geometry prim. */
/* Create a colored material for the sphere prim. */

UsdShade.MaterialBindingAPI.apply(sphere).bind(matDef(stage, color: .red))
UsdShade.MaterialBindingAPI.apply(capsule).bind(matDef(stage, color: .yellow))
UsdShade.MaterialBindingAPI.apply(cylinder).bind(matDef(stage, color: .green))
UsdShade.MaterialBindingAPI.apply(cube).bind(matDef(stage, color: .blue))
UsdShade.MaterialBindingAPI.apply(cone).bind(matDef(stage, color: .purple))

stage.getPseudoRoot().set(doc: "SwiftUSD v\(Pixar.version)")

/* Iterate the stage and print out the path to each prim. */

Expand All @@ -66,6 +44,9 @@ extension UsdView

/* Save the stage to disk. */

stage.getPseudoRoot().set(doc: "SwiftUSD v\(Pixar.version)")
stage.save()

return stage
}
}
44 changes: 12 additions & 32 deletions Sources/UsdView/UsdView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,27 @@ struct UsdView: PixarApp
registerPlugins()

// create a new usd stage.
stage = Usd.Stage.createNew("\(documentsDirPath())/HelloPixarUSD", ext: .usda)
stage = UsdView.createScene()

// setup hydra to render the usd stage.
engine = Hydra.RenderEngine(stage: stage)

runDemo()
Msg.logger.log(level: .info, "UsdView launched | USD v\(Pixar.version).")
}

#if canImport(SwiftUI)
var body: some Scene
var body: some Scene
{
WindowGroup("UsdView", id: "usdview")
{
WindowGroup("UsdView", id: "usdview")
VStack
{
VStack
{
Hydra.Viewport(engine: engine, rgba: rgba)
.ignoresSafeArea()
}
.onAppear
{
#if canImport(SwiftUI)
startColorAnimation()
#endif // canImport(SwiftUI)
}
Hydra.Viewport(engine: engine, rgba: rgba)
.ignoresSafeArea()
}
.onAppear
{
startColorAnimation()
}
}
#else
static func main()
{
let app = UsdView()
}
#endif

func runDemo()
{
// create usd scene.
createScene()

// declarative usd scene.
declareScene()

Msg.logger.log(level: .info, "UsdView launched | USD v\(Pixar.version).")
}
}

0 comments on commit d396ec7

Please sign in to comment.