-
Notifications
You must be signed in to change notification settings - Fork 873
Expand file tree
/
Copy pathPostProcessDebugLayer.cs
More file actions
368 lines (308 loc) · 11.9 KB
/
Copy pathPostProcessDebugLayer.cs
File metadata and controls
368 lines (308 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering.PostProcessing
{
/// <summary>
/// A list of debug overlays.
/// </summary>
public enum DebugOverlay
{
/// <summary>
/// No overlay.
/// </summary>
None,
/// <summary>
/// Displays the depth buffer.
/// </summary>
Depth,
/// <summary>
/// Displays the screen-space normals buffer.
/// </summary>
Normals,
/// <summary>
/// Displays the screen-space motion vectors.
/// </summary>
MotionVectors,
/// <summary>
/// Dims the screen and displays NaN and Inf pixels with a bright pink color.
/// </summary>
NANTracker,
/// <summary>
/// A color blindness simulator.
/// </summary>
ColorBlindnessSimulation,
/// <summary>
/// A menu item separator for the inspector. Do not use.
/// </summary>
_,
/// <summary>
/// Displays the raw ambient occlusion map.
/// </summary>
AmbientOcclusion,
/// <summary>
/// Displays the bloom buffer.
/// </summary>
BloomBuffer,
/// <summary>
/// Displays the thresholded buffer used to generate bloom.
/// </summary>
BloomThreshold,
/// <summary>
/// Displays depth of field helpers.
/// </summary>
DepthOfField
}
/// <summary>
/// A list of color blindness types.
/// </summary>
public enum ColorBlindnessType
{
/// <summary>
/// Deuteranopia (red-green color blindness).
/// </summary>
Deuteranopia,
/// <summary>
/// Protanopia (red-green color blindness).
/// </summary>
Protanopia,
/// <summary>
/// Tritanopia (blue-yellow color blindness).
/// </summary>
Tritanopia
}
/// <summary>
/// This class centralizes rendering commands for debug modes.
/// </summary>
[Serializable]
public sealed class PostProcessDebugLayer
{
/// <summary>
/// Light meter renderer.
/// </summary>
public LightMeterMonitor lightMeter;
/// <summary>
/// Histogram renderer.
/// </summary>
public HistogramMonitor histogram;
/// <summary>
/// Waveform renderer.
/// </summary>
public WaveformMonitor waveform;
/// <summary>
/// Vectorscope monitor.
/// </summary>
public VectorscopeMonitor vectorscope;
Dictionary<MonitorType, Monitor> m_Monitors;
// Current frame size
int frameWidth;
int frameHeight;
/// <summary>
/// The render target used to render debug overlays in.
/// </summary>
public RenderTexture debugOverlayTarget { get; private set; }
/// <summary>
/// Returns <c>true</c> if the frame that was just drawn had an active debug overlay.
/// </summary>
public bool debugOverlayActive { get; private set; }
/// <summary>
/// The debug overlay requested for the current frame. It is reset to <c>None</c> once the
/// frame has finished rendering.
/// </summary>
public DebugOverlay debugOverlay { get; private set; }
/// <summary>
/// Debug overlay settings wrapper.
/// </summary>
[Serializable]
public class OverlaySettings
{
/// <summary>
/// Should we remap depth to a linear range?
/// </summary>
public bool linearDepth = false;
/// <summary>
/// The intensity of motion vector colors.
/// </summary>
[Range(0f, 16f)]
public float motionColorIntensity = 4f;
/// <summary>
/// The size of the motion vector grid.
/// </summary>
[Range(4, 128)]
public int motionGridSize = 64;
/// <summary>
/// The color blindness type to simulate.
/// </summary>
public ColorBlindnessType colorBlindnessType = ColorBlindnessType.Deuteranopia;
/// <summary>
/// The strength of the selected color blindness type.
/// </summary>
[Range(0f, 1f)]
public float colorBlindnessStrength = 1f;
}
/// <summary>
/// Debug overlay settings.
/// </summary>
public OverlaySettings overlaySettings;
internal void OnEnable()
{
RuntimeUtilities.CreateIfNull(ref lightMeter);
RuntimeUtilities.CreateIfNull(ref histogram);
RuntimeUtilities.CreateIfNull(ref waveform);
RuntimeUtilities.CreateIfNull(ref vectorscope);
RuntimeUtilities.CreateIfNull(ref overlaySettings);
m_Monitors = new Dictionary<MonitorType, Monitor>
{
{ MonitorType.LightMeter, lightMeter },
{ MonitorType.Histogram, histogram },
{ MonitorType.Waveform, waveform },
{ MonitorType.Vectorscope, vectorscope }
};
foreach (var kvp in m_Monitors)
kvp.Value.OnEnable();
}
internal void OnDisable()
{
foreach (var kvp in m_Monitors)
kvp.Value.OnDisable();
DestroyDebugOverlayTarget();
}
void DestroyDebugOverlayTarget()
{
RuntimeUtilities.Destroy(debugOverlayTarget);
debugOverlayTarget = null;
}
/// <summary>
/// Requests the drawing of a monitor for the current frame.
/// </summary>
/// <param name="monitor">The monitor to request</param>
public void RequestMonitorPass(MonitorType monitor)
{
m_Monitors[monitor].requested = true;
}
/// <summary>
/// Requests the drawing of a debug overlay for the current frame.
/// </summary>
/// <param name="mode">The debug overlay to request</param>
public void RequestDebugOverlay(DebugOverlay mode)
{
debugOverlay = mode;
}
// Sets the current frame size - used to make sure the debug overlay target is always the
// correct size - mostly useful in the editor as the user can easily resize the gameview.
internal void SetFrameSize(int width, int height)
{
frameWidth = width;
frameHeight = height;
debugOverlayActive = false;
}
/// <summary>
/// Blit a source render target to the debug overlay target.
/// </summary>
/// <param name="cmd">The command buffer to send render commands to</param>
/// <param name="source">The source target</param>
/// <param name="sheet">The property sheet to use for the blit</param>
/// <param name="pass">The pass to use for the property sheet</param>
public void PushDebugOverlay(CommandBuffer cmd, RenderTargetIdentifier source, PropertySheet sheet, int pass)
{
if (debugOverlayTarget == null || !debugOverlayTarget.IsCreated() || debugOverlayTarget.width != frameWidth || debugOverlayTarget.height != frameHeight)
{
RuntimeUtilities.Destroy(debugOverlayTarget);
debugOverlayTarget = new RenderTexture(frameWidth, frameHeight, 0, RenderTextureFormat.ARGB32)
{
name = "Debug Overlay Target",
anisoLevel = 1,
filterMode = FilterMode.Bilinear,
wrapMode = TextureWrapMode.Clamp,
hideFlags = HideFlags.HideAndDontSave
};
debugOverlayTarget.Create();
}
cmd.BlitFullscreenTriangle(source, debugOverlayTarget, sheet, pass);
debugOverlayActive = true;
}
internal DepthTextureMode GetCameraFlags()
{
if (debugOverlay == DebugOverlay.Depth)
return DepthTextureMode.Depth;
if (debugOverlay == DebugOverlay.Normals)
return DepthTextureMode.DepthNormals;
if (debugOverlay == DebugOverlay.MotionVectors)
return DepthTextureMode.MotionVectors | DepthTextureMode.Depth;
return DepthTextureMode.None;
}
internal void RenderMonitors(PostProcessRenderContext context)
{
// Monitors
bool anyActive = false;
bool needsHalfRes = false;
foreach (var kvp in m_Monitors)
{
bool active = kvp.Value.IsRequestedAndSupported(context);
anyActive |= active;
needsHalfRes |= active && kvp.Value.NeedsHalfRes();
}
if (!anyActive)
return;
var cmd = context.command;
cmd.BeginSample("Monitors");
if (needsHalfRes)
{
cmd.GetTemporaryRT(ShaderIDs.HalfResFinalCopy, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, context.sourceFormat);
cmd.Blit(context.destination, ShaderIDs.HalfResFinalCopy);
}
foreach (var kvp in m_Monitors)
{
var monitor = kvp.Value;
if (monitor.requested)
monitor.Render(context);
}
if (needsHalfRes)
cmd.ReleaseTemporaryRT(ShaderIDs.HalfResFinalCopy);
cmd.EndSample("Monitors");
}
internal void RenderSpecialOverlays(PostProcessRenderContext context)
{
if (debugOverlay == DebugOverlay.Depth)
{
var sheet = context.propertySheets.Get(context.resources.shaders.debugOverlays);
sheet.properties.SetVector(ShaderIDs.Params, new Vector4(overlaySettings.linearDepth ? 1f : 0f, 0f, 0f, 0f));
PushDebugOverlay(context.command, BuiltinRenderTextureType.None, sheet, 0);
}
else if (debugOverlay == DebugOverlay.Normals)
{
var sheet = context.propertySheets.Get(context.resources.shaders.debugOverlays);
sheet.ClearKeywords();
#if !UNITY_2022_1_OR_NEWER
if (context.camera.actualRenderingPath == RenderingPath.DeferredLighting)
sheet.EnableKeyword("SOURCE_GBUFFER");
#endif
PushDebugOverlay(context.command, BuiltinRenderTextureType.None, sheet, 1);
}
else if (debugOverlay == DebugOverlay.MotionVectors)
{
var sheet = context.propertySheets.Get(context.resources.shaders.debugOverlays);
sheet.properties.SetVector(ShaderIDs.Params, new Vector4(overlaySettings.motionColorIntensity, overlaySettings.motionGridSize, 0f, 0f));
PushDebugOverlay(context.command, context.source, sheet, 2);
}
else if (debugOverlay == DebugOverlay.NANTracker)
{
var sheet = context.propertySheets.Get(context.resources.shaders.debugOverlays);
PushDebugOverlay(context.command, context.source, sheet, 3);
}
else if (debugOverlay == DebugOverlay.ColorBlindnessSimulation)
{
var sheet = context.propertySheets.Get(context.resources.shaders.debugOverlays);
sheet.properties.SetVector(ShaderIDs.Params, new Vector4(overlaySettings.colorBlindnessStrength, 0f, 0f, 0f));
PushDebugOverlay(context.command, context.source, sheet, 4 + (int)overlaySettings.colorBlindnessType);
}
}
internal void EndFrame()
{
foreach (var kvp in m_Monitors)
kvp.Value.requested = false;
if (!debugOverlayActive)
DestroyDebugOverlayTarget();
debugOverlay = DebugOverlay.None;
}
}
}