Renderer2D.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using UnityEngine.Rendering;
  2. using UnityEngine.Rendering.Universal;
  3. using UnityEngine.Rendering.Universal.Internal;
  4. namespace UnityEngine.Experimental.Rendering.Universal
  5. {
  6. internal class Renderer2D : ScriptableRenderer
  7. {
  8. ColorGradingLutPass m_ColorGradingLutPass;
  9. Render2DLightingPass m_Render2DLightingPass;
  10. PostProcessPass m_PostProcessPass;
  11. FinalBlitPass m_FinalBlitPass;
  12. PostProcessPass m_FinalPostProcessPass;
  13. #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
  14. PostProcessPassCompat m_PostProcessPassCompat;
  15. #endif
  16. bool m_UseDepthStencilBuffer = true;
  17. RenderTargetHandle m_ColorTargetHandle;
  18. RenderTargetHandle m_AfterPostProcessColorHandle;
  19. RenderTargetHandle m_ColorGradingLutHandle;
  20. Material m_BlitMaterial;
  21. Renderer2DData m_Renderer2DData;
  22. public Renderer2D(Renderer2DData data) : base(data)
  23. {
  24. m_BlitMaterial = CoreUtils.CreateEngineMaterial(data.blitShader);
  25. m_ColorGradingLutPass = new ColorGradingLutPass(RenderPassEvent.BeforeRenderingOpaques, data.postProcessData);
  26. m_Render2DLightingPass = new Render2DLightingPass(data);
  27. m_PostProcessPass = new PostProcessPass(RenderPassEvent.BeforeRenderingPostProcessing, data.postProcessData, m_BlitMaterial);
  28. m_FinalPostProcessPass = new PostProcessPass(RenderPassEvent.AfterRenderingPostProcessing, data.postProcessData, m_BlitMaterial);
  29. m_FinalBlitPass = new FinalBlitPass(RenderPassEvent.AfterRendering, m_BlitMaterial);
  30. #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
  31. m_PostProcessPassCompat = new PostProcessPassCompat(RenderPassEvent.BeforeRenderingPostProcessing);
  32. #endif
  33. m_UseDepthStencilBuffer = data.useDepthStencilBuffer;
  34. m_AfterPostProcessColorHandle.Init("_AfterPostProcessTexture");
  35. m_ColorGradingLutHandle.Init("_InternalGradingLut");
  36. m_Renderer2DData = data;
  37. }
  38. protected override void Dispose(bool disposing)
  39. {
  40. CoreUtils.Destroy(m_BlitMaterial);
  41. }
  42. public Renderer2DData GetRenderer2DData()
  43. {
  44. return m_Renderer2DData;
  45. }
  46. public override void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
  47. {
  48. ref CameraData cameraData = ref renderingData.cameraData;
  49. ref var cameraTargetDescriptor = ref cameraData.cameraTargetDescriptor;
  50. PixelPerfectCamera ppc;
  51. cameraData.camera.TryGetComponent<PixelPerfectCamera>(out ppc);
  52. Vector2Int ppcOffscreenRTSize = ppc != null ? ppc.offscreenRTSize : Vector2Int.zero;
  53. bool ppcUsesOffscreenRT = ppcOffscreenRTSize != Vector2Int.zero;
  54. var postProcessFeatureSet = UniversalRenderPipeline.asset.postProcessingFeatureSet;
  55. bool postProcessEnabled = renderingData.cameraData.postProcessEnabled;
  56. bool useOffscreenColorTexture =
  57. ppcUsesOffscreenRT || postProcessEnabled || cameraData.isHdrEnabled || cameraData.isSceneViewCamera || !cameraData.isDefaultViewport || !m_UseDepthStencilBuffer;
  58. // Pixel Perfect Camera may request a different RT size than camera VP size.
  59. // In that case we need to modify cameraTargetDescriptor here so that all the passes would use the same size.
  60. if (ppcUsesOffscreenRT)
  61. {
  62. cameraTargetDescriptor.width = ppcOffscreenRTSize.x;
  63. cameraTargetDescriptor.height = ppcOffscreenRTSize.y;
  64. }
  65. if (useOffscreenColorTexture)
  66. {
  67. var filterMode = ppc != null ? ppc.finalBlitFilterMode : FilterMode.Bilinear;
  68. m_ColorTargetHandle = CreateOffscreenColorTexture(context, ref cameraTargetDescriptor, filterMode);
  69. }
  70. else
  71. m_ColorTargetHandle = RenderTargetHandle.CameraTarget;
  72. ConfigureCameraTarget(m_ColorTargetHandle.Identifier(), BuiltinRenderTextureType.CameraTarget);
  73. m_Render2DLightingPass.ConfigureTarget(m_ColorTargetHandle.Identifier());
  74. EnqueuePass(m_Render2DLightingPass);
  75. bool requireFinalBlitPass = useOffscreenColorTexture;
  76. var finalBlitSourceHandle = m_ColorTargetHandle;
  77. if (postProcessEnabled && postProcessFeatureSet == PostProcessingFeatureSet.Integrated)
  78. {
  79. m_ColorGradingLutPass.Setup(m_ColorGradingLutHandle);
  80. EnqueuePass(m_ColorGradingLutPass);
  81. // When using Upscale Render Texture on a Pixel Perfect Camera, we want all post-processing effects done with a low-res RT,
  82. // and only upscale the low-res RT to fullscreen when blitting it to camera target.
  83. if (ppc != null && ppc.upscaleRT && ppc.isRunning)
  84. {
  85. m_PostProcessPass.Setup(
  86. cameraTargetDescriptor,
  87. m_ColorTargetHandle,
  88. m_AfterPostProcessColorHandle,
  89. RenderTargetHandle.CameraTarget,
  90. m_ColorGradingLutHandle,
  91. false,
  92. false
  93. );
  94. EnqueuePass(m_PostProcessPass);
  95. requireFinalBlitPass = true;
  96. finalBlitSourceHandle = m_AfterPostProcessColorHandle;
  97. }
  98. else if (renderingData.cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing)
  99. {
  100. m_PostProcessPass.Setup(
  101. cameraTargetDescriptor,
  102. m_ColorTargetHandle,
  103. m_AfterPostProcessColorHandle,
  104. RenderTargetHandle.CameraTarget,
  105. m_ColorGradingLutHandle,
  106. true,
  107. false
  108. );
  109. EnqueuePass(m_PostProcessPass);
  110. m_FinalPostProcessPass.SetupFinalPass(m_AfterPostProcessColorHandle);
  111. EnqueuePass(m_FinalPostProcessPass);
  112. requireFinalBlitPass = false;
  113. }
  114. else
  115. {
  116. m_PostProcessPass.Setup(
  117. cameraTargetDescriptor,
  118. m_ColorTargetHandle,
  119. RenderTargetHandle.CameraTarget,
  120. RenderTargetHandle.CameraTarget,
  121. m_ColorGradingLutHandle,
  122. false,
  123. true
  124. );
  125. EnqueuePass(m_PostProcessPass);
  126. requireFinalBlitPass = false;
  127. }
  128. }
  129. #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
  130. else if (postProcessEnabled && postProcessFeatureSet == PostProcessingFeatureSet.PostProcessingV2)
  131. {
  132. m_PostProcessPassCompat.Setup(cameraData.cameraTargetDescriptor, m_ColorTargetHandle, m_ColorTargetHandle);
  133. EnqueuePass(m_PostProcessPassCompat);
  134. }
  135. #endif
  136. if (requireFinalBlitPass)
  137. {
  138. m_FinalBlitPass.Setup(cameraTargetDescriptor, finalBlitSourceHandle);
  139. EnqueuePass(m_FinalBlitPass);
  140. }
  141. }
  142. public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters, ref CameraData cameraData)
  143. {
  144. cullingParameters.cullingOptions = CullingOptions.None;
  145. cullingParameters.isOrthographic = cameraData.camera.orthographic;
  146. cullingParameters.shadowDistance = 0.0f;
  147. }
  148. RenderTargetHandle CreateOffscreenColorTexture(ScriptableRenderContext context, ref RenderTextureDescriptor cameraTargetDescriptor, FilterMode filterMode)
  149. {
  150. RenderTargetHandle colorTextureHandle = new RenderTargetHandle();
  151. colorTextureHandle.Init("_CameraColorTexture");
  152. var colorDescriptor = cameraTargetDescriptor;
  153. colorDescriptor.depthBufferBits = m_UseDepthStencilBuffer ? 32 : 0;
  154. CommandBuffer cmd = CommandBufferPool.Get("Create Camera Textures");
  155. cmd.GetTemporaryRT(colorTextureHandle.id, colorDescriptor, filterMode);
  156. context.ExecuteCommandBuffer(cmd);
  157. CommandBufferPool.Release(cmd);
  158. return colorTextureHandle;
  159. }
  160. public override void FinishRendering(CommandBuffer cmd)
  161. {
  162. if (m_ColorTargetHandle != RenderTargetHandle.CameraTarget)
  163. cmd.ReleaseTemporaryRT(m_ColorTargetHandle.id);
  164. }
  165. }
  166. }