DBufferRenderPass.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System.Collections.Generic;
  2. using UnityEngine.Experimental.Rendering;
  3. using UnityEngine.Rendering.Universal.Internal;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. internal class DecalDrawDBufferSystem : DecalDrawSystem
  7. {
  8. public DecalDrawDBufferSystem(DecalEntityManager entityManager) : base("DecalDrawIntoDBufferSystem.Execute", entityManager) { }
  9. protected override int GetPassIndex(DecalCachedChunk decalCachedChunk) => decalCachedChunk.passIndexDBuffer;
  10. }
  11. internal class DBufferRenderPass : ScriptableRenderPass
  12. {
  13. private static string[] s_DBufferNames = { "_DBufferTexture0", "_DBufferTexture1", "_DBufferTexture2", "_DBufferTexture3" };
  14. private static string s_DBufferDepthName = "DBufferDepth";
  15. private DecalDrawDBufferSystem m_DrawSystem;
  16. private DBufferSettings m_Settings;
  17. private Material m_DBufferClear;
  18. private FilteringSettings m_FilteringSettings;
  19. private List<ShaderTagId> m_ShaderTagIdList;
  20. private int m_DBufferCount;
  21. private ProfilingSampler m_ProfilingSampler;
  22. internal DeferredLights deferredLights { get; set; }
  23. private bool isDeferred => deferredLights != null;
  24. internal RenderTargetIdentifier[] dBufferColorIndentifiers { get; private set; }
  25. internal RenderTargetIdentifier dBufferDepthIndentifier { get; private set; }
  26. internal RenderTargetIdentifier cameraDepthTextureIndentifier { get; private set; }
  27. internal RenderTargetIdentifier cameraDepthAttachmentIndentifier { get; private set; }
  28. public DBufferRenderPass(Material dBufferClear, DBufferSettings settings, DecalDrawDBufferSystem drawSystem)
  29. {
  30. renderPassEvent = RenderPassEvent.AfterRenderingPrePasses + 1;
  31. ConfigureInput(ScriptableRenderPassInput.Normal); // Require depth
  32. m_DrawSystem = drawSystem;
  33. m_Settings = settings;
  34. m_DBufferClear = dBufferClear;
  35. m_ProfilingSampler = new ProfilingSampler("DBuffer Render");
  36. m_FilteringSettings = new FilteringSettings(RenderQueueRange.opaque, -1);
  37. m_ShaderTagIdList = new List<ShaderTagId>();
  38. m_ShaderTagIdList.Add(new ShaderTagId(DecalShaderPassNames.DBufferMesh));
  39. int dBufferCount = (int)settings.surfaceData + 1;
  40. dBufferColorIndentifiers = new RenderTargetIdentifier[dBufferCount];
  41. for (int dbufferIndex = 0; dbufferIndex < dBufferCount; ++dbufferIndex)
  42. dBufferColorIndentifiers[dbufferIndex] = new RenderTargetIdentifier(s_DBufferNames[dbufferIndex]);
  43. m_DBufferCount = dBufferCount;
  44. dBufferDepthIndentifier = new RenderTargetIdentifier(s_DBufferDepthName);
  45. cameraDepthTextureIndentifier = new RenderTargetIdentifier("_CameraDepthTexture");
  46. cameraDepthAttachmentIndentifier = new RenderTargetIdentifier("_CameraDepthAttachment");
  47. }
  48. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  49. {
  50. // base
  51. {
  52. var desc = renderingData.cameraData.cameraTargetDescriptor;
  53. desc.graphicsFormat = QualitySettings.activeColorSpace == ColorSpace.Linear ? GraphicsFormat.R8G8B8A8_SRGB : GraphicsFormat.R8G8B8A8_UNorm;
  54. desc.depthBufferBits = 0;
  55. desc.msaaSamples = 1;
  56. cmd.GetTemporaryRT(Shader.PropertyToID(s_DBufferNames[0]), desc);
  57. }
  58. if (m_Settings.surfaceData == DecalSurfaceData.AlbedoNormal || m_Settings.surfaceData == DecalSurfaceData.AlbedoNormalMAOS)
  59. {
  60. var desc = renderingData.cameraData.cameraTargetDescriptor;
  61. desc.graphicsFormat = GraphicsFormat.R8G8B8A8_UNorm;
  62. desc.depthBufferBits = 0;
  63. desc.msaaSamples = 1;
  64. cmd.GetTemporaryRT(Shader.PropertyToID(s_DBufferNames[1]), desc);
  65. }
  66. if (m_Settings.surfaceData == DecalSurfaceData.AlbedoNormalMAOS)
  67. {
  68. var desc = renderingData.cameraData.cameraTargetDescriptor;
  69. desc.graphicsFormat = GraphicsFormat.R8G8B8A8_UNorm;
  70. desc.depthBufferBits = 0;
  71. desc.msaaSamples = 1;
  72. cmd.GetTemporaryRT(Shader.PropertyToID(s_DBufferNames[2]), desc);
  73. }
  74. // depth
  75. RenderTargetIdentifier depthIdentifier;
  76. if (!isDeferred)
  77. {
  78. var depthDesc = renderingData.cameraData.cameraTargetDescriptor;
  79. depthDesc.graphicsFormat = GraphicsFormat.None; //Depth only rendering
  80. depthDesc.depthStencilFormat = renderingData.cameraData.cameraTargetDescriptor.depthStencilFormat;
  81. depthDesc.msaaSamples = 1;
  82. cmd.GetTemporaryRT(Shader.PropertyToID(s_DBufferDepthName), depthDesc);
  83. depthIdentifier = dBufferDepthIndentifier;
  84. }
  85. else
  86. {
  87. depthIdentifier = deferredLights.DepthAttachmentIdentifier;
  88. }
  89. ConfigureTarget(dBufferColorIndentifiers, depthIdentifier);
  90. }
  91. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  92. {
  93. SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
  94. DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
  95. CommandBuffer cmd = CommandBufferPool.Get();
  96. using (new ProfilingScope(cmd, m_ProfilingSampler))
  97. {
  98. context.ExecuteCommandBuffer(cmd);
  99. cmd.Clear();
  100. if (isDeferred)
  101. {
  102. cmd.SetGlobalTexture("_CameraNormalsTexture", deferredLights.GbufferAttachmentIdentifiers[deferredLights.GBufferNormalSmoothnessIndex]);
  103. }
  104. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT1, m_Settings.surfaceData == DecalSurfaceData.Albedo);
  105. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT2, m_Settings.surfaceData == DecalSurfaceData.AlbedoNormal);
  106. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT3, m_Settings.surfaceData == DecalSurfaceData.AlbedoNormalMAOS);
  107. // TODO: This should be replace with mrt clear once we support it
  108. // Clear render targets
  109. ClearDBuffers(cmd, renderingData.cameraData);
  110. // Split here allows clear to be executed before DrawRenderers
  111. context.ExecuteCommandBuffer(cmd);
  112. cmd.Clear();
  113. m_DrawSystem.Execute(cmd);
  114. context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
  115. }
  116. context.ExecuteCommandBuffer(cmd);
  117. CommandBufferPool.Release(cmd);
  118. }
  119. private void ClearDBuffers(CommandBuffer cmd, in CameraData cameraData)
  120. {
  121. // for alpha compositing, color is cleared to 0, alpha to 1
  122. // https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html
  123. var clearSampleName = "Clear";
  124. cmd.BeginSample(clearSampleName);
  125. Vector4 scaleBias = new Vector4(1, 1, 0, 0);
  126. cmd.SetGlobalVector(ShaderPropertyId.scaleBias, scaleBias);
  127. if (cameraData.xr.enabled)
  128. {
  129. cmd.DrawProcedural(Matrix4x4.identity, m_DBufferClear, 0, MeshTopology.Quads, 4, 1, null);
  130. }
  131. else
  132. {
  133. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity); // Prepare for manual blit
  134. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_DBufferClear, 0, 0);
  135. cmd.SetViewProjectionMatrices(cameraData.camera.worldToCameraMatrix, cameraData.camera.projectionMatrix);
  136. }
  137. cmd.EndSample(clearSampleName);
  138. }
  139. public override void OnCameraCleanup(CommandBuffer cmd)
  140. {
  141. if (cmd == null)
  142. {
  143. throw new System.ArgumentNullException("cmd");
  144. }
  145. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT1, false);
  146. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT2, false);
  147. CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DBufferMRT3, false);
  148. for (int dbufferIndex = 0; dbufferIndex < m_DBufferCount; ++dbufferIndex)
  149. {
  150. cmd.ReleaseTemporaryRT(Shader.PropertyToID(s_DBufferNames[dbufferIndex]));
  151. }
  152. if (!isDeferred)
  153. cmd.ReleaseTemporaryRT(Shader.PropertyToID(s_DBufferDepthName));
  154. }
  155. }
  156. }