Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 3c1e2f9

Browse files
committed
Better monitor handling
Monitors are now only rendered on request per-frame
1 parent b14b71e commit 3c1e2f9

7 files changed

Lines changed: 107 additions & 76 deletions

File tree

PostProcessing/Editor/PostProcessDebugEditor.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ namespace UnityEditor.Rendering.PostProcessing
77
public sealed class PostProcessDebugEditor : BaseEditor<PostProcessDebug>
88
{
99
SerializedProperty m_PostProcessLayer;
10+
SerializedProperty m_LightMeterEnabled;
11+
SerializedProperty m_HistogramEnabled;
12+
SerializedProperty m_WaveformEnabled;
13+
SerializedProperty m_VectorscopeEnabled;
1014

1115
SerializedObject m_Monitors;
12-
SerializedProperty m_LightMeterEnabled;
1316
SerializedProperty m_LightMeterShowCurves;
14-
SerializedProperty m_HistogramEnabled;
1517
SerializedProperty m_HistogramChannel;
16-
SerializedProperty m_WaveformEnabled;
1718
SerializedProperty m_WaveformExposure;
18-
SerializedProperty m_VectorscopeEnabled;
1919
SerializedProperty m_VectorscopeExposure;
2020

2121
void OnEnable()
2222
{
2323
m_PostProcessLayer = FindProperty(x => x.postProcessLayer);
24+
m_LightMeterEnabled = FindProperty(x => x.lightMeter);
25+
m_HistogramEnabled = FindProperty(x => x.histogram);
26+
m_WaveformEnabled = FindProperty(x => x.waveform);
27+
m_VectorscopeEnabled = FindProperty(x => x.vectorscope);
2428

2529
if (m_PostProcessLayer.objectReferenceValue != null)
2630
RebuildProperties();
@@ -33,16 +37,9 @@ void RebuildProperties()
3337

3438
m_Monitors = new SerializedObject(m_Target.postProcessLayer);
3539

36-
m_LightMeterEnabled = m_Monitors.FindProperty("monitors.lightMeter.enabled");
3740
m_LightMeterShowCurves = m_Monitors.FindProperty("monitors.lightMeter.showCurves");
38-
39-
m_HistogramEnabled = m_Monitors.FindProperty("monitors.histogram.enabled");
4041
m_HistogramChannel = m_Monitors.FindProperty("monitors.histogram.channel");
41-
42-
m_WaveformEnabled = m_Monitors.FindProperty("monitors.waveform.enabled");
4342
m_WaveformExposure = m_Monitors.FindProperty("monitors.waveform.exposure");
44-
45-
m_VectorscopeEnabled = m_Monitors.FindProperty("monitors.vectorscope.enabled");
4643
m_VectorscopeExposure = m_Monitors.FindProperty("monitors.vectorscope.exposure");
4744
}
4845

@@ -58,34 +55,19 @@ public override void OnInspectorGUI()
5855
RebuildProperties();
5956
}
6057

61-
serializedObject.ApplyModifiedProperties();
62-
63-
if (m_PostProcessLayer.objectReferenceValue == null)
64-
return;
65-
66-
if (AnyEnabled() && !m_Target.enabled)
67-
EditorGUILayout.HelpBox("The component is disabled but some monitors are still enabled and will be rendered internally. It is recommended to disable them to save performances unless they're needed elsewhere.", MessageType.Warning);
68-
else
69-
EditorGUILayout.Space();
58+
if (m_PostProcessLayer.objectReferenceValue != null)
59+
{
60+
m_Monitors.Update();
7061

71-
m_Monitors.Update();
72-
73-
DoMonitorGUI(EditorUtilities.GetContent("Light Meter"), m_LightMeterEnabled, m_LightMeterShowCurves);
74-
DoMonitorGUI(EditorUtilities.GetContent("Histogram"), m_HistogramEnabled, m_HistogramChannel);
75-
DoMonitorGUI(EditorUtilities.GetContent("Waveform"), m_WaveformEnabled, m_WaveformExposure);
76-
DoMonitorGUI(EditorUtilities.GetContent("Vectoscope"), m_VectorscopeEnabled, m_VectorscopeExposure);
62+
DoMonitorGUI(EditorUtilities.GetContent("Light Meter"), m_LightMeterEnabled, m_LightMeterShowCurves);
63+
DoMonitorGUI(EditorUtilities.GetContent("Histogram"), m_HistogramEnabled, m_HistogramChannel);
64+
DoMonitorGUI(EditorUtilities.GetContent("Waveform"), m_WaveformEnabled, m_WaveformExposure);
65+
DoMonitorGUI(EditorUtilities.GetContent("Vectoscope"), m_VectorscopeEnabled, m_VectorscopeExposure);
7766

78-
m_Monitors.ApplyModifiedProperties();
79-
}
67+
m_Monitors.ApplyModifiedProperties();
68+
}
8069

