Skip to content

Commit

Permalink
chore: add some profiling scopes (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore authored Aug 18, 2024
1 parent 999dc53 commit e724e63
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 47 deletions.
20 changes: 20 additions & 0 deletions Editor/MiscDebugTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
15 changes: 14 additions & 1 deletion Editor/PreviewSystem/Rendering/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Profiling;

#endregion

Expand All @@ -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;
Expand Down Expand Up @@ -55,6 +58,8 @@ ObjectRegistry registry
_context = context;
ObjectRegistry = registry;

_profileSampler_onFrame = CustomSampler.Create(filter.GetType() + ".OnFrame");

OnFrame();
}

Expand All @@ -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();
}
}
}
}
Expand Down
104 changes: 58 additions & 46 deletions Editor/PreviewSystem/Rendering/ProxyObjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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<MeshFilter>();
var filter = _replacementRenderer.GetComponent<MeshFilter>();
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<MeshFilter>();
var filter = _replacementRenderer.GetComponent<MeshFilter>();
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()
Expand Down

0 comments on commit e724e63

Please sign in to comment.