CopyDepthPass.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. namespace UnityEngine.Rendering.Universal.Internal
  3. {
  4. /// <summary>
  5. /// Copy the given depth buffer into the given destination depth buffer.
  6. ///
  7. /// You can use this pass to copy a depth buffer to a destination,
  8. /// so you can use it later in rendering. If the source texture has MSAA
  9. /// enabled, the pass uses a custom MSAA resolve. If the source texture
  10. /// does not have MSAA enabled, the pass uses a Blit or a Copy Texture
  11. /// operation, depending on what the current platform supports.
  12. /// </summary>
  13. public class CopyDepthPass : ScriptableRenderPass
  14. {
  15. private RenderTargetHandle source { get; set; }
  16. private RenderTargetHandle destination { get; set; }
  17. Material m_CopyDepthMaterial;
  18. const string m_ProfilerTag = "Copy Depth";
  19. public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial)
  20. {
  21. m_CopyDepthMaterial = copyDepthMaterial;
  22. renderPassEvent = evt;
  23. }
  24. /// <summary>
  25. /// Configure the pass with the source and destination to execute on.
  26. /// </summary>
  27. /// <param name="source">Source Render Target</param>
  28. /// <param name="destination">Destination Render Targt</param>
  29. public void Setup(RenderTargetHandle source, RenderTargetHandle destination)
  30. {
  31. this.source = source;
  32. this.destination = destination;
  33. }
  34. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  35. {
  36. var descriptor = cameraTextureDescriptor;
  37. descriptor.colorFormat = RenderTextureFormat.Depth;
  38. descriptor.depthBufferBits = 32; //TODO: do we really need this. double check;
  39. descriptor.msaaSamples = 1;
  40. cmd.GetTemporaryRT(destination.id, descriptor, FilterMode.Point);
  41. }
  42. /// <inheritdoc/>
  43. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  44. {
  45. if (m_CopyDepthMaterial == null)
  46. {
  47. Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_CopyDepthMaterial, GetType().Name);
  48. return;
  49. }
  50. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  51. RenderTargetIdentifier depthSurface = source.Identifier();
  52. RenderTargetIdentifier copyDepthSurface = destination.Identifier();
  53. RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
  54. int cameraSamples = descriptor.msaaSamples;
  55. // TODO: we don't need a command buffer here. We can set these via Material.Set* API
  56. cmd.SetGlobalTexture("_CameraDepthAttachment", source.Identifier());
  57. if (cameraSamples > 1)
  58. {
  59. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthNoMsaa);
  60. if (cameraSamples == 4)
  61. {
  62. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  63. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  64. }
  65. else
  66. {
  67. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  68. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  69. }
  70. Blit(cmd, depthSurface, copyDepthSurface, m_CopyDepthMaterial);
  71. }
  72. else
  73. {
  74. cmd.EnableShaderKeyword(ShaderKeywordStrings.DepthNoMsaa);
  75. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa2);
  76. cmd.DisableShaderKeyword(ShaderKeywordStrings.DepthMsaa4);
  77. CopyTexture(cmd, depthSurface, copyDepthSurface, m_CopyDepthMaterial);
  78. }
  79. context.ExecuteCommandBuffer(cmd);
  80. CommandBufferPool.Release(cmd);
  81. }
  82. void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, Material material)
  83. {
  84. // TODO: In order to issue a copyTexture we need to also check if source and dest have same size
  85. //if (SystemInfo.copyTextureSupport != CopyTextureSupport.None)
  86. // cmd.CopyTexture(source, dest);
  87. //else
  88. Blit(cmd, source, dest, material);
  89. }
  90. /// <inheritdoc/>
  91. public override void FrameCleanup(CommandBuffer cmd)
  92. {
  93. if (cmd == null)
  94. throw new ArgumentNullException("cmd");
  95. cmd.ReleaseTemporaryRT(destination.id);
  96. destination = RenderTargetHandle.CameraTarget;
  97. }
  98. }
  99. }