Skip to content

Commit fd32284

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

18 files changed

Lines changed: 186 additions & 247 deletions

Packages/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ string RendererDataDisplayName(ScriptableRendererData data)
981981
}
982982

983983
#endif
984-
private static GraphicsFormat[][] s_LightCookieFormatList = new GraphicsFormat[][]
984+
private static readonly GraphicsFormat[][] k_LightCookieFormatList = new GraphicsFormat[][]
985985
{
986986
/* Grayscale Low */ new GraphicsFormat[] {GraphicsFormat.R8_UNorm},
987987
/* Grayscale High*/ new GraphicsFormat[] {GraphicsFormat.R16_UNorm},
@@ -995,7 +995,7 @@ internal GraphicsFormat additionalLightsCookieFormat
995995
get
996996
{
997997
GraphicsFormat result = GraphicsFormat.None;
998-
foreach (var format in s_LightCookieFormatList[(int)m_AdditionalLightsCookieFormat])
998+
foreach (var format in k_LightCookieFormatList[(int)m_AdditionalLightsCookieFormat])
999999
{
10001000
if (SystemInfo.IsFormatSupported(format, GraphicsFormatUsage.Render))
10011001
{

Packages/com.unity.render-pipelines.universal/Runtime/Decal/DBuffer/DBufferRenderPass.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public DecalDrawDBufferSystem(DecalEntityManager entityManager) : base("DecalDra
1414

1515
internal class DBufferRenderPass : ScriptableRenderPass
1616
{
17-
internal static string[] s_DBufferNames = { "_DBufferTexture0", "_DBufferTexture1", "_DBufferTexture2", "_DBufferTexture3" };
18-
internal static string s_DBufferDepthName = "DBufferDepth";
17+
internal static readonly string[] s_DBufferNames = { "_DBufferTexture0", "_DBufferTexture1", "_DBufferTexture2", "_DBufferTexture3" };
18+
internal static readonly string s_DBufferDepthName = "DBufferDepth";
1919
static readonly int s_SSAOTextureID = Shader.PropertyToID("_ScreenSpaceOcclusionTexture");
2020

2121
private DecalDrawDBufferSystem m_DrawSystem;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// way to being deprecated and removed in future releases
33
using System;
44
using System.ComponentModel;
5+
using Unity.Scripting.LifecycleManagement;
56

67
namespace UnityEngine.Rendering.Universal
78
{
@@ -24,14 +25,16 @@ public partial class AdditionalLightsShadowCasterPass
2425
/// The ID for the additional shadows buffer ID.
2526
/// This has been deprecated. Shadow slice matrix is now passed to the GPU using an entry in buffer m_AdditionalLightsWorldToShadow_SSBO.
2627
/// </summary>
27-
[Obsolete("AdditionalLightsShadowCasterPass.m_AdditionalShadowsBufferId was deprecated. Shadow slice matrix is now passed to the GPU using an entry in buffer m_AdditionalLightsWorldToShadow_SSBO #from(2021.1) #breakingFrom(2023.1)", true)]
28+
[Obsolete("AdditionalLightsShadowCasterPass.m_AdditionalShadowsBufferId was deprecated. Shadow slice matrix is now passed to the GPU using an entry in buffer m_AdditionalLightsWorldToShadow_SSBO. Fast Enter Playmode is not supported. #from(2021.1) #breakingFrom(2023.1)", true)]
29+
[NoAutoStaticsCleanup] // Obsolete API not supported
2830
public static int m_AdditionalShadowsBufferId;
2931

3032
/// <summary>
3133
/// The ID for the additional shadows buffer ID.
3234
/// This has been deprecated. hadow slice index is now passed to the GPU using last member of an entry in buffer m_AdditionalShadowParams_SSBO.
3335
/// </summary>
34-
[Obsolete("AdditionalLightsShadowCasterPass.m_AdditionalShadowsIndicesId was deprecated. Shadow slice index is now passed to the GPU using last member of an entry in buffer m_AdditionalShadowParams_SSBO #from(2021.1) #breakingFrom(2023.1)", true)]
36+
[Obsolete("AdditionalLightsShadowCasterPass.m_AdditionalShadowsIndicesId was deprecated. Shadow slice index is now passed to the GPU using last member of an entry in buffer m_AdditionalShadowParams_SSBO #from(2021.1). Fast Enter Playmode is not supported. #breakingFrom(2023.1)", true)]
37+
[NoAutoStaticsCleanup] // Obsolete API not supported
3538
public static int m_AdditionalShadowsIndicesId;
3639
}
3740
}
@@ -189,7 +192,7 @@ public TextureResources textures
189192
return m_Textures;
190193
}
191194
}
192-
195+
193196
/// <summary>
194197
/// Controls when URP renders via an intermediate texture.
195198
/// </summary>
@@ -603,7 +606,7 @@ public sealed class ShaderResources
603606
[Obsolete("Moved to UniversalRenderPipelineRuntimeShaders on GraphicsSettings. #from(2023.3)")]
604607
public Shader dataDrivenLensFlare;
605608
}
606-
609+
607610
partial class UniversalRenderPipelineGlobalSettings
608611
{
609612
#pragma warning disable 0414

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ internal struct Sorting
1010
{
1111
// Add profling samplers to sorts as they often are a bottleneck when scaling things up.
1212
// By default avoid sampling recursion, but these can be used externally as well.
13-
static public ProfilingSampler s_QuickSortSampler = new ProfilingSampler("QuickSort");
14-
static public ProfilingSampler s_InsertionSortSampler = new ProfilingSampler("InsertionSort");
13+
static public readonly ProfilingSampler s_QuickSortSampler = new ProfilingSampler("QuickSort");
14+
static public readonly ProfilingSampler s_InsertionSortSampler = new ProfilingSampler("InsertionSort");
1515

1616
public static void QuickSort<T>(T[] data, Func<T, T, int> compare)
1717
{

Packages/com.unity.render-pipelines.universal/Runtime/Passes/GBufferPass.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Unity.Collections;
3+
using Unity.Scripting.LifecycleManagement;
34
using UnityEngine.Rendering.RenderGraphModule;
45

56
namespace UnityEngine.Rendering.Universal.Internal
@@ -20,8 +21,16 @@ internal class GBufferPass : ScriptableRenderPass
2021

2122
DeferredLights m_DeferredLights;
2223

23-
static ShaderTagId[] s_ShaderTagValues;
24-
static RenderStateBlock[] s_RenderStateBlocks;
24+
static readonly ShaderTagId[] s_ShaderTagValues = {
25+
s_ShaderTagLit,
26+
s_ShaderTagSimpleLit,
27+
s_ShaderTagUnlit,
28+
s_ShaderTagComplexLit,
29+
s_ShaderTagBakedLit,
30+
new ShaderTagId() // Special catch all case for materials where UniversalMaterialType is not defined or the tag value doesn't match anything we know.
31+
};
32+
33+
RenderStateBlock[] m_RenderStateBlocks;
2534

2635
FilteringSettings m_FilteringSettings;
2736
RenderStateBlock m_RenderStateBlock;
@@ -39,17 +48,7 @@ public GBufferPass(RenderPassEvent evt, RenderQueueRange renderQueueRange, Layer
3948
m_RenderStateBlock.stencilReference = stencilReference;
4049
m_RenderStateBlock.mask = RenderStateMask.Stencil;
4150

42-
s_ShaderTagValues ??= new ShaderTagId[]
43-
{
44-
s_ShaderTagLit,
45-
s_ShaderTagSimpleLit,
46-
s_ShaderTagUnlit,
47-
s_ShaderTagComplexLit,
48-
s_ShaderTagBakedLit,
49-
new ShaderTagId() // Special catch all case for materials where UniversalMaterialType is not defined or the tag value doesn't match anything we know.
50-
};
51-
52-
s_RenderStateBlocks ??= new RenderStateBlock[]
51+
m_RenderStateBlocks = new RenderStateBlock[]
5352
{
5453
DeferredLights.OverwriteStencil(m_RenderStateBlock, (int)StencilUsage.MaterialMask, (int)StencilUsage.MaterialLit),
5554
DeferredLights.OverwriteStencil(m_RenderStateBlock, (int)StencilUsage.MaterialMask, (int)StencilUsage.MaterialSimpleLit),
@@ -123,7 +122,7 @@ private void InitRendererLists( ref PassData passData, ScriptableRenderContext c
123122
#endif
124123

125124
NativeArray<ShaderTagId> tagValues = new NativeArray<ShaderTagId>(s_ShaderTagValues, Allocator.Temp);
126-
NativeArray<RenderStateBlock> stateBlocks = new NativeArray<RenderStateBlock>(s_RenderStateBlocks, Allocator.Temp);
125+
NativeArray<RenderStateBlock> stateBlocks = new NativeArray<RenderStateBlock>(m_RenderStateBlocks, Allocator.Temp);
127126
var param = new RendererListParams(renderingData.cullResults, drawingSettings, filterSettings)
128127
{
129128
tagValues = tagValues,

Packages/com.unity.render-pipelines.universal/Runtime/Passes/ScriptableRenderPass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using Unity.Collections;
6+
using Unity.Scripting.LifecycleManagement;
67
using UnityEngine.Scripting.APIUpdating;
78
using UnityEngine.Experimental.Rendering;
89
using UnityEngine.Rendering.RenderGraphModule;
@@ -174,7 +175,8 @@ internal enum FramebufferFetchEvent
174175
internal static class RenderPassEventsEnumValues
175176
{
176177
// we cache the values in this array at construction time to avoid runtime allocations, which we would cause if we accessed valuesInternal directly
177-
public static int[] values;
178+
[NoAutoStaticsCleanup] // Clearing the array is not necessary when entering play mode because it's reflection data.
179+
public static readonly int[] values;
178180

179181
static RenderPassEventsEnumValues()
180182
{

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ internal class RTHandleResourcePool
2020

2121
protected static int s_CurrentStaleResourceCount = 0;
2222
// Keep stale resources alive for 3 frames
23-
protected static int s_StaleResourceLifetime = 3;
23+
protected const int k_StaleResourceLifetime = 3;
2424
// Store max 32 rtHandles
2525
// 1080p * 32bpp * 32 = 265.4mb
26-
protected static int s_StaleResourceMaxCapacity = 32;
26+
const int k_StaleResourceMaxCapacityDefault = 32;
27+
protected static int s_StaleResourceMaxCapacity = k_StaleResourceMaxCapacityDefault;
28+
29+
#if UNITY_EDITOR
30+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
31+
static void ResetStaticsOnLoad()
32+
{
33+
s_CurrentStaleResourceCount = 0;
34+
s_StaleResourceMaxCapacity = k_StaleResourceMaxCapacityDefault;
35+
}
36+
#endif
2737

2838
/// <summary>
2939
/// Controls the resource pool's max stale resource capacity.
@@ -103,7 +113,7 @@ static protected bool ShouldReleaseResource(int lastUsedFrameIndex, int currentF
103113
// We need to have a delay of a few frames before releasing resources for good.
104114
// Indeed, when having multiple off-screen cameras, they are rendered in a separate SRP render call and thus with a different frame index than main camera
105115
// This causes texture to be deallocated/reallocated every frame if the two cameras don't need the same buffers.
106-
return (lastUsedFrameIndex + s_StaleResourceLifetime) < currentFrameIndex;
116+
return (lastUsedFrameIndex + k_StaleResourceLifetime) < currentFrameIndex;
107117
}
108118

109119
// Release resources that are not used in last couple frames.

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

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

Packages/com.unity.render-pipelines.universal/Runtime/RenderTargetBufferSystem.cs.meta

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

Packages/com.unity.render-pipelines.universal/Runtime/RendererFeatures/FullScreenPassRendererFeature.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal class FullScreenRenderPass : ScriptableRenderPass
135135
private bool m_FetchActiveColor;
136136
private bool m_BindDepthStencilAttachment;
137137

138-
private static MaterialPropertyBlock s_SharedPropertyBlock = new MaterialPropertyBlock();
138+
private readonly MaterialPropertyBlock m_MaterialPropertyBlock = new MaterialPropertyBlock();
139139

140140
public FullScreenRenderPass(string passName)
141141
{
@@ -150,16 +150,16 @@ public void SetupMembers(Material material, int passIndex, bool fetchActiveColor
150150
m_BindDepthStencilAttachment = bindDepthStencilAttachment;
151151
}
152152

153-
private static void ExecuteMainPass(RasterCommandBuffer cmd, RTHandle sourceTexture, Material material, int passIndex, Vector4 blitScaleBias)
153+
private static void ExecuteMainPass(RasterCommandBuffer cmd, MaterialPropertyBlock mbp, RTHandle sourceTexture, Material material, int passIndex, Vector4 blitScaleBias)
154154
{
155-
s_SharedPropertyBlock.Clear();
155+
mbp.Clear();
156156
if (sourceTexture != null)
157-
s_SharedPropertyBlock.SetTexture(ShaderPropertyId.blitTexture, sourceTexture);
157+
mbp.SetTexture(ShaderPropertyId.blitTexture, sourceTexture);
158158

159159
// We need to set the "_BlitScaleBias" uniform for user materials with shaders relying on core Blit.hlsl to work
160-
s_SharedPropertyBlock.SetVector(ShaderPropertyId.blitScaleBias, blitScaleBias);
160+
mbp.SetVector(ShaderPropertyId.blitScaleBias, blitScaleBias);
161161

162-
cmd.DrawProcedural(Matrix4x4.identity, material, passIndex, MeshTopology.Triangles, 3, 1, s_SharedPropertyBlock);
162+
cmd.DrawProcedural(Matrix4x4.identity, material, passIndex, MeshTopology.Triangles, 3, 1, mbp);
163163
}
164164

165165
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
@@ -203,10 +203,11 @@ private void AddFullscreenRenderPassInputPass(RenderGraph renderGraph, Universal
203203
using (var builder = renderGraph.AddRasterRenderPass<MainPassData>(passName, out var passData, profilingSampler))
204204
{
205205
passData.material = m_Material;
206+
passData.materialPropertyBlock = m_MaterialPropertyBlock;
206207
passData.passIndex = m_PassIndex;
207208

208209
passData.source = source;
209-
passData.destination = destination;
210+
passData.destination = destination;
210211

211212
if (passData.source.IsValid())
212213
builder.UseTexture(passData.source, AccessFlags.Read);
@@ -255,13 +256,14 @@ private void AddFullscreenRenderPassInputPass(RenderGraph renderGraph, Universal
255256
builder.SetRenderFunc(static (MainPassData data, RasterGraphContext rgContext) =>
256257
{
257258
Vector4 scaleBias = RenderingUtils.GetFinalBlitScaleBias(rgContext, in data.source, in data.destination);
258-
ExecuteMainPass(rgContext.cmd, data.source, data.material, data.passIndex, scaleBias);
259+
ExecuteMainPass(rgContext.cmd, data.materialPropertyBlock, data.source, data.material, data.passIndex, scaleBias);
259260
});
260261
}
261262
}
262263
private class MainPassData
263264
{
264265
internal Material material;
266+
internal MaterialPropertyBlock materialPropertyBlock;
265267
internal int passIndex;
266268
internal TextureHandle source;
267269
internal TextureHandle destination;

0 commit comments

Comments
 (0)