From b48908b13010430942e232389ead073d1a4f57a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?furby=E2=84=A2?= Date: Sun, 15 Sep 2024 21:42:39 -0600 Subject: [PATCH] Fix imaging crash potential, begin UsdView hydra. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: furby™ --- .../UsdImagingGLEngine.swift | 18 +++++ Sources/UsdImaging/indexProxy.cpp | 3 +- Sources/UsdImaging/instanceAdapter.cpp | 3 +- .../include/UsdImagingGL/engine.h | 1 + Sources/UsdView/HDMTLRenderer.swift | 54 -------------- Sources/UsdView/Hydra+MTLRenderer.swift | 71 +++++++++++++++++++ Sources/UsdView/Hydra.swift | 17 +++++ Sources/UsdView/UsdView.swift | 4 +- 8 files changed, 111 insertions(+), 60 deletions(-) create mode 100644 Sources/PixarUSD/UsdImaging/UsdImagingGLEngine/UsdImagingGLEngine.swift delete mode 100644 Sources/UsdView/HDMTLRenderer.swift create mode 100644 Sources/UsdView/Hydra+MTLRenderer.swift create mode 100644 Sources/UsdView/Hydra.swift diff --git a/Sources/PixarUSD/UsdImaging/UsdImagingGLEngine/UsdImagingGLEngine.swift b/Sources/PixarUSD/UsdImaging/UsdImagingGLEngine/UsdImagingGLEngine.swift new file mode 100644 index 000000000..3394082da --- /dev/null +++ b/Sources/PixarUSD/UsdImaging/UsdImagingGLEngine/UsdImagingGLEngine.swift @@ -0,0 +1,18 @@ +/* ---------------------------------------------------------------- + * :: : 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(UsdImagingGLEngine) + import UsdImagingGLEngine + + public enum UsdImagingGLEngine + {} +#endif // canImport(UsdImagingGLEngine) diff --git a/Sources/UsdImaging/indexProxy.cpp b/Sources/UsdImaging/indexProxy.cpp index c5b14698c..0391c0ae1 100644 --- a/Sources/UsdImaging/indexProxy.cpp +++ b/Sources/UsdImaging/indexProxy.cpp @@ -251,11 +251,10 @@ void UsdImagingIndexProxy::InsertInstancer(SdfPath const &cachePath, primInfo->dirtyBits = HdChangeTracker::AllDirty; _delegate->_dirtyCachePaths.insert(cachePath); - auto& adapterType = (*adapter); TF_DEBUG(USDIMAGING_INSTANCER) .Msg("[Instancer Inserted] %s, adapter = %s\n", cachePath.GetText(), - adapter ? TfType::GetCanonicalTypeName(typeid(adapterType)).c_str() : "none"); + adapter ? TfType::GetCanonicalTypeName(typeid(*adapter)).c_str() : "none"); _AddTask(cachePath); } diff --git a/Sources/UsdImaging/instanceAdapter.cpp b/Sources/UsdImaging/instanceAdapter.cpp index 17f0afc39..6b2adfc8b 100644 --- a/Sources/UsdImaging/instanceAdapter.cpp +++ b/Sources/UsdImaging/instanceAdapter.cpp @@ -335,13 +335,12 @@ SdfPath UsdImagingInstanceAdapter::_Populate(UsdPrim const &prim, protoPath = newProtoPath; } - auto& primAdapterType = (*primAdapter); TF_DEBUG(USDIMAGING_INSTANCER) .Msg("[Add Instance NI] <%s> %s (%s), adapter = %s\n", instancerPath.GetText(), protoPath.GetText(), iter->GetName().GetText(), - primAdapter ? TfType::GetCanonicalTypeName(typeid(primAdapterType)).c_str() : "none"); + primAdapter ? TfType::GetCanonicalTypeName(typeid(*primAdapter)).c_str() : "none"); } } diff --git a/Sources/UsdImagingGL/include/UsdImagingGL/engine.h b/Sources/UsdImagingGL/include/UsdImagingGL/engine.h index 446f0932c..d675ee08c 100644 --- a/Sources/UsdImagingGL/include/UsdImagingGL/engine.h +++ b/Sources/UsdImagingGL/include/UsdImagingGL/engine.h @@ -65,6 +65,7 @@ TF_DECLARE_REF_PTRS(HdsiSceneGlobalsSceneIndex); TF_DECLARE_REF_PTRS(HdSceneIndexBase); using UsdStageWeakPtr = TfWeakPtr; +using UsdImagingGLEngineSharedPtr = std::shared_ptr; namespace UsdImagingGLEngine_Impl { using _AppSceneIndicesSharedPtr = std::shared_ptr; diff --git a/Sources/UsdView/HDMTLRenderer.swift b/Sources/UsdView/HDMTLRenderer.swift deleted file mode 100644 index 7d0c5e6a0..000000000 --- a/Sources/UsdView/HDMTLRenderer.swift +++ /dev/null @@ -1,54 +0,0 @@ -/* ---------------------------------------------------------------- - * :: : 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 - - /** - * ``HDMTLRenderer`` - * - * ## Overview - * - * The Hydra Engine (``Hd``) Metal renderer for the ``UsdView`` - * application conforms to the ``MTKViewDelegate`` protocol, - * allowing it to be set as a ``MTKView`` object's delegate to - * provide a drawing method to a ``MTKView`` object and respond - * to rendering events. */ - class HDMTLRenderer: NSObject, MTKViewDelegate - { - let hgi: Pixar.HgiMetalPtr - - init?(device _: MTLDevice) - { - hgi = HgiMetal.createHgi() - - let driver = HdDriver(name: .renderDriver, driver: VtValue(hgi)) - } - - func info() - { - Msg.logger.log(level: .info, "Created HGI -> Metal API v\(hgi.apiVersion).") - Msg.logger.log(level: .info, "GPU Architecture -> \(hgi.device.architecture.name)") - } - - func mtkView(_: MTKView, drawableSizeWillChange size: CGSize) - { - print("drawableSizeWillChange", size) - } - - func draw(in _: MTKView) - {} - } -#endif /* canImport(Metal) && !os(visionOS) */ diff --git a/Sources/UsdView/Hydra+MTLRenderer.swift b/Sources/UsdView/Hydra+MTLRenderer.swift new file mode 100644 index 000000000..28eb32e3c --- /dev/null +++ b/Sources/UsdView/Hydra+MTLRenderer.swift @@ -0,0 +1,71 @@ +/* ---------------------------------------------------------------- + * :: : 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 + + public extension Hydra + { + /** + * ``MTLRenderer`` + * + * ## Overview + * + * The Hydra Engine (``Hd``) Metal renderer for the ``UsdView`` + * application conforms to the ``MTKViewDelegate`` protocol, + * allowing it to be set as a ``MTKView`` object's delegate to + * provide a drawing method to a ``MTKView`` object and respond + * to rendering events. */ + class MTLRenderer: NSObject, MTKViewDelegate + { + let hgi: Pixar.HgiMetalPtr + // let engine: UsdImagingGLEngineSharedPtr + // let stage: UsdStageRefPtr + + public init?(device _: MTLDevice) + { + hgi = HgiMetal.createHgi() + + // let excludedPaths = SdfPathVector() + let driver = HdDriver(name: .renderDriver, driver: VtValue(hgi)) + + // engine.reset(UsdImagingGLEngineSharedPtr( + // stage.getPseudoRoot().getPath(), + // excludedPaths, + // SdfPathVector(), + // SdfPath.AbsoluteRootPath(), + // driver + // )) + + // engine.SetEnablePresentation(false) + // engine.SetRendererAov(false) + } + + public func info() + { + Msg.logger.log(level: .info, "Created HGI -> Metal API v\(hgi.apiVersion).") + Msg.logger.log(level: .info, "GPU Architecture -> \(hgi.device.architecture.name)") + } + + public func mtkView(_: MTKView, drawableSizeWillChange size: CGSize) + { + print("drawableSizeWillChange", size) + } + + public func draw(in _: MTKView) + {} + } + } +#endif // canImport(Metal) && !os(visionOS) diff --git a/Sources/UsdView/Hydra.swift b/Sources/UsdView/Hydra.swift new file mode 100644 index 000000000..9b1a7f261 --- /dev/null +++ b/Sources/UsdView/Hydra.swift @@ -0,0 +1,17 @@ +/* ---------------------------------------------------------------- + * :: : 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 enum Hydra +{} diff --git a/Sources/UsdView/UsdView.swift b/Sources/UsdView/UsdView.swift index 462f70a74..3a4b3d629 100644 --- a/Sources/UsdView/UsdView.swift +++ b/Sources/UsdView/UsdView.swift @@ -40,7 +40,7 @@ import PixarUSD struct UsdView: PixarApp { #if canImport(Metal) && !os(visionOS) - let hydra: HDMTLRenderer + let hydra: Hydra.MTLRenderer #endif /* canImport(Metal) && !os(visionOS) */ public init() @@ -65,7 +65,7 @@ struct UsdView: PixarApp #endif /* canImport(PyBundle) */ #if canImport(Metal) && !os(visionOS) - hydra = HDMTLRenderer(device: MTLCreateSystemDefaultDevice()!)! + hydra = Hydra.MTLRenderer(device: MTLCreateSystemDefaultDevice()!)! #endif /* canImport(Metal) && !os(visionOS) */ #if canImport(SwiftUI)