DrawObjectsPass.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. static readonly int s_DrawObjectPassDataPropID = Shader.PropertyToID("_DrawObjectPassData");
  21. public DrawObjectsPass(string profilerTag, ShaderTagId[] shaderTagIds, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  22. {
  23. base.profilingSampler = new ProfilingSampler(nameof(DrawObjectsPass));
  24. m_ProfilerTag = profilerTag;
  25. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  26. foreach (ShaderTagId sid in shaderTagIds)
  27. m_ShaderTagIdList.Add(sid);
  28. renderPassEvent = evt;
  29. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  30. m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
  31. m_IsOpaque = opaque;
  32. if (stencilState.enabled)
  33. {
  34. m_RenderStateBlock.stencilReference = stencilReference;
  35. m_RenderStateBlock.mask = RenderStateMask.Stencil;
  36. m_RenderStateBlock.stencilState = stencilState;
  37. }
  38. }
  39. public DrawObjectsPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  40. : this(profilerTag,
  41. new ShaderTagId[] { new ShaderTagId("SRPDefaultUnlit"), new ShaderTagId("UniversalForward"), new ShaderTagId("UniversalForwardOnly"), new ShaderTagId("LightweightForward")},
  42. opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
  43. {}
  44. internal DrawObjectsPass(URPProfileId profileId, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  45. : this(profileId.GetType().Name, opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
  46. {
  47. m_ProfilingSampler = ProfilingSampler.Get(profileId);
  48. }
  49. /// <inheritdoc/>
  50. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  51. {
  52. // NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
  53. // Currently there's an issue which results in mismatched markers.
  54. CommandBuffer cmd = CommandBufferPool.Get();
  55. using (new ProfilingScope(cmd, m_ProfilingSampler))
  56. {
  57. // Global render pass data containing various settings.
  58. // x,y,z are currently unused
  59. // w is used for knowing whether the object is opaque(1) or alpha blended(0)
  60. Vector4 drawObjectPassData = new Vector4(0.0f, 0.0f, 0.0f, (m_IsOpaque) ? 1.0f : 0.0f);
  61. cmd.SetGlobalVector(s_DrawObjectPassDataPropID, drawObjectPassData);
  62. // scaleBias.x = flipSign
  63. // scaleBias.y = scale
  64. // scaleBias.z = bias
  65. // scaleBias.w = unused
  66. float flipSign = (renderingData.cameraData.IsCameraProjectionMatrixFlipped()) ? -1.0f : 1.0f;
  67. Vector4 scaleBias = (flipSign < 0.0f)
  68. ? new Vector4(flipSign, 1.0f, -1.0f, 1.0f)
  69. : new Vector4(flipSign, 0.0f, 1.0f, 1.0f);
  70. cmd.SetGlobalVector(ShaderPropertyId.scaleBiasRt, scaleBias);
  71. context.ExecuteCommandBuffer(cmd);
  72. cmd.Clear();
  73. Camera camera = renderingData.cameraData.camera;
  74. var sortFlags = (m_IsOpaque) ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
  75. var drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortFlags);
  76. var filterSettings = m_FilteringSettings;
  77. #if UNITY_EDITOR
  78. // When rendering the preview camera, we want the layer mask to be forced to Everything
  79. if (renderingData.cameraData.isPreviewCamera)
  80. {
  81. filterSettings.layerMask = -1;
  82. }
  83. #endif
  84. context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings, ref m_RenderStateBlock);
  85. // Render objects that did not match any shader pass with error shader
  86. RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None);
  87. }
  88. context.ExecuteCommandBuffer(cmd);
  89. CommandBufferPool.Release(cmd);
  90. }
  91. }
  92. }