2026-10-21 20:45:23 +01:00
using UnityEngine.Experimental.Rendering ;
using UnityEngine.Rendering.RenderGraphModule ;
using UnityEngine.Rendering.Universal.Internal ;
using static UnityEngine . Rendering . Universal . UniversalResourceDataBase ;
using CommonResourceData = UnityEngine . Rendering . Universal . UniversalResourceData ;
namespace UnityEngine.Rendering.Universal
{
internal sealed partial class Renderer2D : ScriptableRenderer
{
private struct RenderPassInputSummary
{
internal bool requiresDepthTexture ;
internal bool requiresColorTexture ;
}
private struct ImportResourceSummary
{
internal RenderTargetInfo importInfo ;
internal RenderTargetInfo importInfoDepth ;
internal ImportResourceParams cameraColorParams ;
internal ImportResourceParams cameraDepthParams ;
internal ImportResourceParams backBufferColorParams ;
internal ImportResourceParams backBufferDepthParams ;
}
const int k_FinalBlitPassQueueOffset = 1 ;
const int k_AfterFinalBlitPassQueueOffset = k_FinalBlitPassQueueOffset + 1 ;
// TODO RENDERGRAPH: Once all cameras will run in a single RenderGraph we should remove all RTHandles and use per frame RG textures.
// We use 2 camera color handles so we can handle the edge case when a pass might want to read and write the same target.
// This is not allowed so we just swap the current target, this keeps camera stacking working and avoids an extra blit pass.
2026-11-30 19:13:17 +00:00
static int s_CurrentColorHandle = 0 ;
static readonly RTHandle [] s_RenderGraphCameraColorHandles = { null , null };
internal static RTHandle s_RenderGraphCameraDepthHandle ;
static RTHandle s_OffscreenUIColorHandle ;
2026-10-21 20:45:23 +01:00
RTHandle m_RenderGraphBackbufferColorHandle ;
RTHandle m_RenderGraphBackbufferDepthHandle ;
RTHandle m_CameraSortingLayerHandle ;
Material m_BlitMaterial ;
Material m_BlitHDRMaterial ;
Material m_BlitOffscreenUICoverMaterial ;
Material m_SamplingMaterial ;
// 2D specific render passes
DrawNormal2DPass m_NormalPass = new DrawNormal2DPass ();
DrawLight2DPass m_LightPass = new DrawLight2DPass ();
DrawShadow2DPass m_ShadowPass = new DrawShadow2DPass ();
DrawRenderer2DPass m_RendererPass = new DrawRenderer2DPass ();
CopyDepthPass m_CopyDepthPass ;
UpscalePass m_UpscalePass ;
CopyCameraSortingLayerPass m_CopyCameraSortingLayerPass ;
2026-11-30 19:13:17 +00:00
CapturePass m_CapturePass ;
2026-10-21 20:45:23 +01:00
FinalBlitPass m_FinalBlitPass ;
FinalBlitPass m_OffscreenUICoverPrepass ;
DrawScreenSpaceUIPass m_DrawOffscreenUIPass ;
DrawScreenSpaceUIPass m_DrawOverlayUIPass ; // from HDRP code
Renderer2DData m_Renderer2DData ;
LayerBatch [] m_LayerBatches ;
int m_BatchCount ;
internal bool m_CreateColorTexture ;
internal bool m_CreateDepthTexture ;
bool ppcUpscaleRT = false ;
2026-11-30 19:13:17 +00:00
static internal bool s_IsLightingActive ;
2026-10-21 20:45:23 +01:00
PostProcessPassRenderGraph m_PostProcessPassRenderGraph ;
ColorGradingLutPass m_ColorGradingLutPassRenderGraph ;
private RTHandle currentRenderGraphCameraColorHandle
{
get
{
2026-11-30 19:13:17 +00:00
return s_RenderGraphCameraColorHandles [ s_CurrentColorHandle ];
2026-10-21 20:45:23 +01:00
}
}
2026-11-30 19:13:17 +00:00
// get the next s_RenderGraphCameraColorHandles and make it the new current for future accesses
2026-10-21 20:45:23 +01:00
private RTHandle nextRenderGraphCameraColorHandle
{
get
{
2026-11-30 19:13:17 +00:00
s_CurrentColorHandle = ( s_CurrentColorHandle + 1 ) % 2 ;
2026-10-21 20:45:23 +01:00
return currentRenderGraphCameraColorHandle ;
}
}
/// <inheritdoc/>
public override int SupportedCameraStackingTypes ()
{
return 1 << ( int ) CameraRenderType . Base | 1 << ( int ) CameraRenderType . Overlay ;
}
public Renderer2D ( Renderer2DData data ) : base ( data )
{
if ( GraphicsSettings . TryGetRenderPipelineSettings < UniversalRenderPipelineRuntimeShaders >( out var shadersResources ))
{
m_BlitMaterial = CoreUtils . CreateEngineMaterial ( shadersResources . coreBlitPS );
m_BlitHDRMaterial = CoreUtils . CreateEngineMaterial ( shadersResources . blitHDROverlay );
m_BlitOffscreenUICoverMaterial = CoreUtils . CreateEngineMaterial ( shadersResources . blitHDROverlay );
m_SamplingMaterial = CoreUtils . CreateEngineMaterial ( shadersResources . samplingPS );
}
if ( GraphicsSettings . TryGetRenderPipelineSettings < Renderer2DResources >( out var renderer2DResources ))
{
m_CopyDepthPass = new CopyDepthPass (
RenderPassEvent . AfterRenderingTransparents ,
renderer2DResources . copyDepthPS ,
shouldClear : true ,
copyResolvedDepth : RenderingUtils . MultisampleDepthResolveSupported ());
}
m_UpscalePass = new UpscalePass ( RenderPassEvent . AfterRenderingPostProcessing , m_BlitMaterial );
m_CopyCameraSortingLayerPass = new CopyCameraSortingLayerPass ( m_BlitMaterial );
2026-11-30 19:13:17 +00:00
m_CapturePass = new CapturePass ( RenderPassEvent . AfterRendering );
2026-10-21 20:45:23 +01:00
m_FinalBlitPass = new FinalBlitPass ( RenderPassEvent . AfterRendering + k_FinalBlitPassQueueOffset , m_BlitMaterial , m_BlitHDRMaterial );
m_OffscreenUICoverPrepass = new FinalBlitPass ( RenderPassEvent . BeforeRenderingPostProcessing , m_BlitMaterial , m_BlitOffscreenUICoverMaterial );
m_DrawOffscreenUIPass = new DrawScreenSpaceUIPass ( RenderPassEvent . BeforeRenderingPostProcessing , true );
m_DrawOverlayUIPass = new DrawScreenSpaceUIPass ( RenderPassEvent . AfterRendering + k_AfterFinalBlitPassQueueOffset , false ); // after m_FinalBlitPass
m_Renderer2DData = data ;
m_Renderer2DData . lightCullResult = new Light2DCullResult ();
LensFlareCommonSRP . mergeNeeded = 0 ;
LensFlareCommonSRP . maxLensFlareWithOcclusionTemporalSample = 1 ;
LensFlareCommonSRP . Initialize ();
Light2DManager . Initialize ();
if ( data . postProcessData != null )
{
m_PostProcessPassRenderGraph = new PostProcessPassRenderGraph ( data . postProcessData , GraphicsFormat . B10G11R11_UFloatPack32 );
m_ColorGradingLutPassRenderGraph = new ColorGradingLutPass ( RenderPassEvent . BeforeRenderingPrePasses , data . postProcessData );
}
PlatformAutoDetect . Initialize ();
#if ENABLE_VR && ENABLE_XR_MODULE
if ( GraphicsSettings . TryGetRenderPipelineSettings < UniversalRenderPipelineRuntimeXRResources >( out var xrResources ))
{
XRSystem . Initialize ( XRPassUniversal . Create , xrResources . xrOcclusionMeshPS , xrResources . xrMirrorViewPS );
}
#endif
#if URP_COMPATIBILITY_MODE
InitializeCompatibilityMode ( data );
#endif
}
internal static bool IsDepthUsageAllowed ( ContextContainer frameData , Renderer2DData rendererData )
{
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
2026-11-30 19:13:17 +00:00
// Renderer2DData.useDepthStencilBuffer
2026-10-21 20:45:23 +01:00
bool allowDepth = rendererData . useDepthStencilBuffer ;
// 3D Render Textures with depth/stencil buffer are not supported
if ( cameraData . targetTexture != null )
allowDepth &= cameraData . targetTexture . dimension != TextureDimension . Tex3D ;
2026-11-30 19:13:17 +00:00
// The back buffer has none when PlayerSettings.disableDepthAndStencilBuffers is enabled on a graphics API that acts on it
allowDepth &= Graphics . backBufferHasDepthStencil ;
2026-10-21 20:45:23 +01:00
return allowDepth ;
}
2026-11-30 19:13:17 +00:00
static bool ShouldImportBackBufferDepth ( ContextContainer frameData , Renderer2DData rendererData , bool createColorTexture )
{
if ( IsDepthUsageAllowed ( frameData , rendererData ))
return true ;
// OpenGL binds the default framebuffer's depth with its color even when no pass uses it, so it must still be cleared (UUM-159093)
var deviceType = SystemInfo . graphicsDeviceType ;
if ( deviceType != GraphicsDeviceType . OpenGLES3 && deviceType != GraphicsDeviceType . OpenGLCore )
return false ;
// Only when the 2D passes draw straight into the back buffer, since they are what attach and clear it
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
bool isBackBuffer = cameraData . targetTexture == null && ! createColorTexture ;
#if ENABLE_VR && ENABLE_XR_MODULE
isBackBuffer &= ! cameraData . xr . enabled ;
#endif
return isBackBuffer && Graphics . backBufferHasDepthStencil ;
}
2026-10-21 20:45:23 +01:00
private bool IsPixelPerfectCameraEnabled ( UniversalCameraData cameraData , out PixelPerfectCamera ppc )
{
ppc = null ;
// Pixel Perfect Camera doesn't support camera stacking.
if ( cameraData . renderType == CameraRenderType . Base && cameraData . resolveFinalTarget )
cameraData . camera . TryGetComponent ( out ppc );
return ppc != null && ppc . enabled ;
}
private RenderPassInputSummary GetRenderPassInputs ( UniversalCameraData cameraData )
{
RenderPassInputSummary inputSummary = new RenderPassInputSummary ();
for ( int i = 0 ; i < activeRenderPassQueue . Count ; ++ i )
{
ScriptableRenderPass pass = activeRenderPassQueue [ i ];
bool needsDepth = ( pass . input & ScriptableRenderPassInput . Depth ) != ScriptableRenderPassInput . None ;
bool needsColor = ( pass . input & ScriptableRenderPassInput . Color ) != ScriptableRenderPassInput . None ;
inputSummary . requiresDepthTexture |= needsDepth ;
inputSummary . requiresColorTexture |= needsColor ;
2026-11-30 19:13:17 +00:00
inputSummary . requiresColorTexture |= pass . requiresIntermediateTexture ;
2026-10-21 20:45:23 +01:00
}
2026-11-30 19:13:17 +00:00
// Volumetric shadow pass requires intermediate texture
bool hasVolumetricShadowPass = false ;
for ( int i = 0 ; i < m_BatchCount ; ++ i )
hasVolumetricShadowPass |= m_LayerBatches [ i ]. lightStats . useVolumetricShadowLights ;
2026-10-21 20:45:23 +01:00
inputSummary . requiresColorTexture |= cameraData . postProcessEnabled
|| cameraData . isHdrEnabled
|| cameraData . isSceneViewCamera
|| ! cameraData . isDefaultViewport
|| cameraData . requireSrgbConversion
|| ! cameraData . resolveFinalTarget
|| cameraData . cameraTargetDescriptor . msaaSamples > 1 && UniversalRenderer . PlatformRequiresExplicitMsaaResolve ()
|| m_Renderer2DData . useCameraSortingLayerTexture
|| ! Mathf . Approximately ( cameraData . renderScale , 1.0f )
2026-11-30 19:13:17 +00:00
|| ( DebugHandler != null && DebugHandler . WriteToDebugScreenTexture ( cameraData . resolveFinalTarget ))
|| cameraData . captureActions != null
|| hasVolumetricShadowPass ;
2026-10-21 20:45:23 +01:00
return inputSummary ;
}
ImportResourceSummary GetImportResourceSummary ( RenderGraph renderGraph , UniversalCameraData cameraData )
{
ImportResourceSummary output = new ImportResourceSummary ();
bool clearColor = cameraData . renderType == CameraRenderType . Base ;
bool clearDepth = cameraData . renderType == CameraRenderType . Base || cameraData . clearDepth ;
// Clear back buffer color if pixel perfect crop frame is used
// Non-base cameras the back buffer should never be cleared
bool ppcEnabled = IsPixelPerfectCameraEnabled ( cameraData , out var ppc ) && ppc . cropFrame != PixelPerfectCamera . CropFrame . None ;
bool clearColorBackbufferOnFirstUse = ( cameraData . renderType == CameraRenderType . Base ) && (! m_CreateColorTexture || ppcEnabled );
bool clearDepthBackbufferOnFirstUse = ( cameraData . renderType == CameraRenderType . Base ) && ! m_CreateColorTexture ;
// if the camera background type is "uninitialized" clear using a yellow color, so users can clearly understand the underlying behaviour
Color cameraBackgroundColor = ( cameraData . camera . clearFlags == CameraClearFlags . Nothing ) ? Color . yellow : cameraData . backgroundColor ;
Color backBufferBackgroundColor = ppcEnabled ? Color . black : cameraBackgroundColor ;
if ( IsSceneFilteringEnabled ( cameraData . camera ))
{
cameraBackgroundColor . a = 0 ;
clearDepth = false ;
}
// Certain debug modes (e.g. wireframe/overdraw modes) require that we override clear flags and clear everything.
var debugHandler = cameraData . renderer . DebugHandler ;
if ( debugHandler != null && debugHandler . IsActiveForCamera ( cameraData . isPreviewCamera ) && debugHandler . IsScreenClearNeeded )
{
clearColor = true ;
clearDepth = true ;
debugHandler . TryGetScreenClearColor ( ref cameraBackgroundColor );
}
output . cameraColorParams . clearOnFirstUse = clearColor ;
output . cameraColorParams . clearColor = cameraBackgroundColor ;
output . cameraColorParams . discardOnLastUse = false ;
output . cameraDepthParams . clearOnFirstUse = clearDepth ;
output . cameraDepthParams . clearColor = cameraBackgroundColor ;
output . cameraDepthParams . discardOnLastUse = false ;
output . backBufferColorParams . clearOnFirstUse = clearColorBackbufferOnFirstUse ;
output . backBufferColorParams . clearColor = backBufferBackgroundColor ;
output . backBufferColorParams . discardOnLastUse = false ;
output . backBufferDepthParams . clearOnFirstUse = clearDepthBackbufferOnFirstUse ;
output . backBufferDepthParams . clearColor = backBufferBackgroundColor ;
output . backBufferDepthParams . discardOnLastUse = true ;
bool isBuiltInTexture = cameraData . targetTexture == null ;
bool useActualBackbufferOrienation = ! cameraData . isSceneViewCamera && ! cameraData . isPreviewCamera && cameraData . targetTexture == null ;
TextureUVOrigin backbufferTextureUVOrigin = useActualBackbufferOrienation ? ( SystemInfo . graphicsUVStartsAtTop ? TextureUVOrigin . TopLeft : TextureUVOrigin . BottomLeft ) : TextureUVOrigin . BottomLeft ;
output . backBufferColorParams . textureUVOrigin = backbufferTextureUVOrigin ;
output . backBufferDepthParams . textureUVOrigin = backbufferTextureUVOrigin ;
#if ENABLE_VR && ENABLE_XR_MODULE
if ( cameraData . xr . enabled )
{
isBuiltInTexture = false ;
}
#endif
if (! isBuiltInTexture )
{
#if ENABLE_VR && ENABLE_XR_MODULE
if ( cameraData . xr . enabled )
{
output . importInfo . width = cameraData . xr . renderTargetDesc . width ;
output . importInfo . height = cameraData . xr . renderTargetDesc . height ;
output . importInfo . volumeDepth = cameraData . xr . renderTargetDesc . volumeDepth ;
output . importInfo . msaaSamples = cameraData . xr . renderTargetDesc . msaaSamples ;
output . importInfo . format = cameraData . xr . renderTargetDesc . graphicsFormat ;
if (! UniversalRenderer . PlatformRequiresExplicitMsaaResolve ())
output . importInfo . bindMS = output . importInfo . msaaSamples > 1 ;
output . importInfoDepth = output . importInfo ;
output . importInfoDepth . format = cameraData . xr . renderTargetDesc . depthStencilFormat ;
}
else
#endif
{
output . importInfo . width = cameraData . targetTexture . width ;
output . importInfo . height = cameraData . targetTexture . height ;
output . importInfo . volumeDepth = cameraData . targetTexture . volumeDepth ;
output . importInfo . msaaSamples = cameraData . targetTexture . antiAliasing ;
output . importInfo . format = cameraData . targetTexture . graphicsFormat ;
output . importInfoDepth = output . importInfo ;
output . importInfoDepth . format = cameraData . targetTexture . depthStencilFormat ;
// We let users know that a depth format is required for correct usage, but we fallback to the old default depth format behaviour to avoid regressions
if ( output . importInfoDepth . format == GraphicsFormat . None )
{
output . importInfoDepth . format = SystemInfo . GetGraphicsFormat ( DefaultFormat . DepthStencil );
Debug . LogWarning ( "In the render graph API, the output Render Texture must have a depth buffer. When you select a Render Texture in any camera's Output Texture property, the Depth Stencil Format property of the texture must be set to a value other than None." );
}
}
}
else
{
// Backbuffer is the final render target, we obtain its number of MSAA samples through Screen API
// in some cases we disable multisampling for optimization purpose
int numSamples = AdjustAndGetScreenMSAASamples ( renderGraph , m_CreateColorTexture );
2026-11-30 19:13:17 +00:00
output . importInfo . width = cameraData . backbufferWidth ;
output . importInfo . height = cameraData . backbufferHeight ;
2026-10-21 20:45:23 +01:00
output . importInfo . volumeDepth = 1 ;
output . importInfo . msaaSamples = numSamples ;
output . importInfo . format = cameraData . cameraTargetDescriptor . graphicsFormat ;
output . importInfoDepth = output . importInfo ;
output . importInfoDepth . format = SystemInfo . GetGraphicsFormat ( DefaultFormat . DepthStencil );
}
return output ;
}
public override void SetupCullingParameters ( ref ScriptableCullingParameters cullingParameters , ref CameraData cameraData )
{
cullingParameters . cullingOptions = CullingOptions . None ;
cullingParameters . isOrthographic = cameraData . camera . orthographic ;
cullingParameters . shadowDistance = 0.0f ;
var cullResult = m_Renderer2DData . lightCullResult as Light2DCullResult ;
cullResult . SetupCulling ( ref cullingParameters , cameraData . camera );
}
void InitializeLayerBatches ()
{
Universal2DResourceData resourceData = frameData . Get < Universal2DResourceData >();
2026-11-30 19:13:17 +00:00
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
2026-10-21 20:45:23 +01:00
m_LayerBatches = LayerUtility . CalculateBatches ( m_Renderer2DData , out m_BatchCount );
2026-11-30 19:13:17 +00:00
s_IsLightingActive = IsSceneViewOrPreviewLightingActive ( cameraData );
2026-10-21 20:45:23 +01:00
// Initialize textures dependent on batch size
if ( resourceData . normalsTexture . Length != m_BatchCount )
resourceData . normalsTexture = new TextureHandle [ m_BatchCount ];
if ( resourceData . shadowTextures . Length != m_BatchCount )
resourceData . shadowTextures = new TextureHandle [ m_BatchCount ][];
if ( resourceData . lightTextures . Length != m_BatchCount )
resourceData . lightTextures = new TextureHandle [ m_BatchCount ][];
// Initialize light textures based on active blend styles to save on resources
for ( int i = 0 ; i < resourceData . lightTextures . Length ; ++ i )
{
if ( resourceData . lightTextures [ i ] == null || resourceData . lightTextures [ i ]. Length != m_LayerBatches [ i ]. activeBlendStylesIndices . Length )
resourceData . lightTextures [ i ] = new TextureHandle [ m_LayerBatches [ i ]. activeBlendStylesIndices . Length ];
}
for ( int i = 0 ; i < resourceData . shadowTextures . Length ; ++ i )
{
if ( resourceData . shadowTextures [ i ] == null || resourceData . shadowTextures [ i ]. Length != m_LayerBatches [ i ]. shadowIndices . Count )
resourceData . shadowTextures [ i ] = new TextureHandle [ m_LayerBatches [ i ]. shadowIndices . Count ];
}
}
void CreateResources ( RenderGraph renderGraph )
{
Universal2DResourceData universal2DResourceData = frameData . Get < Universal2DResourceData >();
CommonResourceData commonResourceData = frameData . Get < CommonResourceData >();
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
ref var cameraTargetDescriptor = ref cameraData . cameraTargetDescriptor ;
var cameraTargetFilterMode = FilterMode . Bilinear ;
bool lastCameraInTheStack = cameraData . resolveFinalTarget ;
#if UNITY_EDITOR
// The scene view camera cannot be uninitialized or skybox when using the 2D renderer.
if ( cameraData . cameraType == CameraType . SceneView )
{
cameraData . camera . clearFlags = CameraClearFlags . SolidColor ;
}
#endif
bool forceCreateColorTexture = false ;
// Pixel Perfect Camera doesn't support camera stacking.
if ( cameraData . renderType == CameraRenderType . Base && lastCameraInTheStack )
{
cameraData . camera . TryGetComponent < PixelPerfectCamera >( out var ppc );
if ( ppc != null && ppc . enabled )
{
if ( ppc . offscreenRTSize != Vector2Int . zero )
{
forceCreateColorTexture = true ;
// Pixel Perfect Camera may request a different RT size than camera VP size.
// In that case we need to modify cameraTargetDescriptor here so that all the passes would use the same size.
cameraTargetDescriptor . width = ppc . offscreenRTSize . x ;
cameraTargetDescriptor . height = ppc . offscreenRTSize . y ;
}
cameraTargetFilterMode = FilterMode . Point ;
ppcUpscaleRT = ppc . gridSnapping == PixelPerfectCamera . GridSnapping . UpscaleRenderTexture || ppc . requiresUpscalePass ;
if ( ppc . requiresUpscalePass )
{
var upscaleDescriptor = cameraTargetDescriptor ;
upscaleDescriptor . width = ppc . refResolutionX * ppc . pixelRatio ;
upscaleDescriptor . height = ppc . refResolutionY * ppc . pixelRatio ;
upscaleDescriptor . depthStencilFormat = GraphicsFormat . None ;
universal2DResourceData . upscaleTexture = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , upscaleDescriptor , "_UpscaleTexture" , true , ppc . finalBlitFilterMode );
}
}
}
2026-11-30 19:13:17 +00:00
// Account for Quality->Render Scale and Light Texture Render Scale
2026-10-21 20:45:23 +01:00
var renderTextureScale = m_Renderer2DData . lightRenderTextureScale ;
2026-11-30 19:13:17 +00:00
var width = ( int ) Mathf . Max ( 1 , cameraTargetDescriptor . width * renderTextureScale );
var height = ( int ) Mathf . Max ( 1 , cameraTargetDescriptor . height * renderTextureScale );
2026-10-21 20:45:23 +01:00
// Normals and Light textures have to be of the same renderTextureScale, to prevent any sampling artifacts during lighting calculations
CreateCameraNormalsTextures ( renderGraph , cameraTargetDescriptor , width , height );
CreateLightTextures ( renderGraph , width , height );
CreateShadowTextures ( renderGraph , width , height );
if ( m_Renderer2DData . useCameraSortingLayerTexture )
CreateCameraSortingLayerTexture ( renderGraph , cameraTargetDescriptor );
// Create the attachments
if ( cameraData . renderType == CameraRenderType . Base ) // require intermediate textures
{
RenderPassInputSummary renderPassInputs = GetRenderPassInputs ( cameraData );
m_CreateColorTexture = renderPassInputs . requiresColorTexture ;
m_CreateDepthTexture = renderPassInputs . requiresDepthTexture ;
m_CreateColorTexture |= forceCreateColorTexture ;
// RTHandles do not support combining color and depth in the same texture so we create them separately
m_CreateDepthTexture |= m_CreateColorTexture ;
2026-11-30 19:13:17 +00:00
m_CreateDepthTexture &= IsDepthUsageAllowed ( frameData , m_Renderer2DData );
2026-10-21 20:45:23 +01:00
// Camera Target Color
if ( m_CreateColorTexture )
{
cameraTargetDescriptor . useMipMap = false ;
cameraTargetDescriptor . autoGenerateMips = false ;
cameraTargetDescriptor . depthStencilFormat = GraphicsFormat . None ;
2026-11-30 19:13:17 +00:00
RenderingUtils . ReAllocateHandleIfNeeded ( ref s_RenderGraphCameraColorHandles [ 0 ], cameraTargetDescriptor , cameraTargetFilterMode , TextureWrapMode . Clamp , name : "_CameraTargetAttachmentA" );
RenderingUtils . ReAllocateHandleIfNeeded ( ref s_RenderGraphCameraColorHandles [ 1 ], cameraTargetDescriptor , cameraTargetFilterMode , TextureWrapMode . Clamp , name : "_CameraTargetAttachmentB" );
2026-10-21 20:45:23 +01:00
commonResourceData . activeColorID = ActiveID . Camera ;
}
else
commonResourceData . activeColorID = ActiveID . BackBuffer ;
// Camera Target Depth
if ( m_CreateDepthTexture )
{
var depthDescriptor = cameraData . cameraTargetDescriptor ;
depthDescriptor . useMipMap = false ;
depthDescriptor . autoGenerateMips = false ;
bool hasMSAA = depthDescriptor . msaaSamples > 1 && ( SystemInfo . supportsMultisampledTextures != 0 );
bool resolveDepth = RenderingUtils . MultisampleDepthResolveSupported () && renderGraph . nativeRenderPassesEnabled ;
depthDescriptor . bindMS = ! resolveDepth && hasMSAA ;
// binding MS surfaces is not supported by the GLES backend
if ( IsGLESDevice ())
depthDescriptor . bindMS = false ;
if ( m_CopyDepthPass != null )
{
m_CopyDepthPass . MsaaSamples = depthDescriptor . msaaSamples ;
m_CopyDepthPass . m_CopyResolvedDepth = ! depthDescriptor . bindMS ;
}
depthDescriptor . graphicsFormat = GraphicsFormat . None ;
depthDescriptor . depthStencilFormat = CoreUtils . GetDefaultDepthStencilFormat ();
2026-11-30 19:13:17 +00:00
RenderingUtils . ReAllocateHandleIfNeeded ( ref s_RenderGraphCameraDepthHandle , depthDescriptor , FilterMode . Point , TextureWrapMode . Clamp , name : "_CameraDepthAttachment" );
2026-10-21 20:45:23 +01:00
}
}
else // Overlay camera
{
cameraData . baseCamera . TryGetComponent < UniversalAdditionalCameraData >( out var baseCameraData );
var baseRenderer = ( Renderer2D ) baseCameraData . scriptableRenderer ;
m_RenderGraphBackbufferColorHandle = baseRenderer . m_RenderGraphBackbufferColorHandle ;
m_RenderGraphBackbufferDepthHandle = baseRenderer . m_RenderGraphBackbufferDepthHandle ;
m_CreateColorTexture = baseRenderer . m_CreateColorTexture ;
m_CreateDepthTexture = baseRenderer . m_CreateDepthTexture ;
commonResourceData . activeColorID = m_CreateColorTexture ? ActiveID . Camera : ActiveID . BackBuffer ;
}
2026-11-30 19:13:17 +00:00
// An intermediate color target is never paired with the back buffer depth
commonResourceData . activeDepthID = m_CreateDepthTexture || m_CreateColorTexture ? ActiveID . Camera : ActiveID . BackBuffer ;
2026-10-21 20:45:23 +01:00
ImportResourceSummary importSummary = GetImportResourceSummary ( renderGraph , cameraData );
if ( m_CreateColorTexture )
{
importSummary . cameraColorParams . discardOnLastUse = lastCameraInTheStack ;
commonResourceData . cameraColor = renderGraph . ImportTexture ( currentRenderGraphCameraColorHandle , importSummary . cameraColorParams );
2026-11-30 19:13:17 +00:00
}
if ( m_CreateDepthTexture )
{
importSummary . cameraDepthParams . discardOnLastUse = lastCameraInTheStack ;
commonResourceData . cameraDepth = renderGraph . ImportTexture ( s_RenderGraphCameraDepthHandle , importSummary . cameraDepthParams );
2026-10-21 20:45:23 +01:00
}
RenderTargetIdentifier targetColorId = cameraData . targetTexture != null ? new RenderTargetIdentifier ( cameraData . targetTexture ) : BuiltinRenderTextureType . CameraTarget ;
RenderTargetIdentifier targetDepthId = cameraData . targetTexture != null ? new RenderTargetIdentifier ( cameraData . targetTexture ) : BuiltinRenderTextureType . Depth ;
#if ENABLE_VR && ENABLE_XR_MODULE
if ( cameraData . xr . enabled )
{
targetColorId = cameraData . xr . renderTarget ;
targetDepthId = cameraData . xr . renderTarget ;
}
#endif
if ( m_RenderGraphBackbufferColorHandle == null )
{
m_RenderGraphBackbufferColorHandle = RTHandles . Alloc ( targetColorId , "Backbuffer color" );
}
else if ( m_RenderGraphBackbufferColorHandle . nameID != targetColorId )
{
RTHandleStaticHelpers . SetRTHandleUserManagedWrapper ( ref m_RenderGraphBackbufferColorHandle , targetColorId );
}
commonResourceData . backBufferColor = renderGraph . ImportTexture ( m_RenderGraphBackbufferColorHandle , importSummary . importInfo , importSummary . backBufferColorParams );
2026-11-30 19:13:17 +00:00
if ( ShouldImportBackBufferDepth ( frameData , m_Renderer2DData , m_CreateColorTexture ))
{
if ( m_RenderGraphBackbufferDepthHandle == null )
{
m_RenderGraphBackbufferDepthHandle = RTHandles . Alloc ( targetDepthId , "Backbuffer depth" );
}
else if ( m_RenderGraphBackbufferDepthHandle . nameID != targetDepthId )
{
RTHandleStaticHelpers . SetRTHandleUserManagedWrapper ( ref m_RenderGraphBackbufferDepthHandle , targetDepthId );
}
commonResourceData . backBufferDepth = renderGraph . ImportTexture ( m_RenderGraphBackbufferDepthHandle , importSummary . importInfoDepth , importSummary . backBufferDepthParams );
}
2026-10-21 20:45:23 +01:00
var postProcessDesc = PostProcessPassRenderGraph . GetCompatibleDescriptor ( cameraTargetDescriptor , cameraTargetDescriptor . width , cameraTargetDescriptor . height , cameraTargetDescriptor . graphicsFormat );
commonResourceData . afterPostProcessColor = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , postProcessDesc , "_AfterPostProcessTexture" , true );
if ( RequiresDepthCopyPass ( cameraData ))
CreateCameraDepthCopyTexture ( renderGraph , cameraTargetDescriptor );
if ( cameraData . isHDROutputActive && cameraData . rendersOverlayUI )
CreateOffscreenUITexture ( renderGraph , cameraTargetDescriptor );
}
void CreateCameraNormalsTextures ( RenderGraph renderGraph , RenderTextureDescriptor descriptor , int width , int height )
{
Universal2DResourceData resourceData = frameData . Get < Universal2DResourceData >();
var desc = new RenderTextureDescriptor ( width , height );
desc . graphicsFormat = RendererLighting . GetRenderTextureFormat ();
desc . autoGenerateMips = false ;
desc . msaaSamples = descriptor . msaaSamples ;
2026-11-30 19:13:17 +00:00
bool useNormals = false ;
2026-10-21 20:45:23 +01:00
for ( int i = 0 ; i < resourceData . normalsTexture . Length ; ++ i )
2026-11-30 19:13:17 +00:00
{
useNormals |= m_LayerBatches [ i ]. useNormals ;
2026-10-21 20:45:23 +01:00
resourceData . normalsTexture [ i ] = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , desc , "_NormalMap" , true , RendererLighting . k_NormalClearColor );
2026-11-30 19:13:17 +00:00
}
2026-10-21 20:45:23 +01:00
2026-11-30 19:13:17 +00:00
if ( useNormals && IsDepthUsageAllowed ( frameData , m_Renderer2DData ))
2026-10-21 20:45:23 +01:00
{
// Normals pass can reuse active depth if same dimensions, if not create a new depth texture
#if !( ENABLE_VR && ENABLE_XR_MODULE )
if ( descriptor . width != width || descriptor . height != height )
#endif
{
var normalsDepthDesc = new RenderTextureDescriptor ( width , height );
normalsDepthDesc . graphicsFormat = GraphicsFormat . None ;
normalsDepthDesc . autoGenerateMips = false ;
normalsDepthDesc . msaaSamples = descriptor . msaaSamples ;
normalsDepthDesc . depthStencilFormat = CoreUtils . GetDefaultDepthStencilFormat ();
resourceData . normalsDepth = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , normalsDepthDesc , "_NormalDepth" , false , FilterMode . Bilinear );
}
}
}
void CreateLightTextures ( RenderGraph renderGraph , int width , int height )
{
Universal2DResourceData resourceData = frameData . Get < Universal2DResourceData >();
var desc = new RenderTextureDescriptor ( width , height );
desc . graphicsFormat = RendererLighting . GetRenderTextureFormat ();
desc . autoGenerateMips = false ;
for ( int i = 0 ; i < resourceData . lightTextures . Length ; ++ i )
{
for ( var j = 0 ; j < m_LayerBatches [ i ]. activeBlendStylesIndices . Length ; ++ j )
{
var index = m_LayerBatches [ i ]. activeBlendStylesIndices [ j ];
if (! Light2DManager . GetGlobalColor ( m_LayerBatches [ i ]. startLayerID , index , out var clearColor ))
clearColor = Color . black ;
resourceData . lightTextures [ i ][ j ] = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , desc , RendererLighting . k_ShapeLightTextureIDs [ index ], true , clearColor , FilterMode . Bilinear );
}
}
}
void CreateShadowTextures ( RenderGraph renderGraph , int width , int height )
{
Universal2DResourceData resourceData = frameData . Get < Universal2DResourceData >();
var shadowDesc = new RenderTextureDescriptor ( width , height );
shadowDesc . graphicsFormat = GraphicsFormat . B10G11R11_UFloatPack32 ;
shadowDesc . autoGenerateMips = false ;
for ( int i = 0 ; i < resourceData . shadowTextures . Length ; ++ i )
{
for ( var j = 0 ; j < m_LayerBatches [ i ]. shadowIndices . Count ; ++ j )
{
resourceData . shadowTextures [ i ][ j ] = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , shadowDesc , "_ShadowTex" , false , FilterMode . Bilinear );
}
}
var shadowDepthDesc = new RenderTextureDescriptor ( width , height );
shadowDepthDesc . graphicsFormat = GraphicsFormat . None ;
shadowDepthDesc . autoGenerateMips = false ;
shadowDepthDesc . depthStencilFormat = CoreUtils . GetDefaultDepthStencilFormat ();
resourceData . shadowDepth = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , shadowDepthDesc , "_ShadowDepth" , false , FilterMode . Bilinear );
}
2026-11-30 19:13:17 +00:00
void CreateCameraSortingLayerTexture ( RenderGraph renderGraph , RenderTextureDescriptor cameraDesc )
2026-10-21 20:45:23 +01:00
{
Universal2DResourceData resourceData = frameData . Get < Universal2DResourceData >();
2026-11-30 19:13:17 +00:00
CopyCameraSortingLayerPass . ConfigureDescriptor ( m_Renderer2DData . cameraSortingLayerDownsamplingMethod , ref cameraDesc , out var filterMode );
RenderingUtils . ReAllocateHandleIfNeeded ( ref m_CameraSortingLayerHandle , cameraDesc , filterMode , TextureWrapMode . Clamp , name : CopyCameraSortingLayerPass . k_CameraSortingLayerTexture );
2026-10-21 20:45:23 +01:00
resourceData . cameraSortingLayerTexture = renderGraph . ImportTexture ( m_CameraSortingLayerHandle );
}
bool RequiresDepthCopyPass ( UniversalCameraData cameraData )
{
var renderPassInputs = GetRenderPassInputs ( cameraData );
bool requiresDepthTexture = cameraData . requiresDepthTexture || renderPassInputs . requiresDepthTexture ;
2026-11-30 19:13:17 +00:00
bool cameraHasPostProcessingWithDepth = cameraData . postProcessEnabled && m_PostProcessPassRenderGraph != null && ( cameraData . postProcessingRequiresDepthTexture || ! LensFlareCommonSRP . Instance . IsEmpty ());
2026-10-21 20:45:23 +01:00
bool requiresDepthCopyPass = ( cameraHasPostProcessingWithDepth || requiresDepthTexture ) && m_CreateDepthTexture ;
return requiresDepthCopyPass ;
}
void CreateCameraDepthCopyTexture ( RenderGraph renderGraph , RenderTextureDescriptor descriptor )
{
CommonResourceData resourceData = frameData . Get < CommonResourceData >();
var depthDescriptor = descriptor ;
depthDescriptor . msaaSamples = 1 ; // Depth-Only pass don't use MSAA
depthDescriptor . graphicsFormat = GraphicsFormat . R32_SFloat ;
depthDescriptor . depthStencilFormat = GraphicsFormat . None ;
resourceData . cameraDepthTexture = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , depthDescriptor , "_CameraDepthTexture" , true );
}
void CreateOffscreenUITexture ( RenderGraph renderGraph , in RenderTextureDescriptor descriptor )
{
UniversalResourceData resourceData = frameData . Get < CommonResourceData >();
2026-11-30 19:13:17 +00:00
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
2026-10-21 20:45:23 +01:00
TextureDesc textureDesc = new TextureDesc ( descriptor );
2026-11-30 19:13:17 +00:00
DrawScreenSpaceUIPass . ConfigureOffscreenUITextureDesc ( ref textureDesc , cameraData );
RenderingUtils . ReAllocateHandleIfNeeded ( ref s_OffscreenUIColorHandle , textureDesc , name : "_OverlayUITexture" );
resourceData . overlayUITexture = renderGraph . ImportTexture ( s_OffscreenUIColorHandle );
2026-10-21 20:45:23 +01:00
}
public override void OnBeginRenderGraphFrame ()
{
Universal2DResourceData universal2DResourceData = frameData . Create < Universal2DResourceData >();
CommonResourceData commonResourceData = frameData . GetOrCreate < CommonResourceData >();
universal2DResourceData . InitFrame ();
commonResourceData . InitFrame ();
}
internal void RecordCustomRenderGraphPasses ( RenderGraph renderGraph , RenderPassEvent2D activeRPEvent )
{
foreach ( ScriptableRenderPass pass in activeRenderPassQueue )
{
pass . GetInjectionPoint2D ( out RenderPassEvent2D rpEvent , out int rpLayer );
if ( rpEvent == activeRPEvent )
pass . RecordRenderGraph ( renderGraph , frameData );
}
}
internal override void OnRecordRenderGraph ( RenderGraph renderGraph , ScriptableRenderContext context )
{
CommonResourceData commonResourceData = frameData . GetOrCreate < CommonResourceData >();
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
InitializeLayerBatches ();
CreateResources ( renderGraph );
DebugHandler ?. Setup ( renderGraph , cameraData . isPreviewCamera );
SetupRenderGraphCameraProperties ( renderGraph , commonResourceData . activeColorTexture );
#if VISUAL_EFFECT_GRAPH_0_0_1_OR_NEWER
ProcessVFXCameraCommand ( renderGraph );
#endif
OnBeforeRendering ( renderGraph );
RecordCustomRenderGraphPasses ( renderGraph , RenderPassEvent2D . BeforeRendering );
BeginRenderGraphXRRendering ( renderGraph );
OnMainRendering ( renderGraph );
RecordCustomRenderGraphPasses ( renderGraph , RenderPassEvent2D . BeforeRenderingPostProcessing );
OnAfterRendering ( renderGraph );
EndRenderGraphXRRendering ( renderGraph );
}
public override void OnEndRenderGraphFrame ()
{
Universal2DResourceData universal2DResourceData = frameData . Get < Universal2DResourceData >();
CommonResourceData commonResourceData = frameData . Get < CommonResourceData >();
universal2DResourceData . EndFrame ();
commonResourceData . EndFrame ();
}
internal override void OnFinishRenderGraphRendering ( CommandBuffer cmd )
{
m_CopyDepthPass ?. OnCameraCleanup ( cmd );
}
private void OnBeforeRendering ( RenderGraph renderGraph )
{
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
m_LightPass . Setup ( renderGraph , ref m_Renderer2DData );
// Before rendering the lights cache some values that are expensive to get/calculate
var culledLights = m_Renderer2DData . lightCullResult . visibleLights ;
for ( var i = 0 ; i < culledLights . Count ; i ++)
{
culledLights [ i ]. CacheValues ();
}
ShadowCasterGroup2DManager . CacheValues ();
ShadowRendering . CallOnBeforeRender ( cameraData . camera , m_Renderer2DData . lightCullResult );
RendererLighting . lightBatch . Reset ();
}
private void OnMainRendering ( RenderGraph renderGraph )
{
Universal2DResourceData universal2DResourceData = frameData . Get < Universal2DResourceData >();
CommonResourceData commonResourceData = frameData . Get < CommonResourceData >();
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
// Color Grading LUT
bool requiredColorGradingLutPass = cameraData . postProcessEnabled && m_PostProcessPassRenderGraph != null ;
if ( requiredColorGradingLutPass )
{
TextureHandle internalColorLut ;
m_ColorGradingLutPassRenderGraph . Render ( renderGraph , frameData , out internalColorLut );
commonResourceData . internalColorLut = internalColorLut ;
}
var cameraSortingLayerBoundsIndex = m_Renderer2DData . GetCameraSortingLayerBoundsIndex ();
bool useLights = false ;
for ( int i = 0 ; i < m_BatchCount ; ++ i )
useLights |= m_LayerBatches [ i ]. lightStats . useLights ;
// Set Global Properties and Textures
GlobalPropertiesPass . Setup ( renderGraph , frameData , m_Renderer2DData , cameraData , useLights );
// Main render passes
// Normal Pass
for ( var i = 0 ; i < m_BatchCount ; i ++)
m_NormalPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches [ i ], i );
// Shadow Pass (TODO: Optimize RT swapping between shadow and light textures)
for ( var i = 0 ; i < m_BatchCount ; i ++)
m_ShadowPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches [ i ], i );
// Light Pass
for ( var i = 0 ; i < m_BatchCount ; i ++)
m_LightPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches [ i ], i );
// Default Render Pass
for ( var i = 0 ; i < m_BatchCount ; i ++)
{
if (! renderGraph . nativeRenderPassesEnabled && i == 0 )
{
RTClearFlags clearFlags = ( RTClearFlags ) GetCameraClearFlag ( cameraData );
if ( clearFlags != RTClearFlags . None )
ClearTargetsPass . Render ( renderGraph , commonResourceData . activeColorTexture , commonResourceData . activeDepthTexture , clearFlags , cameraData . backgroundColor );
}
ref var layerBatch = ref m_LayerBatches [ i ];
LayerUtility . GetFilterSettings ( m_Renderer2DData , ref m_LayerBatches [ i ], out var filterSettings );
m_RendererPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches , i , ref filterSettings );
// Shadow Volumetric Pass
m_ShadowPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches [ i ], i , true );
// Light Volumetric Pass
m_LightPass . Render ( renderGraph , frameData , m_Renderer2DData , ref m_LayerBatches [ i ], i , true );
// Camera Sorting Layer Pass
if ( m_Renderer2DData . useCameraSortingLayerTexture )
{
if ( cameraSortingLayerBoundsIndex >= layerBatch . layerRange . lowerBound && cameraSortingLayerBoundsIndex <= layerBatch . layerRange . upperBound )
{
m_CopyCameraSortingLayerPass . Render ( renderGraph , frameData );
}
}
}
if ( RequiresDepthCopyPass ( cameraData ))
m_CopyDepthPass ?. Render ( renderGraph , frameData , commonResourceData . cameraDepthTexture , commonResourceData . activeDepthTexture , true );
bool shouldRenderUI = cameraData . rendersOverlayUI ;
bool outputToHDR = cameraData . isHDROutputActive ;
if ( shouldRenderUI && outputToHDR )
{
if ( cameraData . rendersOffscreenUI )
{
m_DrawOffscreenUIPass . RenderOffscreen ( renderGraph , frameData , CoreUtils . GetDefaultDepthStencilFormat (), commonResourceData . overlayUITexture );
if ( cameraData . blitsOffscreenUICover )
{
var blackTextureDesc = new RenderTextureDescriptor ( 1 , 1 , GraphicsFormat . R8G8B8A8_SRGB , 0 );
var source = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , blackTextureDesc , "BlackTexture" , false );
m_OffscreenUICoverPrepass . Render ( renderGraph , frameData , cameraData , source , commonResourceData . backBufferColor , commonResourceData . overlayUITexture , true );
}
}
else
{
// When the first camera renders the shared offscreen UI texture, register it as a global texture so subsequent cameras can use it in their final passes.
RenderGraphUtils . SetGlobalTexture ( renderGraph , ShaderPropertyId . overlayUITexture , commonResourceData . overlayUITexture );
}
}
}
private void OnAfterRendering ( RenderGraph renderGraph )
{
Universal2DResourceData universal2DResourceData = frameData . Get < Universal2DResourceData >();
CommonResourceData commonResourceData = frameData . Get < CommonResourceData >();
UniversalRenderingData renderingData = frameData . Get < UniversalRenderingData >();
UniversalCameraData cameraData = frameData . Get < UniversalCameraData >();
UniversalPostProcessingData postProcessingData = frameData . Get < UniversalPostProcessingData >();
bool drawGizmos = UniversalRenderPipelineDebugDisplaySettings . Instance . renderingSettings . sceneOverrideMode == DebugSceneOverrideMode . None ;
if ( drawGizmos )
DrawRenderGraphGizmos ( renderGraph , frameData , commonResourceData . activeColorTexture , commonResourceData . activeDepthTexture , GizmoSubset . PreImageEffects );
DebugHandler debugHandler = ScriptableRenderPass . GetActiveDebugHandler ( cameraData );
bool resolveToDebugScreen = debugHandler != null && debugHandler . WriteToDebugScreenTexture ( cameraData . resolveFinalTarget );
// Allocate debug screen texture if the debug mode needs it.
if ( resolveToDebugScreen )
{
RenderTextureDescriptor colorDesc = cameraData . cameraTargetDescriptor ;
DebugHandler . ConfigureColorDescriptorForDebugScreen ( ref colorDesc , cameraData . pixelWidth , cameraData . pixelHeight );
commonResourceData . debugScreenColor = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , colorDesc , "_DebugScreenColor" , false );
RenderTextureDescriptor depthDesc = cameraData . cameraTargetDescriptor ;
DebugHandler . ConfigureDepthDescriptorForDebugScreen ( ref depthDesc , CoreUtils . GetDefaultDepthStencilFormat (), cameraData . pixelWidth , cameraData . pixelHeight );
commonResourceData . debugScreenDepth = UniversalRenderer . CreateRenderGraphTexture ( renderGraph , depthDesc , "_DebugScreenDepth" , false );
}
bool applyPostProcessing = cameraData . postProcessEnabled && m_PostProcessPassRenderGraph != null ;
bool anyPostProcessing = postProcessingData . isEnabled && m_PostProcessPassRenderGraph != null ;
bool requirePixelPerfectUpscale = IsPixelPerfectCameraEnabled ( cameraData , out var ppc ) && ppc . requiresUpscalePass ;
// When using Upscale Render Texture on a Pixel Perfect Camera, we want all post-processing effects done with a low-res RT,
// 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,
// so FXAA is not supported (you don't want to apply FXAA when everything is intentionally pixelated).
2026-11-30 19:13:17 +00:00
bool applyFinalPostProcessing = cameraData . resolveFinalTarget && ! ppcUpscaleRT && anyPostProcessing &&
(( cameraData . antialiasing == AntialiasingMode . FastApproximateAntialiasing ) ||
( cameraData . imageScalingMode == ImageScalingMode . Upscaling && cameraData . upscalingFilter == ImageUpscalingFilter . FSR ));
2026-10-21 20:45:23 +01:00
bool hasPassesAfterPostProcessing = activeRenderPassQueue . Find ( x => x . renderPassEvent == RenderPassEvent . AfterRenderingPostProcessing ) != null ;
bool needsColorEncoding = DebugHandler == null || ! DebugHandler . HDRDebugViewIsActive ( cameraData . resolveFinalTarget );
// Don't resolve during post processing if there are passes after or pixel perfect camera is used
bool pixelPerfectCameraEnabled = ppc != null && ppc . enabled ;
bool hasCaptureActions = cameraData . captureActions != null && cameraData . resolveFinalTarget ;
bool resolvePostProcessingToCameraTarget = cameraData . resolveFinalTarget && ! hasCaptureActions && ! hasPassesAfterPostProcessing && ! applyFinalPostProcessing && ! pixelPerfectCameraEnabled ;
bool doSRGBEncoding = resolvePostProcessingToCameraTarget && needsColorEncoding ;
if ( applyPostProcessing )
{
TextureHandle activeColor = commonResourceData . activeColorTexture ;
bool isTargetBackbuffer = resolvePostProcessingToCameraTarget ;
2026-11-30 19:13:17 +00:00
if ( isTargetBackbuffer )
2026-10-21 20:45:23 +01:00
{
2026-11-30 19:13:17 +00:00
commonResourceData . activeColorID = ActiveID . BackBuffer ;
commonResourceData . activeDepthID = ActiveID . BackBuffer ;
}
else
{
// if the postprocessing pass is trying to read and write to the same CameraColor target, we need to swap so it writes to a different target,
// since reading a pass attachment is not possible. Normally this would be possible using temporary RenderGraph managed textures.
// The reason why in this case we need to use "external" RTHandles is to preserve the results for camera stacking.
// TODO RENDERGRAPH: Once all cameras will run in a single RenderGraph we can just use temporary RenderGraph textures as intermediate buffer.
2026-10-21 20:45:23 +01:00
ImportResourceParams importColorParams = new ImportResourceParams ();
importColorParams . clearOnFirstUse = true ;
importColorParams . clearColor = Color . black ;
importColorParams . discardOnLastUse = cameraData . resolveFinalTarget ; // check if last camera in the stack
commonResourceData . cameraColor = renderGraph . ImportTexture ( nextRenderGraphCameraColorHandle , importColorParams );
}
// Desired target for post-processing pass.
var target = isTargetBackbuffer ? commonResourceData . backBufferColor : commonResourceData . cameraColor ;
if ( resolveToDebugScreen && isTargetBackbuffer )
target = commonResourceData . debugScreenColor ;
m_PostProcessPassRenderGraph . RenderPostProcessingRenderGraph (
renderGraph ,
frameData ,
activeColor ,
commonResourceData . internalColorLut ,
commonResourceData . overlayUITexture ,
target ,
applyFinalPostProcessing ,
resolveToDebugScreen ,
doSRGBEncoding );
}
RecordCustomRenderGraphPasses ( renderGraph , RenderPassEvent2D . AfterRenderingPostProcessing );
var finalColorHandle = commonResourceData . activeColorTexture ;
// Do PixelPerfect upscaling when using the Stretch Fill option
if ( requirePixelPerfectUpscale )
{
m_UpscalePass . Render ( renderGraph , cameraData . camera , in finalColorHandle , universal2DResourceData . upscaleTexture );
finalColorHandle = universal2DResourceData . upscaleTexture ;
}
// We need to switch the "final" blit target to debugScreenColor if HDR debug views are enabled.
var finalBlitTarget = resolveToDebugScreen ? commonResourceData . debugScreenColor : commonResourceData . backBufferColor ;
var finalDepthHandle = resolveToDebugScreen ? commonResourceData . debugScreenDepth : commonResourceData . backBufferDepth ;
2026-11-30 19:13:17 +00:00
// Capture pass for Unity Recorder
if ( hasCaptureActions )
{
m_CapturePass . RecordRenderGraph ( renderGraph , frameData );
}
2026-10-21 20:45:23 +01:00
if ( applyFinalPostProcessing )
{
m_PostProcessPassRenderGraph . RenderFinalPassRenderGraph ( renderGraph , frameData , in finalColorHandle , commonResourceData . overlayUITexture , in finalBlitTarget , needsColorEncoding );
finalColorHandle = finalBlitTarget ;
commonResourceData . activeColorID = ActiveID . BackBuffer ;
commonResourceData . activeDepthID = ActiveID . BackBuffer ;
}
// If post-processing then we already resolved to camera target while doing post.
// Also only do final blit if camera is not rendering to RT.
bool cameraTargetResolved =
// final PP always blit to camera target
applyFinalPostProcessing ||
// no final PP but we have PP stack. In that case it blit unless there are render pass after PP or pixel perfect camera is used
( applyPostProcessing && ! hasPassesAfterPostProcessing && ! hasCaptureActions && ! pixelPerfectCameraEnabled );
if (! commonResourceData . isActiveTargetBackBuffer && cameraData . resolveFinalTarget && ! cameraTargetResolved )
{
m_FinalBlitPass . Render ( renderGraph , frameData , cameraData , finalColorHandle , finalBlitTarget , commonResourceData . overlayUITexture );
finalColorHandle = finalBlitTarget ;
commonResourceData . activeColorID = ActiveID . BackBuffer ;
commonResourceData . activeDepthID = ActiveID . BackBuffer ;
}
// We can explicitly render the overlay UI from URP when HDR output is not enabled.
// SupportedRenderingFeatures.active.rendersUIOverlay should also be set to true.
bool shouldRenderUI = cameraData . rendersOverlayUI && cameraData . isLastBaseCamera ;
bool outputToHDR = cameraData . isHDROutputActive ;
if ( shouldRenderUI && ! outputToHDR )
m_DrawOverlayUIPass . RenderOverlay ( renderGraph , frameData , in finalColorHandle , in finalDepthHandle );
// If HDR debug views are enabled, DebugHandler will perform the blit from debugScreenColor (== finalColorHandle) to backBufferColor.
DebugHandler ?. Render ( renderGraph , cameraData , finalColorHandle , commonResourceData . overlayUITexture , commonResourceData . backBufferColor );
if ( cameraData . resolveFinalTarget )
{
if ( cameraData . isSceneViewCamera )
DrawRenderGraphWireOverlay ( renderGraph , frameData , commonResourceData . backBufferColor );
if ( drawGizmos )
DrawRenderGraphGizmos ( renderGraph , frameData , commonResourceData . activeColorTexture , commonResourceData . activeDepthTexture , GizmoSubset . PostImageEffects );
}
}
public Renderer2DData GetRenderer2DData ()
{
return m_Renderer2DData ;
}
protected override void Dispose ( bool disposing )
{
CleanupRenderGraphResources ();
#if URP_COMPATIBILITY_MODE
CleanupCompatibilityModeResources ();
#endif
base . Dispose ( disposing );
}
private void CleanupRenderGraphResources ()
{
m_Renderer2DData . Dispose ();
m_UpscalePass . Dispose ();
m_CopyDepthPass ?. Dispose ();
m_FinalBlitPass ?. Dispose ();
m_OffscreenUICoverPrepass ?. Dispose ();
m_DrawOffscreenUIPass ?. Dispose ();
m_DrawOverlayUIPass ?. Dispose ();
m_PostProcessPassRenderGraph ?. Cleanup ();
m_ColorGradingLutPassRenderGraph ?. Cleanup ();
2026-11-30 19:13:17 +00:00
s_RenderGraphCameraColorHandles [ 0 ]?. Release ();
s_RenderGraphCameraColorHandles [ 1 ]?. Release ();
s_RenderGraphCameraDepthHandle ?. Release ();
2026-10-21 20:45:23 +01:00
m_RenderGraphBackbufferColorHandle ?. Release ();
m_RenderGraphBackbufferDepthHandle ?. Release ();
m_CameraSortingLayerHandle ?. Release ();
Light2DManager . Dispose ();
Light2DLookupTexture . Release ();
2026-11-30 19:13:17 +00:00
LensFlareCommonSRP . Dispose ();
2026-10-21 20:45:23 +01:00
CoreUtils . Destroy ( m_BlitMaterial );
CoreUtils . Destroy ( m_BlitHDRMaterial );
CoreUtils . Destroy ( m_BlitOffscreenUICoverMaterial );
CoreUtils . Destroy ( m_SamplingMaterial );
#if ENABLE_VR && ENABLE_XR_MODULE
XRSystem . Dispose ();
#endif
}
internal static bool IsGLESDevice ()
{
return SystemInfo . graphicsDeviceType == GraphicsDeviceType . OpenGLES3 ;
}
internal static bool supportsMRT
{
get => ! IsGLESDevice ();
}
internal override bool supportsNativeRenderPassRendergraphCompiler => true ;
2026-11-30 19:13:17 +00:00
static internal bool IsSceneViewOrPreviewLightingActive ( UniversalCameraData cameraData )
{
DebugHandler debugHandler = ScriptableRenderPass . GetActiveDebugHandler ( cameraData );
var isLightingActive = debugHandler ?. IsLightingActive ?? true ;
#if UNITY_EDITOR
if ( cameraData . isSceneViewCamera && UnityEditor . SceneView . currentDrawingSceneView != null )
isLightingActive &= UnityEditor . SceneView . currentDrawingSceneView . sceneLighting ;
if ( cameraData . isPreviewCamera && cameraData . camera . name == "Preview Scene Camera" )
isLightingActive = false ;
#endif
return isLightingActive ;
}
2026-10-21 20:45:23 +01:00
}
}