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.

UniversalRendererDebug.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using UnityEngine.Experimental.Rendering;
  2. using UnityEngine.Rendering.RenderGraphModule;
  3. using UnityEngine.Rendering.Universal.Internal;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. public sealed partial class UniversalRenderer
  7. {
  8. bool DebugHandlerRequireDepthPass(UniversalCameraData cameraData)
  9. {
  10. if ((DebugHandler != null) && DebugHandler.IsActiveForCamera(cameraData.isPreviewCamera))
  11. {
  12. if (DebugHandler.TryGetFullscreenDebugMode(out _))
  13. return true;
  14. }
  15. return false;
  16. }
  17. void CreateDebugTexture(RenderTextureDescriptor descriptor)
  18. {
  19. var debugTexDescriptor = descriptor;
  20. debugTexDescriptor.useMipMap = false;
  21. debugTexDescriptor.autoGenerateMips = false;
  22. debugTexDescriptor.bindMS = false;
  23. debugTexDescriptor.depthBufferBits = 0;
  24. RenderingUtils.ReAllocateHandleIfNeeded(ref m_RenderGraphDebugTextureHandle, debugTexDescriptor, FilterMode.Point, TextureWrapMode.Clamp, name: "_RenderingDebuggerTexture");
  25. }
  26. private Rect CalculateUVRect(UniversalCameraData cameraData, float width, float height)
  27. {
  28. float normalizedSizeX = width / cameraData.pixelWidth;
  29. float normalizedSizeY = height / cameraData.pixelHeight;
  30. return new Rect(1 - normalizedSizeX, 1 - normalizedSizeY, normalizedSizeX, normalizedSizeY);
  31. }
  32. private Rect CalculateUVRect(UniversalCameraData cameraData, int textureHeightPercent)
  33. {
  34. var relativeSize = Mathf.Clamp01(textureHeightPercent / 100f);
  35. var width = relativeSize * cameraData.pixelWidth;
  36. var height = relativeSize * cameraData.pixelHeight;
  37. return CalculateUVRect(cameraData, width, height);
  38. }
  39. private void CorrectForTextureAspectRatio(ref float width, ref float height, float sourceWidth, float sourceHeight)
  40. {
  41. if (sourceWidth != 0 && sourceHeight != 0)
  42. {
  43. // Ensure that atlas is not stretched, but doesn't take up more than the percentage in any dimension.
  44. var targetWidth = height * sourceWidth / sourceHeight;
  45. if (targetWidth > width)
  46. {
  47. height = width * sourceHeight / sourceWidth;
  48. }
  49. else
  50. {
  51. width = targetWidth;
  52. }
  53. }
  54. }
  55. private void SetupRenderGraphFinalPassDebug(RenderGraph renderGraph, ContextContainer frameData)
  56. {
  57. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  58. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  59. if ((DebugHandler != null) && DebugHandler.IsActiveForCamera(cameraData.isPreviewCamera))
  60. {
  61. if (DebugHandler.TryGetFullscreenDebugMode(out DebugFullScreenMode fullScreenDebugMode, out int textureHeightPercent) &&
  62. (fullScreenDebugMode != DebugFullScreenMode.ReflectionProbeAtlas || m_Clustering) &&
  63. (fullScreenDebugMode != DebugFullScreenMode.STP))
  64. {
  65. float screenWidth = cameraData.pixelWidth;
  66. float screenHeight = cameraData.pixelHeight;
  67. var relativeSize = Mathf.Clamp01(textureHeightPercent / 100f);
  68. var height = relativeSize * screenHeight;
  69. var width = relativeSize * screenWidth;
  70. bool supportsStereo = false;
  71. Vector4 dataRangeRemap = Vector4.zero; // zero = off, .x = old min, .y = old max, .z = new min, .w = new max
  72. // visualize RG internal resources
  73. {
  74. // if we want to visualize RG internal resources, we need to create an RTHandle external to RG and copy to it the textures to visualize
  75. // this is required because the lifetime of these resources is limited to the RenderGraph execution, and we cannot access the actual resources here
  76. // we also copy external resources to make them "read only". CreateDebugTexture() can lead to (external) texture reallocation.
  77. var debugDescriptor = cameraData.cameraTargetDescriptor;
  78. // Ensure target can hold all source values. Source can be signed for example.
  79. if(SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, GraphicsFormatUsage.Linear | GraphicsFormatUsage.Render))
  80. debugDescriptor.graphicsFormat = GraphicsFormat.R16G16B16A16_SFloat;
  81. CreateDebugTexture(debugDescriptor);
  82. ImportResourceParams importParams = new ImportResourceParams();
  83. importParams.clearOnFirstUse = false;
  84. importParams.discardOnLastUse = false;
  85. TextureHandle debugDepthTexture = renderGraph.ImportTexture(m_RenderGraphDebugTextureHandle, importParams);
  86. switch (fullScreenDebugMode)
  87. {
  88. case DebugFullScreenMode.Depth:
  89. {
  90. CopyToDebugTexture(renderGraph, resourceData.cameraDepthTexture, debugDepthTexture);
  91. supportsStereo = true;
  92. break;
  93. }
  94. case DebugFullScreenMode.MotionVector:
  95. {
  96. CopyToDebugTexture(renderGraph, resourceData.motionVectorColor, debugDepthTexture);
  97. supportsStereo = true;
  98. // Motion vectors are in signed UV space, zoom in and normalize for visualization. (note: maybe add an option to use (angle, mag) visualization)
  99. const float zoom = 0.01f;
  100. dataRangeRemap.x = -zoom;
  101. dataRangeRemap.y = zoom;
  102. dataRangeRemap.z = 0;
  103. dataRangeRemap.w = 1.0f;
  104. break;
  105. }
  106. case DebugFullScreenMode.AdditionalLightsShadowMap:
  107. {
  108. CopyToDebugTexture(renderGraph, resourceData.additionalShadowsTexture, debugDepthTexture);
  109. break;
  110. }
  111. case DebugFullScreenMode.MainLightShadowMap:
  112. {
  113. CopyToDebugTexture(renderGraph, resourceData.mainShadowsTexture, debugDepthTexture);
  114. break;
  115. }
  116. case DebugFullScreenMode.AdditionalLightsCookieAtlas:
  117. {
  118. if (m_LightCookieManager != null)
  119. {
  120. // Copy atlas texture to make it "readonly". Direct reference (debug=atlas) can lead to handle->texture reallocation.
  121. var texHdl = renderGraph.ImportTexture(m_LightCookieManager.AdditionalLightsCookieAtlasTexture);
  122. CopyToDebugTexture(renderGraph, texHdl, debugDepthTexture);
  123. }
  124. break;
  125. }
  126. case DebugFullScreenMode.ReflectionProbeAtlas:
  127. {
  128. if (m_ForwardLights.reflectionProbeManager.atlasRT != null)
  129. {
  130. // Copy atlas texture to make it "readonly". Direct reference (debug=atlas) can lead to handle->texture reallocation.
  131. var texHdl = renderGraph.ImportTexture(RTHandles.Alloc(m_ForwardLights.reflectionProbeManager.atlasRT, transferOwnership: true));
  132. CopyToDebugTexture(renderGraph, texHdl, debugDepthTexture);
  133. }
  134. break;
  135. }
  136. default:
  137. {
  138. break;
  139. }
  140. }
  141. }
  142. // Textures that are not in screen aspect ration need to be corrected
  143. {
  144. RenderTexture source = null;
  145. switch (fullScreenDebugMode)
  146. {
  147. case DebugFullScreenMode.AdditionalLightsShadowMap: source = m_AdditionalLightsShadowCasterPass?.m_AdditionalLightsShadowmapHandle?.rt; break;
  148. case DebugFullScreenMode.MainLightShadowMap: source = m_MainLightShadowCasterPass?.m_MainLightShadowmapTexture?.rt; break;
  149. case DebugFullScreenMode.AdditionalLightsCookieAtlas: source = m_LightCookieManager?.AdditionalLightsCookieAtlasTexture?.rt; break;
  150. case DebugFullScreenMode.ReflectionProbeAtlas: source = m_ForwardLights?.reflectionProbeManager.atlasRT; break;
  151. default:
  152. break;
  153. }
  154. // Ensure that atlas is not stretched, but doesn't take up more than the percentage in any dimension.
  155. if (source != null)
  156. CorrectForTextureAspectRatio(ref width, ref height, source.width, source.height);
  157. }
  158. Rect uvRect = CalculateUVRect(cameraData, width, height);
  159. DebugHandler.SetDebugRenderTarget(m_RenderGraphDebugTextureHandle, uvRect, supportsStereo, dataRangeRemap);
  160. }
  161. else
  162. {
  163. DebugHandler.ResetDebugRenderTarget();
  164. }
  165. }
  166. if (DebugHandler != null)
  167. {
  168. if (!DebugHandler.TryGetFullscreenDebugMode(out DebugFullScreenMode fullScreenDebugMode, out int textureHeightPercent))
  169. {
  170. var debugSettings = DebugHandler.DebugDisplaySettings.gpuResidentDrawerSettings;
  171. GPUResidentDrawer.RenderDebugOcclusionTestOverlay(renderGraph, debugSettings, cameraData.camera.GetInstanceID(), resourceData.activeColorTexture);
  172. float screenWidth = (int)(cameraData.pixelHeight * cameraData.renderScale);
  173. float screenHeight = (int)(cameraData.pixelHeight * cameraData.renderScale);
  174. float maxHeight = screenHeight * textureHeightPercent / 100.0f;
  175. GPUResidentDrawer.RenderDebugOccluderOverlay(renderGraph, debugSettings, new Vector2(0.25f * screenWidth, screenHeight - 1.5f * maxHeight), maxHeight, resourceData.activeColorTexture);
  176. }
  177. }
  178. }
  179. private void SetupAfterPostRenderGraphFinalPassDebug(RenderGraph renderGraph, ContextContainer frameData)
  180. {
  181. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  182. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  183. if ((DebugHandler != null) && DebugHandler.IsActiveForCamera(cameraData.isPreviewCamera))
  184. {
  185. if (DebugHandler.TryGetFullscreenDebugMode(out var debugFullscreenMode, out int textureHeightPercent) &&
  186. (debugFullscreenMode == DebugFullScreenMode.STP))
  187. {
  188. CreateDebugTexture(cameraData.cameraTargetDescriptor);
  189. ImportResourceParams importParams = new ImportResourceParams();
  190. importParams.clearOnFirstUse = false;
  191. importParams.discardOnLastUse = false;
  192. TextureHandle debugTexture = renderGraph.ImportTexture(m_RenderGraphDebugTextureHandle, importParams);
  193. CopyToDebugTexture(renderGraph, resourceData.stpDebugView, debugTexture);
  194. Rect uvRect = CalculateUVRect(cameraData, textureHeightPercent);
  195. Vector4 rangeRemap = Vector4.zero; // Off
  196. DebugHandler.SetDebugRenderTarget(m_RenderGraphDebugTextureHandle, uvRect, true, rangeRemap);
  197. }
  198. }
  199. }
  200. class CopyToDebugTexturePassData
  201. {
  202. internal TextureHandle src;
  203. internal TextureHandle dest;
  204. }
  205. private void CopyToDebugTexture(RenderGraph renderGraph, TextureHandle source, TextureHandle destination, string passName = "Copy To Debug Texture")
  206. {
  207. using (var builder = renderGraph.AddRasterRenderPass<CopyToDebugTexturePassData>(passName, out var passData))
  208. {
  209. if (source.IsValid())
  210. {
  211. passData.src = source;
  212. builder.UseTexture(source);
  213. }
  214. else
  215. {
  216. passData.src = renderGraph.defaultResources.blackTexture;
  217. }
  218. passData.dest = destination;
  219. builder.SetRenderAttachment(destination, 0);
  220. builder.AllowPassCulling(false);
  221. builder.SetRenderFunc((CopyToDebugTexturePassData data, RasterGraphContext context) =>
  222. {
  223. Blitter.BlitTexture(context.cmd, data.src, new Vector4(1,1,0,0), 0, false);
  224. });
  225. }
  226. }
  227. }
  228. }