From e724e633d42bb8e5dae61717152ccb72eba92711 Mon Sep 17 00:00:00 2001 From: bd_ Date: Sun, 18 Aug 2024 12:53:38 -0700 Subject: [PATCH] chore: add some profiling scopes (#336) --- Editor/MiscDebugTools.cs | 20 ++++ .../PreviewSystem/Rendering/NodeController.cs | 15 ++- .../Rendering/ProxyObjectController.cs | 104 ++++++++++-------- 3 files changed, 92 insertions(+), 47 deletions(-) diff --git a/Editor/MiscDebugTools.cs b/Editor/MiscDebugTools.cs index d5b0c3e..69ebc94 100644 --- a/Editor/MiscDebugTools.cs +++ b/Editor/MiscDebugTools.cs @@ -15,5 +15,25 @@ public static void DumpScenes() Debug.Log($"Scene {i}: {scene.name}"); } } + + [MenuItem("Tools/NDM Framework/Debug Tools/Dump Scene Objects")] + public static void DumpSceneObjects() + { + foreach (var scene in SceneManager.GetAllScenes()) + { + Debug.Log($"Scene {scene.name}:"); + foreach (var gameObject in scene.GetRootGameObjects()) + { + Debug.Log($" {gameObject.name}"); + } + } + } + + [MenuItem("Tools/NDM Framework/Debug Tools/Reload Domain")] + public static void ReloadDomain() + { + Debug.Log("Reloading domain..."); + UnityEditor.EditorUtility.RequestScriptReload(); + } } } \ No newline at end of file diff --git a/Editor/PreviewSystem/Rendering/NodeController.cs b/Editor/PreviewSystem/Rendering/NodeController.cs index e983765..8f99db5 100644 --- a/Editor/PreviewSystem/Rendering/NodeController.cs +++ b/Editor/PreviewSystem/Rendering/NodeController.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading.Tasks; using UnityEngine; +using UnityEngine.Profiling; #endregion @@ -24,6 +25,8 @@ private class RefCount private readonly RefCount _refCount; private readonly ComputeContext _context; + + private CustomSampler _profileSampler_onFrame; internal RenderAspects WhatChanged = RenderAspects.Everything; internal RenderGroup Group => _group; @@ -55,6 +58,8 @@ ObjectRegistry registry _context = context; ObjectRegistry = registry; + _profileSampler_onFrame = CustomSampler.Create(filter.GetType() + ".OnFrame"); + OnFrame(); } @@ -64,7 +69,15 @@ internal void OnFrame() { if (original != null && proxy.Renderer != null) { - _node.OnFrame(original, proxy.Renderer); + _profileSampler_onFrame.Begin(original.gameObject); + try + { + _node.OnFrame(original, proxy.Renderer); + } + finally + { + _profileSampler_onFrame.End(); + } } } } diff --git a/Editor/PreviewSystem/Rendering/ProxyObjectController.cs b/Editor/PreviewSystem/Rendering/ProxyObjectController.cs index d04c108..c241fa4 100644 --- a/Editor/PreviewSystem/Rendering/ProxyObjectController.cs +++ b/Editor/PreviewSystem/Rendering/ProxyObjectController.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using UnityEngine; +using UnityEngine.Profiling; using UnityEngine.SceneManagement; using Object = UnityEngine.Object; @@ -26,6 +27,8 @@ internal class ProxyObjectController : IDisposable internal ComputeContext _monitorRenderer, _monitorMaterials, _monitorMesh; internal Task OnInvalidate; + + private static CustomSampler _onPreFrameSampler = CustomSampler.Create("ProxyObjectController.OnPreFrame"); public static bool IsProxyObject(GameObject obj) { @@ -120,68 +123,77 @@ internal bool OnPreFrame() return false; } - var target = _replacementRenderer; - var original = _originalRenderer; - - target.gameObject.SetActive(original.enabled && original.gameObject.activeInHierarchy); + _onPreFrameSampler.Begin(_originalRenderer.gameObject); - SkinnedMeshRenderer smr = null; - if (_originalRenderer is SkinnedMeshRenderer smr_) + try { - smr = smr_; + var target = _replacementRenderer; + var original = _originalRenderer; - var replacementSMR = (SkinnedMeshRenderer)_replacementRenderer; - replacementSMR.sharedMesh = smr_.sharedMesh; - replacementSMR.bones = smr_.bones; - - target.transform.position = original.transform.position; - target.transform.rotation = original.transform.rotation; - } - else - { - var originalFilter = _originalRenderer.GetComponent(); - var filter = _replacementRenderer.GetComponent(); - filter.sharedMesh = originalFilter.sharedMesh; + target.gameObject.SetActive(original.enabled && original.gameObject.activeInHierarchy); - var shadowBone = ShadowBoneManager.Instance.GetBone(_originalRenderer.transform).proxy; - - var rendererTransform = _replacementRenderer.transform; - if (shadowBone != rendererTransform.parent) + SkinnedMeshRenderer smr = null; + if (_originalRenderer is SkinnedMeshRenderer smr_) { - rendererTransform.SetParent(shadowBone, false); - rendererTransform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity); - rendererTransform.localScale = Vector3.one; + smr = smr_; + + var replacementSMR = (SkinnedMeshRenderer)_replacementRenderer; + replacementSMR.sharedMesh = smr_.sharedMesh; + replacementSMR.bones = smr_.bones; + + target.transform.position = original.transform.position; + target.transform.rotation = original.transform.rotation; } - } + else + { + var originalFilter = _originalRenderer.GetComponent(); + var filter = _replacementRenderer.GetComponent(); + filter.sharedMesh = originalFilter.sharedMesh; - _replacementRenderer.sharedMaterials = _originalRenderer.sharedMaterials; + var shadowBone = ShadowBoneManager.Instance.GetBone(_originalRenderer.transform).proxy; + var rendererTransform = _replacementRenderer.transform; + if (shadowBone != rendererTransform.parent) + { + rendererTransform.SetParent(shadowBone, false); + rendererTransform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity); + rendererTransform.localScale = Vector3.one; + } + } + + _replacementRenderer.sharedMaterials = _originalRenderer.sharedMaterials; - target.localBounds = original.localBounds; - if (target is SkinnedMeshRenderer targetSMR && original is SkinnedMeshRenderer originalSMR) - { - targetSMR.rootBone = originalSMR.rootBone != null ? originalSMR.rootBone : originalSMR.transform; - targetSMR.quality = originalSMR.quality; - if (targetSMR.sharedMesh != null) + target.localBounds = original.localBounds; + if (target is SkinnedMeshRenderer targetSMR && original is SkinnedMeshRenderer originalSMR) { - var blendShapeCount = targetSMR.sharedMesh.blendShapeCount; - for (var i = 0; i < blendShapeCount; i++) + targetSMR.rootBone = originalSMR.rootBone != null ? originalSMR.rootBone : originalSMR.transform; + targetSMR.quality = originalSMR.quality; + + if (targetSMR.sharedMesh != null) { - targetSMR.SetBlendShapeWeight(i, originalSMR.GetBlendShapeWeight(i)); + var blendShapeCount = targetSMR.sharedMesh.blendShapeCount; + for (var i = 0; i < blendShapeCount; i++) + { + targetSMR.SetBlendShapeWeight(i, originalSMR.GetBlendShapeWeight(i)); + } } } - } - target.shadowCastingMode = original.shadowCastingMode; - target.receiveShadows = original.receiveShadows; - target.lightProbeUsage = original.lightProbeUsage; - target.reflectionProbeUsage = original.reflectionProbeUsage; - target.probeAnchor = original.probeAnchor; - target.motionVectorGenerationMode = original.motionVectorGenerationMode; - target.allowOcclusionWhenDynamic = original.allowOcclusionWhenDynamic; + target.shadowCastingMode = original.shadowCastingMode; + target.receiveShadows = original.receiveShadows; + target.lightProbeUsage = original.lightProbeUsage; + target.reflectionProbeUsage = original.reflectionProbeUsage; + target.probeAnchor = original.probeAnchor; + target.motionVectorGenerationMode = original.motionVectorGenerationMode; + target.allowOcclusionWhenDynamic = original.allowOcclusionWhenDynamic; - return true; + return true; + } + finally + { + _onPreFrameSampler.End(); + } } private void CreateReplacementObject()