暫無描述
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.

Renderer2D.cs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. using UnityEngine.Rendering.Universal.Internal;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. internal sealed partial class Renderer2D : ScriptableRenderer
  7. {
  8. #if UNITY_SWITCH || UNITY_EMBEDDED_LINUX || UNITY_QNX
  9. const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D24_UNorm_S8_UInt;
  10. internal const int k_DepthBufferBits = 24;
  11. #else
  12. const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D32_SFloat_S8_UInt;
  13. internal const int k_DepthBufferBits = 32;
  14. #endif
  15. const int k_FinalBlitPassQueueOffset = 1;
  16. const int k_AfterFinalBlitPassQueueOffset = k_FinalBlitPassQueueOffset + 1;
  17. Render2DLightingPass m_Render2DLightingPass;
  18. PixelPerfectBackgroundPass m_PixelPerfectBackgroundPass;
  19. UpscalePass m_UpscalePass;
  20. CopyDepthPass m_CopyDepthPass;
  21. CopyCameraSortingLayerPass m_CopyCameraSortingLayerPass;
  22. FinalBlitPass m_FinalBlitPass;
  23. DrawScreenSpaceUIPass m_DrawOffscreenUIPass;
  24. DrawScreenSpaceUIPass m_DrawOverlayUIPass; // from HDRP code
  25. internal RenderTargetBufferSystem m_ColorBufferSystem;
  26. private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler("Create Camera Textures");
  27. bool m_UseDepthStencilBuffer = true;
  28. bool m_CreateColorTexture;
  29. bool m_CreateDepthTexture;
  30. // We probably should declare these names in the base class,
  31. // as they must be the same across all ScriptableRenderer types for camera stacking to work.
  32. internal RTHandle m_ColorTextureHandle;
  33. internal RTHandle m_DepthTextureHandle;
  34. #if UNITY_EDITOR
  35. internal RTHandle m_DefaultWhiteTextureHandle;
  36. #endif
  37. Material m_BlitMaterial;
  38. Material m_BlitHDRMaterial;
  39. Material m_SamplingMaterial;
  40. Renderer2DData m_Renderer2DData;
  41. internal bool createColorTexture => m_CreateColorTexture;
  42. internal bool createDepthTexture => m_CreateDepthTexture;
  43. PostProcessPasses m_PostProcessPasses;
  44. internal ColorGradingLutPass colorGradingLutPass { get => m_PostProcessPasses.colorGradingLutPass; }
  45. internal PostProcessPass postProcessPass { get => m_PostProcessPasses.postProcessPass; }
  46. internal PostProcessPass finalPostProcessPass { get => m_PostProcessPasses.finalPostProcessPass; }
  47. internal RTHandle afterPostProcessColorHandle { get => m_PostProcessPasses.afterPostProcessColor; }
  48. internal RTHandle colorGradingLutHandle { get => m_PostProcessPasses.colorGradingLut; }
  49. /// <inheritdoc/>
  50. public override int SupportedCameraStackingTypes()
  51. {
  52. return 1 << (int)CameraRenderType.Base | 1 << (int)CameraRenderType.Overlay;
  53. }
  54. public Renderer2D(Renderer2DData data) : base(data)
  55. {
  56. if (GraphicsSettings.TryGetRenderPipelineSettings<UniversalRenderPipelineRuntimeShaders>(
  57. out var shadersResources))
  58. {
  59. m_BlitMaterial = CoreUtils.CreateEngineMaterial(shadersResources.coreBlitPS);
  60. m_BlitHDRMaterial = CoreUtils.CreateEngineMaterial(shadersResources.blitHDROverlay);
  61. m_SamplingMaterial = CoreUtils.CreateEngineMaterial(shadersResources.samplingPS);
  62. }
  63. if (GraphicsSettings.TryGetRenderPipelineSettings<Renderer2DResources>(out var renderer2DResources))
  64. {
  65. m_Render2DLightingPass = new Render2DLightingPass(data, m_BlitMaterial, m_SamplingMaterial, renderer2DResources.fallOffLookup);
  66. m_CopyDepthPass = new CopyDepthPass(
  67. RenderPassEvent.AfterRenderingTransparents,
  68. renderer2DResources.copyDepthPS,
  69. shouldClear: true,
  70. copyResolvedDepth: RenderingUtils.MultisampleDepthResolveSupported());
  71. }
  72. // we should determine why clearing the camera target is set so late in the events... sounds like it could be earlier
  73. m_PixelPerfectBackgroundPass = new PixelPerfectBackgroundPass(RenderPassEvent.AfterRenderingTransparents);
  74. m_UpscalePass = new UpscalePass(RenderPassEvent.AfterRenderingPostProcessing, m_BlitMaterial);
  75. m_CopyCameraSortingLayerPass = new CopyCameraSortingLayerPass(m_BlitMaterial);
  76. m_FinalBlitPass = new FinalBlitPass(RenderPassEvent.AfterRendering + k_FinalBlitPassQueueOffset, m_BlitMaterial, m_BlitHDRMaterial);
  77. m_DrawOffscreenUIPass = new DrawScreenSpaceUIPass(RenderPassEvent.BeforeRenderingPostProcessing, true);
  78. m_DrawOverlayUIPass = new DrawScreenSpaceUIPass(RenderPassEvent.AfterRendering + k_AfterFinalBlitPassQueueOffset, false); // after m_FinalBlitPass
  79. // RenderTexture format depends on camera and pipeline (HDR, non HDR, etc)
  80. // Samples (MSAA) depend on camera and pipeline
  81. m_ColorBufferSystem = new RenderTargetBufferSystem("_CameraColorAttachment");
  82. var ppParams = PostProcessParams.Create();
  83. ppParams.blitMaterial = m_BlitMaterial;
  84. ppParams.requestColorFormat = GraphicsFormat.B10G11R11_UFloatPack32;
  85. m_PostProcessPasses = new PostProcessPasses(data.postProcessData, ref ppParams);
  86. m_UseDepthStencilBuffer = data.useDepthStencilBuffer;
  87. m_Renderer2DData = data;
  88. supportedRenderingFeatures = new RenderingFeatures();
  89. m_Renderer2DData.lightCullResult = new Light2DCullResult();
  90. LensFlareCommonSRP.mergeNeeded = 0;
  91. LensFlareCommonSRP.maxLensFlareWithOcclusionTemporalSample = 1;
  92. LensFlareCommonSRP.Initialize();
  93. }
  94. protected override void Dispose(bool disposing)
  95. {
  96. m_Renderer2DData.Dispose();
  97. m_Render2DLightingPass?.Dispose();
  98. m_PostProcessPasses.Dispose();
  99. m_ColorTextureHandle?.Release();
  100. m_DepthTextureHandle?.Release();
  101. ReleaseRenderTargets();
  102. m_UpscalePass.Dispose();
  103. m_CopyDepthPass?.Dispose();
  104. m_FinalBlitPass?.Dispose();
  105. m_DrawOffscreenUIPass?.Dispose();
  106. m_DrawOverlayUIPass?.Dispose();
  107. CoreUtils.Destroy(m_BlitMaterial);
  108. CoreUtils.Destroy(m_BlitHDRMaterial);
  109. CoreUtils.Destroy(m_SamplingMaterial);
  110. CleanupRenderGraphResources();
  111. base.Dispose(disposing);
  112. }
  113. internal override void ReleaseRenderTargets()
  114. {
  115. m_ColorBufferSystem.Dispose();
  116. m_PostProcessPasses.ReleaseRenderTargets();
  117. }
  118. public Renderer2DData GetRenderer2DData()
  119. {
  120. return m_Renderer2DData;
  121. }
  122. private struct RenderPassInputSummary
  123. {
  124. internal bool requiresDepthTexture;
  125. internal bool requiresColorTexture;
  126. }
  127. private RenderPassInputSummary GetRenderPassInputs(UniversalCameraData cameraData)
  128. {
  129. RenderPassInputSummary inputSummary = new RenderPassInputSummary();
  130. for (int i = 0; i < activeRenderPassQueue.Count; ++i)
  131. {
  132. ScriptableRenderPass pass = activeRenderPassQueue[i];
  133. bool needsDepth = (pass.input & ScriptableRenderPassInput.Depth) != ScriptableRenderPassInput.None;
  134. bool needsColor = (pass.input & ScriptableRenderPassInput.Color) != ScriptableRenderPassInput.None;
  135. inputSummary.requiresDepthTexture |= needsDepth;
  136. inputSummary.requiresColorTexture |= needsColor;
  137. }
  138. inputSummary.requiresColorTexture |= cameraData.postProcessEnabled
  139. || cameraData.isHdrEnabled
  140. || cameraData.isSceneViewCamera
  141. || !cameraData.isDefaultViewport
  142. || cameraData.requireSrgbConversion
  143. || !cameraData.resolveFinalTarget
  144. || m_Renderer2DData.useCameraSortingLayerTexture
  145. || !Mathf.Approximately(cameraData.renderScale, 1.0f)
  146. || (DebugHandler != null && DebugHandler.WriteToDebugScreenTexture(cameraData.resolveFinalTarget));
  147. inputSummary.requiresDepthTexture |= (!cameraData.resolveFinalTarget && m_UseDepthStencilBuffer);
  148. return inputSummary;
  149. }
  150. void CreateRenderTextures(
  151. ref RenderPassInputSummary renderPassInputs,
  152. CommandBuffer cmd,
  153. UniversalCameraData cameraData,
  154. bool forceCreateColorTexture,
  155. FilterMode colorTextureFilterMode,
  156. out RTHandle colorTargetHandle,
  157. out RTHandle depthTargetHandle)
  158. {
  159. ref var cameraTargetDescriptor = ref cameraData.cameraTargetDescriptor;
  160. var colorDescriptor = cameraTargetDescriptor;
  161. colorDescriptor.depthBufferBits = (int)DepthBits.None;
  162. m_ColorBufferSystem.SetCameraSettings(colorDescriptor, colorTextureFilterMode);
  163. if (cameraData.renderType == CameraRenderType.Base)
  164. {
  165. m_CreateColorTexture = renderPassInputs.requiresColorTexture;
  166. m_CreateDepthTexture = renderPassInputs.requiresDepthTexture;
  167. m_CreateColorTexture |= forceCreateColorTexture;
  168. // RTHandles do not support combining color and depth in the same texture so we create them separately
  169. m_CreateDepthTexture |= createColorTexture;
  170. if (createColorTexture)
  171. {
  172. if (m_ColorBufferSystem.PeekBackBuffer() == null || m_ColorBufferSystem.PeekBackBuffer().nameID != BuiltinRenderTextureType.CameraTarget)
  173. {
  174. m_ColorTextureHandle = m_ColorBufferSystem.GetBackBuffer(cmd);
  175. cmd.SetGlobalTexture("_CameraColorTexture", m_ColorTextureHandle.nameID);
  176. //Set _AfterPostProcessTexture, users might still rely on this although it is now always the cameratarget due to swapbuffer
  177. cmd.SetGlobalTexture("_AfterPostProcessTexture", m_ColorTextureHandle.nameID);
  178. }
  179. m_ColorTextureHandle = m_ColorBufferSystem.PeekBackBuffer();
  180. }
  181. if (createDepthTexture)
  182. {
  183. var depthDescriptor = cameraTargetDescriptor;
  184. depthDescriptor.colorFormat = RenderTextureFormat.Depth;
  185. depthDescriptor.depthBufferBits = k_DepthBufferBits;
  186. if (!cameraData.resolveFinalTarget && m_UseDepthStencilBuffer)
  187. depthDescriptor.bindMS = depthDescriptor.msaaSamples > 1 && !SystemInfo.supportsMultisampleAutoResolve && (SystemInfo.supportsMultisampledTextures != 0);
  188. RenderingUtils.ReAllocateHandleIfNeeded(ref m_DepthTextureHandle, depthDescriptor, FilterMode.Point, wrapMode: TextureWrapMode.Clamp, name: "_CameraDepthAttachment");
  189. }
  190. colorTargetHandle = createColorTexture ? m_ColorTextureHandle : k_CameraTarget;
  191. depthTargetHandle = createDepthTexture ? m_DepthTextureHandle : k_CameraTarget;
  192. }
  193. else // Overlay camera
  194. {
  195. cameraData.baseCamera.TryGetComponent<UniversalAdditionalCameraData>(out var baseCameraData);
  196. var baseRenderer = (Renderer2D)baseCameraData.scriptableRenderer;
  197. if (m_ColorBufferSystem != baseRenderer.m_ColorBufferSystem)
  198. {
  199. m_ColorBufferSystem.Dispose();
  200. m_ColorBufferSystem = baseRenderer.m_ColorBufferSystem;
  201. }
  202. // These render textures are created by the base camera, but it's the responsibility of the last overlay camera's ScriptableRenderer
  203. // to release the textures in its FinishRendering().
  204. m_CreateColorTexture = true;
  205. m_CreateDepthTexture = true;
  206. m_ColorTextureHandle = baseRenderer.m_ColorTextureHandle;
  207. m_DepthTextureHandle = baseRenderer.m_DepthTextureHandle;
  208. colorTargetHandle = m_ColorTextureHandle;
  209. depthTargetHandle = m_DepthTextureHandle;
  210. }
  211. }
  212. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  213. public override void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
  214. {
  215. UniversalRenderingData universalRenderingData = frameData.Get<UniversalRenderingData>();
  216. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  217. UniversalPostProcessingData postProcessingData = frameData.Get<UniversalPostProcessingData>();
  218. ref var cameraTargetDescriptor = ref cameraData.cameraTargetDescriptor;
  219. bool stackHasPostProcess = postProcessingData.isEnabled && m_PostProcessPasses.isCreated;
  220. bool hasPostProcess = cameraData.postProcessEnabled && m_PostProcessPasses.isCreated;
  221. bool lastCameraInStack = cameraData.resolveFinalTarget;
  222. var colorTextureFilterMode = FilterMode.Bilinear;
  223. PixelPerfectCamera ppc = null;
  224. bool ppcUsesOffscreenRT = false;
  225. bool ppcUpscaleRT = false;
  226. if (DebugHandler != null)
  227. {
  228. #if UNITY_EDITOR
  229. UnityEditorInternal.SpriteMaskUtility.EnableDebugMode(DebugHandler.DebugDisplaySettings.materialSettings.materialDebugMode == DebugMaterialMode.SpriteMask);
  230. #endif
  231. if (DebugHandler.AreAnySettingsActive)
  232. {
  233. stackHasPostProcess = stackHasPostProcess && DebugHandler.IsPostProcessingAllowed;
  234. hasPostProcess = hasPostProcess && DebugHandler.IsPostProcessingAllowed;
  235. }
  236. DebugHandler.Setup(universalRenderingData.commandBuffer, cameraData.isPreviewCamera);
  237. if (DebugHandler.IsActiveForCamera(cameraData.isPreviewCamera))
  238. {
  239. if (DebugHandler.WriteToDebugScreenTexture(cameraData.resolveFinalTarget))
  240. {
  241. RenderTextureDescriptor descriptor = cameraData.cameraTargetDescriptor;
  242. DebugHandler.ConfigureColorDescriptorForDebugScreen(ref descriptor, cameraData.pixelWidth, cameraData.pixelHeight);
  243. RenderingUtils.ReAllocateHandleIfNeeded(ref DebugHandler.DebugScreenColorHandle, descriptor, name: "_DebugScreenColor");
  244. RenderTextureDescriptor depthDesc = cameraData.cameraTargetDescriptor;
  245. DebugHandler.ConfigureDepthDescriptorForDebugScreen(ref depthDesc, k_DepthStencilFormat, cameraData.pixelWidth, cameraData.pixelHeight);
  246. RenderingUtils.ReAllocateHandleIfNeeded(ref DebugHandler.DebugScreenDepthHandle, depthDesc, name: "_DebugScreenDepth");
  247. }
  248. if (DebugHandler.HDRDebugViewIsActive(cameraData.resolveFinalTarget))
  249. {
  250. DebugHandler.hdrDebugViewPass.Setup(cameraData, DebugHandler.DebugDisplaySettings.lightingSettings.hdrDebugMode);
  251. EnqueuePass(DebugHandler.hdrDebugViewPass);
  252. }
  253. }
  254. }
  255. #if UNITY_EDITOR
  256. // The scene view camera cannot be uninitialized or skybox when using the 2D renderer.
  257. if (cameraData.cameraType == CameraType.SceneView)
  258. {
  259. cameraData.camera.clearFlags = CameraClearFlags.SolidColor;
  260. }
  261. #endif
  262. // Pixel Perfect Camera doesn't support camera stacking.
  263. if (cameraData.renderType == CameraRenderType.Base && lastCameraInStack)
  264. {
  265. cameraData.camera.TryGetComponent(out ppc);
  266. if (ppc != null && ppc.enabled)
  267. {
  268. if (ppc.offscreenRTSize != Vector2Int.zero)
  269. {
  270. ppcUsesOffscreenRT = true;
  271. // Pixel Perfect Camera may request a different RT size than camera VP size.
  272. // In that case we need to modify cameraTargetDescriptor here so that all the passes would use the same size.
  273. cameraTargetDescriptor.width = ppc.offscreenRTSize.x;
  274. cameraTargetDescriptor.height = ppc.offscreenRTSize.y;
  275. // If using FullScreenRenderPass with Pixel Perfect, we need to reallocate the size of the RT used
  276. var fullScreenRenderPass = activeRenderPassQueue.Find(x => x is FullScreenPassRendererFeature.FullScreenRenderPass) as FullScreenPassRendererFeature.FullScreenRenderPass;
  277. fullScreenRenderPass?.ReAllocate(cameraTargetDescriptor);
  278. }
  279. colorTextureFilterMode = FilterMode.Point;
  280. ppcUpscaleRT = ppc.gridSnapping == PixelPerfectCamera.GridSnapping.UpscaleRenderTexture || ppc.requiresUpscalePass;
  281. }
  282. }
  283. RenderPassInputSummary renderPassInputs = GetRenderPassInputs(cameraData);
  284. RTHandle colorTargetHandle;
  285. RTHandle depthTargetHandle;
  286. var cmd = universalRenderingData.commandBuffer;
  287. #if UNITY_EDITOR
  288. if(m_DefaultWhiteTextureHandle == null)
  289. m_DefaultWhiteTextureHandle = RTHandles.Alloc(Texture2D.whiteTexture, "_DefaultWhiteTex");
  290. cmd.SetGlobalTexture(m_DefaultWhiteTextureHandle.name, m_DefaultWhiteTextureHandle.nameID);
  291. #endif
  292. using (new ProfilingScope(cmd, m_ProfilingSampler))
  293. {
  294. CreateRenderTextures(ref renderPassInputs, cmd, cameraData, ppcUsesOffscreenRT, colorTextureFilterMode,
  295. out colorTargetHandle, out depthTargetHandle);
  296. }
  297. context.ExecuteCommandBuffer(cmd);
  298. cmd.Clear();
  299. ConfigureCameraTarget(colorTargetHandle, depthTargetHandle);
  300. if (hasPostProcess)
  301. {
  302. colorGradingLutPass.ConfigureDescriptor(in postProcessingData, out var desc, out var filterMode);
  303. RenderingUtils.ReAllocateHandleIfNeeded(ref m_PostProcessPasses.m_ColorGradingLut, desc, filterMode, TextureWrapMode.Clamp, name: "_InternalGradingLut");
  304. colorGradingLutPass.Setup(colorGradingLutHandle);
  305. EnqueuePass(colorGradingLutPass);
  306. }
  307. m_Render2DLightingPass.Setup(renderPassInputs.requiresDepthTexture || m_UseDepthStencilBuffer);
  308. // Disable obsolete warning for internal usage
  309. #pragma warning disable CS0618
  310. m_Render2DLightingPass.ConfigureTarget(colorTargetHandle, depthTargetHandle);
  311. #pragma warning restore CS0618
  312. EnqueuePass(m_Render2DLightingPass);
  313. bool shouldRenderUI = cameraData.rendersOverlayUI;
  314. bool outputToHDR = cameraData.isHDROutputActive;
  315. if (shouldRenderUI && outputToHDR)
  316. {
  317. m_DrawOffscreenUIPass.Setup(cameraData, k_DepthStencilFormat);
  318. EnqueuePass(m_DrawOffscreenUIPass);
  319. }
  320. // TODO: Investigate how to make FXAA work with HDR output.
  321. bool isFXAAEnabled = cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing && !outputToHDR;
  322. // When using Upscale Render Texture on a Pixel Perfect Camera, we want all post-processing effects done with a low-res RT,
  323. // and only upscale the low-res RT to fullscreen when blitting it to camera target. Also, final post processing pass is not run in this case,
  324. // so FXAA is not supported (you don't want to apply FXAA when everything is intentionally pixelated).
  325. bool requireFinalPostProcessPass =
  326. lastCameraInStack && !ppcUpscaleRT && stackHasPostProcess && isFXAAEnabled;
  327. bool hasPassesAfterPostProcessing = activeRenderPassQueue.Find(x => x.renderPassEvent == RenderPassEvent.AfterRenderingPostProcessing) != null;
  328. bool needsColorEncoding = DebugHandler == null || !DebugHandler.HDRDebugViewIsActive(cameraData.resolveFinalTarget);
  329. if (hasPostProcess)
  330. {
  331. var desc = PostProcessPass.GetCompatibleDescriptor(cameraTargetDescriptor, cameraTargetDescriptor.width, cameraTargetDescriptor.height, cameraTargetDescriptor.graphicsFormat, DepthBits.None);
  332. RenderingUtils.ReAllocateHandleIfNeeded(ref m_PostProcessPasses.m_AfterPostProcessColor, desc, FilterMode.Point, TextureWrapMode.Clamp, name: "_AfterPostProcessTexture");
  333. postProcessPass.Setup(
  334. cameraTargetDescriptor,
  335. colorTargetHandle,
  336. afterPostProcessColorHandle,
  337. depthTargetHandle,
  338. colorGradingLutHandle,
  339. requireFinalPostProcessPass,
  340. afterPostProcessColorHandle.nameID == k_CameraTarget.nameID && needsColorEncoding);
  341. EnqueuePass(postProcessPass);
  342. }
  343. RTHandle finalTargetHandle = colorTargetHandle;
  344. if (ppc != null && ppc.enabled && ppc.cropFrame != PixelPerfectCamera.CropFrame.None)
  345. {
  346. EnqueuePass(m_PixelPerfectBackgroundPass);
  347. // Queue PixelPerfect UpscalePass. Only used when using the Stretch Fill option
  348. if (ppc.requiresUpscalePass)
  349. {
  350. int upscaleWidth = ppc.refResolutionX * ppc.pixelRatio;
  351. int upscaleHeight = ppc.refResolutionY * ppc.pixelRatio;
  352. m_UpscalePass.Setup(colorTargetHandle, upscaleWidth, upscaleHeight, ppc.finalBlitFilterMode, cameraData.cameraTargetDescriptor, out finalTargetHandle);
  353. EnqueuePass(m_UpscalePass);
  354. }
  355. }
  356. if (requireFinalPostProcessPass)
  357. {
  358. finalPostProcessPass.SetupFinalPass(finalTargetHandle, hasPassesAfterPostProcessing, needsColorEncoding);
  359. EnqueuePass(finalPostProcessPass);
  360. }
  361. else if (lastCameraInStack && finalTargetHandle != k_CameraTarget)
  362. {
  363. m_FinalBlitPass.Setup(cameraTargetDescriptor, finalTargetHandle);
  364. EnqueuePass(m_FinalBlitPass);
  365. }
  366. // We can explicitely render the overlay UI from URP when HDR output is not enabled.
  367. // SupportedRenderingFeatures.active.rendersUIOverlay should also be set to true.
  368. if (shouldRenderUI && !outputToHDR)
  369. {
  370. EnqueuePass(m_DrawOverlayUIPass);
  371. }
  372. }
  373. public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters, ref CameraData cameraData)
  374. {
  375. cullingParameters.cullingOptions = CullingOptions.None;
  376. cullingParameters.isOrthographic = cameraData.camera.orthographic;
  377. cullingParameters.shadowDistance = 0.0f;
  378. var cullResult = m_Renderer2DData.lightCullResult as Light2DCullResult;
  379. cullResult.SetupCulling(ref cullingParameters, cameraData.camera);
  380. }
  381. internal override void SwapColorBuffer(CommandBuffer cmd)
  382. {
  383. m_ColorBufferSystem.Swap();
  384. // Disable obsolete warning for internal usage
  385. #pragma warning disable CS0618
  386. //Check if we are using the depth that is attached to color buffer
  387. if (m_DepthTextureHandle.nameID != BuiltinRenderTextureType.CameraTarget)
  388. ConfigureCameraTarget(m_ColorBufferSystem.GetBackBuffer(cmd), m_DepthTextureHandle);
  389. else
  390. ConfigureCameraColorTarget(m_ColorBufferSystem.GetBackBuffer(cmd));
  391. #pragma warning restore CS0618
  392. m_ColorTextureHandle = m_ColorBufferSystem.GetBackBuffer(cmd);
  393. cmd.SetGlobalTexture("_CameraColorTexture", m_ColorTextureHandle.nameID);
  394. //Set _AfterPostProcessTexture, users might still rely on this although it is now always the cameratarget due to swapbuffer
  395. cmd.SetGlobalTexture("_AfterPostProcessTexture", m_ColorTextureHandle.nameID);
  396. }
  397. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  398. internal override RTHandle GetCameraColorFrontBuffer(CommandBuffer cmd)
  399. {
  400. return m_ColorBufferSystem.GetFrontBuffer(cmd);
  401. }
  402. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  403. internal override RTHandle GetCameraColorBackBuffer(CommandBuffer cmd)
  404. {
  405. return m_ColorBufferSystem.GetBackBuffer(cmd);
  406. }
  407. internal override void EnableSwapBufferMSAA(bool enable)
  408. {
  409. m_ColorBufferSystem.EnableMSAA(enable);
  410. }
  411. internal static bool IsGLDevice()
  412. {
  413. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3 || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLCore;
  414. }
  415. internal static bool supportsMRT
  416. {
  417. get => !IsGLDevice();
  418. }
  419. internal override bool supportsNativeRenderPassRendergraphCompiler
  420. {
  421. get => !IsGLDevice(); // GLES and doesn't support MSAA resolve with the NRP API
  422. }
  423. }
  424. }