PostProcessDebugLayer.cs 12 KB

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