DrawObjectsPass.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Profiling;
  4. namespace UnityEngine.Rendering.Universal.Internal
  5. {
  6. /// <summary>
  7. /// Draw objects into the given color and depth target
  8. ///
  9. /// You can use this pass to render objects that have a material and/or shader
  10. /// with the pass names UniversalForward or SRPDefaultUnlit.
  11. /// </summary>
  12. public class DrawObjectsPass : ScriptableRenderPass
  13. {
  14. FilteringSettings m_FilteringSettings;
  15. RenderStateBlock m_RenderStateBlock;
  16. List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
  17. string m_ProfilerTag;
  18. ProfilingSampler m_ProfilingSampler;
  19. bool m_IsOpaque;
  20. bool m_UseDepthPriming;
  21. static readonly int s_DrawObjectPassDataPropID = Shader.PropertyToID("_DrawObjectPassData");
  22. public DrawObjectsPass(string profilerTag, ShaderTagId[] shaderTagIds, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  23. {
  24. base.profilingSampler = new ProfilingSampler(nameof(DrawObjectsPass));
  25. m_ProfilerTag = profilerTag;
  26. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  27. foreach (ShaderTagId sid in shaderTagIds)
  28. m_ShaderTagIdList.Add(sid);
  29. renderPassEvent = evt;
  30. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  31. m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
  32. m_IsOpaque = opaque;
  33. if (stencilState.enabled)
  34. {
  35. m_RenderStateBlock.stencilReference = stencilReference;
  36. m_RenderStateBlock.mask = RenderStateMask.Stencil;
  37. m_RenderStateBlock.stencilState = stencilState;
  38. }
  39. }
  40. public DrawObjectsPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  41. : this(profilerTag,
  42. new ShaderTagId[] { new ShaderTagId("SRPDefaultUnlit"), new ShaderTagId("UniversalForward"), new ShaderTagId("UniversalForwardOnly") },
  43. opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
  44. { }
  45. internal DrawObjectsPass(URPProfileId profileId, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  46. : this(profileId.GetType().Name, opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
  47. {
  48. m_ProfilingSampler = ProfilingSampler.Get(profileId);
  49. }
  50. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  51. {
  52. if (renderingData.cameraData.renderer.useDepthPriming && m_IsOpaque && (renderingData.cameraData.renderType == CameraRenderType.Base || renderingData.cameraData.clearDepth))
  53. {
  54. m_RenderStateBlock.depthState = new DepthState(false, CompareFunction.Equal);
  55. m_RenderStateBlock.mask |= RenderStateMask.Depth;
  56. }
  57. else if (m_RenderStateBlock.depthState.compareFunction == CompareFunction.Equal)
  58. {
  59. m_RenderStateBlock.depthState = new DepthState(true, CompareFunction.LessEqual);
  60. m_RenderStateBlock.mask |= RenderStateMask.Depth;
  61. }
  62. }
  63. /// <inheritdoc/>
  64. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  65. {
  66. // NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
  67. // Currently there's an issue which results in mismatched markers.
  68. CommandBuffer cmd = CommandBufferPool.Get();
  69. using (new ProfilingScope(cmd, m_ProfilingSampler))
  70. {
  71. // Global render pass data containing various settings.
  72. // x,y,z are currently unused
  73. // w is used for knowing whether the object is opaque(1) or alpha blended(0)
  74. Vector4 drawObjectPassData = new Vector4(0.0f, 0.0f, 0.0f, (m_IsOpaque) ? 1.0f : 0.0f);
  75. cmd.SetGlobalVector(s_DrawObjectPassDataPropID, drawObjectPassData);
  76. // scaleBias.x = flipSign
  77. // scaleBias.y = scale
  78. // scaleBias.z = bias
  79. // scaleBias.w = unused
  80. float flipSign = (renderingData.cameraData.IsCameraProjectionMatrixFlipped()) ? -1.0f : 1.0f;
  81. Vector4 scaleBias = (flipSign < 0.0f)
  82. ? new Vector4(flipSign, 1.0f, -1.0f, 1.0f)
  83. : new Vector4(flipSign, 0.0f, 1.0f, 1.0f);
  84. cmd.SetGlobalVector(ShaderPropertyId.scaleBiasRt, scaleBias);
  85. context.ExecuteCommandBuffer(cmd);
  86. cmd.Clear();
  87. Camera camera = renderingData.cameraData.camera;
  88. var sortFlags = (m_IsOpaque) ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
  89. if (renderingData.cameraData.renderer.useDepthPriming && m_IsOpaque && (renderingData.cameraData.renderType == CameraRenderType.Base || renderingData.cameraData.clearDepth))
  90. sortFlags = SortingCriteria.SortingLayer | SortingCriteria.RenderQueue | SortingCriteria.OptimizeStateChanges | SortingCriteria.CanvasOrder;
  91. var filterSettings = m_FilteringSettings;
  92. #if UNITY_EDITOR
  93. // When rendering the preview camera, we want the layer mask to be forced to Everything
  94. if (renderingData.cameraData.isPreviewCamera)
  95. {
  96. filterSettings.layerMask = -1;
  97. }
  98. #endif
  99. DrawingSettings drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortFlags);
  100. var activeDebugHandler = GetActiveDebugHandler(renderingData);
  101. if (activeDebugHandler != null)
  102. {
  103. activeDebugHandler.DrawWithDebugRenderState(context, cmd, ref renderingData, ref drawSettings, ref filterSettings, ref m_RenderStateBlock,
  104. (ScriptableRenderContext ctx, ref RenderingData data, ref DrawingSettings ds, ref FilteringSettings fs, ref RenderStateBlock rsb) =>
  105. {
  106. ctx.DrawRenderers(data.cullResults, ref ds, ref fs, ref rsb);
  107. });
  108. }
  109. else
  110. {
  111. context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings, ref m_RenderStateBlock);
  112. // Render objects that did not match any shader pass with error shader
  113. RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None);
  114. }
  115. }
  116. context.ExecuteCommandBuffer(cmd);
  117. CommandBufferPool.Release(cmd);
  118. }
  119. }
  120. }