DebugRenderSetup.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. namespace UnityEngine.Rendering.Universal
  3. {
  4. class DebugRenderSetup : IDisposable
  5. {
  6. private readonly DebugHandler m_DebugHandler;
  7. private readonly ScriptableRenderContext m_Context;
  8. private readonly CommandBuffer m_CommandBuffer;
  9. private readonly int m_Index;
  10. private DebugDisplaySettingsMaterial MaterialSettings => m_DebugHandler.DebugDisplaySettings.MaterialSettings;
  11. private DebugDisplaySettingsRendering RenderingSettings => m_DebugHandler.DebugDisplaySettings.RenderingSettings;
  12. private DebugDisplaySettingsLighting LightingSettings => m_DebugHandler.DebugDisplaySettings.LightingSettings;
  13. private void Begin()
  14. {
  15. DebugSceneOverrideMode sceneOverrideMode = RenderingSettings.debugSceneOverrideMode;
  16. switch (sceneOverrideMode)
  17. {
  18. case DebugSceneOverrideMode.Wireframe:
  19. {
  20. m_Context.Submit();
  21. GL.wireframe = true;
  22. break;
  23. }
  24. case DebugSceneOverrideMode.SolidWireframe:
  25. case DebugSceneOverrideMode.ShadedWireframe:
  26. {
  27. if (m_Index == 1)
  28. {
  29. m_Context.Submit();
  30. GL.wireframe = true;
  31. }
  32. break;
  33. }
  34. }
  35. m_DebugHandler.SetupShaderProperties(m_CommandBuffer, m_Index);
  36. m_Context.ExecuteCommandBuffer(m_CommandBuffer);
  37. m_CommandBuffer.Clear();
  38. }
  39. private void End()
  40. {
  41. DebugSceneOverrideMode sceneOverrideMode = RenderingSettings.debugSceneOverrideMode;
  42. switch (sceneOverrideMode)
  43. {
  44. case DebugSceneOverrideMode.Wireframe:
  45. {
  46. m_Context.Submit();
  47. GL.wireframe = false;
  48. break;
  49. }
  50. case DebugSceneOverrideMode.SolidWireframe:
  51. case DebugSceneOverrideMode.ShadedWireframe:
  52. {
  53. if (m_Index == 1)
  54. {
  55. m_Context.Submit();
  56. GL.wireframe = false;
  57. }
  58. break;
  59. }
  60. }
  61. }
  62. internal DebugRenderSetup(DebugHandler debugHandler, ScriptableRenderContext context, CommandBuffer commandBuffer, int index)
  63. {
  64. m_DebugHandler = debugHandler;
  65. m_Context = context;
  66. m_CommandBuffer = commandBuffer;
  67. m_Index = index;
  68. Begin();
  69. }
  70. internal DrawingSettings CreateDrawingSettings(DrawingSettings drawingSettings)
  71. {
  72. bool usesReplacementMaterial = (MaterialSettings.DebugVertexAttributeIndexData != DebugVertexAttributeMode.None);
  73. if (usesReplacementMaterial)
  74. {
  75. Material replacementMaterial = m_DebugHandler.ReplacementMaterial;
  76. DrawingSettings modifiedDrawingSettings = drawingSettings;
  77. modifiedDrawingSettings.overrideMaterial = replacementMaterial;
  78. modifiedDrawingSettings.overrideMaterialPassIndex = 0;
  79. return modifiedDrawingSettings;
  80. }
  81. // No overrides, return original
  82. return drawingSettings;
  83. }
  84. internal RenderStateBlock GetRenderStateBlock(RenderStateBlock renderStateBlock)
  85. {
  86. DebugSceneOverrideMode sceneOverrideMode = RenderingSettings.debugSceneOverrideMode;
  87. // Potentially override parts of the RenderStateBlock
  88. switch (sceneOverrideMode)
  89. {
  90. case DebugSceneOverrideMode.Overdraw:
  91. {
  92. RenderTargetBlendState additiveBlend = new RenderTargetBlendState(sourceColorBlendMode: BlendMode.One, destinationColorBlendMode: BlendMode.One);
  93. // Additive-blend but leave z-write and culling as they are when we draw normally
  94. renderStateBlock.blendState = new BlendState { blendState0 = additiveBlend };
  95. renderStateBlock.mask = RenderStateMask.Blend;
  96. break;
  97. }
  98. case DebugSceneOverrideMode.SolidWireframe:
  99. case DebugSceneOverrideMode.ShadedWireframe:
  100. {
  101. if (m_Index == 1)
  102. {
  103. // Ensure we render the wireframe in front of the solid triangles of the previous pass...
  104. renderStateBlock.rasterState = new RasterState(offsetUnits: -1, offsetFactor: -1);
  105. renderStateBlock.mask = RenderStateMask.Raster;
  106. }
  107. break;
  108. }
  109. }
  110. return renderStateBlock;
  111. }
  112. public void Dispose()
  113. {
  114. End();
  115. }
  116. }
  117. }