-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathRenderer2DRendergraph.cs
More file actions
1078 lines (861 loc) · 54.8 KB
/
Copy pathRenderer2DRendergraph.cs
File metadata and controls
1078 lines (861 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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.
static int m_CurrentColorHandle = 0;
internal RTHandle[] m_RenderGraphCameraColorHandles = new RTHandle[]
{
null, null
};
internal RTHandle m_RenderGraphCameraDepthHandle;
RTHandle m_RenderGraphBackbufferColorHandle;
RTHandle m_RenderGraphBackbufferDepthHandle;
RTHandle m_CameraSortingLayerHandle;
static RTHandle m_OffscreenUIColorHandle;
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;
CapturePass m_CapturePass;
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;
PostProcessPassRenderGraph m_PostProcessPassRenderGraph;
ColorGradingLutPass m_ColorGradingLutPassRenderGraph;
private RTHandle currentRenderGraphCameraColorHandle
{
get
{
return m_RenderGraphCameraColorHandles[m_CurrentColorHandle];
}
}
// get the next m_RenderGraphCameraColorHandles and make it the new current for future accesses
private RTHandle nextRenderGraphCameraColorHandle
{
get
{
m_CurrentColorHandle = (m_CurrentColorHandle + 1) % 2;
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);
m_CapturePass = new CapturePass(RenderPassEvent.AfterRendering);
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();
supportedRenderingFeatures = new RenderingFeatures();
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>();
bool allowDepth = rendererData.useDepthStencilBuffer;
// 3D Render Textures with depth/stencil buffer are not supported
if (cameraData.targetTexture != null)
allowDepth &= cameraData.targetTexture.dimension != TextureDimension.Tex3D;
return allowDepth;
}
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;
}
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)
|| (DebugHandler != null && DebugHandler.WriteToDebugScreenTexture(cameraData.resolveFinalTarget))
|| cameraData.captureActions != null;
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);
//NOTE: Careful what you use here as many of the properties bake-in the camera rect so for example
//cameraData.cameraTargetDescriptor.width is the width of the rectangle but not the actual render target
//same with cameraData.camera.pixelWidth
output.importInfo.width = Screen.width;
output.importInfo.height = Screen.height;
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>();
m_LayerBatches = LayerUtility.CalculateBatches(m_Renderer2DData, out m_BatchCount);
// 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);
}
}
}
var renderTextureScale = m_Renderer2DData.lightRenderTextureScale;
var width = (int)Mathf.Max(1, cameraData.cameraTargetDescriptor.width * renderTextureScale);
var height = (int)Mathf.Max(1, cameraData.cameraTargetDescriptor.height * renderTextureScale);
// 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;
// Camera Target Color
if (m_CreateColorTexture)
{
cameraTargetDescriptor.useMipMap = false;
cameraTargetDescriptor.autoGenerateMips = false;
cameraTargetDescriptor.depthStencilFormat = GraphicsFormat.None;
RenderingUtils.ReAllocateHandleIfNeeded(ref m_RenderGraphCameraColorHandles[0], cameraTargetDescriptor, cameraTargetFilterMode, TextureWrapMode.Clamp, name: "_CameraTargetAttachmentA");
RenderingUtils.ReAllocateHandleIfNeeded(ref m_RenderGraphCameraColorHandles[1], cameraTargetDescriptor, cameraTargetFilterMode, TextureWrapMode.Clamp, name: "_CameraTargetAttachmentB");
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();
RenderingUtils.ReAllocateHandleIfNeeded(ref m_RenderGraphCameraDepthHandle, depthDescriptor, FilterMode.Point, TextureWrapMode.Clamp, name: "_CameraDepthAttachment");
commonResourceData.activeDepthID = ActiveID.Camera;
}
else
commonResourceData.activeDepthID = ActiveID.BackBuffer;
}
else // Overlay camera
{
cameraData.baseCamera.TryGetComponent<UniversalAdditionalCameraData>(out var baseCameraData);
var baseRenderer = (Renderer2D)baseCameraData.scriptableRenderer;
m_RenderGraphCameraColorHandles = baseRenderer.m_RenderGraphCameraColorHandles;
m_RenderGraphCameraDepthHandle = baseRenderer.m_RenderGraphCameraDepthHandle;
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;
commonResourceData.activeDepthID = m_CreateDepthTexture ? ActiveID.Camera : ActiveID.BackBuffer;
}
ImportResourceSummary importSummary = GetImportResourceSummary(renderGraph, cameraData);
if (m_CreateColorTexture)
{
importSummary.cameraColorParams.discardOnLastUse = lastCameraInTheStack;
importSummary.cameraDepthParams.discardOnLastUse = lastCameraInTheStack;
commonResourceData.cameraColor = renderGraph.ImportTexture(currentRenderGraphCameraColorHandle, importSummary.cameraColorParams);
commonResourceData.cameraDepth = renderGraph.ImportTexture(m_RenderGraphCameraDepthHandle, importSummary.cameraDepthParams);
}
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);
}
if (m_RenderGraphBackbufferDepthHandle == null)
{
m_RenderGraphBackbufferDepthHandle = RTHandles.Alloc(targetDepthId, "Backbuffer depth");
}
else if (m_RenderGraphBackbufferDepthHandle.nameID != targetDepthId)
{
RTHandleStaticHelpers.SetRTHandleUserManagedWrapper(ref m_RenderGraphBackbufferDepthHandle, targetDepthId);
}
commonResourceData.backBufferColor = renderGraph.ImportTexture(m_RenderGraphBackbufferColorHandle, importSummary.importInfo, importSummary.backBufferColorParams);
commonResourceData.backBufferDepth = renderGraph.ImportTexture(m_RenderGraphBackbufferDepthHandle, importSummary.importInfoDepth, importSummary.backBufferDepthParams);
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;
for (int i = 0; i < resourceData.normalsTexture.Length; ++i)
resourceData.normalsTexture[i] = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "_NormalMap", true, RendererLighting.k_NormalClearColor);
if (IsDepthUsageAllowed(frameData, m_Renderer2DData))
{
// 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);
}
void CreateCameraSortingLayerTexture(RenderGraph renderGraph, RenderTextureDescriptor descriptor)
{
Universal2DResourceData resourceData = frameData.Get<Universal2DResourceData>();
descriptor.msaaSamples = 1;
CopyCameraSortingLayerPass.ConfigureDescriptor(m_Renderer2DData.cameraSortingLayerDownsamplingMethod, ref descriptor, out var filterMode);
RenderingUtils.ReAllocateHandleIfNeeded(ref m_CameraSortingLayerHandle, descriptor, filterMode, TextureWrapMode.Clamp, name: CopyCameraSortingLayerPass.k_CameraSortingLayerTexture);
resourceData.cameraSortingLayerTexture = renderGraph.ImportTexture(m_CameraSortingLayerHandle);
}
bool RequiresDepthCopyPass(UniversalCameraData cameraData)
{
var renderPassInputs = GetRenderPassInputs(cameraData);
bool requiresDepthTexture = cameraData.requiresDepthTexture || renderPassInputs.requiresDepthTexture;
bool cameraHasPostProcessingWithDepth = cameraData.postProcessEnabled && m_PostProcessPassRenderGraph != null && cameraData.postProcessingRequiresDepthTexture;
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>();
TextureDesc textureDesc = new TextureDesc(descriptor);
DrawScreenSpaceUIPass.ConfigureOffscreenUITextureDesc(ref textureDesc);
RenderingUtils.ReAllocateHandleIfNeeded(ref m_OffscreenUIColorHandle, textureDesc, name: "_OverlayUITexture");
resourceData.overlayUITexture = renderGraph.ImportTexture(m_OffscreenUIColorHandle);
}
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).
bool applyFinalPostProcessing = cameraData.resolveFinalTarget && !ppcUpscaleRT && anyPostProcessing && cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing;
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;
// 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.
if (!isTargetBackbuffer)
{
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);
if (isTargetBackbuffer)
{
commonResourceData.activeColorID = ActiveID.BackBuffer;
commonResourceData.activeDepthID = ActiveID.BackBuffer;
}
}
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;
// Capture pass for Unity Recorder
if (hasCaptureActions)
{
m_CapturePass.RecordRenderGraph(renderGraph, frameData);
}
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;