Skip to content

Commit 35140d9

Browse files
Cache base types of Volume Manager to improve perf/memory #3804
1 parent 4460168 commit 35140d9

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

com.unity.render-pipelines.core/Editor/Volume/VolumeComponentProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void CreateComponentTree(List<Element> tree)
5151
{
5252
tree.Add(new GroupElement(0, "Volume Overrides"));
5353

54-
var types = VolumeManager.instance.baseComponentTypes;
54+
var types = VolumeManager.instance.baseComponentTypeArray;
5555
var rootNode = new PathNode();
5656

5757
foreach (var t in types)

com.unity.render-pipelines.core/Runtime/Volume/VolumeManager.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ public sealed class VolumeManager
3030
/// <summary>
3131
/// The current list of all available types that derive from <see cref="VolumeComponent"/>.
3232
/// </summary>
33-
public IEnumerable<Type> baseComponentTypes { get; private set; }
33+
[Obsolete("Please use baseComponentTypeArray instead.")]
34+
public IEnumerable<Type> baseComponentTypes
35+
{
36+
get
37+
{
38+
return baseComponentTypeArray;
39+
}
40+
private set
41+
{
42+
baseComponentTypeArray = value.ToArray();
43+
}
44+
}
45+
46+
public Type[] baseComponentTypeArray { get; private set; }
3447

3548
// Max amount of layers available in Unity
3649
const int k_MaxLayerCount = 32;
@@ -75,7 +88,7 @@ public sealed class VolumeManager
7588
public VolumeStack CreateStack()
7689
{
7790
var stack = new VolumeStack();
78-
stack.Reload(baseComponentTypes);
91+
stack.Reload(baseComponentTypeArray);
7992
return stack;
8093
}
8194

@@ -95,13 +108,13 @@ void ReloadBaseTypes()
95108
m_ComponentsDefaultState.Clear();
96109

97110
// Grab all the component types we can find
98-
baseComponentTypes = CoreUtils.GetAllTypesDerivedFrom<VolumeComponent>()
99-
.Where(t => !t.IsAbstract);
111+
baseComponentTypeArray = CoreUtils.GetAllTypesDerivedFrom<VolumeComponent>()
112+
.Where(t => !t.IsAbstract).ToArray();
100113

101114
var flags = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
102115
// Keep an instance of each type to be used in a virtual lowest priority global volume
103116
// so that we have a default state to fallback to when exiting volumes
104-
foreach (var type in baseComponentTypes)
117+
foreach (var type in baseComponentTypeArray)
105118
{
106119
type.GetMethod("Init", flags)?.Invoke(null, null);
107120
var inst = (VolumeComponent)ScriptableObject.CreateInstance(type);
@@ -262,15 +275,15 @@ public void CheckStack(VolumeStack stack)
262275

263276
if (components == null)
264277
{
265-
stack.Reload(baseComponentTypes);
278+
stack.Reload(baseComponentTypeArray);
266279
return;
267280
}
268281

269282
foreach (var kvp in components)
270283
{
271284
if (kvp.Key == null || kvp.Value == null)
272285
{
273-
stack.Reload(baseComponentTypes);
286+
stack.Reload(baseComponentTypeArray);
274287
return;
275288
}
276289
}

com.unity.render-pipelines.core/Runtime/Volume/VolumeStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal VolumeStack()
1818
{
1919
}
2020

21-
internal void Reload(IEnumerable<Type> baseTypes)
21+
internal void Reload(Type[] baseTypes)
2222
{
2323
if (components == null)
2424
components = new Dictionary<Type, VolumeComponent>();

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
156156
- Tidy up of platform abstraction code for shader optimization.
157157
- Changed Path Tracing's maximum intensity from clamped (0 to 100) to positive value (case 1310514).
158158
- Avoid unnecessary RenderGraphBuilder.ReadTexture in the "Set Final Target" pass
159+
- Cached the base types of Volume Manager to improve memory and cpu usage.
159160

160161
## [10.3.0] - 2020-12-01
161162

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static public List<Type> componentTypes
130130
{
131131
if (s_ComponentTypes == null)
132132
{
133-
s_ComponentTypes = VolumeManager.instance.baseComponentTypes
133+
s_ComponentTypes = VolumeManager.instance.baseComponentTypeArray
134134
.Where(t => !t.IsDefined(typeof(VolumeComponentDeprecated), false))
135135
.OrderBy(t => ComponentDisplayName(t))
136136
.ToList();

0 commit comments

Comments
 (0)