CopyDepthPass.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. namespace UnityEngine.Rendering.Universal.Internal
  4. {
  5. /// <summary>
  6. /// Copy the given depth buffer into the given destination depth buffer.
  7. ///
  8. /// You can use this pass to copy a depth buffer to a destination,
  9. /// so you can use it later in rendering. If the source texture has MSAA
  10. /// enabled, the pass uses a custom MSAA resolve. If the source texture
  11. /// does not have MSAA enabled, the pass uses a Blit or a Copy Texture
  12. /// operation, depending on what the current platform supports.
  13. /// </summary>
  14. public class CopyDepthPass : ScriptableRenderPass
  15. {
  16. private RenderTargetHandle source { get; set; }
  17. private RenderTargetHandle destination { get; set; }
  18. internal bool AllocateRT { get; set; }
  19. internal int MssaSamples { get; set; }
  20. Material m_CopyDepthMaterial;
  21. public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial)
  22. {
  23. base.profilingSampler = new ProfilingSampler(nameof(CopyDepthPass));
  24. AllocateRT = true;
  25. m_CopyDepthMaterial = copyDepthMaterial;
  26. renderPassEvent = evt;
  27. }
  28. /// <summary>
  29. /// Configure the pass with the source and destination to execute on.
  30. /// </summary>
  31. /// <param name="source">Source Render Target</param>
  32. /// <param name="destination">Destination Render Targt</param>
  33. public void Setup(RenderTargetHandle source, RenderTargetHandle destination)
  34. {
  35. this.source = source;
  36. this.destination = destination;
  37. this.AllocateRT = !destination.HasInternalRenderTargetId();
  38. this.MssaSamples = -1;
  39. }
  40. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  41. {
  42. var descriptor = renderingData.cameraData.cameraTargetDescriptor;
  43. descriptor.colorFormat = RenderTextureFormat.Depth;
  44. descriptor.depthBufferBits = 32; //TODO: do we really need this. double check;
  45. descriptor.msaaSamples = 1;
  46. if (this.AllocateRT)
  47. cmd.GetTemporaryRT(destination.id, descriptor, FilterMode.Point);
  48. // On Metal iOS, prevent camera attachments to be bound and cleared during this pass.
  49. ConfigureTarget(new RenderTargetIdentifier(destination.Identifier(), 0, CubemapFace.Unknown, -1), descriptor.depthStencilFormat, descriptor.width, descriptor.height, descriptor.msaaSamples, true);
  50. ConfigureClear(ClearFlag.None, Color.black);
  51. }
  52. /// <inheritdoc/>
  53. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  54. {
  55. if (m_CopyDepthMaterial == null)
  56. {
  57. Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_CopyDepthMaterial, GetType().Name);
  58. return;
  59. }
  60. CommandBuffer cmd = CommandBufferPool.Get();
  61. using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.CopyDepth)))
  62. {
  63. int cameraSamples = 0;
  64. if (MssaSamples == -1)
  65. {
  66. RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
  67. cameraSamples = descriptor.msaaSamples;
  68. }
  69. else
  70. cameraSamples = MssaSamples;
  71. // When auto resolve is supported or multisampled texture is not supported, set camera samples to 1
  72. if (SystemInfo.supportsMultisampleAutoResolve || SystemInfo.supportsMultisampledTextures == 0)
  73. cameraSamples = 1;
  74. CameraData cameraData = renderingData.cameraData;
  75. switch (cameraSamples)
  76. {
  77. case 8:
  78. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  79. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  80. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthMsaa8);
  81. break;
  82. case 4:
  83. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  84. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  85. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa8);
  86. break;
  87. case 2:
  88. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  89. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  90. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa8);
  91. break;
  92. // MSAA disabled, auto resolve supported or ms textures not supported
  93. default:
  94. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  95. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  96. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa8);
  97. break;
  98. }
  99. cmd.SetGlobalTexture("_CameraDepthAttachment", source.Identifier());
  100. #if ENABLE_VR && ENABLE_XR_MODULE
  101. // XR uses procedural draw instead of cmd.blit or cmd.DrawFullScreenMesh
  102. if (renderingData.cameraData.xr.enabled)
  103. {
  104. // XR flip logic is not the same as non-XR case because XR uses draw procedure
  105. // and draw procedure does not need to take projection matrix yflip into account
  106. // We y-flip if
  107. // 1) we are bliting from render texture to back buffer and
  108. // 2) renderTexture starts UV at top
  109. // XRTODO: handle scalebias and scalebiasRt for src and dst separately
  110. bool isRenderToBackBufferTarget = destination.Identifier() == cameraData.xr.renderTarget && !cameraData.xr.renderTargetIsRenderTexture;
  111. bool yflip = isRenderToBackBufferTarget && SystemInfo.graphicsUVStartsAtTop;
  112. float flipSign = (yflip) ? -1.0f : 1.0f;
  113. Vector4 scaleBiasRt = (flipSign < 0.0f)
  114. ? new Vector4(flipSign, 1.0f, -1.0f, 1.0f)
  115. : new Vector4(flipSign, 0.0f, 1.0f, 1.0f);
  116. cmd.SetGlobalVector(ShaderPropertyId.scaleBiasRt, scaleBiasRt);
  117. cmd.DrawProcedural(Matrix4x4.identity, m_CopyDepthMaterial, 0, MeshTopology.Quads, 4);
  118. }
  119. else
  120. #endif
  121. {
  122. // Blit has logic to flip projection matrix when rendering to render texture.
  123. // Currently the y-flip is handled in CopyDepthPass.hlsl by checking _ProjectionParams.x
  124. // If you replace this Blit with a Draw* that sets projection matrix double check
  125. // to also update shader.
  126. // scaleBias.x = flipSign
  127. // scaleBias.y = scale
  128. // scaleBias.z = bias
  129. // scaleBias.w = unused
  130. // In game view final target acts as back buffer were target is not flipped
  131. bool isGameViewFinalTarget = (cameraData.cameraType == CameraType.Game && destination == RenderTargetHandle.CameraTarget);
  132. bool yflip = (cameraData.IsCameraProjectionMatrixFlipped()) && !isGameViewFinalTarget;
  133. float flipSign = yflip ? -1.0f : 1.0f;
  134. Vector4 scaleBiasRt = (flipSign < 0.0f)
  135. ? new Vector4(flipSign, 1.0f, -1.0f, 1.0f)
  136. : new Vector4(flipSign, 0.0f, 1.0f, 1.0f);
  137. cmd.SetGlobalVector(ShaderPropertyId.scaleBiasRt, scaleBiasRt);
  138. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_CopyDepthMaterial);
  139. }
  140. }
  141. context.ExecuteCommandBuffer(cmd);
  142. CommandBufferPool.Release(cmd);
  143. }
  144. /// <inheritdoc/>
  145. public override void OnCameraCleanup(CommandBuffer cmd)
  146. {
  147. if (cmd == null)
  148. throw new ArgumentNullException("cmd");
  149. if (this.AllocateRT)
  150. cmd.ReleaseTemporaryRT(destination.id);
  151. destination = RenderTargetHandle.CameraTarget;
  152. }
  153. }
  154. }