Skip to content

Commit 1800f2b

Browse files
giulia-u3dEvergreen
authored andcommitted
BugFix: RTHandle doesn't try to destroy persistent object in editor (UUM-36359)
Fix for [UUM-36359](https://jira.unity3d.com/browse/UUM-36359). Originally `RTHandle` manages external texture but doesn't have the same check for `RenderTexture` that generated as external resources.
1 parent ed973f4 commit 1800f2b

File tree

7 files changed

+30
-12
lines changed

7 files changed

+30
-12
lines changed

Packages/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using System.Collections.Generic;
33
using UnityEngine.Rendering;
44

5+
#if UNITY_EDITOR
6+
using UnityEditor;
7+
#endif
8+
59
namespace UnityEngine.Rendering
610
{
711
/// <summary>
@@ -64,6 +68,7 @@ public class RTHandle
6468
internal bool m_EnableMSAA = false;
6569
internal bool m_EnableRandomWrite = false;
6670
internal bool m_EnableHWDynamicScale = false;
71+
internal bool m_RTHasOwnership = true;
6772
internal string m_Name;
6873

6974
internal bool m_UseCustomHandleScales = false;
@@ -175,10 +180,11 @@ public static implicit operator RenderTexture(RTHandle handle)
175180
return handle.rt;
176181
}
177182

178-
internal void SetRenderTexture(RenderTexture rt)
183+
internal void SetRenderTexture(RenderTexture rt, bool transferOwnership = true)
179184
{
180185
m_RT = rt;
181186
m_ExternalTexture = null;
187+
m_RTHasOwnership = transferOwnership;
182188
m_NameID = new RenderTargetIdentifier(rt);
183189
}
184190

@@ -216,10 +222,12 @@ public int GetInstanceID()
216222
public void Release()
217223
{
218224
m_Owner.Remove(this);
219-
CoreUtils.Destroy(m_RT);
225+
if (m_RTHasOwnership)
226+
CoreUtils.Destroy(m_RT);
220227
m_NameID = BuiltinRenderTextureType.None;
221228
m_RT = null;
222229
m_ExternalTexture = null;
230+
m_RTHasOwnership = true;
223231
}
224232

225233
/// <summary>

Packages/com.unity.render-pipelines.core/Runtime/Textures/RTHandleSystem.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
using UnityEngine.Assertions;
44
using UnityEngine.Experimental.Rendering;
55

6+
//#if UNITY_EDITOR
7+
using UnityEditor;
8+
//#endif
9+
610
namespace UnityEngine.Rendering
711
{
812
/// <summary>
@@ -1162,11 +1166,16 @@ RTHandle AllocAutoSizedRenderTexture(int width, int height, RTHandleAllocInfo in
11621166
/// Allocate a RTHandle from a regular RenderTexture.
11631167
/// </summary>
11641168
/// <param name="texture">Input texture</param>
1169+
/// <param name="transferOwnership">Says if the RTHandleSystem has the ownership of the external RenderTarget, false by default</param>
11651170
/// <returns>A new RTHandle referencing the input texture.</returns>
1166-
public RTHandle Alloc(RenderTexture texture)
1171+
public RTHandle Alloc(RenderTexture texture, bool transferOwnership = false)
11671172
{
11681173
var rth = new RTHandle(this);
1169-
rth.SetRenderTexture(texture);
1174+
#if UNITY_EDITOR
1175+
Debug.Assert(!(EditorUtility.IsPersistent(texture) == true && transferOwnership == true),
1176+
"RTHandle should not have ownership of RenderTarget asset, set transfer ownership as false");
1177+
#endif
1178+
rth.SetRenderTexture(texture, transferOwnership);
11701179
rth.m_EnableMSAA = false;
11711180
rth.m_EnableRandomWrite = false;
11721181
rth.useScaling = false;

Packages/com.unity.render-pipelines.core/Runtime/Textures/RTHandles.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,11 @@ public static RTHandle Alloc(Texture tex)
533533
/// Allocate a RTHandle from a regular RenderTexture for the default RTHandle system.
534534
/// </summary>
535535
/// <param name="tex">Input texture</param>
536+
/// <param name="transferOwnership">To transfer ownership of the RenderTexture to the default RTHandles system, false by default</param>
536537
/// <returns>A new RTHandle referencing the input texture.</returns>
537-
public static RTHandle Alloc(RenderTexture tex)
538+
public static RTHandle Alloc(RenderTexture tex, bool transferOwnership = false)
538539
{
539-
return s_DefaultInstance.Alloc(tex);
540+
return s_DefaultInstance.Alloc(tex, transferOwnership);
540541
}
541542

542543
/// <summary>

Packages/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionLayer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void Init(string layerID = "", bool allowUndo = false)
299299
// check and fix RT handle
300300
if (m_OutputTarget != OutputTarget.CameraStack && m_RTHandle == null && m_RenderTarget != null)
301301
{
302-
m_RTHandle = RTHandles.Alloc(m_RenderTarget);
302+
m_RTHandle = RTHandles.Alloc(m_RenderTarget, transferOwnership: true);
303303
}
304304

305305
if (m_OutputTarget != OutputTarget.CameraStack && m_AOVBitmask != MaterialSharedProperty.None)
@@ -320,7 +320,7 @@ public void Init(string layerID = "", bool allowUndo = false)
320320
{
321321
m_AOVMap[aovNames[i]] = outputIndex;
322322
m_AOVRenderTargets.Add(new RenderTexture(pixelWidth, pixelHeight, 24, (GraphicsFormat)m_ColorBufferFormat));
323-
m_AOVHandles.Add(RTHandles.Alloc(m_AOVRenderTargets[outputIndex]));
323+
m_AOVHandles.Add(RTHandles.Alloc(m_AOVRenderTargets[outputIndex], transferOwnership: true));
324324
outputIndex++;
325325
}
326326
}

Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Reflection/HDProbe.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public RenderTexture realtimeTexture
410410
{
411411
if (m_RealtimeTexture != null)
412412
m_RealtimeTexture.Release();
413-
m_RealtimeTexture = RTHandles.Alloc(value);
413+
m_RealtimeTexture = RTHandles.Alloc(value, transferOwnership: true);
414414
m_RealtimeTexture.rt.name = $"ProbeRealTimeTexture_{name}";
415415
}
416416
}
@@ -428,7 +428,7 @@ public RenderTexture realtimeDepthTexture
428428
{
429429
if (m_RealtimeDepthBuffer != null)
430430
m_RealtimeDepthBuffer.Release();
431-
m_RealtimeDepthBuffer = RTHandles.Alloc(value);
431+
m_RealtimeDepthBuffer = RTHandles.Alloc(value, transferOwnership: true);
432432
m_RealtimeDepthBuffer.rt.name = $"ProbeRealTimeDepthTexture_{name}";
433433
}
434434
}

Packages/com.unity.render-pipelines.universal/Runtime/ReflectionProbeManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Init()
8383
m_AtlasTexture0.filterMode = FilterMode.Bilinear;
8484
m_AtlasTexture0.hideFlags = HideFlags.HideAndDontSave;
8585
m_AtlasTexture0.Create();
86-
m_AtlasTexture0Handle = RTHandles.Alloc(m_AtlasTexture0);
86+
m_AtlasTexture0Handle = RTHandles.Alloc(m_AtlasTexture0, transferOwnership: true);
8787

8888
m_AtlasTexture1 = new RenderTexture(m_AtlasTexture0.descriptor);
8989
m_AtlasTexture1.name = k_ReflectionProbeAtlasName;

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererDebug.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void SetupRenderGraphFinalPassDebug(RenderGraph renderGraph, ContextCont
7878
width = targetWidth;
7979
}
8080

81-
m_RenderGraphDebugTextureHandle = RTHandles.Alloc(m_ForwardLights.reflectionProbeManager.atlasRT);
81+
m_RenderGraphDebugTextureHandle = RTHandles.Alloc(m_ForwardLights.reflectionProbeManager.atlasRT, transferOwnership: true);
8282
}
8383
else // visualize RG internal resources
8484
{

0 commit comments

Comments
 (0)