Skip to content

Commit

Permalink
[VFX] Add test about mesh deletion while rendering
Browse files Browse the repository at this point in the history
Initially, I was investigating on UUM-6234 but I can't repro:

https://media.github.cds.internal.unity3d.com/user/42/files/af4a29ce-dcfe-41f1-a855-2e4345d5fa36

Not sure what fixes this, it can be relative to 
- https://github.cds.internal.unity3d.com/unity/unity/pull/15841 : Safer access to Mesh buffer using Inc/Dec RefCount
- https://github.cds.internal.unity3d.com/unity/unity/pull/20023: Change culling access, maybe could have been done to early in some cases ?
  • Loading branch information
PaulDemeulenaere authored and Evergreen committed Mar 7, 2023
1 parent e837679 commit a484e13
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Packages/com.unity.visualeffectgraph/Editor/PackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.EditorTests-testable")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests-testable")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Editor")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Editor-testable")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Tests")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Tests-testable")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.EditorTests")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "Unity.Testing.VisualEffectGraph.Editor",
"rootNamespace": "",
"references": [
"Unity.Testing.VisualEffectGraph"
"Unity.Testing.VisualEffectGraph",
"Unity.VisualEffectGraph.Editor",
"Unity.VisualEffectGraph.Runtime",
"Unity.Timeline",
"Unity.Timeline.Editor"
],
"includePlatforms": [
"Editor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"Unity.VisualEffectGraph.Editor",
"UnityEditor.TestTools.Graphics",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
"UnityEditor.TestRunner",
"Unity.Timeline",
"Unity.Timeline.Editor"
],
"includePlatforms": [
"Editor"
Expand All @@ -23,6 +25,17 @@
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"versionDefines": [
{
"name": "com.unity.timeline",
"expression": "[0.0.0-builtin]",
"define": "VFX_HAS_TIMELINE"
},
{
"name": "com.unity.timeline",
"expression": "1.0.0",
"define": "VFX_HAS_TIMELINE"
}
],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System.Collections;
using System.IO;
using System.Linq;
using UnityEngine.TestTools;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.VFX;
using UnityEditor.VFX;

namespace UnityEditor.VFX.Test
{
public class VisualEffectTest : VFXPlayModeTest
{
[OneTimeSetUp]
public void Init()
{
VFXTestCommon.CloseAllUnecessaryWindows();
}

[OneTimeTearDown]
public void CleanUp()
{
VFXTestCommon.DeleteAllTemporaryGraph();
}

[UnityTest, Description("Regression test UUM-6234")]
public IEnumerator Delete_Mesh_While_Rendering()
{
yield return new EnterPlayMode();

var graph = VFXTestCommon.MakeTemporaryGraph();
string meshFilePath;
{
//Create Mesh Asset
Mesh mesh;
{
var resourceMesh = new Mesh()
{
vertices = new[]
{
new Vector3(0, 0, 0),
new Vector3(1, 1, 0),
new Vector3(1, 0, 0),
},
triangles = new[] {0, 1, 2}
};
var guid = System.Guid.NewGuid().ToString();
meshFilePath = string.Format(VFXTestCommon.tempBasePath + "Mesh_{0}.asset", guid);
AssetDatabase.CreateAsset(resourceMesh, meshFilePath);
AssetDatabase.SaveAssets();
mesh = AssetDatabase.LoadAssetAtPath<Mesh>(meshFilePath);
}
Assert.IsNotNull(mesh);

//Create VFXAsset
{
var staticMeshOutput = ScriptableObject.CreateInstance<VFXStaticMeshOutput>();
var slots = staticMeshOutput.inputSlots.Where(o => o.value is Mesh).ToArray();
Assert.IsTrue(slots.Any());
foreach (var slot in slots)
{
if (slot.value is Mesh)
slot.value = mesh;
}
graph.AddChild(staticMeshOutput);

var particleOutput = ScriptableObject.CreateInstance<VFXMeshOutput>();
particleOutput.SetSettingValue("castShadows", true);
slots = particleOutput.inputSlots.Where(o => o.value is Mesh).ToArray();
Assert.IsTrue(slots.Any());
foreach (var slot in slots)
{
if (slot.value is Mesh)
slot.value = mesh;
}
graph.AddChild(particleOutput);

var contextInitialize = ScriptableObject.CreateInstance<VFXBasicInitialize>();
contextInitialize.LinkTo(particleOutput);
graph.AddChild(contextInitialize);

var spawner = ScriptableObject.CreateInstance<VFXBasicSpawner>();
spawner.LinkTo(contextInitialize);
graph.AddChild(spawner);

var burst = ScriptableObject.CreateInstance<VFXSpawnerBurst>();
spawner.AddChild(burst);
burst.inputSlots[0].value = 1.0f;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graph));
}
}

var cameraTransform = Camera.main.transform;
cameraTransform.localPosition = Vector3.one;
cameraTransform.LookAt(Vector3.zero);

//Create object and wait to have visible particles
GameObject currentObject = new GameObject("Delete_Mesh_While_Rendered_With_Output", /*typeof(Transform),*/ typeof(VisualEffect));
var vfx = currentObject.GetComponent<VisualEffect>();
vfx.visualEffectAsset = graph.visualEffectResource.asset;
int maxFrame = 64;
while (vfx.aliveParticleCount == 0 && maxFrame-- > 0)
yield return null;
Assert.IsTrue(maxFrame > 0);

//Delete Mesh & Wait a few frame
File.Delete(meshFilePath);
File.Delete(meshFilePath + ".meta");
for (int i = 0; i < 4; ++i)
yield return null;
AssetDatabase.Refresh();
for (int i = 0; i < 4; ++i)
yield return null;

//Check content from VFX
{
var meshOutput = graph.children.OfType<VFXMeshOutput>().First();
var meshSlot = meshOutput.inputSlots.First(o => o.property.type == typeof(Mesh));

var mesh = meshSlot.value as UnityEngine.Object;
Assert.IsTrue(mesh == null); //Mesh should be deleted at this point...
Assert.IsFalse(ReferenceEquals(mesh, null)); //... but expected missing reference
}

yield return new ExitPlayMode();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
using UnityEngine.UIElements;
using UnityEditor.Experimental.GraphView;
using System.IO;
using System.Runtime.CompilerServices;
#if VFX_HAS_TIMELINE
using UnityEngine.Playables;
using UnityEngine.Timeline;
#endif

[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.EditorTests")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.EditorTests-testable")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests-testable")]

namespace UnityEditor.VFX.Test
{
Expand Down Expand Up @@ -56,6 +64,7 @@ public static VFXGraph CopyTemporaryGraph(string path)
return graph;
}

#if VFX_HAS_TIMELINE
public static TimelineAsset CopyTemporaryTimeline(string path)
{
var guid = System.Guid.NewGuid().ToString();
Expand All @@ -67,6 +76,7 @@ public static TimelineAsset CopyTemporaryTimeline(string path)
var asset = AssetDatabase.LoadAssetAtPath<TimelineAsset>(tempFilePath);
return asset;
}
#endif

public static VFXViewController StartEditTestAsset()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "Unity.VisualEffectGraph.EditorTests",
"references": [
"Unity.Testing.VisualEffectGraph.Editor",
"Unity.Testing.VisualEffectGraph.EditorTests",
"Unity.VisualEffectGraph.Editor",
"Unity.VisualEffectGraph.Runtime",
"Unity.ShaderGraph.Editor",
Expand Down

0 comments on commit a484e13

Please sign in to comment.