81-
bool AnyEnabled()
82-
{
83-
bool any = false;
84-
any |= m_LightMeterEnabled.boolValue;
85-
any |= m_HistogramEnabled.boolValue;
86-
any |= m_WaveformEnabled.boolValue;
87-
any |= m_VectorscopeEnabled.boolValue;
88-
return any;
70+
serializedObject.ApplyModifiedProperties();
8971
}
9072

9173
void DoMonitorGUI(GUIContent content, SerializedProperty prop, params SerializedProperty[] settings)

PostProcessing/Runtime/Monitors/HistogramMonitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ internal override void OnDisable()
3131
m_Data = null;
3232
}
3333

34+
internal override bool NeedsHalfRes()
35+
{
36+
return true;
37+
}
38+
3439
internal override void Render(PostProcessRenderContext context)
3540
{
3641
CheckOutput(width, height);

PostProcessing/Runtime/Monitors/Monitor.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
namespace UnityEngine.Rendering.PostProcessing
22
{
3+
public enum MonitorType
4+
{
5+
LightMeter,
6+
Histogram,
7+
Waveform,
8+
Vectorscope
9+
}
10+
311
public abstract class Monitor
412
{
5-
public bool enabled = false;
613
public RenderTexture output { get; protected set; }
714

8-
public bool IsEnabledAndSupported()
15+
internal bool requested = false;
16+
17+
public bool IsRequestedAndSupported()
918
{
10-
return enabled
19+
return requested
1120
&& SystemInfo.supportsComputeShaders;
1221
}
1322

23+
internal virtual bool NeedsHalfRes()
24+
{
25+
return false;
26+
}
27+
1428
protected void CheckOutput(int width, int height)
1529
{
1630
if (output == null || !output.IsCreated() || output.width != width || output.height != height)

PostProcessing/Runtime/Monitors/VectorscopeMonitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ internal override void OnDisable()
2121
m_Data = null;
2222
}
2323

24+
internal override bool NeedsHalfRes()
25+
{
26+
return true;
27+
}
28+
2429
internal override void Render(PostProcessRenderContext context)
2530
{
2631
CheckOutput(size, size);

PostProcessing/Runtime/Monitors/WaveformMonitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ internal override void OnDisable()
2020
m_Data = null;
2121
}
2222

23+
internal override bool NeedsHalfRes()
24+
{
25+
return true;
26+
}
27+
2328
internal override void Render(PostProcessRenderContext context)
2429
{
2530
// Waveform show localized data, so width depends on the aspect ratio

PostProcessing/Runtime/PostProcessDebug.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ public sealed class PostProcessDebug : MonoBehaviour
55
{
66
public PostProcessLayer postProcessLayer;
77

8+
public bool lightMeter;
9+
public bool histogram;
10+
public bool waveform;
11+
public bool vectorscope;
12+
813
void Reset()
914
{
1015
postProcessLayer = GetComponent<PostProcessLayer>();
1116
}
1217

18+
void Update()
19+
{
20+
if (lightMeter) postProcessLayer.monitors.RequestMonitorPass(MonitorType.LightMeter);
21+
if (histogram) postProcessLayer.monitors.RequestMonitorPass(MonitorType.Histogram);
22+
if (waveform) postProcessLayer.monitors.RequestMonitorPass(MonitorType.Waveform);
23+
if (vectorscope) postProcessLayer.monitors.RequestMonitorPass(MonitorType.Vectorscope);
24+
}
25+
1326
void OnGUI()
1427
{
1528
if (postProcessLayer == null || !postProcessLayer.enabled)
@@ -18,18 +31,15 @@ void OnGUI()
1831
var monitors = postProcessLayer.monitors;
1932
var rect = new Rect(5, 5, 0, 0);
2033

21-
DrawMonitor(ref rect, monitors.lightMeter);
22-
DrawMonitor(ref rect, monitors.histogram);
23-
DrawMonitor(ref rect, monitors.waveform);
24-
DrawMonitor(ref rect, monitors.vectorscope);
34+
DrawMonitor(ref rect, monitors.lightMeter, lightMeter);
35+
DrawMonitor(ref rect, monitors.histogram, histogram);
36+
DrawMonitor(ref rect, monitors.waveform, waveform);
37+
DrawMonitor(ref rect, monitors.vectorscope, vectorscope);
2538
}
2639

27-
void DrawMonitor(ref Rect rect, Monitor monitor)
40+
void DrawMonitor(ref Rect rect, Monitor monitor, bool enabled)
2841
{
29-
if (!monitor.IsEnabledAndSupported())
30-
return;
31-
32-
if (monitor.output == null)
42+
if (!enabled || monitor.output == null)
3343
return;
3444

3545
rect.width = monitor.output.width;

PostProcessing/Runtime/PostProcessMonitors.cs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace UnityEngine.Rendering.PostProcessing
45
{
@@ -10,6 +11,13 @@ public sealed class PostProcessMonitors
1011
public WaveformMonitor waveform;
1112
public VectorscopeMonitor vectorscope;
1213

14+
Dictionary<MonitorType, Monitor> m_Monitors;
15+
16+
public void RequestMonitorPass(MonitorType monitor)
17+
{
18+
m_Monitors[monitor].requested = true;
19+
}
20+
1321
internal void OnEnable()
1422
{
1523
if (lightMeter == null)
@@ -24,55 +32,57 @@ internal void OnEnable()
2432
if (vectorscope == null)
2533
vectorscope = new VectorscopeMonitor();
2634

27-
lightMeter.OnEnable();
28-
histogram.OnEnable();
29-
waveform.OnEnable();
30-
vectorscope.OnEnable();
35+
m_Monitors = new Dictionary<MonitorType, Monitor>
36+
{
37+
{ MonitorType.LightMeter, lightMeter },
38+
{ MonitorType.Histogram, histogram },
39+
{ MonitorType.Waveform, waveform },
40+
{ MonitorType.Vectorscope, vectorscope }
41+
};
42+
43+
foreach (var kvp in m_Monitors)
44+
kvp.Value.OnEnable();
3145
}
3246

3347
internal void OnDisable()
3448
{
35-
lightMeter.OnDisable();
36-
histogram.OnDisable();
37-
waveform.OnDisable();
38-
vectorscope.OnDisable();
49+
foreach (var kvp in m_Monitors)
50+
kvp.Value.OnDisable();
3951
}
4052

4153
internal void Render(PostProcessRenderContext context)
4254
{
43-
bool lightMeterActive = lightMeter.IsEnabledAndSupported();
44-
bool histogramActive = histogram.IsEnabledAndSupported();
45-
bool waveformActive = waveform.IsEnabledAndSupported();
46-
bool vectorscopeActive = vectorscope.IsEnabledAndSupported();
47-
bool needHalfRes = histogramActive || vectorscopeActive || waveformActive;
48-
bool anyActive = lightMeterActive
49-
|| histogramActive
50-
|| waveformActive
51-
|| vectorscopeActive;
55+
bool anyActive = false;
56+
bool needsHalfRes = false;
57+
58+
foreach (var kvp in m_Monitors)
59+
{
60+
bool active = kvp.Value.IsRequestedAndSupported();
61+
anyActive |= active;
62+
needsHalfRes |= active && kvp.Value.NeedsHalfRes();
63+
}
5264

5365
var cmd = context.command;
5466
if (anyActive)
5567
cmd.BeginSample("Monitors");
5668

57-
if (needHalfRes)
69+
if (needsHalfRes)
5870
{
5971
cmd.GetTemporaryRT(ShaderIDs.HalfResFinalCopy, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, context.sourceFormat);
6072
cmd.Blit(context.destination, ShaderIDs.HalfResFinalCopy);
6173
}
6274

63-
if (lightMeterActive)
64-
lightMeter.Render(context);
65-
66-
if (histogramActive)
67-
histogram.Render(context);
75+
foreach (var kvp in m_Monitors)
76+
{
77+
var monitor = kvp.Value;
6878

69-
if (waveformActive)
70-
waveform.Render(context);
79+
if (monitor.requested)
80+
monitor.Render(context);
7181

72-
if (vectorscopeActive)
73-
vectorscope.Render(context);
82+
monitor.requested = false;
83+
}
7484

75-
if (needHalfRes)
85+
if (needsHalfRes)
7686
cmd.ReleaseTemporaryRT(ShaderIDs.HalfResFinalCopy);
7787

7888
if (anyActive)

0 commit comments

Comments
 (0)