Skip to content

Commit 32c9b34

Browse files
gabrieldelacruzEvergreen
authored andcommitted
[VFX] Release batch instance when disabled
1 parent 9a44aa1 commit 32c9b34

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Packages/com.unity.visualeffectgraph/Editor/Inspector/VisualEffectEditor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class VisualEffectEditor : Editor
7272
SerializedProperty m_RandomSeed;
7373
SerializedProperty m_VFXPropertySheet;
7474
SerializedProperty m_AllowInstancing;
75+
SerializedProperty m_ReleaseInstanceOnDisable;
7576

7677
RendererEditor m_RendererEditor;
7778

@@ -107,6 +108,7 @@ protected void OnEnable()
107108
m_VisualEffectAsset = serializedObject.FindProperty("m_Asset");
108109
m_VFXPropertySheet = m_SingleSerializedObject.FindProperty("m_PropertySheet");
109110
m_AllowInstancing = serializedObject.FindProperty("m_AllowInstancing");
111+
m_ReleaseInstanceOnDisable = serializedObject.FindProperty("m_ReleaseInstanceOnDisable");
110112

111113
var renderers = targets.Cast<Component>().Select(t => t.GetComponent<VFXRenderer>()).ToArray();
112114
m_RendererEditor = new RendererEditor(renderers);
@@ -1271,13 +1273,14 @@ private void DrawInstancingProperties()
12711273
{
12721274
EditorGUI.BeginChangeCheck();
12731275
EditorGUILayout.PropertyField(m_AllowInstancing, Contents.allowInstancing);
1276+
EditorGUILayout.PropertyField(m_ReleaseInstanceOnDisable, Contents.releaseInstanceOnDisable);
12741277
if (EditorGUI.EndChangeCheck())
12751278
{
12761279
serializedObject.ApplyModifiedProperties();
12771280

12781281
foreach (var visualEffect in targets.OfType<VisualEffect>())
12791282
{
1280-
visualEffect.RecreateData();
1283+
visualEffect.RecreateBatchInstance();
12811284
}
12821285
}
12831286
}
@@ -1503,6 +1506,7 @@ protected static class Contents
15031506
public static readonly GUILayoutOption playRateDropdownWidth = GUILayout.Width(40);
15041507

15051508
public static readonly GUIContent allowInstancing = EditorGUIUtility.TrTextContent("Allow Instancing", "When enabled, the effect will try to be batched with other of the same type.");
1509+
public static readonly GUIContent releaseInstanceOnDisable = EditorGUIUtility.TrTextContent("Release Instance On Disable", "When enabled, the effect instance will be created when the component is enabled, and released when it is disabled, to optimize memory usage.");
15061510

15071511
public static readonly GUIContent graphInBundle = EditorGUIUtility.TrTextContent("Exposed properties are hidden in the Inspector when Visual Effect Assets are stored in Asset Bundles.");
15081512
public static readonly GUIContent play = new GUIContent("Send Play Event");

Tests/SRPTests/Packages/com.unity.testing.visualeffectgraph/Tests/Runtime/VFXRuntimeTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,50 @@ public IEnumerator Load_Keyword_Scene_With_Instancing()
576576
}
577577
#endif
578578

579+
private IEnumerator Release_Batch_Instance_When_Disabled_CompareInstanceCount(string testName, VisualEffectAsset vfxAsset, uint expectedInstanceCount)
580+
{
581+
yield return null;
582+
583+
uint currentInstanceCount = VFXManager.GetBatchedEffectInfo(vfxAsset).activeInstanceCount;
584+
Assert.AreEqual(expectedInstanceCount, currentInstanceCount, $"{testName}: Instance count should match:");
585+
}
586+
587+
588+
[UnityTest]
589+
public IEnumerator Release_Batch_Instance_When_Disabled()
590+
{
591+
SceneManagement.SceneManager.LoadScene("Packages/com.unity.testing.visualeffectgraph/Scenes/024_StripIndirectAndSorting.unity");
592+
yield return null;
593+
594+
VisualEffect vfxComponent = null;
595+
foreach (var component in Object.FindObjectsByType<VisualEffect>(FindObjectsSortMode.None))
596+
{
597+
if (component.visualEffectAsset.name == "024_StripIndirectAndSorting_NoIndirect")
598+
{
599+
vfxComponent = component;
600+
break;
601+
}
602+
}
603+
var vfxAsset = vfxComponent.visualEffectAsset;
604+
605+
uint initialInstanceCount = VFXManager.GetBatchedEffectInfo(vfxAsset).activeInstanceCount;
606+
607+
vfxComponent.enabled = false;
608+
yield return Release_Batch_Instance_When_Disabled_CompareInstanceCount("Disable keeps instances by default", vfxAsset, initialInstanceCount);
609+
610+
vfxComponent.releaseInstanceWhenDisabled = true;
611+
yield return Release_Batch_Instance_When_Disabled_CompareInstanceCount("Setting releaseInstanceWhenDisabled releases instance if already disabled", vfxAsset, initialInstanceCount - 1);
612+
613+
vfxComponent.enabled = true;
614+
yield return Release_Batch_Instance_When_Disabled_CompareInstanceCount("Enabling the component again recreates the instance", vfxAsset, initialInstanceCount);
615+
616+
vfxComponent.enabled = false;
617+
yield return Release_Batch_Instance_When_Disabled_CompareInstanceCount("Disabling the component with releaseInstanceWhenDisabled releases the instance", vfxAsset, initialInstanceCount - 1);
618+
619+
vfxComponent.releaseInstanceWhenDisabled = false;
620+
yield return Release_Batch_Instance_When_Disabled_CompareInstanceCount("Clearing releaseInstanceWhenDisabled when disabled recreates the instance", vfxAsset, initialInstanceCount);
621+
}
622+
579623
private static Vector4[] s_SampleGradient_Branch_Instancing_Readback = null;
580624

581625
static void SampleGradient_Branch_Instancing_Readback(AsyncGPUReadbackRequest request)

0 commit comments

Comments
 (0)