From 533d7b8c0a33871b1849fbda862d4fa1899822f4 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Mon, 6 Oct 2025 10:19:55 +0900 Subject: [PATCH] fix: [improve performance] `UpdateViewMatrix` is called even though (direct-)parent canvas is disabled close #373 --- .../Utilities/ShaderVariantRegistry.cs | 62 ++++++++++++++----- Packages/src/Runtime/UIEffectBase.cs | 36 +++++++++-- Packages/src/Runtime/UIEffectContext.cs | 7 ++- Packages/src/Runtime/UIEffectReplica.cs | 4 +- .../src/Runtime/Utilities/GraphicProxy.cs | 3 +- Packages/src/Runtime/Utilities/TmpProxy.cs | 3 +- 6 files changed, 87 insertions(+), 28 deletions(-) diff --git a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs index 111c761..c2da4bb 100644 --- a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs +++ b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs @@ -6,6 +6,7 @@ using UnityEngine.Profiling; using System.IO; using System.Linq; using System.Reflection; +using System.Text; using System.Text.RegularExpressions; using UnityEditor; using UnityEditorInternal; @@ -122,10 +123,15 @@ namespace Coffee.UIEffectInternal #if UNITY_EDITOR private readonly HashSet _logVariants = new HashSet(); + private readonly StringBuilder _sb = new StringBuilder(1024); + + private readonly Dictionary _cachedVariants = + new Dictionary(); public void ClearCache() { _cachedOptionalShaders.Clear(); + _cachedVariants.Clear(); } /// @@ -213,21 +219,10 @@ namespace Coffee.UIEffectInternal if (!material || !material.shader || !m_Asset) return; Profiler.BeginSample("(EDITOR/COF)[ShaderVariantRegistry] RegisterVariant"); - var shaderName = material.shader.name; - var validKeywords = material.shaderKeywords - .Where(x => !Regex.IsMatch(x, "(_EDITOR|EDITOR_)")) - .ToArray(); - var keywords = string.Join(" ", validKeywords); - var variant = new ShaderVariantCollection.ShaderVariant - { - shader = material.shader, - keywords = validKeywords - }; - - // Already registered. - var pair = new StringPair() { key = shaderName, value = keywords }; + var (variant, pair) = GetVariant(material); if (m_Asset.Contains(variant)) { + // Already registered. m_UnregisteredVariants.Remove(pair); Profiler.EndSample(); return; @@ -243,7 +238,8 @@ namespace Coffee.UIEffectInternal if (_logVariants.Add(pair)) { - keywords = string.IsNullOrEmpty(keywords) ? "no keywords" : keywords; + var shaderName = pair.key; + var keywords = string.IsNullOrEmpty(pair.value) ? "no keywords" : pair.value; Debug.LogError($"Shader variant '{shaderName} <{keywords}>' is not registered.\n" + $"Register it in 'ProjectSettings > {path}' to use it in player.", m_Asset); } @@ -256,6 +252,44 @@ namespace Coffee.UIEffectInternal m_UnregisteredVariants.Remove(pair); Profiler.EndSample(); } + + private (ShaderVariantCollection.ShaderVariant, StringPair) GetVariant(Material material) + { + Profiler.BeginSample("(EDITOR/COF)[ShaderVariantRegistry] GetVariant"); + var shader = material.shader; + _sb.Length = 0; + foreach (var keyword in material.shaderKeywords) + { + // Ignore editor keyword. + if (keyword.Contains("_EDITOR") || keyword.Contains("EDITOR_")) continue; + + _sb.Append(keyword); + _sb.Append(' '); + } + + if (0 < _sb.Length) + { + _sb.Length--; // Remove last space. + } + + var hash = new Hash128((uint)shader.GetInstanceID(), (uint)_sb.GetHashCode(), 0, 0); + if (_cachedVariants.TryGetValue(hash, out var result)) + { + Profiler.EndSample(); + return result; + } + + var pair = new StringPair() { key = shader.name, value = _sb.ToString() }; + var variant = new ShaderVariantCollection.ShaderVariant + { + shader = shader, + keywords = pair.value.Split(' ') + }; + + _cachedVariants.Add(hash, (variant, pair)); + Profiler.EndSample(); + return (variant, pair); + } #endif } diff --git a/Packages/src/Runtime/UIEffectBase.cs b/Packages/src/Runtime/UIEffectBase.cs index bad1f6d..06d3930 100644 --- a/Packages/src/Runtime/UIEffectBase.cs +++ b/Packages/src/Runtime/UIEffectBase.cs @@ -32,6 +32,8 @@ namespace Coffee.UIEffects private Action _onAfterCanvasRebuild; private bool _canModifyMesh; private Matrix4x4 _prevTransformHash; + private Canvas _canvas; + private bool _canvasCached = false; public Material effectMaterial => _material; public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent(); @@ -39,6 +41,23 @@ namespace Coffee.UIEffects public virtual float actualSamplingScale => 1; public virtual bool canModifyShape => true; + protected Canvas canvas + { + get + { + if (_canvasCached) return _canvas; + + _canvasCached = true; + _canvas = GetComponentInParent(); + if (!_canvas || !_canvas.isActiveAndEnabled) + { + _canvas = null; + } + + return _canvas; + } + } + public virtual UIEffectContext context { get @@ -104,7 +123,7 @@ namespace Coffee.UIEffects s_ContextPool.Return(ref _context); } - protected virtual void OnBeforeCanvasRebuild() + private void OnBeforeCanvasRebuild() { // If the mesh is not modified in the previous frame, try to modify it if possible. if (!_canModifyMesh && CanModifyMesh()) @@ -114,12 +133,18 @@ namespace Coffee.UIEffects } } - protected virtual void OnAfterCanvasRebuild() + private void OnAfterCanvasRebuild() { - if (!_material || !graphic || !graphic.canvas || context == null) return; + if (!_material || !graphic || !canvas || context == null) return; if (!context.useViewMatrix) return; - context.UpdateViewMatrix(GetCurrentMaterial(), transitionRoot, graphic.canvas.rootCanvas); + context.UpdateViewMatrix(GetCurrentMaterial(), transitionRoot, canvas.rootCanvas); + } + + protected override void OnCanvasHierarchyChanged() + { + _canvasCached = false; + _canvas = null; } public void ModifyMesh(Mesh mesh) @@ -133,13 +158,14 @@ namespace Coffee.UIEffects _canModifyMesh = CanModifyMesh(); if (_canModifyMesh) { - context.ModifyMesh(graphic, transitionRoot, vh, canModifyShape); + context.ModifyMesh(graphic, canvas, transitionRoot, vh, canModifyShape); } } private bool CanModifyMesh() { if (!graphic || !graphic.isActiveAndEnabled) return false; + if (!isActiveAndEnabled) return false; // The transitionRoot is same as the transform => true. var root = transitionRoot; diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index a512d09..47c3f6b 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -451,7 +451,7 @@ namespace Coffee.UIEffects { if (!material) return; - Profiler.BeginSample("(UIE)[UIEffect] GetModifiedMaterial"); + Profiler.BeginSample("(UIE)[UIEffect] ApplyToMaterial"); material.SetInt(s_SrcBlend, (int)m_SrcBlendMode); material.SetInt(s_DstBlend, (int)m_DstBlendMode); @@ -706,7 +706,8 @@ namespace Coffee.UIEffects } } - public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelper vh, bool canModifyShape) + public void ModifyMesh(Graphic graphic, Canvas canvas, RectTransform transitionRoot, VertexHelper vh, + bool canModifyShape) { // Apply flip (without effect). ApplyFlipWithoutEffect(vh, m_Flip); @@ -716,7 +717,7 @@ namespace Coffee.UIEffects var effectProxy = GraphicProxy.Find(graphic); var isText = effectProxy.IsText(graphic); - effectProxy.OnPreModifyMesh(graphic); + effectProxy.OnPreModifyMesh(graphic, canvas); var verts = s_WorkingVertices; var expandSize = effectProxy.ModifyExpandSize(graphic, GetExpandSize(canModifyShape)); diff --git a/Packages/src/Runtime/UIEffectReplica.cs b/Packages/src/Runtime/UIEffectReplica.cs index 9005579..02add73 100644 --- a/Packages/src/Runtime/UIEffectReplica.cs +++ b/Packages/src/Runtime/UIEffectReplica.cs @@ -124,9 +124,9 @@ namespace Coffee.UIEffects { get { - if (preset && useTargetTransform && !m_CustomRoot && graphic && graphic.canvas) + if (preset && useTargetTransform && !m_CustomRoot && graphic && canvas) { - return graphic.canvas.transform as RectTransform; + return canvas.transform as RectTransform; } if (useTargetTransform && isTargetInScene) diff --git a/Packages/src/Runtime/Utilities/GraphicProxy.cs b/Packages/src/Runtime/Utilities/GraphicProxy.cs index f55e8e2..f94314e 100644 --- a/Packages/src/Runtime/Utilities/GraphicProxy.cs +++ b/Packages/src/Runtime/Utilities/GraphicProxy.cs @@ -55,13 +55,12 @@ namespace Coffee.UIEffects /// /// Called before modifying the mesh. /// - public virtual void OnPreModifyMesh(Graphic graphic) + public virtual void OnPreModifyMesh(Graphic graphic, Canvas canvas) { UIVertexUtil.onLerpVertex = s_OnLerpVertex; ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow; UIEffectContext.onModifyVertex = s_OnModifyVertex; - var canvas = graphic.canvas; if (canvas) { canvas.additionalShaderChannels |= AdditionalCanvasShaderChannels.TexCoord1; diff --git a/Packages/src/Runtime/Utilities/TmpProxy.cs b/Packages/src/Runtime/Utilities/TmpProxy.cs index 9619fae..4e629a6 100644 --- a/Packages/src/Runtime/Utilities/TmpProxy.cs +++ b/Packages/src/Runtime/Utilities/TmpProxy.cs @@ -40,13 +40,12 @@ namespace Coffee.UIEffects /// /// Called before modifying the mesh. /// - public override void OnPreModifyMesh(Graphic graphic) + public override void OnPreModifyMesh(Graphic graphic, Canvas canvas) { UIVertexUtil.onLerpVertex = s_OnLerpVertex; ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow; UIEffectContext.onModifyVertex = s_OnModifyVertex; - var canvas = graphic.canvas; if (canvas) { canvas.additionalShaderChannels |= AdditionalCanvasShaderChannels.TexCoord2;