Skip to content

Commit f5ee508

Browse files
arttu-peltonenEvergreen
authored andcommitted
Reset static variables for CoreCLR / Fast Enter Play Mode (SRP Core Team, render-pipelines.high-definition package)
1 parent eb10b20 commit f5ee508

25 files changed

Lines changed: 241 additions & 300 deletions

Packages/com.unity.render-pipelines.high-definition/Runtime/Core/CoreResources/GPUCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public GPUCopy(ComputeShader shader)
1616
static readonly int _RectOffset = Shader.PropertyToID("_RectOffset");
1717
static readonly int _Result1 = Shader.PropertyToID("_Result1");
1818
static readonly int _Source4 = Shader.PropertyToID("_Source4");
19-
static int[] _IntParams = new int[2];
19+
static readonly int[] _IntParams = new int[2];
2020

2121
void SampleCopyChannel(
2222
CommandBuffer cmd,

Packages/com.unity.render-pipelines.high-definition/Runtime/Core/Debugging/FrameSettingsFieldAttribute.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text;
33
using System.Collections.Generic;
44
using System.Reflection;
5+
using Unity.Scripting.LifecycleManagement;
56

67
namespace UnityEngine.Rendering.HighDefinition
78
{
@@ -42,9 +43,10 @@ public enum DisplayType { BoolAsCheckbox, BoolAsEnumPopup, Others }
4243
public readonly FrameSettingsField[] dependencies;
4344
private readonly int dependencySeparator;
4445

45-
static int autoOrder = 0;
46-
47-
private static Dictionary<FrameSettingsField, string> s_FrameSettingsEnumNameMap = null;
46+
// No cleanup needed (only modified when FrameSettingsFieldAttribute are instantiated)
47+
[NoAutoStaticsCleanup] static int s_AutoOrder = 0;
48+
// No cleanup needed (reflection data cache)
49+
[NoAutoStaticsCleanup] static Dictionary<FrameSettingsField, string> s_FrameSettingsEnumNameMap = null;
4850

4951
public static Dictionary<FrameSettingsField, string> GetEnumNameMap()
5052
{
@@ -103,8 +105,8 @@ public FrameSettingsFieldAttribute(
103105
// Editor and Runtime debug menu
104106
this.group = group;
105107
if (customOrderInGroup != -1)
106-
autoOrder = customOrderInGroup; //start again numbering from this value
107-
this.orderInGroup = autoOrder++;
108+
s_AutoOrder = customOrderInGroup; //start again numbering from this value
109+
this.orderInGroup = s_AutoOrder++;
108110
this.displayedName = displayedName;
109111
this.type = type;
110112
this.targetType = targetType;

Packages/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/EncodeBC6H.cs

Lines changed: 0 additions & 123 deletions
This file was deleted.

Packages/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/EncodeBC6H.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Packages/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCache.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private struct SliceEntry
3939
private Texture[] m_autoContentArray = new Texture[1];
4040

4141
// Constant values
42-
private static uint g_MaxFrameCount = unchecked((uint)(-1));
42+
const uint k_MaxFrameCount = unchecked((uint)(-1));
4343

4444
protected const int k_FP16SizeInByte = 2;
4545
protected const int k_NbChannel = 4;
@@ -81,7 +81,7 @@ protected bool AllocTextureArray(int numTextures)
8181
m_NumTextures = numTextures / m_SliceSize;
8282
for (int i = 0; i < m_NumTextures; i++)
8383
{
84-
m_SliceArray[i].countLRU = g_MaxFrameCount; // never used before
84+
m_SliceArray[i].countLRU = k_MaxFrameCount; // never used before
8585
m_SliceArray[i].texId = EntityId.None;
8686
m_SortedIdxArray[i] = i;
8787
}
@@ -208,7 +208,7 @@ public int FetchSlice(CommandBuffer cmd, Texture texture, uint textureHash, bool
208208
return sliceIndex;
209209
}
210210

211-
private static List<int> s_TempIntList = new List<int>();
211+
private static readonly List<int> s_TempIntList = new List<int>(); // No clear needed for static member, always cleared before usage
212212
public void NewFrame()
213213
{
214214
var numNonZeros = 0;
@@ -235,7 +235,7 @@ public void NewFrame()
235235

236236
for (int i = 0; i < m_NumTextures; i++)
237237
{
238-
if (m_SliceArray[i].countLRU < g_MaxFrameCount) ++m_SliceArray[i].countLRU; // next frame
238+
if (m_SliceArray[i].countLRU < k_MaxFrameCount) ++m_SliceArray[i].countLRU; // next frame
239239
}
240240

241241
//for(int q=1; q<m_numTextures; q++)
@@ -279,7 +279,7 @@ public void RemoveEntryFromSlice(Texture texture)
279279

280280
// delete from m_locatorInSliceArray and m_pSliceArray.
281281
m_LocatorInSliceDictionnary.Remove(texId);
282-
m_SliceArray[sliceIndex].countLRU = g_MaxFrameCount; // never used before
282+
m_SliceArray[sliceIndex].countLRU = k_MaxFrameCount; // never used before
283283
m_SliceArray[sliceIndex].texId = EntityId.None;
284284
}
285285

Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using Unity.Scripting.LifecycleManagement;
56
using UnityEditor.Rendering;
67
using UnityEngine.Rendering.HighDefinition.Attributes;
78
using UnityEngine.Rendering.RenderGraphModule;
@@ -254,24 +255,25 @@ public enum DepthPyramidDebugView
254255
[Serializable]
255256
public partial class DebugDisplaySettings : IDebugData, ISerializedDebugDisplaySettings
256257
{
257-
static string k_PanelMaterials = "Material";
258-
static string k_PanelLighting = "Lighting";
259-
static string k_PanelRendering = "Rendering";
258+
static readonly string k_PanelMaterials = "Material";
259+
static readonly string k_PanelLighting = "Lighting";
260+
static readonly string k_PanelRendering = "Rendering";
260261

261262
DebugUI.Widget[] m_DebugMaterialItems;
262263
DebugUI.Widget[] m_DebugLightingItems;
263264
DebugUI.Widget[] m_DebugRenderingItems;
264265

265-
static GUIContent[] s_LightingFullScreenDebugStrings = null;
266-
static int[] s_LightingFullScreenDebugValues = null;
267-
static GUIContent[] s_RenderingFullScreenDebugStrings = null;
268-
static int[] s_RenderingFullScreenDebugValues = null;
269-
static GUIContent[] s_MaterialFullScreenDebugStrings = null;
270-
static int[] s_MaterialFullScreenDebugValues = null;
271-
static GUIContent[] s_RenderingHistoryBuffersStrings = null;
272-
static int[] s_RenderingHistoryBuffersValues = null;
273-
static GUIContent[] s_RenderingMipmapDebugMaterialTextureSlotStrings = null;
274-
static int[] s_RenderingMipmapDebugMaterialTextureSlotValues = null;
266+
// No cleanup needed (populated once and not modified)
267+
[NoAutoStaticsCleanup] static GUIContent[] s_LightingFullScreenDebugStrings = null;
268+
[NoAutoStaticsCleanup] static int[] s_LightingFullScreenDebugValues = null;
269+
[NoAutoStaticsCleanup] static GUIContent[] s_RenderingFullScreenDebugStrings = null;
270+
[NoAutoStaticsCleanup] static int[] s_RenderingFullScreenDebugValues = null;
271+
[NoAutoStaticsCleanup] static GUIContent[] s_MaterialFullScreenDebugStrings = null;
272+
[NoAutoStaticsCleanup] static int[] s_MaterialFullScreenDebugValues = null;
273+
[NoAutoStaticsCleanup] static GUIContent[] s_RenderingHistoryBuffersStrings = null;
274+
[NoAutoStaticsCleanup] static int[] s_RenderingHistoryBuffersValues = null;
275+
[NoAutoStaticsCleanup] static GUIContent[] s_RenderingMipmapDebugMaterialTextureSlotStrings = null;
276+
[NoAutoStaticsCleanup] static int[] s_RenderingMipmapDebugMaterialTextureSlotValues = null;
275277

276278
#if ENABLE_NVIDIA && ENABLE_NVIDIA_MODULE
277279
internal UnityEngine.NVIDIA.DebugView nvidiaDebugView { get; } = new UnityEngine.NVIDIA.DebugView();

Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/CustomPostProcessing/CustomPostProcessVolumeComponent.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine.Rendering;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Unity.Scripting.LifecycleManagement;
45

56
namespace UnityEngine.Rendering.HighDefinition
67
{
@@ -16,7 +17,8 @@ public abstract class CustomPostProcessVolumeComponent : VolumeComponent
1617
internal string passName { get; private set; }
1718

1819
// Keep track of all the instances alive of the custom post process component so we can release them when needed
19-
internal static HashSet<CustomPostProcessVolumeComponent> instances = new HashSet<CustomPostProcessVolumeComponent>();
20+
[NoAutoStaticsCleanup] // Cleaned up when pipeline is disposed by calling CleanupAllCustomPostProcesses()
21+
internal static readonly HashSet<CustomPostProcessVolumeComponent> instances = new HashSet<CustomPostProcessVolumeComponent>();
2022

2123
/// <summary>
2224
/// Injection point of the custom post process in HDRP.

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityEngine.Experimental.Rendering;
66
using UnityEngine.Rendering.RenderGraphModule;
77
using Unity.Collections;
8+
using Unity.Scripting.LifecycleManagement;
89

910
namespace UnityEngine.Rendering.HighDefinition
1011
{
@@ -1945,8 +1946,8 @@ internal bool isSceneViewCameraWithExposureOverride
19451946
&& m_AdditionalCameraData.doesSceneViewOverrideExposure;
19461947

19471948
internal float sceneViewExposureOverride
1948-
=> m_AdditionalCameraData == null
1949-
? 10f
1949+
=> m_AdditionalCameraData == null
1950+
? 10f
19501951
: m_AdditionalCameraData.sceneViewOverrideExposureValue;
19511952
#endif
19521953

@@ -1955,9 +1956,10 @@ internal float sceneViewExposureOverride
19551956

19561957
#region Private API
19571958

1959+
// s_Cameras and s_Cleanup are cleared when pipeline is disposed by calling HDCamera.ClearAll()
1960+
[NoAutoStaticsCleanup] static readonly Dictionary<(Camera, int, HistoryChannel), HDCamera> s_Cameras = new Dictionary<(Camera, int, HistoryChannel), HDCamera>();
1961+
[NoAutoStaticsCleanup] static readonly List<(Camera, int, HistoryChannel)> s_Cleanup = new List<(Camera, int, HistoryChannel)>(); // Recycled to reduce GC pressure
19581962

1959-
static Dictionary<(Camera, int, HistoryChannel), HDCamera> s_Cameras = new Dictionary<(Camera, int, HistoryChannel), HDCamera>();
1960-
static List<(Camera, int, HistoryChannel)> s_Cleanup = new List<(Camera, int, HistoryChannel)>(); // Recycled to reduce GC pressure
19611963
HDAdditionalCameraData m_AdditionalCameraData = null; // Init in Update
19621964
BufferedRTHandleSystem m_HistoryRTSystem = new BufferedRTHandleSystem();
19631965
int m_HistoryViewCount = 0; // Used to track view count change if XR is enabled/disabled

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/CullingGroupManager.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ namespace UnityEngine.Rendering.HighDefinition
44
{
55
class CullingGroupManager
66
{
7-
static CullingGroupManager m_Instance;
8-
static public CullingGroupManager instance
7+
static CullingGroupManager s_Instance;
8+
9+
#if UNITY_EDITOR
10+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
11+
static void ResetStaticsOnLoad()
912
{
10-
get
11-
{
12-
if (m_Instance == null)
13-
m_Instance = new CullingGroupManager();
14-
return m_Instance;
15-
}
13+
s_Instance?.Cleanup();
14+
s_Instance = null;
1615
}
16+
#endif
17+
18+
public static CullingGroupManager instance => s_Instance ??= new CullingGroupManager();
1719

1820
private Stack<CullingGroup> m_FreeList = new Stack<CullingGroup>();
1921

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDDynamicResolution.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
/// </remarks>
1414
public class HDDynamicResolution : MonoBehaviour
1515
{
16+
#if UNITY_EDITOR
17+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
18+
static void ResetStaticsOnLoad()
19+
{
20+
ResetScale();
21+
}
22+
#endif
23+
1624
/// <summary>
1725
/// Target frame rate for dynamic resolution. If Application.targetFrameRate is already set, Application.targetFrameRate overrides this parameter.
1826
/// </summary>

0 commit comments

Comments
 (0)