DebugHandler.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. class DebugHandler : IDebugDisplaySettingsQuery
  7. {
  8. #region Property Id Constants
  9. static readonly int k_DebugColorInvalidModePropertyId = Shader.PropertyToID("_DebugColorInvalidMode");
  10. static readonly int k_DebugColorPropertyId = Shader.PropertyToID("_DebugColor");
  11. static readonly int k_DebugTexturePropertyId = Shader.PropertyToID("_DebugTexture");
  12. static readonly int k_DebugTextureNoStereoPropertyId = Shader.PropertyToID("_DebugTextureNoStereo");
  13. static readonly int k_DebugTextureDisplayRect = Shader.PropertyToID("_DebugTextureDisplayRect");
  14. static readonly int k_DebugRenderTargetSupportsStereo = Shader.PropertyToID("_DebugRenderTargetSupportsStereo");
  15. // Material settings...
  16. static readonly int k_DebugMaterialModeId = Shader.PropertyToID("_DebugMaterialMode");
  17. static readonly int k_DebugVertexAttributeModeId = Shader.PropertyToID("_DebugVertexAttributeMode");
  18. static readonly int k_DebugMaterialValidationModeId = Shader.PropertyToID("_DebugMaterialValidationMode");
  19. // Rendering settings...
  20. static readonly int k_DebugMipInfoModeId = Shader.PropertyToID("_DebugMipInfoMode");
  21. static readonly int k_DebugSceneOverrideModeId = Shader.PropertyToID("_DebugSceneOverrideMode");
  22. static readonly int k_DebugFullScreenModeId = Shader.PropertyToID("_DebugFullScreenMode");
  23. static readonly int k_DebugValidationModeId = Shader.PropertyToID("_DebugValidationMode");
  24. static readonly int k_DebugValidateBelowMinThresholdColorPropertyId = Shader.PropertyToID("_DebugValidateBelowMinThresholdColor");
  25. static readonly int k_DebugValidateAboveMaxThresholdColorPropertyId = Shader.PropertyToID("_DebugValidateAboveMaxThresholdColor");
  26. // Lighting settings...
  27. static readonly int k_DebugLightingModeId = Shader.PropertyToID("_DebugLightingMode");
  28. static readonly int k_DebugLightingFeatureFlagsId = Shader.PropertyToID("_DebugLightingFeatureFlags");
  29. static readonly int k_DebugValidateAlbedoMinLuminanceId = Shader.PropertyToID("_DebugValidateAlbedoMinLuminance");
  30. static readonly int k_DebugValidateAlbedoMaxLuminanceId = Shader.PropertyToID("_DebugValidateAlbedoMaxLuminance");
  31. static readonly int k_DebugValidateAlbedoSaturationToleranceId = Shader.PropertyToID("_DebugValidateAlbedoSaturationTolerance");
  32. static readonly int k_DebugValidateAlbedoHueToleranceId = Shader.PropertyToID("_DebugValidateAlbedoHueTolerance");
  33. static readonly int k_DebugValidateAlbedoCompareColorId = Shader.PropertyToID("_DebugValidateAlbedoCompareColor");
  34. static readonly int k_DebugValidateMetallicMinValueId = Shader.PropertyToID("_DebugValidateMetallicMinValue");
  35. static readonly int k_DebugValidateMetallicMaxValueId = Shader.PropertyToID("_DebugValidateMetallicMaxValue");
  36. static readonly int k_ValidationChannelsId = Shader.PropertyToID("_ValidationChannels");
  37. static readonly int k_RangeMinimumId = Shader.PropertyToID("_RangeMinimum");
  38. static readonly int k_RangeMaximumId = Shader.PropertyToID("_RangeMaximum");
  39. #endregion
  40. readonly Material m_ReplacementMaterial;
  41. bool m_HasDebugRenderTarget;
  42. bool m_DebugRenderTargetSupportsStereo;
  43. Vector4 m_DebugRenderTargetPixelRect;
  44. RenderTargetIdentifier m_DebugRenderTargetIdentifier;
  45. readonly DebugDisplaySettings m_DebugDisplaySettings;
  46. DebugDisplaySettingsLighting LightingSettings => m_DebugDisplaySettings.LightingSettings;
  47. DebugDisplaySettingsMaterial MaterialSettings => m_DebugDisplaySettings.MaterialSettings;
  48. DebugDisplaySettingsRendering RenderingSettings => m_DebugDisplaySettings.RenderingSettings;
  49. #region IDebugDisplaySettingsQuery
  50. public bool AreAnySettingsActive => m_DebugDisplaySettings.AreAnySettingsActive;
  51. public bool IsPostProcessingAllowed => m_DebugDisplaySettings.IsPostProcessingAllowed;
  52. public bool IsLightingActive => m_DebugDisplaySettings.IsLightingActive;
  53. // These modes would require putting custom data into gbuffer, so instead we just disable deferred mode.
  54. internal bool IsActiveModeUnsupportedForDeferred =>
  55. m_DebugDisplaySettings.LightingSettings.DebugLightingMode != DebugLightingMode.None ||
  56. m_DebugDisplaySettings.LightingSettings.DebugLightingFeatureFlagsMask != DebugLightingFeatureFlags.None ||
  57. m_DebugDisplaySettings.RenderingSettings.debugSceneOverrideMode != DebugSceneOverrideMode.None ||
  58. m_DebugDisplaySettings.MaterialSettings.DebugMaterialModeData != DebugMaterialMode.None ||
  59. m_DebugDisplaySettings.MaterialSettings.DebugVertexAttributeIndexData != DebugVertexAttributeMode.None ||
  60. m_DebugDisplaySettings.MaterialSettings.MaterialValidationMode != DebugMaterialValidationMode.None;
  61. public bool TryGetScreenClearColor(ref Color color)
  62. {
  63. return m_DebugDisplaySettings.TryGetScreenClearColor(ref color);
  64. }
  65. #endregion
  66. internal Material ReplacementMaterial => m_ReplacementMaterial;
  67. internal DebugDisplaySettings DebugDisplaySettings => m_DebugDisplaySettings;
  68. internal bool IsScreenClearNeeded
  69. {
  70. get
  71. {
  72. Color color = Color.black;
  73. return TryGetScreenClearColor(ref color);
  74. }
  75. }
  76. internal bool IsRenderPassSupported
  77. {
  78. get
  79. {
  80. return RenderingSettings.debugSceneOverrideMode == DebugSceneOverrideMode.None || RenderingSettings.debugSceneOverrideMode == DebugSceneOverrideMode.Overdraw;
  81. }
  82. }
  83. internal DebugHandler(ScriptableRendererData scriptableRendererData)
  84. {
  85. Shader debugReplacementShader = scriptableRendererData.debugShaders.debugReplacementPS;
  86. m_DebugDisplaySettings = DebugDisplaySettings.Instance;
  87. m_ReplacementMaterial = (debugReplacementShader == null) ? null : CoreUtils.CreateEngineMaterial(debugReplacementShader);
  88. }
  89. internal bool IsActiveForCamera(ref CameraData cameraData)
  90. {
  91. return !cameraData.isPreviewCamera && AreAnySettingsActive;
  92. }
  93. internal bool TryGetFullscreenDebugMode(out DebugFullScreenMode debugFullScreenMode)
  94. {
  95. return TryGetFullscreenDebugMode(out debugFullScreenMode, out _);
  96. }
  97. internal bool TryGetFullscreenDebugMode(out DebugFullScreenMode debugFullScreenMode, out int textureHeightPercent)
  98. {
  99. debugFullScreenMode = RenderingSettings.debugFullScreenMode;
  100. textureHeightPercent = RenderingSettings.debugFullScreenModeOutputSizeScreenPercent;
  101. return debugFullScreenMode != DebugFullScreenMode.None;
  102. }
  103. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  104. internal void SetupShaderProperties(CommandBuffer cmd, int passIndex = 0)
  105. {
  106. if (LightingSettings.DebugLightingMode == DebugLightingMode.ShadowCascades)
  107. {
  108. // we disable cubemap reflections, too distracting (in TemplateLWRP for ex.)
  109. cmd.EnableShaderKeyword("_DEBUG_ENVIRONMENTREFLECTIONS_OFF");
  110. }
  111. else
  112. {
  113. cmd.DisableShaderKeyword("_DEBUG_ENVIRONMENTREFLECTIONS_OFF");
  114. }
  115. switch (RenderingSettings.debugSceneOverrideMode)
  116. {
  117. case DebugSceneOverrideMode.Overdraw:
  118. {
  119. cmd.SetGlobalColor(k_DebugColorPropertyId, new Color(0.1f, 0.01f, 0.01f, 1));
  120. break;
  121. }
  122. case DebugSceneOverrideMode.Wireframe:
  123. {
  124. cmd.SetGlobalColor(k_DebugColorPropertyId, Color.black);
  125. break;
  126. }
  127. case DebugSceneOverrideMode.SolidWireframe:
  128. {
  129. cmd.SetGlobalColor(k_DebugColorPropertyId, (passIndex == 0) ? Color.white : Color.black);
  130. break;
  131. }
  132. case DebugSceneOverrideMode.ShadedWireframe:
  133. {
  134. if (passIndex == 0)
  135. {
  136. cmd.DisableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  137. }
  138. else if (passIndex == 1)
  139. {
  140. cmd.SetGlobalColor(k_DebugColorPropertyId, Color.black);
  141. cmd.EnableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  142. }
  143. break;
  144. }
  145. }
  146. switch (MaterialSettings.MaterialValidationMode)
  147. {
  148. case DebugMaterialValidationMode.Albedo:
  149. cmd.SetGlobalFloat(k_DebugValidateAlbedoMinLuminanceId, MaterialSettings.AlbedoMinLuminance);
  150. cmd.SetGlobalFloat(k_DebugValidateAlbedoMaxLuminanceId, MaterialSettings.AlbedoMaxLuminance);
  151. cmd.SetGlobalFloat(k_DebugValidateAlbedoSaturationToleranceId, MaterialSettings.AlbedoSaturationTolerance);
  152. cmd.SetGlobalFloat(k_DebugValidateAlbedoHueToleranceId, MaterialSettings.AlbedoHueTolerance);
  153. cmd.SetGlobalColor(k_DebugValidateAlbedoCompareColorId, MaterialSettings.AlbedoCompareColor.linear);
  154. break;
  155. case DebugMaterialValidationMode.Metallic:
  156. cmd.SetGlobalFloat(k_DebugValidateMetallicMinValueId, MaterialSettings.MetallicMinValue);
  157. cmd.SetGlobalFloat(k_DebugValidateMetallicMaxValueId, MaterialSettings.MetallicMaxValue);
  158. break;
  159. }
  160. }
  161. internal void SetDebugRenderTarget(RenderTargetIdentifier renderTargetIdentifier, Rect displayRect, bool supportsStereo)
  162. {
  163. m_HasDebugRenderTarget = true;
  164. m_DebugRenderTargetSupportsStereo = supportsStereo;
  165. m_DebugRenderTargetIdentifier = renderTargetIdentifier;
  166. m_DebugRenderTargetPixelRect = new Vector4(displayRect.x, displayRect.y, displayRect.width, displayRect.height);
  167. }
  168. internal void ResetDebugRenderTarget()
  169. {
  170. m_HasDebugRenderTarget = false;
  171. }
  172. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  173. internal void UpdateShaderGlobalPropertiesForFinalValidationPass(CommandBuffer cmd, ref CameraData cameraData, bool isFinalPass)
  174. {
  175. // Ensure final validation & fullscreen debug modes are only done once in the very final pass, for the last camera on the stack.
  176. bool isFinal = isFinalPass && cameraData.resolveFinalTarget;
  177. if (!isFinal)
  178. {
  179. cmd.DisableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  180. return;
  181. }
  182. if (IsActiveForCamera(ref cameraData))
  183. {
  184. cmd.EnableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  185. }
  186. else
  187. {
  188. cmd.DisableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  189. }
  190. if (m_HasDebugRenderTarget)
  191. {
  192. cmd.SetGlobalTexture(m_DebugRenderTargetSupportsStereo ? k_DebugTexturePropertyId : k_DebugTextureNoStereoPropertyId, m_DebugRenderTargetIdentifier);
  193. cmd.SetGlobalVector(k_DebugTextureDisplayRect, m_DebugRenderTargetPixelRect);
  194. cmd.SetGlobalInteger(k_DebugRenderTargetSupportsStereo, m_DebugRenderTargetSupportsStereo ? 1 : 0);
  195. }
  196. var renderingSettings = m_DebugDisplaySettings.RenderingSettings;
  197. if (renderingSettings.validationMode == DebugValidationMode.HighlightOutsideOfRange)
  198. {
  199. cmd.SetGlobalInteger(k_ValidationChannelsId, (int)renderingSettings.validationChannels);
  200. cmd.SetGlobalFloat(k_RangeMinimumId, renderingSettings.ValidationRangeMin);
  201. cmd.SetGlobalFloat(k_RangeMaximumId, renderingSettings.ValidationRangeMax);
  202. }
  203. }
  204. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  205. internal void Setup(ScriptableRenderContext context, ref CameraData cameraData)
  206. {
  207. var cmd = CommandBufferPool.Get("");
  208. if (IsActiveForCamera(ref cameraData))
  209. {
  210. cmd.EnableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  211. // Material settings...
  212. cmd.SetGlobalFloat(k_DebugMaterialModeId, (int)MaterialSettings.DebugMaterialModeData);
  213. cmd.SetGlobalFloat(k_DebugVertexAttributeModeId, (int)MaterialSettings.DebugVertexAttributeIndexData);
  214. cmd.SetGlobalInteger(k_DebugMaterialValidationModeId, (int)MaterialSettings.MaterialValidationMode);
  215. // Rendering settings...
  216. cmd.SetGlobalInteger(k_DebugMipInfoModeId, (int)RenderingSettings.debugMipInfoMode);
  217. cmd.SetGlobalInteger(k_DebugSceneOverrideModeId, (int)RenderingSettings.debugSceneOverrideMode);
  218. cmd.SetGlobalInteger(k_DebugFullScreenModeId, (int)RenderingSettings.debugFullScreenMode);
  219. cmd.SetGlobalInteger(k_DebugValidationModeId, (int)RenderingSettings.validationMode);
  220. cmd.SetGlobalColor(k_DebugValidateBelowMinThresholdColorPropertyId, Color.red);
  221. cmd.SetGlobalColor(k_DebugValidateAboveMaxThresholdColorPropertyId, Color.blue);
  222. // Lighting settings...
  223. cmd.SetGlobalFloat(k_DebugLightingModeId, (int)LightingSettings.DebugLightingMode);
  224. cmd.SetGlobalInteger(k_DebugLightingFeatureFlagsId, (int)LightingSettings.DebugLightingFeatureFlagsMask);
  225. // Set-up any other persistent properties...
  226. cmd.SetGlobalColor(k_DebugColorInvalidModePropertyId, Color.red);
  227. }
  228. else
  229. {
  230. cmd.DisableShaderKeyword(ShaderKeywordStrings.DEBUG_DISPLAY);
  231. }
  232. context.ExecuteCommandBuffer(cmd);
  233. CommandBufferPool.Release(cmd);
  234. }
  235. #region DebugRenderPasses
  236. private class DebugRenderPassEnumerable : IEnumerable<DebugRenderSetup>
  237. {
  238. private class Enumerator : IEnumerator<DebugRenderSetup>
  239. {
  240. private readonly DebugHandler m_DebugHandler;
  241. private readonly ScriptableRenderContext m_Context;
  242. private readonly CommandBuffer m_CommandBuffer;
  243. private readonly int m_NumIterations;
  244. private int m_Index;
  245. public DebugRenderSetup Current { get; private set; }
  246. object IEnumerator.Current => Current;
  247. public Enumerator(DebugHandler debugHandler, ScriptableRenderContext context, CommandBuffer commandBuffer)
  248. {
  249. DebugSceneOverrideMode sceneOverrideMode = debugHandler.DebugDisplaySettings.RenderingSettings.debugSceneOverrideMode;
  250. m_DebugHandler = debugHandler;
  251. m_Context = context;
  252. m_CommandBuffer = commandBuffer;
  253. m_NumIterations = ((sceneOverrideMode == DebugSceneOverrideMode.SolidWireframe) || (sceneOverrideMode == DebugSceneOverrideMode.ShadedWireframe)) ? 2 : 1;
  254. m_Index = -1;
  255. }
  256. #region IEnumerator<DebugRenderSetup>
  257. public bool MoveNext()
  258. {
  259. Current?.Dispose();
  260. if (++m_Index >= m_NumIterations)
  261. {
  262. return false;
  263. }
  264. else
  265. {
  266. Current = new DebugRenderSetup(m_DebugHandler, m_Context, m_CommandBuffer, m_Index);
  267. return true;
  268. }
  269. }
  270. public void Reset()
  271. {
  272. if (Current != null)
  273. {
  274. Current.Dispose();
  275. Current = null;
  276. }
  277. m_Index = -1;
  278. }
  279. public void Dispose()
  280. {
  281. Current?.Dispose();
  282. }
  283. #endregion
  284. }
  285. private readonly DebugHandler m_DebugHandler;
  286. private readonly ScriptableRenderContext m_Context;
  287. private readonly CommandBuffer m_CommandBuffer;
  288. public DebugRenderPassEnumerable(DebugHandler debugHandler, ScriptableRenderContext context, CommandBuffer commandBuffer)
  289. {
  290. m_DebugHandler = debugHandler;
  291. m_Context = context;
  292. m_CommandBuffer = commandBuffer;
  293. }
  294. #region IEnumerable<DebugRenderSetup>
  295. public IEnumerator<DebugRenderSetup> GetEnumerator()
  296. {
  297. return new Enumerator(m_DebugHandler, m_Context, m_CommandBuffer);
  298. }
  299. IEnumerator IEnumerable.GetEnumerator()
  300. {
  301. return GetEnumerator();
  302. }
  303. #endregion
  304. }
  305. internal IEnumerable<DebugRenderSetup> CreateDebugRenderSetupEnumerable(ScriptableRenderContext context,
  306. CommandBuffer commandBuffer)
  307. {
  308. return new DebugRenderPassEnumerable(this, context, commandBuffer);
  309. }
  310. internal delegate void DrawFunction(
  311. ScriptableRenderContext context,
  312. ref RenderingData renderingData,
  313. ref DrawingSettings drawingSettings,
  314. ref FilteringSettings filteringSettings,
  315. ref RenderStateBlock renderStateBlock);
  316. internal void DrawWithDebugRenderState(
  317. ScriptableRenderContext context,
  318. CommandBuffer cmd,
  319. ref RenderingData renderingData,
  320. ref DrawingSettings drawingSettings,
  321. ref FilteringSettings filteringSettings,
  322. ref RenderStateBlock renderStateBlock,
  323. DrawFunction func)
  324. {
  325. foreach (DebugRenderSetup debugRenderSetup in CreateDebugRenderSetupEnumerable(context, cmd))
  326. {
  327. DrawingSettings debugDrawingSettings = debugRenderSetup.CreateDrawingSettings(drawingSettings);
  328. RenderStateBlock debugRenderStateBlock = debugRenderSetup.GetRenderStateBlock(renderStateBlock);
  329. func(context, ref renderingData, ref debugDrawingSettings, ref filteringSettings, ref debugRenderStateBlock);
  330. }
  331. }
  332. #endregion
  333. }
  334. }