DepthOnlyPass.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. namespace UnityEngine.Rendering.Universal.Internal
  3. {
  4. /// <summary>
  5. /// Render all objects that have a 'DepthOnly' pass into the given depth buffer.
  6. ///
  7. /// You can use this pass to prime a depth buffer for subsequent rendering.
  8. /// Use it as a z-prepass, or use it to generate a depth buffer.
  9. /// </summary>
  10. public class DepthOnlyPass : ScriptableRenderPass
  11. {
  12. int kDepthBufferBits = 32;
  13. private RenderTargetHandle depthAttachmentHandle { get; set; }
  14. internal RenderTextureDescriptor descriptor { get; private set; }
  15. FilteringSettings m_FilteringSettings;
  16. const string m_ProfilerTag = "Depth Prepass";
  17. ProfilingSampler m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
  18. ShaderTagId m_ShaderTagId = new ShaderTagId("DepthOnly");
  19. /// <summary>
  20. /// Create the DepthOnlyPass
  21. /// </summary>
  22. public DepthOnlyPass(RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask)
  23. {
  24. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  25. renderPassEvent = evt;
  26. }
  27. /// <summary>
  28. /// Configure the pass
  29. /// </summary>
  30. public void Setup(
  31. RenderTextureDescriptor baseDescriptor,
  32. RenderTargetHandle depthAttachmentHandle)
  33. {
  34. this.depthAttachmentHandle = depthAttachmentHandle;
  35. baseDescriptor.colorFormat = RenderTextureFormat.Depth;
  36. baseDescriptor.depthBufferBits = kDepthBufferBits;
  37. // Depth-Only pass don't use MSAA
  38. baseDescriptor.msaaSamples = 1;
  39. descriptor = baseDescriptor;
  40. }
  41. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  42. {
  43. cmd.GetTemporaryRT(depthAttachmentHandle.id, descriptor, FilterMode.Point);
  44. ConfigureTarget(depthAttachmentHandle.Identifier());
  45. ConfigureClear(ClearFlag.All, Color.black);
  46. }
  47. /// <inheritdoc/>
  48. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  49. {
  50. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  51. using (new ProfilingScope(cmd, m_ProfilingSampler))
  52. {
  53. context.ExecuteCommandBuffer(cmd);
  54. cmd.Clear();
  55. var sortFlags = renderingData.cameraData.defaultOpaqueSortFlags;
  56. var drawSettings = CreateDrawingSettings(m_ShaderTagId, ref renderingData, sortFlags);
  57. drawSettings.perObjectData = PerObjectData.None;
  58. ref CameraData cameraData = ref renderingData.cameraData;
  59. Camera camera = cameraData.camera;
  60. if (cameraData.isStereoEnabled)
  61. {
  62. context.StartMultiEye(camera, eyeIndex);
  63. }
  64. context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref m_FilteringSettings);
  65. }
  66. context.ExecuteCommandBuffer(cmd);
  67. CommandBufferPool.Release(cmd);
  68. }
  69. /// <inheritdoc/>
  70. public override void FrameCleanup(CommandBuffer cmd)
  71. {
  72. if (cmd == null)
  73. throw new ArgumentNullException("cmd");
  74. if (depthAttachmentHandle != RenderTargetHandle.CameraTarget)
  75. {
  76. cmd.ReleaseTemporaryRT(depthAttachmentHandle.id);
  77. depthAttachmentHandle = RenderTargetHandle.CameraTarget;
  78. }
  79. }
  80. }
  81. }