No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebugHandler.cs 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using UnityEditor;
  6. using UnityEngine.Experimental.Rendering;
  7. using UnityEngine.Rendering.RenderGraphModule;
  8. namespace UnityEngine.Rendering.Universal
  9. {
  10. class DebugHandler : IDebugDisplaySettingsQuery
  11. {
  12. #region Property Id Constants
  13. static readonly int k_DebugColorInvalidModePropertyId = Shader.PropertyToID("_DebugColorInvalidMode");
  14. static readonly int k_DebugCurrentRealTimeId = Shader.PropertyToID("_DebugCurrentRealTime");
  15. static readonly int k_DebugColorPropertyId = Shader.PropertyToID("_DebugColor");
  16. static readonly int k_DebugTexturePropertyId = Shader.PropertyToID("_DebugTexture");
  17. static readonly int k_DebugFontId = Shader.PropertyToID("_DebugFont");
  18. static readonly int k_DebugTextureNoStereoPropertyId = Shader.PropertyToID("_DebugTextureNoStereo");
  19. static readonly int k_DebugTextureDisplayRect = Shader.PropertyToID("_DebugTextureDisplayRect");
  20. static readonly int k_DebugRenderTargetSupportsStereo = Shader.PropertyToID("_DebugRenderTargetSupportsStereo");
  21. static readonly int k_DebugRenderTargetRangeRemap = Shader.PropertyToID("_DebugRenderTargetRangeRemap");
  22. // Material settings...
  23. static readonly int k_DebugMaterialModeId = Shader.PropertyToID("_DebugMaterialMode");
  24. static readonly int k_DebugVertexAttributeModeId = Shader.PropertyToID("_DebugVertexAttributeMode");
  25. static readonly int k_DebugMaterialValidationModeId = Shader.PropertyToID("_DebugMaterialValidationMode");
  26. // Rendering settings...
  27. static readonly int k_DebugMipInfoModeId = Shader.PropertyToID("_DebugMipInfoMode");
  28. static readonly int k_DebugMipMapStatusModeId = Shader.PropertyToID("_DebugMipMapStatusMode");
  29. static readonly int k_DebugMipMapShowStatusCodeId = Shader.PropertyToID("_DebugMipMapShowStatusCode");
  30. static readonly int k_DebugMipMapOpacityId = Shader.PropertyToID("_DebugMipMapOpacity");
  31. static readonly int k_DebugMipMapRecentlyUpdatedCooldownId = Shader.PropertyToID("_DebugMipMapRecentlyUpdatedCooldown");
  32. static readonly int k_DebugMipMapTerrainTextureModeId = Shader.PropertyToID("_DebugMipMapTerrainTextureMode");
  33. static readonly int k_DebugSceneOverrideModeId = Shader.PropertyToID("_DebugSceneOverrideMode");
  34. static readonly int k_DebugFullScreenModeId = Shader.PropertyToID("_DebugFullScreenMode");
  35. static readonly int k_DebugValidationModeId = Shader.PropertyToID("_DebugValidationMode");
  36. static readonly int k_DebugValidateBelowMinThresholdColorPropertyId = Shader.PropertyToID("_DebugValidateBelowMinThresholdColor");
  37. static readonly int k_DebugValidateAboveMaxThresholdColorPropertyId = Shader.PropertyToID("_DebugValidateAboveMaxThresholdColor");
  38. static readonly int k_DebugMaxPixelCost = Shader.PropertyToID("_DebugMaxPixelCost");
  39. // Lighting settings...
  40. static readonly int k_DebugLightingModeId = Shader.PropertyToID("_DebugLightingMode");
  41. static readonly int k_DebugLightingFeatureFlagsId = Shader.PropertyToID("_DebugLightingFeatureFlags");
  42. static readonly int k_DebugValidateAlbedoMinLuminanceId = Shader.PropertyToID("_DebugValidateAlbedoMinLuminance");
  43. static readonly int k_DebugValidateAlbedoMaxLuminanceId = Shader.PropertyToID("_DebugValidateAlbedoMaxLuminance");
  44. static readonly int k_DebugValidateAlbedoSaturationToleranceId = Shader.PropertyToID("_DebugValidateAlbedoSaturationTolerance");
  45. static readonly int k_DebugValidateAlbedoHueToleranceId = Shader.PropertyToID("_DebugValidateAlbedoHueTolerance");
  46. static readonly int k_DebugValidateAlbedoCompareColorId = Shader.PropertyToID("_DebugValidateAlbedoCompareColor");
  47. static readonly int k_DebugValidateMetallicMinValueId = Shader.PropertyToID("_DebugValidateMetallicMinValue");
  48. static readonly int k_DebugValidateMetallicMaxValueId = Shader.PropertyToID("_DebugValidateMetallicMaxValue");
  49. static readonly int k_ValidationChannelsId = Shader.PropertyToID("_ValidationChannels");
  50. static readonly int k_RangeMinimumId = Shader.PropertyToID("_RangeMinimum");
  51. static readonly int k_RangeMaximumId = Shader.PropertyToID("_RangeMaximum");
  52. #endregion
  53. #region Pass Data
  54. private static readonly ProfilingSampler s_DebugSetupSampler = new ProfilingSampler("Setup Debug Properties");
  55. private static readonly ProfilingSampler s_DebugFinalValidationSampler = new ProfilingSampler(nameof(UpdateShaderGlobalPropertiesForFinalValidationPass));
  56. DebugSetupPassData s_DebugSetupPassData = new DebugSetupPassData();
  57. DebugFinalValidationPassData s_DebugFinalValidationPassData = new DebugFinalValidationPassData();
  58. #endregion
  59. readonly Material m_ReplacementMaterial;
  60. readonly Material m_HDRDebugViewMaterial;
  61. HDRDebugViewPass m_HDRDebugViewPass;
  62. RTHandle m_DebugScreenColorHandle;
  63. RTHandle m_DebugScreenDepthHandle;
  64. readonly UniversalRenderPipelineRuntimeTextures m_RuntimeTextures;
  65. bool m_HasDebugRenderTarget;
  66. bool m_DebugRenderTargetSupportsStereo;
  67. Vector4 m_DebugRenderTargetPixelRect;
  68. Vector4 m_DebugRenderTargetRangeRemap;
  69. RTHandle m_DebugRenderTarget;
  70. RTHandle m_DebugFontTexture;
  71. readonly UniversalRenderPipelineDebugDisplaySettings m_DebugDisplaySettings;
  72. DebugDisplaySettingsLighting LightingSettings => m_DebugDisplaySettings.lightingSettings;
  73. DebugDisplaySettingsMaterial MaterialSettings => m_DebugDisplaySettings.materialSettings;
  74. DebugDisplaySettingsRendering RenderingSettings => m_DebugDisplaySettings.renderingSettings;
  75. #region IDebugDisplaySettingsQuery
  76. /// <inheritdoc/>
  77. public bool AreAnySettingsActive => m_DebugDisplaySettings.AreAnySettingsActive;
  78. /// <inheritdoc/>
  79. public bool IsPostProcessingAllowed => m_DebugDisplaySettings.IsPostProcessingAllowed;
  80. /// <inheritdoc/>
  81. public bool IsLightingActive => m_DebugDisplaySettings.IsLightingActive;
  82. // These modes would require putting custom data into gbuffer, so instead we just disable deferred mode.
  83. internal bool IsActiveModeUnsupportedForDeferred =>
  84. m_DebugDisplaySettings.lightingSettings.lightingDebugMode != DebugLightingMode.None ||
  85. m_DebugDisplaySettings.lightingSettings.lightingFeatureFlags != DebugLightingFeatureFlags.None ||
  86. m_DebugDisplaySettings.renderingSettings.sceneOverrideMode != DebugSceneOverrideMode.None ||
  87. m_DebugDisplaySettings.materialSettings.materialDebugMode != DebugMaterialMode.None ||
  88. m_DebugDisplaySettings.materialSettings.vertexAttributeDebugMode != DebugVertexAttributeMode.None ||
  89. m_DebugDisplaySettings.materialSettings.materialValidationMode != DebugMaterialValidationMode.None ||
  90. m_DebugDisplaySettings.renderingSettings.mipInfoMode != DebugMipInfoMode.None;
  91. /// <inheritdoc/>
  92. public bool TryGetScreenClearColor(ref Color color)
  93. {
  94. return m_DebugDisplaySettings.TryGetScreenClearColor(ref color);
  95. }
  96. #endregion
  97. internal Material ReplacementMaterial => m_ReplacementMaterial;
  98. internal UniversalRenderPipelineDebugDisplaySettings DebugDisplaySettings => m_DebugDisplaySettings;
  99. internal ref RTHandle DebugScreenColorHandle => ref m_DebugScreenColorHandle;
  100. internal ref RTHandle DebugScreenDepthHandle => ref m_DebugScreenDepthHandle;
  101. internal HDRDebugViewPass hdrDebugViewPass => m_HDRDebugViewPass;
  102. internal bool HDRDebugViewIsActive(bool resolveFinalTarget)
  103. {
  104. // HDR debug views should only apply to the last camera in the stack
  105. return DebugDisplaySettings.lightingSettings.hdrDebugMode != HDRDebugMode.None && resolveFinalTarget;
  106. }
  107. internal bool WriteToDebugScreenTexture(bool resolveFinalTarget)
  108. {
  109. return HDRDebugViewIsActive(resolveFinalTarget);
  110. }
  111. internal bool IsScreenClearNeeded
  112. {
  113. get
  114. {
  115. Color color = Color.black;
  116. return TryGetScreenClearColor(ref color);
  117. }
  118. }
  119. internal bool IsRenderPassSupported
  120. {
  121. get
  122. {
  123. return RenderingSettings.sceneOverrideMode == DebugSceneOverrideMode.None || RenderingSettings.sceneOverrideMode == DebugSceneOverrideMode.Overdraw;
  124. }
  125. }
  126. internal int stpDebugViewIndex { get { return RenderingSettings.stpDebugViewIndex; } }
  127. internal DebugHandler()
  128. {
  129. m_DebugDisplaySettings = UniversalRenderPipelineDebugDisplaySettings.Instance;
  130. if (GraphicsSettings.TryGetRenderPipelineSettings<UniversalRenderPipelineDebugShaders>(out var shaders))
  131. {
  132. m_ReplacementMaterial = (shaders.debugReplacementPS != null) ? CoreUtils.CreateEngineMaterial(shaders.debugReplacementPS) : null;
  133. m_HDRDebugViewMaterial = (shaders.hdrDebugViewPS != null) ? CoreUtils.CreateEngineMaterial(shaders.hdrDebugViewPS) : null;
  134. }
  135. m_HDRDebugViewPass = new HDRDebugViewPass(m_HDRDebugViewMaterial);
  136. m_RuntimeTextures = GraphicsSettings.GetRenderPipelineSettings<UniversalRenderPipelineRuntimeTextures>();
  137. if (m_RuntimeTextures != null)
  138. {
  139. m_DebugFontTexture = RTHandles.Alloc(m_RuntimeTextures.debugFontTexture);
  140. }
  141. }
  142. public void Dispose()
  143. {
  144. m_HDRDebugViewPass.Dispose();
  145. m_DebugScreenColorHandle?.Release();
  146. m_DebugScreenDepthHandle?.Release();
  147. m_DebugFontTexture?.Release();
  148. CoreUtils.Destroy(m_HDRDebugViewMaterial);
  149. CoreUtils.Destroy(m_ReplacementMaterial);
  150. }
  151. internal bool IsActiveForCamera(bool isPreviewCamera)
  152. {
  153. return !isPreviewCamera && AreAnySettingsActive;
  154. }
  155. internal bool TryGetFullscreenDebugMode(out DebugFullScreenMode debugFullScreenMode)
  156. {
  157. return TryGetFullscreenDebugMode(out debugFullScreenMode, out _);
  158. }
  159. internal bool TryGetFullscreenDebugMode(out DebugFullScreenMode debugFullScreenMode, out int textureHeightPercent)
  160. {
  161. debugFullScreenMode = RenderingSettings.fullScreenDebugMode;
  162. textureHeightPercent = RenderingSettings.fullScreenDebugModeOutputSizeScreenPercent;
  163. return debugFullScreenMode != DebugFullScreenMode.None;
  164. }
  165. internal static void ConfigureColorDescriptorForDebugScreen(ref RenderTextureDescriptor descriptor, int cameraWidth, int cameraHeight)
  166. {
  167. descriptor.width = cameraWidth;
  168. descriptor.height = cameraHeight;
  169. descriptor.useMipMap = false;
  170. descriptor.autoGenerateMips = false;
  171. descriptor.useDynamicScale = true;
  172. descriptor.depthStencilFormat = GraphicsFormat.None;
  173. }
  174. internal static void ConfigureDepthDescriptorForDebugScreen(ref RenderTextureDescriptor descriptor, GraphicsFormat depthStencilFormat, int cameraWidth, int cameraHeight)
  175. {
  176. descriptor.width = cameraWidth;
  177. descriptor.height = cameraHeight;
  178. descriptor.useMipMap = false;
  179. descriptor.autoGenerateMips = false;
  180. descriptor.useDynamicScale = true;
  181. descriptor.depthStencilFormat = depthStencilFormat;
  182. }
  183. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  184. internal void SetupShaderProperties(RasterCommandBuffer cmd, int passIndex = 0)
  185. {
  186. if (LightingSettings.lightingDebugMode == DebugLightingMode.ShadowCascades)
  187. {
  188. // we disable cubemap reflections, too distracting (in TemplateLWRP for ex.)
  189. cmd.EnableShaderKeyword("_DEBUG_ENVIRONMENTREFLECTIONS_OFF");
  190. }
  191. else
  192. {
  193. cmd.DisableShaderKeyword("_DEBUG_ENVIRONMENTREFLECTIONS_OFF");
  194. }
  195. switch (RenderingSettings.sceneOverrideMode)
  196. {
  197. case DebugSceneOverrideMode.Overdraw:
  198. {
  199. // Target texture can contains only a value between 0 and 1
  200. // So we encode the number of overdraw in the range (0, max displayed overdaw count)
  201. // The value will be clamped by the GPU driver
  202. var value = 1 / (float)RenderingSettings.maxOverdrawCount;
  203. cmd.SetGlobalColor(k_DebugColorPropertyId, new Color(value, value, value, 1));
  204. break;
  205. }
  206. case DebugSceneOverrideMode.Wireframe:
  207. {
  208. cmd.SetGlobalColor(k_DebugColorPropertyId, Color.black);
  209. break;
  210. }
  211. case DebugSceneOverrideMode.SolidWireframe:
  212. {
  213. cmd.SetGlobalColor(k_DebugColorPropertyId, (passIndex == 0) ? Color.white : Color.black);
  214. break;
  215. }
  216. case DebugSceneOverrideMode.ShadedWireframe:
  217. {
  218. if (passIndex == 0)
  219. {
  220. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, false);
  221. }
  222. else if (passIndex == 1)
  223. {
  224. cmd.SetGlobalColor(k_DebugColorPropertyId, Color.black);
  225. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, true);
  226. }
  227. break;
  228. }
  229. }
  230. switch (MaterialSettings.materialValidationMode)
  231. {
  232. case DebugMaterialValidationMode.Albedo:
  233. cmd.SetGlobalFloat(k_DebugValidateAlbedoMinLuminanceId, MaterialSettings.albedoMinLuminance);
  234. cmd.SetGlobalFloat(k_DebugValidateAlbedoMaxLuminanceId, MaterialSettings.albedoMaxLuminance);
  235. cmd.SetGlobalFloat(k_DebugValidateAlbedoSaturationToleranceId, MaterialSettings.albedoSaturationTolerance);
  236. cmd.SetGlobalFloat(k_DebugValidateAlbedoHueToleranceId, MaterialSettings.albedoHueTolerance);
  237. cmd.SetGlobalColor(k_DebugValidateAlbedoCompareColorId, MaterialSettings.albedoCompareColor.linear);
  238. break;
  239. case DebugMaterialValidationMode.Metallic:
  240. cmd.SetGlobalFloat(k_DebugValidateMetallicMinValueId, MaterialSettings.metallicMinValue);
  241. cmd.SetGlobalFloat(k_DebugValidateMetallicMaxValueId, MaterialSettings.metallicMaxValue);
  242. break;
  243. }
  244. }
  245. internal void SetDebugRenderTarget(RTHandle renderTarget, Rect displayRect, bool supportsStereo, Vector4 dataRangeRemap)
  246. {
  247. m_HasDebugRenderTarget = true;
  248. m_DebugRenderTargetSupportsStereo = supportsStereo;
  249. m_DebugRenderTarget = renderTarget;
  250. m_DebugRenderTargetPixelRect = new Vector4(displayRect.x, displayRect.y, displayRect.width, displayRect.height);
  251. m_DebugRenderTargetRangeRemap = dataRangeRemap;
  252. }
  253. internal void ResetDebugRenderTarget()
  254. {
  255. m_HasDebugRenderTarget = false;
  256. }
  257. class DebugFinalValidationPassData
  258. {
  259. public bool isFinalPass;
  260. public bool resolveFinalTarget;
  261. public bool isActiveForCamera;
  262. public bool hasDebugRenderTarget;
  263. public TextureHandle debugRenderTargetHandle;
  264. public int debugTexturePropertyId;
  265. public Vector4 debugRenderTargetPixelRect;
  266. public int debugRenderTargetSupportsStereo;
  267. public Vector4 debugRenderTargetRangeRemap;
  268. public TextureHandle debugFontTextureHandle;
  269. // NOTE: The settings are references.
  270. // It's assumed they're the same for the whole frame. Build timeline != execution timeline!
  271. // Ideally these would be copied without any allocs.
  272. public DebugDisplaySettingsRendering renderingSettings;
  273. }
  274. DebugFinalValidationPassData InitDebugFinalValidationPassData(DebugFinalValidationPassData passData, UniversalCameraData cameraData, bool isFinalPass)
  275. {
  276. passData.isFinalPass = isFinalPass;
  277. passData.resolveFinalTarget = cameraData.resolveFinalTarget;
  278. passData.isActiveForCamera = IsActiveForCamera(cameraData.isPreviewCamera);
  279. passData.hasDebugRenderTarget = m_HasDebugRenderTarget;
  280. passData.debugRenderTargetHandle = TextureHandle.nullHandle;
  281. passData.debugTexturePropertyId = m_DebugRenderTargetSupportsStereo ? k_DebugTexturePropertyId : k_DebugTextureNoStereoPropertyId;
  282. passData.debugRenderTargetPixelRect = m_DebugRenderTargetPixelRect;
  283. passData.debugRenderTargetSupportsStereo = m_DebugRenderTargetSupportsStereo ? 1 : 0;
  284. passData.debugRenderTargetRangeRemap = m_DebugRenderTargetRangeRemap;
  285. passData.debugFontTextureHandle = TextureHandle.nullHandle;
  286. passData.renderingSettings = RenderingSettings;
  287. return passData;
  288. }
  289. static void UpdateShaderGlobalPropertiesForFinalValidationPass(RasterCommandBuffer cmd, DebugFinalValidationPassData data)
  290. {
  291. // Ensure final validation & fullscreen debug modes are only done once in the very final pass, for the last camera on the stack.
  292. bool isFinal = data.isFinalPass && data.resolveFinalTarget;
  293. if (!isFinal)
  294. {
  295. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, false);
  296. return;
  297. }
  298. if (data.isActiveForCamera)
  299. {
  300. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, true);
  301. }
  302. else
  303. {
  304. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, false);
  305. }
  306. if (data.hasDebugRenderTarget)
  307. {
  308. if(data.debugRenderTargetHandle.IsValid())
  309. cmd.SetGlobalTexture(data.debugTexturePropertyId, data.debugRenderTargetHandle);
  310. cmd.SetGlobalVector(k_DebugTextureDisplayRect, data.debugRenderTargetPixelRect);
  311. cmd.SetGlobalInteger(k_DebugRenderTargetSupportsStereo, data.debugRenderTargetSupportsStereo);
  312. cmd.SetGlobalVector(k_DebugRenderTargetRangeRemap, data.debugRenderTargetRangeRemap);
  313. }
  314. var renderingSettings = data.renderingSettings;
  315. if (renderingSettings.validationMode == DebugValidationMode.HighlightOutsideOfRange)
  316. {
  317. cmd.SetGlobalInteger(k_ValidationChannelsId, (int)renderingSettings.validationChannels);
  318. cmd.SetGlobalFloat(k_RangeMinimumId, renderingSettings.validationRangeMin);
  319. cmd.SetGlobalFloat(k_RangeMaximumId, renderingSettings.validationRangeMax);
  320. }
  321. if (renderingSettings.mipInfoMode != DebugMipInfoMode.None)
  322. {
  323. // some (not all) of these need text rendering
  324. cmd.SetGlobalTexture(k_DebugFontId, data.debugFontTextureHandle);
  325. }
  326. }
  327. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  328. internal void UpdateShaderGlobalPropertiesForFinalValidationPass(CommandBuffer cmd, UniversalCameraData cameraData, bool isFinalPass)
  329. {
  330. UpdateShaderGlobalPropertiesForFinalValidationPass(CommandBufferHelpers.GetRasterCommandBuffer(cmd), InitDebugFinalValidationPassData(s_DebugFinalValidationPassData, cameraData, isFinalPass));
  331. cmd.SetGlobalTexture(s_DebugFinalValidationPassData.debugTexturePropertyId, m_DebugRenderTarget);
  332. if (RenderingSettings.mipInfoMode != DebugMipInfoMode.None)
  333. {
  334. // some (not all) of these need text rendering
  335. cmd.SetGlobalTexture(k_DebugFontId, m_RuntimeTextures.debugFontTexture);
  336. }
  337. }
  338. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  339. internal void UpdateShaderGlobalPropertiesForFinalValidationPass(RenderGraph renderGraph, UniversalCameraData cameraData, bool isFinalPass)
  340. {
  341. using (var builder = renderGraph.AddRasterRenderPass<DebugFinalValidationPassData>(nameof(UpdateShaderGlobalPropertiesForFinalValidationPass), out var passData, s_DebugFinalValidationSampler))
  342. {
  343. InitDebugFinalValidationPassData(passData, cameraData, isFinalPass);
  344. passData.debugRenderTargetHandle = renderGraph.ImportTexture(m_DebugRenderTarget);
  345. passData.debugFontTextureHandle = renderGraph.ImportTexture(m_DebugFontTexture);
  346. builder.AllowPassCulling(false);
  347. builder.AllowGlobalStateModification(true);
  348. builder.SetGlobalTextureAfterPass(passData.debugRenderTargetHandle, passData.debugTexturePropertyId);
  349. builder.SetGlobalTextureAfterPass(passData.debugFontTextureHandle, k_DebugFontId);
  350. builder.UseTexture(passData.debugRenderTargetHandle);
  351. builder.UseTexture(passData.debugFontTextureHandle);
  352. builder.SetRenderFunc(static (DebugFinalValidationPassData data, RasterGraphContext context) =>
  353. {
  354. UpdateShaderGlobalPropertiesForFinalValidationPass(context.cmd, data);
  355. });
  356. }
  357. }
  358. class DebugSetupPassData
  359. {
  360. public bool isActiveForCamera;
  361. // NOTE: The settings are references.
  362. // It's assumed they're the same for the whole frame. Build timeline != execution timeline!
  363. // Ideally these would be copied without any allocs.
  364. public DebugDisplaySettingsMaterial materialSettings;
  365. public DebugDisplaySettingsRendering renderingSettings;
  366. public DebugDisplaySettingsLighting lightingSettings;
  367. }
  368. DebugSetupPassData InitDebugSetupPassData(DebugSetupPassData passData, bool isPreviewCamera)
  369. {
  370. passData.isActiveForCamera = IsActiveForCamera(isPreviewCamera);
  371. passData.materialSettings = MaterialSettings;
  372. passData.renderingSettings = RenderingSettings;
  373. passData.lightingSettings = LightingSettings;
  374. return passData;
  375. }
  376. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  377. static void Setup(RasterCommandBuffer cmd, DebugSetupPassData passData)
  378. {
  379. if (passData.isActiveForCamera)
  380. {
  381. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, true);
  382. // Material settings...
  383. cmd.SetGlobalFloat(k_DebugMaterialModeId, (int)passData.materialSettings.materialDebugMode);
  384. cmd.SetGlobalFloat(k_DebugVertexAttributeModeId, (int)passData.materialSettings.vertexAttributeDebugMode);
  385. cmd.SetGlobalInteger(k_DebugMaterialValidationModeId, (int)passData.materialSettings.materialValidationMode);
  386. // Rendering settings...
  387. cmd.SetGlobalInteger(k_DebugMipInfoModeId, (int)passData.renderingSettings.mipInfoMode);
  388. cmd.SetGlobalInteger(k_DebugMipMapStatusModeId, (int)passData.renderingSettings.mipDebugStatusMode);
  389. cmd.SetGlobalInteger(k_DebugMipMapShowStatusCodeId, passData.renderingSettings.mipDebugStatusShowCode ? 1 : 0);
  390. cmd.SetGlobalFloat(k_DebugMipMapOpacityId, passData.renderingSettings.mipDebugOpacity);
  391. cmd.SetGlobalFloat(k_DebugMipMapRecentlyUpdatedCooldownId, passData.renderingSettings.mipDebugRecentUpdateCooldown);
  392. cmd.SetGlobalFloat(k_DebugMipMapTerrainTextureModeId, (int)passData.renderingSettings.mipDebugTerrainTexture);
  393. cmd.SetGlobalInteger(k_DebugSceneOverrideModeId, (int)passData.renderingSettings.sceneOverrideMode);
  394. cmd.SetGlobalInteger(k_DebugFullScreenModeId, (int)passData.renderingSettings.fullScreenDebugMode);
  395. cmd.SetGlobalInteger(k_DebugMaxPixelCost, (int)passData.renderingSettings.maxOverdrawCount);
  396. cmd.SetGlobalInteger(k_DebugValidationModeId, (int)passData.renderingSettings.validationMode);
  397. cmd.SetGlobalColor(k_DebugValidateBelowMinThresholdColorPropertyId, Color.red);
  398. cmd.SetGlobalColor(k_DebugValidateAboveMaxThresholdColorPropertyId, Color.blue);
  399. // Lighting settings...
  400. cmd.SetGlobalFloat(k_DebugLightingModeId, (int)passData.lightingSettings.lightingDebugMode);
  401. cmd.SetGlobalInteger(k_DebugLightingFeatureFlagsId, (int)passData.lightingSettings.lightingFeatureFlags);
  402. // Set-up any other persistent properties...
  403. cmd.SetGlobalColor(k_DebugColorInvalidModePropertyId, Color.red);
  404. #if UNITY_EDITOR
  405. cmd.SetGlobalFloat(k_DebugCurrentRealTimeId, (float)EditorApplication.timeSinceStartup);
  406. #else
  407. cmd.SetGlobalFloat(k_DebugCurrentRealTimeId, Time.realtimeSinceStartup);
  408. #endif
  409. }
  410. else
  411. {
  412. cmd.SetKeyword(ShaderGlobalKeywords.DEBUG_DISPLAY, false);
  413. }
  414. }
  415. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  416. internal void Setup(CommandBuffer cmd, bool isPreviewCamera)
  417. {
  418. Setup(CommandBufferHelpers.GetRasterCommandBuffer(cmd), InitDebugSetupPassData(s_DebugSetupPassData, isPreviewCamera));
  419. }
  420. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  421. internal void Setup(RenderGraph renderGraph, bool isPreviewCamera)
  422. {
  423. using (var builder = renderGraph.AddRasterRenderPass<DebugSetupPassData>(s_DebugSetupSampler.name, out var passData, s_DebugSetupSampler))
  424. {
  425. InitDebugSetupPassData(passData, isPreviewCamera);
  426. builder.AllowPassCulling(false);
  427. builder.AllowGlobalStateModification(true);
  428. builder.SetRenderFunc(static (DebugSetupPassData data, RasterGraphContext context) =>
  429. {
  430. Setup(context.cmd, data);
  431. });
  432. }
  433. }
  434. [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
  435. internal void Render(RenderGraph renderGraph, UniversalCameraData cameraData, TextureHandle srcColor, TextureHandle overlayTexture, TextureHandle dstColor)
  436. {
  437. if (IsActiveForCamera(cameraData.isPreviewCamera) && HDRDebugViewIsActive(cameraData.resolveFinalTarget))
  438. {
  439. m_HDRDebugViewPass.RenderHDRDebug(renderGraph, cameraData, srcColor, overlayTexture, dstColor, LightingSettings.hdrDebugMode);
  440. }
  441. }
  442. #region DebugRendererLists
  443. internal DebugRendererLists CreateRendererListsWithDebugRenderState(
  444. ScriptableRenderContext context,
  445. ref CullingResults cullResults,
  446. ref DrawingSettings drawingSettings,
  447. ref FilteringSettings filteringSettings,
  448. ref RenderStateBlock renderStateBlock)
  449. {
  450. DebugRendererLists debug = new DebugRendererLists(this, filteringSettings);
  451. debug.CreateRendererListsWithDebugRenderState(context, ref cullResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);
  452. return debug;
  453. }
  454. internal DebugRendererLists CreateRendererListsWithDebugRenderState(
  455. RenderGraph renderGraph,
  456. ref CullingResults cullResults,
  457. ref DrawingSettings drawingSettings,
  458. ref FilteringSettings filteringSettings,
  459. ref RenderStateBlock renderStateBlock)
  460. {
  461. DebugRendererLists debug = new DebugRendererLists(this, filteringSettings);
  462. debug.CreateRendererListsWithDebugRenderState(renderGraph, ref cullResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);
  463. return debug;
  464. }
  465. #endregion
  466. }
  467. internal class DebugRendererLists
  468. {
  469. private readonly DebugHandler m_DebugHandler;
  470. readonly FilteringSettings m_FilteringSettings;
  471. List<DebugRenderSetup> m_DebugRenderSetups = new List<DebugRenderSetup>(2);
  472. List<RendererList> m_ActiveDebugRendererList = new List<RendererList>(2);
  473. List<RendererListHandle> m_ActiveDebugRendererListHdl = new List<RendererListHandle>(2);
  474. public DebugRendererLists(DebugHandler debugHandler,
  475. FilteringSettings filteringSettings)
  476. {
  477. m_DebugHandler = debugHandler;
  478. m_FilteringSettings = filteringSettings;
  479. }
  480. private void CreateDebugRenderSetups(FilteringSettings filteringSettings)
  481. {
  482. var sceneOverrideMode = m_DebugHandler.DebugDisplaySettings.renderingSettings.sceneOverrideMode;
  483. var numIterations = ((sceneOverrideMode == DebugSceneOverrideMode.SolidWireframe) || (sceneOverrideMode == DebugSceneOverrideMode.ShadedWireframe)) ? 2 : 1;
  484. for (var i = 0; i < numIterations; i++)
  485. m_DebugRenderSetups.Add(new DebugRenderSetup(m_DebugHandler, i, filteringSettings));
  486. }
  487. void DisposeDebugRenderLists()
  488. {
  489. foreach (var debugRenderSetup in m_DebugRenderSetups)
  490. {
  491. debugRenderSetup.Dispose();
  492. }
  493. m_DebugRenderSetups.Clear();
  494. m_ActiveDebugRendererList.Clear();
  495. m_ActiveDebugRendererListHdl.Clear();
  496. }
  497. internal void CreateRendererListsWithDebugRenderState(
  498. ScriptableRenderContext context,
  499. ref CullingResults cullResults,
  500. ref DrawingSettings drawingSettings,
  501. ref FilteringSettings filteringSettings,
  502. ref RenderStateBlock renderStateBlock)
  503. {
  504. CreateDebugRenderSetups(filteringSettings);
  505. foreach (DebugRenderSetup debugRenderSetup in m_DebugRenderSetups)
  506. {
  507. DrawingSettings debugDrawingSettings = debugRenderSetup.CreateDrawingSettings(drawingSettings);
  508. RenderStateBlock debugRenderStateBlock = debugRenderSetup.GetRenderStateBlock(renderStateBlock);
  509. RendererList rendererList = new RendererList();
  510. RenderingUtils.CreateRendererListWithRenderStateBlock(context, ref cullResults, debugDrawingSettings, filteringSettings, debugRenderStateBlock, ref rendererList);
  511. m_ActiveDebugRendererList.Add((rendererList));
  512. }
  513. }
  514. internal void CreateRendererListsWithDebugRenderState(
  515. RenderGraph renderGraph,
  516. ref CullingResults cullResults,
  517. ref DrawingSettings drawingSettings,
  518. ref FilteringSettings filteringSettings,
  519. ref RenderStateBlock renderStateBlock)
  520. {
  521. CreateDebugRenderSetups(filteringSettings);
  522. foreach (DebugRenderSetup debugRenderSetup in m_DebugRenderSetups)
  523. {
  524. DrawingSettings debugDrawingSettings = debugRenderSetup.CreateDrawingSettings(drawingSettings);
  525. RenderStateBlock debugRenderStateBlock = debugRenderSetup.GetRenderStateBlock(renderStateBlock);
  526. RendererListHandle rendererListHdl = new RendererListHandle();
  527. RenderingUtils.CreateRendererListWithRenderStateBlock(renderGraph, ref cullResults, debugDrawingSettings, filteringSettings, debugRenderStateBlock, ref rendererListHdl);
  528. m_ActiveDebugRendererListHdl.Add((rendererListHdl));
  529. }
  530. }
  531. internal void PrepareRendererListForRasterPass(IRasterRenderGraphBuilder builder)
  532. {
  533. foreach (RendererListHandle rendererListHdl in m_ActiveDebugRendererListHdl)
  534. {
  535. builder.UseRendererList(rendererListHdl);
  536. }
  537. }
  538. internal void DrawWithRendererList(RasterCommandBuffer cmd)
  539. {
  540. foreach (DebugRenderSetup debugRenderSetup in m_DebugRenderSetups)
  541. {
  542. debugRenderSetup.Begin(cmd);
  543. RendererList rendererList = new RendererList();
  544. if (m_ActiveDebugRendererList.Count > 0)
  545. {
  546. rendererList = m_ActiveDebugRendererList[debugRenderSetup.GetIndex()];
  547. }
  548. else if(m_ActiveDebugRendererListHdl.Count > 0)
  549. {
  550. rendererList = m_ActiveDebugRendererListHdl[debugRenderSetup.GetIndex()];
  551. }
  552. debugRenderSetup.DrawWithRendererList(cmd, ref rendererList);
  553. debugRenderSetup.End(cmd);
  554. }
  555. DisposeDebugRenderLists();
  556. }
  557. }
  558. }