Nessuna descrizione
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.

DepthBlitCopyDepthPass.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. using UnityEngine.Rendering.Universal;
  5. // This pass is a simplified version of the URP CopyDepthPass. This pass copies the depth texture to an RTHandle.
  6. // Unlike the original URP CopyDepthPass, this example does not use the _CameraDepthTexture texture, and demonstrates how to copy from the depth buffer to a custom RTHandle instead.
  7. public class DepthBlitCopyDepthPass : ScriptableRenderPass
  8. {
  9. private const string k_PassName = "DepthBlitCopyDepthPass";
  10. private readonly int m_DepthBufferId = Shader.PropertyToID("_CameraDepthAttachment");
  11. private Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
  12. private ProfilingSampler m_ProfilingSampler = new ProfilingSampler(k_PassName);
  13. private RTHandle m_DestRT; // The RTHandle for storing the depth texture, set by the Renderer Feature
  14. private Material m_CopyDepthMaterial;
  15. private GlobalKeyword m_Keyword_DepthMsaa2;
  16. private GlobalKeyword m_Keyword_DepthMsaa4;
  17. private GlobalKeyword m_Keyword_DepthMsaa8;
  18. private GlobalKeyword m_Keyword_OutputDepth;
  19. class PassData
  20. {
  21. public Material copyDepthMaterial;
  22. public TextureHandle source;
  23. public Vector4 scaleBias;
  24. public int depthBufferId;
  25. }
  26. public DepthBlitCopyDepthPass(RenderPassEvent evt, Shader copyDepthShader, RTHandle destination)
  27. {
  28. renderPassEvent = evt;
  29. m_DestRT = destination;
  30. m_CopyDepthMaterial = copyDepthShader != null ? CoreUtils.CreateEngineMaterial(copyDepthShader) : null;
  31. m_Keyword_DepthMsaa2 = GlobalKeyword.Create(ShaderKeywordStrings.DepthMsaa2);
  32. m_Keyword_DepthMsaa4 = GlobalKeyword.Create(ShaderKeywordStrings.DepthMsaa4);
  33. m_Keyword_DepthMsaa8 = GlobalKeyword.Create(ShaderKeywordStrings.DepthMsaa8);
  34. m_Keyword_OutputDepth = GlobalKeyword.Create(ShaderKeywordStrings._OUTPUT_DEPTH);
  35. }
  36. #pragma warning disable 618, 672 // Type or member is obsolete, Member overrides obsolete member
  37. // Set the RTHandle as the output target in the Compatibility mode.
  38. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  39. {
  40. ConfigureTarget(m_DestRT);
  41. }
  42. // Unity calls the Execute method in the Compatibility mode
  43. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  44. {
  45. var cameraData = renderingData.cameraData;
  46. if (cameraData.camera.cameraType != CameraType.Game)
  47. return;
  48. // Bind the depth buffer to material
  49. RTHandle source = cameraData.renderer.cameraDepthTargetHandle;
  50. m_CopyDepthMaterial.SetTexture(m_DepthBufferId, source);
  51. CommandBuffer cmd = CommandBufferPool.Get();
  52. using (new ProfilingScope(cmd, m_ProfilingSampler))
  53. {
  54. // Enable an MSAA shader keyword based on the source texture MSAA sample count.
  55. int cameraSamples = source.rt.antiAliasing;
  56. cmd.SetKeyword(m_Keyword_DepthMsaa2, cameraSamples == 2);
  57. cmd.SetKeyword(m_Keyword_DepthMsaa4, cameraSamples == 4);
  58. cmd.SetKeyword(m_Keyword_DepthMsaa8, cameraSamples == 8);
  59. // This example does not copy the depth values back to the depth buffer, so we disable this keyword.
  60. cmd.SetKeyword(m_Keyword_OutputDepth, false);
  61. // Perform the blit operation
  62. Blitter.BlitTexture(cmd, source, m_ScaleBias, m_CopyDepthMaterial, 0);
  63. }
  64. context.ExecuteCommandBuffer(cmd);
  65. cmd.Clear();
  66. CommandBufferPool.Release(cmd);
  67. }
  68. #pragma warning restore 618, 672
  69. // Unity calls the RecordRenderGraph method to add and configure one or more render passes in the render graph system.
  70. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  71. {
  72. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  73. DepthBlitFeature.TexRefData texRefData = frameData.GetOrCreate<DepthBlitFeature.TexRefData>();
  74. // Avoid blitting from the backbuffer
  75. if (resourceData.isActiveTargetBackBuffer)
  76. return;
  77. // Set the texture resources for this render graph instance.
  78. TextureHandle src = resourceData.cameraDepth;
  79. TextureHandle dest = renderGraph.ImportTexture(m_DestRT);
  80. texRefData.depthTextureHandle = dest;
  81. if(!src.IsValid() || !dest.IsValid())
  82. return;
  83. using (var builder = renderGraph.AddRasterRenderPass<PassData>(k_PassName, out var passData, m_ProfilingSampler))
  84. {
  85. passData.copyDepthMaterial = m_CopyDepthMaterial;
  86. passData.source = src;
  87. passData.scaleBias = m_ScaleBias;
  88. passData.depthBufferId = m_DepthBufferId;
  89. builder.UseTexture(src, AccessFlags.Read);
  90. builder.SetRenderAttachment(dest, 0, AccessFlags.Write);
  91. builder.AllowGlobalStateModification(true);
  92. builder.AllowPassCulling(false);
  93. builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
  94. {
  95. // Enable an MSAA shader keyword based on the source texture MSAA sample count
  96. RTHandle sourceTex = data.source;
  97. int cameraSamples = sourceTex.rt.antiAliasing;
  98. context.cmd.SetKeyword(m_Keyword_DepthMsaa2, cameraSamples == 2);
  99. context.cmd.SetKeyword(m_Keyword_DepthMsaa4, cameraSamples == 4);
  100. context.cmd.SetKeyword(m_Keyword_DepthMsaa8, cameraSamples == 8);
  101. // This example does not copy the depth values back to the depth buffer, so we disable this keyword.
  102. context.cmd.SetKeyword(m_Keyword_OutputDepth, false);
  103. // Bind the depth buffer to the material
  104. data.copyDepthMaterial.SetTexture(data.depthBufferId, data.source);
  105. // Perform the blit operation
  106. Blitter.BlitTexture(context.cmd, data.source, data.scaleBias, data.copyDepthMaterial, 0);
  107. });
  108. }
  109. }
  110. public void Dispose()
  111. {
  112. CoreUtils.Destroy(m_CopyDepthMaterial);
  113. }
  114. }