From a499188c78a4bcc50e6939f51eca209490fd3fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Stompo=CC=81r?= Date: Mon, 22 Dec 2025 12:08:30 +0100 Subject: [PATCH 1/2] Faster bound retrieval --- Engine.xcodeproj/project.pbxproj | 4 ++++ .../ModelIO/MDLAxisAlignedBoundingBox+Extension.swift | 11 +++++++++++ Engine/Core/Import/SceneLoader/PNISceneLoader.swift | 7 ++++++- .../Core/Import/Translator/PNISceneTranslator.swift | 11 ++++------- 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 Engine/Core/Extensions/ModelIO/MDLAxisAlignedBoundingBox+Extension.swift diff --git a/Engine.xcodeproj/project.pbxproj b/Engine.xcodeproj/project.pbxproj index b3431a36..f4912364 100644 --- a/Engine.xcodeproj/project.pbxproj +++ b/Engine.xcodeproj/project.pbxproj @@ -236,6 +236,7 @@ 5677AD2B28C1505000BF822C /* simd_float3x3+Extension+Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5677AD2228C1505000BF822C /* simd_float3x3+Extension+Tests.swift */; }; 5677AD3128C151CA00BF822C /* XCTest+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5677AD3028C151CA00BF822C /* XCTest+Extension.swift */; }; 567F5C3D28A7AC3000AD0A6C /* NSImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F5C3C28A7AC3000AD0A6C /* NSImage+Extension.swift */; platformFilters = (macos, ); }; + 56865D8C2EF955B50024896F /* MDLAxisAlignedBoundingBox+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56865D8B2EF955B20024896F /* MDLAxisAlignedBoundingBox+Extension.swift */; }; 5686BD9A273C1CD300329FA0 /* PNIStaticBuffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5686BD99273C1CD300329FA0 /* PNIStaticBuffer.swift */; }; 5686BD9D273D78B700329FA0 /* Animation.metal in Sources */ = {isa = PBXBuildFile; fileRef = 5686BD9C273D78B700329FA0 /* Animation.metal */; }; 568807462747EEC000C95F55 /* PNSkeleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568807452747EEC000C95F55 /* PNSkeleton.swift */; }; @@ -601,6 +602,7 @@ 5677AD2628C1505000BF822C /* Array+Extension+Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Extension+Tests.swift"; sourceTree = ""; }; 5677AD3028C151CA00BF822C /* XCTest+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "XCTest+Extension.swift"; sourceTree = ""; }; 567F5C3C28A7AC3000AD0A6C /* NSImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+Extension.swift"; sourceTree = ""; }; + 56865D8B2EF955B20024896F /* MDLAxisAlignedBoundingBox+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MDLAxisAlignedBoundingBox+Extension.swift"; sourceTree = ""; }; 5686BD99273C1CD300329FA0 /* PNIStaticBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PNIStaticBuffer.swift; sourceTree = ""; }; 5686BD9B273D78A900329FA0 /* Animation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Animation.h; sourceTree = ""; }; 5686BD9C273D78B700329FA0 /* Animation.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Animation.metal; sourceTree = ""; }; @@ -2014,6 +2016,7 @@ 56D1ED592725F84C00D68EA7 /* ModelIO */ = { isa = PBXGroup; children = ( + 56865D8B2EF955B20024896F /* MDLAxisAlignedBoundingBox+Extension.swift */, 561666812719E2CC000063B1 /* MDLMaterialProperty+Extension.swift */, 561666832719E2CC000063B1 /* MDLTransformComponent+Extension.swift */, 561666862719E2CC000063B1 /* MDLMaterial+Extension.swift */, @@ -2550,6 +2553,7 @@ 5648531C28C9F26300BA4CC0 /* PNKeyframeAnimation.swift in Sources */, 5688076827483F4600C95F55 /* PNIMaterial.swift in Sources */, 5648532328CA154F00BA4CC0 /* PNISinglePlaySampler.swift in Sources */, + 56865D8C2EF955B50024896F /* MDLAxisAlignedBoundingBox+Extension.swift in Sources */, 566795DA274FC561005993D7 /* PNEngine.swift in Sources */, 564DE040284169B9005C2029 /* Comparable+Global.swift in Sources */, 5620C8152754F3B60098D6FA /* PNSpotLight.swift in Sources */, diff --git a/Engine/Core/Extensions/ModelIO/MDLAxisAlignedBoundingBox+Extension.swift b/Engine/Core/Extensions/ModelIO/MDLAxisAlignedBoundingBox+Extension.swift new file mode 100644 index 00000000..e6868c49 --- /dev/null +++ b/Engine/Core/Extensions/ModelIO/MDLAxisAlignedBoundingBox+Extension.swift @@ -0,0 +1,11 @@ +// +// Copyright © 2025 Mateusz Stompór. All rights reserved. +// + +import ModelIO + +extension MDLAxisAlignedBoundingBox { + var pnBound: PNBound { + PNBound(min: minBounds, max: maxBounds) + } +} diff --git a/Engine/Core/Import/SceneLoader/PNISceneLoader.swift b/Engine/Core/Import/SceneLoader/PNISceneLoader.swift index b0117fe9..de233890 100644 --- a/Engine/Core/Import/SceneLoader/PNISceneLoader.swift +++ b/Engine/Core/Import/SceneLoader/PNISceneLoader.swift @@ -21,10 +21,15 @@ public final class PNISceneLoader: PNSceneLoader { return translator.process(asset: asset) } public func resource(name: String, extension: String, bundle: Bundle) -> PNScene? { + let resourceRetrieval = psignposter.beginInterval("Resource retrieval") guard let asset = assetLoader.resource(name: name, extension: `extension`, bundle: bundle) else { return nil } - return translator.process(asset: asset) + psignposter.endInterval("Resource retrieval", resourceRetrieval) + let sceneTranslation = psignposter.beginInterval("Scene translation") + let processedScene = translator.process(asset: asset) + psignposter.endInterval("Scene translation", sceneTranslation) + return processedScene } public static func `default`(device: MTLDevice) -> PNISceneLoader { PNISceneLoader(device: device, diff --git a/Engine/Core/Import/Translator/PNISceneTranslator.swift b/Engine/Core/Import/Translator/PNISceneTranslator.swift index 1f0bea16..3defcab0 100644 --- a/Engine/Core/Import/Translator/PNISceneTranslator.swift +++ b/Engine/Core/Import/Translator/PNISceneTranslator.swift @@ -8,6 +8,7 @@ import PNShared public final class PNISceneTranslator: PNSceneTranslator { private let device: MTLDevice + private let interactor = PNIBoundingBoxInteractor.default private var materialCache = [String: PNMaterial]() public init(device: MTLDevice) { self.device = device @@ -21,9 +22,7 @@ public final class PNISceneTranslator: PNSceneTranslator { parent: passedValue) passedValue?.children.append(node) return node - } /* else if let object = object as? MDLLight { - fatalError("Not implemented") - } */ else if let object = object as? MDLMesh { + } else if let object = object as? MDLMesh { guard let mesh = convert(mesh: object) else { fatalError("Could not convert mesh") } @@ -110,7 +109,6 @@ public final class PNISceneTranslator: PNSceneTranslator { assert(mesh.vertexBuffers.count == 1, "Only object that have a single buffer assigned are supported") let buffer = mesh.vertexBuffers[0].rawData - let bounds = PNIBoundEstimator().bound(vertexBuffer: buffer) guard let deviceBuffer = device.makeBufferShared(data: buffer) else { return nil } @@ -138,10 +136,9 @@ public final class PNISceneTranslator: PNSceneTranslator { material: loadedMaterial) pieceDescriptions.append(description) } - } - let interactor = PNIBoundingBoxInteractor.default - return PNMesh(boundingBox: interactor.from(bound: bounds), + + return PNMesh(boundingBox: interactor.from(bound: mesh.boundingBox.pnBound), vertexBuffer: dataBuffer, pieceDescriptions: pieceDescriptions) } From 1645204074b32368d12656da9b6b880c5f19e6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Stompo=CC=81r?= Date: Mon, 22 Dec 2025 12:09:05 +0100 Subject: [PATCH 2/2] Lint fix --- Engine/Core/Import/Translator/PNISceneTranslator.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Core/Import/Translator/PNISceneTranslator.swift b/Engine/Core/Import/Translator/PNISceneTranslator.swift index 3defcab0..37486472 100644 --- a/Engine/Core/Import/Translator/PNISceneTranslator.swift +++ b/Engine/Core/Import/Translator/PNISceneTranslator.swift @@ -137,7 +137,7 @@ public final class PNISceneTranslator: PNSceneTranslator { pieceDescriptions.append(description) } } - + return PNMesh(boundingBox: interactor.from(bound: mesh.boundingBox.pnBound), vertexBuffer: dataBuffer, pieceDescriptions: pieceDescriptions)