-
Notifications
You must be signed in to change notification settings - Fork 830
/
Copy pathUniversalRenderer.cs
1669 lines (1436 loc) · 94.8 KB
/
UniversalRenderer.cs
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 System;
using System.Collections.Generic;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering.Universal.Internal;
namespace UnityEngine.Rendering.Universal
{
/// <summary>
/// Rendering modes for Universal renderer.
/// </summary>
public enum RenderingMode
{
/// <summary>Render all objects and lighting in one pass, with a hard limit on the number of lights that can be applied on an object.</summary>
Forward = 0,
/// <summary>Render all objects and lighting in one pass using a clustered data structure to access lighting data.</summary>
[InspectorName("Forward+")]
ForwardPlus = 2,
/// <summary>Render all objects first in a g-buffer pass, then apply all lighting in a separate pass using deferred shading.</summary>
Deferred = 1
};
/// <summary>
/// When the Universal Renderer should use Depth Priming in Forward mode.
/// </summary>
public enum DepthPrimingMode
{
/// <summary>Depth Priming will never be used.</summary>
Disabled,
/// <summary>Depth Priming will only be used if there is a depth prepass needed by any of the render passes.</summary>
Auto,
/// <summary>A depth prepass will be explicitly requested so Depth Priming can be used.</summary>
Forced,
}
/// <summary>
/// Default renderer for Universal RP.
/// This renderer is supported on all Universal RP supported platforms.
/// It uses a classic forward rendering strategy with per-object light culling.
/// </summary>
public sealed partial class UniversalRenderer : ScriptableRenderer
{
// Don't use 24 bit format in the editor, because it leads to unnecessary depth buffer re-allocations
#if (UNITY_SWITCH || UNITY_ANDROID) && !UNITY_EDITOR
const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D24_UNorm_S8_UInt;
const int k_DepthBufferBits = 24;
#else
const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D32_SFloat_S8_UInt;
const int k_DepthBufferBits = 32;
#endif
const int k_FinalBlitPassQueueOffset = 1;
const int k_AfterFinalBlitPassQueueOffset = k_FinalBlitPassQueueOffset + 1;
static readonly List<ShaderTagId> k_DepthNormalsOnly = new List<ShaderTagId> { new ShaderTagId("DepthNormalsOnly") };
private static class Profiling
{
private const string k_Name = nameof(UniversalRenderer);
public static readonly ProfilingSampler createCameraRenderTarget = new ProfilingSampler($"{k_Name}.{nameof(CreateCameraRenderTarget)}");
}
/// <inheritdoc/>
public override int SupportedCameraStackingTypes()
{
switch (m_RenderingMode)
{
case RenderingMode.Forward:
case RenderingMode.ForwardPlus:
return 1 << (int)CameraRenderType.Base | 1 << (int)CameraRenderType.Overlay;
case RenderingMode.Deferred:
return 1 << (int)CameraRenderType.Base;
default:
return 0;
}
}
// Rendering mode setup from UI. The final rendering mode used can be different. See renderingModeActual.
internal RenderingMode renderingModeRequested => m_RenderingMode;
// Actual rendering mode, which may be different (ex: wireframe rendering, hardware not capable of deferred rendering).
internal RenderingMode renderingModeActual => renderingModeRequested == RenderingMode.Deferred && (GL.wireframe || (DebugHandler != null && DebugHandler.IsActiveModeUnsupportedForDeferred) || m_DeferredLights == null || !m_DeferredLights.IsRuntimeSupportedThisFrame() || m_DeferredLights.IsOverlay)
? RenderingMode.Forward
: this.renderingModeRequested;
bool m_Clustering;
internal bool accurateGbufferNormals => m_DeferredLights != null ? m_DeferredLights.AccurateGbufferNormals : false;
#if ADAPTIVE_PERFORMANCE_2_1_0_OR_NEWER
internal bool needTransparencyPass { get { return !UniversalRenderPipeline.asset.useAdaptivePerformance || !AdaptivePerformance.AdaptivePerformanceRenderSettings.SkipTransparentObjects;; } }
#endif
/// <summary>Property to control the depth priming behavior of the forward rendering path.</summary>
public DepthPrimingMode depthPrimingMode { get { return m_DepthPrimingMode; } set { m_DepthPrimingMode = value; } }
DepthOnlyPass m_DepthPrepass;
DepthNormalOnlyPass m_DepthNormalPrepass;
CopyDepthPass m_PrimedDepthCopyPass;
MotionVectorRenderPass m_MotionVectorPass;
MainLightShadowCasterPass m_MainLightShadowCasterPass;
AdditionalLightsShadowCasterPass m_AdditionalLightsShadowCasterPass;
GBufferPass m_GBufferPass;
CopyDepthPass m_GBufferCopyDepthPass;
DeferredPass m_DeferredPass;
DrawObjectsPass m_RenderOpaqueForwardOnlyPass;
DrawObjectsPass m_RenderOpaqueForwardPass;
DrawObjectsWithRenderingLayersPass m_RenderOpaqueForwardWithRenderingLayersPass;
DrawSkyboxPass m_DrawSkyboxPass;
CopyDepthPass m_CopyDepthPass;
CopyColorPass m_CopyColorPass;
TransparentSettingsPass m_TransparentSettingsPass;
DrawObjectsPass m_RenderTransparentForwardPass;
InvokeOnRenderObjectCallbackPass m_OnRenderObjectCallbackPass;
FinalBlitPass m_FinalBlitPass;
CapturePass m_CapturePass;
#if ENABLE_VR && ENABLE_XR_MODULE
XROcclusionMeshPass m_XROcclusionMeshPass;
CopyDepthPass m_XRCopyDepthPass;
#endif
#if UNITY_EDITOR
CopyDepthPass m_FinalDepthCopyPass;
#endif
DrawScreenSpaceUIPass m_DrawOffscreenUIPass;
DrawScreenSpaceUIPass m_DrawOverlayUIPass;
internal RenderTargetBufferSystem m_ColorBufferSystem;
internal RTHandle m_ActiveCameraColorAttachment;
RTHandle m_ColorFrontBuffer;
internal RTHandle m_ActiveCameraDepthAttachment;
internal RTHandle m_CameraDepthAttachment;
RTHandle m_XRTargetHandleAlias;
internal RTHandle m_CameraDepthAttachment_D3d_11;
internal RTHandle m_DepthTexture;
RTHandle m_NormalsTexture;
RTHandle m_DecalLayersTexture;
RTHandle m_OpaqueColor;
RTHandle m_MotionVectorColor;
RTHandle m_MotionVectorDepth;
ForwardLights m_ForwardLights;
DeferredLights m_DeferredLights;
RenderingMode m_RenderingMode;
DepthPrimingMode m_DepthPrimingMode;
CopyDepthMode m_CopyDepthMode;
bool m_DepthPrimingRecommended;
StencilState m_DefaultStencilState;
LightCookieManager m_LightCookieManager;
IntermediateTextureMode m_IntermediateTextureMode;
bool m_VulkanEnablePreTransform;
// Materials used in URP Scriptable Render Passes
Material m_BlitMaterial = null;
Material m_BlitHDRMaterial = null;
Material m_CopyDepthMaterial = null;
Material m_SamplingMaterial = null;
Material m_StencilDeferredMaterial = null;
Material m_CameraMotionVecMaterial = null;
Material m_ObjectMotionVecMaterial = null;
PostProcessPasses m_PostProcessPasses;
internal ColorGradingLutPass colorGradingLutPass { get => m_PostProcessPasses.colorGradingLutPass; }
internal PostProcessPass postProcessPass { get => m_PostProcessPasses.postProcessPass; }
internal PostProcessPass finalPostProcessPass { get => m_PostProcessPasses.finalPostProcessPass; }
internal RTHandle colorGradingLut { get => m_PostProcessPasses.colorGradingLut; }
internal DeferredLights deferredLights { get => m_DeferredLights; }
/// <summary>
/// Constructor for the Universal Renderer.
/// </summary>
/// <param name="data">The settings to create the renderer with.</param>
public UniversalRenderer(UniversalRendererData data) : base(data)
{
// Query and cache runtime platform info first before setting up URP.
PlatformAutoDetect.Initialize();
#if ENABLE_VR && ENABLE_XR_MODULE
Experimental.Rendering.XRSystem.Initialize(XRPassUniversal.Create, data.xrSystemData.shaders.xrOcclusionMeshPS, data.xrSystemData.shaders.xrMirrorViewPS);
#endif
m_BlitMaterial = CoreUtils.CreateEngineMaterial(data.shaders.coreBlitPS);
m_BlitHDRMaterial = CoreUtils.CreateEngineMaterial(data.shaders.blitHDROverlay);
m_CopyDepthMaterial = CoreUtils.CreateEngineMaterial(data.shaders.copyDepthPS);
m_SamplingMaterial = CoreUtils.CreateEngineMaterial(data.shaders.samplingPS);
m_StencilDeferredMaterial = CoreUtils.CreateEngineMaterial(data.shaders.stencilDeferredPS);
m_CameraMotionVecMaterial = CoreUtils.CreateEngineMaterial(data.shaders.cameraMotionVector);
m_ObjectMotionVecMaterial = CoreUtils.CreateEngineMaterial(data.shaders.objectMotionVector);
StencilStateData stencilData = data.defaultStencilState;
m_DefaultStencilState = StencilState.defaultValue;
m_DefaultStencilState.enabled = stencilData.overrideStencilState;
m_DefaultStencilState.SetCompareFunction(stencilData.stencilCompareFunction);
m_DefaultStencilState.SetPassOperation(stencilData.passOperation);
m_DefaultStencilState.SetFailOperation(stencilData.failOperation);
m_DefaultStencilState.SetZFailOperation(stencilData.zFailOperation);
m_IntermediateTextureMode = data.intermediateTextureMode;
if (UniversalRenderPipeline.asset?.supportsLightCookies ?? false)
{
var settings = LightCookieManager.Settings.Create();
var asset = UniversalRenderPipeline.asset;
if (asset)
{
settings.atlas.format = asset.additionalLightsCookieFormat;
settings.atlas.resolution = asset.additionalLightsCookieResolution;
}
m_LightCookieManager = new LightCookieManager(ref settings);
}
this.stripShadowsOffVariants = true;
this.stripAdditionalLightOffVariants = true;
#if ENABLE_VR && ENABLE_VR_MODULE
#if PLATFORM_WINRT || PLATFORM_ANDROID
// AdditionalLightOff variant is available on HL&Quest platform due to performance consideration.
this.stripAdditionalLightOffVariants = !PlatformAutoDetect.isXRMobile;
#endif
#endif
ForwardLights.InitParams forwardInitParams;
forwardInitParams.lightCookieManager = m_LightCookieManager;
forwardInitParams.forwardPlus = data.renderingMode == RenderingMode.ForwardPlus;
m_Clustering = data.renderingMode == RenderingMode.ForwardPlus;
m_ForwardLights = new ForwardLights(forwardInitParams);
//m_DeferredLights.LightCulling = data.lightCulling;
this.m_RenderingMode = data.renderingMode;
this.m_DepthPrimingMode = data.depthPrimingMode;
this.m_CopyDepthMode = data.copyDepthMode;
#if UNITY_ANDROID || UNITY_IOS || UNITY_TVOS
this.m_DepthPrimingRecommended = false;
#else
this.m_DepthPrimingRecommended = true;
#endif
// Note: Since all custom render passes inject first and we have stable sort,
// we inject the builtin passes in the before events.
m_MainLightShadowCasterPass = new MainLightShadowCasterPass(RenderPassEvent.BeforeRenderingShadows);
m_AdditionalLightsShadowCasterPass = new AdditionalLightsShadowCasterPass(RenderPassEvent.BeforeRenderingShadows);
#if ENABLE_VR && ENABLE_XR_MODULE
m_XROcclusionMeshPass = new XROcclusionMeshPass(RenderPassEvent.BeforeRenderingOpaques);
// Schedule XR copydepth right after m_FinalBlitPass
m_XRCopyDepthPass = new CopyDepthPass(RenderPassEvent.AfterRendering + k_AfterFinalBlitPassQueueOffset, m_CopyDepthMaterial);
#endif
m_DepthPrepass = new DepthOnlyPass(RenderPassEvent.BeforeRenderingPrePasses, RenderQueueRange.opaque, data.opaqueLayerMask);
m_DepthNormalPrepass = new DepthNormalOnlyPass(RenderPassEvent.BeforeRenderingPrePasses, RenderQueueRange.opaque, data.opaqueLayerMask);
if (renderingModeRequested == RenderingMode.Forward || renderingModeRequested == RenderingMode.ForwardPlus)
{
m_PrimedDepthCopyPass = new CopyDepthPass(RenderPassEvent.AfterRenderingPrePasses, m_CopyDepthMaterial, true);
}
if (this.renderingModeRequested == RenderingMode.Deferred)
{
var deferredInitParams = new DeferredLights.InitParams();
deferredInitParams.stencilDeferredMaterial = m_StencilDeferredMaterial;
deferredInitParams.lightCookieManager = m_LightCookieManager;
m_DeferredLights = new DeferredLights(deferredInitParams, useRenderPassEnabled);
m_DeferredLights.AccurateGbufferNormals = data.accurateGbufferNormals;
m_GBufferPass = new GBufferPass(RenderPassEvent.BeforeRenderingGbuffer, RenderQueueRange.opaque, data.opaqueLayerMask, m_DefaultStencilState, stencilData.stencilReference, m_DeferredLights);
// Forward-only pass only runs if deferred renderer is enabled.
// It allows specific materials to be rendered in a forward-like pass.
// We render both gbuffer pass and forward-only pass before the deferred lighting pass so we can minimize copies of depth buffer and
// benefits from some depth rejection.
// - If a material can be rendered either forward or deferred, then it should declare a UniversalForward and a UniversalGBuffer pass.
// - If a material cannot be lit in deferred (unlit, bakedLit, special material such as hair, skin shader), then it should declare UniversalForwardOnly pass
// - Legacy materials have unamed pass, which is implicitely renamed as SRPDefaultUnlit. In that case, they are considered forward-only too.
// TO declare a material with unnamed pass and UniversalForward/UniversalForwardOnly pass is an ERROR, as the material will be rendered twice.
StencilState forwardOnlyStencilState = DeferredLights.OverwriteStencil(m_DefaultStencilState, (int)StencilUsage.MaterialMask);
ShaderTagId[] forwardOnlyShaderTagIds = new ShaderTagId[]
{
new ShaderTagId("UniversalForwardOnly"),
new ShaderTagId("SRPDefaultUnlit"), // Legacy shaders (do not have a gbuffer pass) are considered forward-only for backward compatibility
new ShaderTagId("LightweightForward") // Legacy shaders (do not have a gbuffer pass) are considered forward-only for backward compatibility
};
int forwardOnlyStencilRef = stencilData.stencilReference | (int)StencilUsage.MaterialUnlit;
m_GBufferCopyDepthPass = new CopyDepthPass(RenderPassEvent.BeforeRenderingGbuffer + 1, m_CopyDepthMaterial, true);
m_DeferredPass = new DeferredPass(RenderPassEvent.BeforeRenderingDeferredLights, m_DeferredLights);
m_RenderOpaqueForwardOnlyPass = new DrawObjectsPass("Render Opaques Forward Only", forwardOnlyShaderTagIds, true, RenderPassEvent.BeforeRenderingOpaques, RenderQueueRange.opaque, data.opaqueLayerMask, forwardOnlyStencilState, forwardOnlyStencilRef);
}
// Always create this pass even in deferred because we use it for wireframe rendering in the Editor or offscreen depth texture rendering.
m_RenderOpaqueForwardPass = new DrawObjectsPass(URPProfileId.DrawOpaqueObjects, true, RenderPassEvent.BeforeRenderingOpaques, RenderQueueRange.opaque, data.opaqueLayerMask, m_DefaultStencilState, stencilData.stencilReference);
m_RenderOpaqueForwardWithRenderingLayersPass = new DrawObjectsWithRenderingLayersPass(URPProfileId.DrawOpaqueObjects, true, RenderPassEvent.BeforeRenderingOpaques, RenderQueueRange.opaque, data.opaqueLayerMask, m_DefaultStencilState, stencilData.stencilReference);
bool copyDepthAfterTransparents = m_CopyDepthMode == CopyDepthMode.AfterTransparents;
RenderPassEvent copyDepthEvent = copyDepthAfterTransparents ? RenderPassEvent.AfterRenderingTransparents : RenderPassEvent.AfterRenderingSkybox;
m_CopyDepthPass = new CopyDepthPass(
copyDepthEvent,
m_CopyDepthMaterial,
shouldClear: true,
copyResolvedDepth: RenderingUtils.MultisampleDepthResolveSupported() && SystemInfo.supportsMultisampleAutoResolve && copyDepthAfterTransparents);
// Motion vectors depend on the (copy) depth texture. Depth is reprojected to calculate motion vectors.
m_MotionVectorPass = new MotionVectorRenderPass(copyDepthEvent + 1, m_CameraMotionVecMaterial, m_ObjectMotionVecMaterial, data.opaqueLayerMask);
m_DrawSkyboxPass = new DrawSkyboxPass(RenderPassEvent.BeforeRenderingSkybox);
m_CopyColorPass = new CopyColorPass(RenderPassEvent.AfterRenderingSkybox, m_SamplingMaterial, m_BlitMaterial);
#if ADAPTIVE_PERFORMANCE_2_1_0_OR_NEWER
if (needTransparencyPass)
#endif
{
m_TransparentSettingsPass = new TransparentSettingsPass(RenderPassEvent.BeforeRenderingTransparents, data.shadowTransparentReceive);
m_RenderTransparentForwardPass = new DrawObjectsPass(URPProfileId.DrawTransparentObjects, false, RenderPassEvent.BeforeRenderingTransparents, RenderQueueRange.transparent, data.transparentLayerMask, m_DefaultStencilState, stencilData.stencilReference);
}
m_OnRenderObjectCallbackPass = new InvokeOnRenderObjectCallbackPass(RenderPassEvent.BeforeRenderingPostProcessing);
m_DrawOffscreenUIPass = new DrawScreenSpaceUIPass(RenderPassEvent.BeforeRenderingPostProcessing, true);
m_DrawOverlayUIPass = new DrawScreenSpaceUIPass(RenderPassEvent.AfterRendering + k_AfterFinalBlitPassQueueOffset, false); // after m_FinalBlitPass
{
var postProcessParams = PostProcessParams.Create();
postProcessParams.blitMaterial = m_BlitMaterial;
postProcessParams.requestHDRFormat = GraphicsFormat.B10G11R11_UFloatPack32;
var asset = UniversalRenderPipeline.asset;
if (asset)
postProcessParams.requestHDRFormat = UniversalRenderPipeline.MakeRenderTextureGraphicsFormat(asset.supportsHDR, asset.hdrColorBufferPrecision, false);
m_PostProcessPasses = new PostProcessPasses(data.postProcessData, ref postProcessParams);
}
m_CapturePass = new CapturePass(RenderPassEvent.AfterRendering);
m_FinalBlitPass = new FinalBlitPass(RenderPassEvent.AfterRendering + k_FinalBlitPassQueueOffset, m_BlitMaterial, m_BlitHDRMaterial);
#if UNITY_EDITOR
m_FinalDepthCopyPass = new CopyDepthPass(RenderPassEvent.AfterRendering + 9, m_CopyDepthMaterial);
#endif
// RenderTexture format depends on camera and pipeline (HDR, non HDR, etc)
// Samples (MSAA) depend on camera and pipeline
m_ColorBufferSystem = new RenderTargetBufferSystem("_CameraColorAttachment");
supportedRenderingFeatures = new RenderingFeatures();
if (this.renderingModeRequested == RenderingMode.Deferred)
{
// Deferred rendering does not support MSAA.
this.supportedRenderingFeatures.msaa = false;
// Avoid legacy platforms: use vulkan instead.
unsupportedGraphicsDeviceTypes = new GraphicsDeviceType[]
{
GraphicsDeviceType.OpenGLCore,
GraphicsDeviceType.OpenGLES2,
GraphicsDeviceType.OpenGLES3
};
}
LensFlareCommonSRP.mergeNeeded = 0;
LensFlareCommonSRP.maxLensFlareWithOcclusionTemporalSample = 1;
LensFlareCommonSRP.Initialize();
m_VulkanEnablePreTransform = GraphicsSettings.HasShaderDefine(BuiltinShaderDefine.UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION);
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
m_ForwardLights.Cleanup();
m_GBufferPass?.Dispose();
m_PostProcessPasses.Dispose();
m_FinalBlitPass?.Dispose();
m_DrawOffscreenUIPass?.Dispose();
m_DrawOverlayUIPass?.Dispose();
m_XRTargetHandleAlias?.Release();
ReleaseRenderTargets();
base.Dispose(disposing);
CoreUtils.Destroy(m_BlitMaterial);
CoreUtils.Destroy(m_BlitHDRMaterial);
CoreUtils.Destroy(m_CopyDepthMaterial);
CoreUtils.Destroy(m_SamplingMaterial);
CoreUtils.Destroy(m_StencilDeferredMaterial);
CoreUtils.Destroy(m_CameraMotionVecMaterial);
CoreUtils.Destroy(m_ObjectMotionVecMaterial);
CleanupRenderGraphResources();
LensFlareCommonSRP.Dispose();
}
internal override void ReleaseRenderTargets()
{
m_ColorBufferSystem.Dispose();
if (m_DeferredLights != null && !m_DeferredLights.UseRenderPass)
m_GBufferPass?.Dispose();
m_PostProcessPasses.ReleaseRenderTargets();
m_MainLightShadowCasterPass?.Dispose();
m_AdditionalLightsShadowCasterPass?.Dispose();
m_CameraDepthAttachment?.Release();
m_CameraDepthAttachment_D3d_11?.Release();
m_DepthTexture?.Release();
m_NormalsTexture?.Release();
m_DecalLayersTexture?.Release();
m_OpaqueColor?.Release();
m_MotionVectorColor?.Release();
m_MotionVectorDepth?.Release();
hasReleasedRTs = true;
}
private void SetupFinalPassDebug(ref CameraData cameraData)
{
if ((DebugHandler != null) && DebugHandler.IsActiveForCamera(ref cameraData))
{
if (DebugHandler.TryGetFullscreenDebugMode(out DebugFullScreenMode fullScreenDebugMode, out int textureHeightPercent) &&
(fullScreenDebugMode != DebugFullScreenMode.ReflectionProbeAtlas || m_Clustering))
{
Camera camera = cameraData.camera;
float screenWidth = camera.pixelWidth;
float screenHeight = camera.pixelHeight;
var relativeSize = Mathf.Clamp01(textureHeightPercent / 100f);
var height = relativeSize * screenHeight;
var width = relativeSize * screenWidth;
if (fullScreenDebugMode == DebugFullScreenMode.ReflectionProbeAtlas)
{
// Ensure that atlas is not stretched, but doesn't take up more than the percentage in any dimension.
var texture = m_ForwardLights.reflectionProbeManager.atlasRT;
var targetWidth = height * texture.width / texture.height;
if (targetWidth > width)
{
height = width * texture.height / texture.width;
}
else
{
width = targetWidth;
}
}
float normalizedSizeX = width / screenWidth;
float normalizedSizeY = height / screenHeight;
Rect normalizedRect = new Rect(1 - normalizedSizeX, 1 - normalizedSizeY, normalizedSizeX, normalizedSizeY);
switch (fullScreenDebugMode)
{
case DebugFullScreenMode.Depth:
{
DebugHandler.SetDebugRenderTarget(m_DepthTexture.nameID, normalizedRect, true);
break;
}
case DebugFullScreenMode.AdditionalLightsShadowMap:
{
DebugHandler.SetDebugRenderTarget(m_AdditionalLightsShadowCasterPass.m_AdditionalLightsShadowmapHandle, normalizedRect, false);
break;
}
case DebugFullScreenMode.MainLightShadowMap:
{
DebugHandler.SetDebugRenderTarget(m_MainLightShadowCasterPass.m_MainLightShadowmapTexture, normalizedRect, false);
break;
}
case DebugFullScreenMode.ReflectionProbeAtlas:
{
DebugHandler.SetDebugRenderTarget(m_ForwardLights.reflectionProbeManager.atlasRT, normalizedRect, false);
break;
}
default:
{
break;
}
}
}
else
{
DebugHandler.ResetDebugRenderTarget();
}
}
}
/// <summary>
/// Returns if the camera renders to a offscreen depth texture.
/// </summary>
/// <param name="cameraData">The camera data for the camera being rendered.</param>
/// <returns>Returns true if the camera renders to depth without any color buffer. It will return false otherwise.</returns>
public static bool IsOffscreenDepthTexture(in CameraData cameraData) => cameraData.targetTexture != null && cameraData.targetTexture.format == RenderTextureFormat.Depth;
bool IsDepthPrimingEnabled(ref CameraData cameraData)
{
#if UNITY_EDITOR
// We need to disable depth-priming for DrawCameraMode.Wireframe, since depth-priming forces ZTest to Equal
// for opaques rendering, which breaks wireframe rendering.
if (cameraData.isSceneViewCamera)
{
foreach (var sceneViewObject in UnityEditor.SceneView.sceneViews)
{
var sceneView = sceneViewObject as UnityEditor.SceneView;
if (sceneView != null && sceneView.camera == cameraData.camera && sceneView.cameraMode.drawMode == UnityEditor.DrawCameraMode.Wireframe)
return false;
}
}
#endif
// depth priming requires an extra depth copy, disable it on platforms not supporting it (like GLES when MSAA is on)
if (!CanCopyDepth(ref cameraData))
return false;
// Depth Priming causes rendering errors with WebGL on Apple Arm64 GPUs.
bool isNotWebGL = !IsWebGL();
bool depthPrimingRequested = (m_DepthPrimingRecommended && m_DepthPrimingMode == DepthPrimingMode.Auto) || m_DepthPrimingMode == DepthPrimingMode.Forced;
bool isForwardRenderingMode = m_RenderingMode == RenderingMode.Forward || m_RenderingMode == RenderingMode.ForwardPlus;
bool isFirstCameraToWriteDepth = cameraData.renderType == CameraRenderType.Base || cameraData.clearDepth;
// Enabled Depth priming when baking Reflection Probes causes artefacts (UUM-12397)
bool isNotReflectionCamera = cameraData.cameraType != CameraType.Reflection;
// Depth is not rendered in a depth-only camera setup with depth priming (UUM-38158)
bool isNotOffscreenDepthTexture = !IsOffscreenDepthTexture(cameraData);
return depthPrimingRequested && isForwardRenderingMode && isFirstCameraToWriteDepth && isNotReflectionCamera && isNotOffscreenDepthTexture && isNotWebGL;
}
bool IsWebGL()
{
#if PLATFORM_WEBGL
return IsGLESDevice();
#else
return false;
#endif
}
bool IsGLESDevice()
{
return SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES2 || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3;
}
bool IsGLDevice()
{
return IsGLESDevice() || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLCore;
}
/// <inheritdoc />
public override void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
{
m_ForwardLights.PreSetup(ref renderingData);
ref CameraData cameraData = ref renderingData.cameraData;
Camera camera = cameraData.camera;
RenderTextureDescriptor cameraTargetDescriptor = cameraData.cameraTargetDescriptor;
var cmd = renderingData.commandBuffer;
if (DebugHandler != null)
{
DebugHandler.Setup(context, ref renderingData);
if (DebugHandler.IsActiveForCamera(ref cameraData))
{
if (DebugHandler.WriteToDebugScreenTexture(ref cameraData))
{
RenderTextureDescriptor colorDesc = cameraData.cameraTargetDescriptor;
DebugHandler.ConfigureColorDescriptorForDebugScreen(ref colorDesc, cameraData.pixelWidth, cameraData.pixelHeight);
RenderingUtils.ReAllocateIfNeeded(ref DebugHandler.DebugScreenColorHandle, colorDesc, name: "_DebugScreenColor");
RenderTextureDescriptor depthDesc = cameraData.cameraTargetDescriptor;
DebugHandler.ConfigureDepthDescriptorForDebugScreen(ref depthDesc, k_DepthBufferBits, cameraData.pixelWidth, cameraData.pixelHeight);
RenderingUtils.ReAllocateIfNeeded(ref DebugHandler.DebugScreenDepthHandle, depthDesc, name: "_DebugScreenDepth");
}
if (DebugHandler.HDRDebugViewIsActive(ref cameraData))
{
DebugHandler.hdrDebugViewPass.Setup(ref cameraData, DebugHandler.DebugDisplaySettings.lightingSettings.hdrDebugMode);
EnqueuePass(DebugHandler.hdrDebugViewPass);
}
}
}
if (cameraData.cameraType != CameraType.Game)
useRenderPassEnabled = false;
// Because of the shortcutting done by depth only offscreen cameras, useDepthPriming must be computed early
useDepthPriming = IsDepthPrimingEnabled(ref cameraData);
// Special path for depth only offscreen cameras. Only write opaques + transparents.
if (IsOffscreenDepthTexture(in cameraData))
{
ConfigureCameraTarget(k_CameraTarget, k_CameraTarget);
SetupRenderPasses(in renderingData);
EnqueuePass(m_RenderOpaqueForwardPass);
#if ADAPTIVE_PERFORMANCE_2_1_0_OR_NEWER
if (!needTransparencyPass)
return;
#endif
EnqueuePass(m_RenderTransparentForwardPass);
return;
}
// Assign the camera color target early in case it is needed during AddRenderPasses.
bool isPreviewCamera = cameraData.isPreviewCamera;
var createColorTexture = ((rendererFeatures.Count != 0 && m_IntermediateTextureMode == IntermediateTextureMode.Always) && !isPreviewCamera) ||
(Application.isEditor && m_Clustering);
// Gather render passe input requirements
RenderPassInputSummary renderPassInputs = GetRenderPassInputs(ref renderingData);
// Gather render pass require rendering layers event and mask size
bool requiresRenderingLayer = RenderingLayerUtils.RequireRenderingLayers(this, rendererFeatures,
cameraTargetDescriptor.msaaSamples,
out var renderingLayersEvent, out var renderingLayerMaskSize);
// All passes that use write to rendering layers are excluded from gl
// So we disable it to avoid setting multiple render targets
if (IsGLDevice())
requiresRenderingLayer = false;
bool renderingLayerProvidesByDepthNormalPass = false;
bool renderingLayerProvidesRenderObjectPass = false;
if (requiresRenderingLayer && renderingModeActual != RenderingMode.Deferred)
{
switch (renderingLayersEvent)
{
case RenderingLayerUtils.Event.DepthNormalPrePass:
renderingLayerProvidesByDepthNormalPass = true;
break;
case RenderingLayerUtils.Event.Opaque:
renderingLayerProvidesRenderObjectPass = true;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
// Enable depth normal prepass
if (renderingLayerProvidesByDepthNormalPass)
renderPassInputs.requiresNormalsTexture = true;
// TODO: investigate the order of call, had to change because of requiresRenderingLayer
if (m_DeferredLights != null)
{
m_DeferredLights.RenderingLayerMaskSize = renderingLayerMaskSize;
m_DeferredLights.UseDecalLayers = requiresRenderingLayer;
// TODO: This needs to be setup early, otherwise gbuffer attachments will be allocated with wrong size
m_DeferredLights.HasNormalPrepass = renderPassInputs.requiresNormalsTexture;
m_DeferredLights.ResolveMixedLightingMode(ref renderingData);
m_DeferredLights.IsOverlay = cameraData.renderType == CameraRenderType.Overlay;
if (m_DeferredLights.UseRenderPass)
{
// At this point we only have injected renderer features in the queue and can do assumptions on whether we'll need Framebuffer Fetch
foreach (var pass in activeRenderPassQueue)
{
if (pass.renderPassEvent >= RenderPassEvent.AfterRenderingGbuffer &&
pass.renderPassEvent <= RenderPassEvent.BeforeRenderingDeferredLights)
{
m_DeferredLights.DisableFramebufferFetchInput();
break;
}
}
}
}
// Should apply post-processing after rendering this camera?
bool applyPostProcessing = cameraData.postProcessEnabled && m_PostProcessPasses.isCreated;
// There's at least a camera in the camera stack that applies post-processing
bool anyPostProcessing = renderingData.postProcessingEnabled && m_PostProcessPasses.isCreated;
// If Camera's PostProcessing is enabled and if there any enabled PostProcessing requires depth texture as shader read resource (Motion Blur/DoF)
bool cameraHasPostProcessingWithDepth = applyPostProcessing && cameraData.postProcessingRequiresDepthTexture;
// TODO: We could cache and generate the LUT before rendering the stack
bool generateColorGradingLUT = cameraData.postProcessEnabled && m_PostProcessPasses.isCreated;
bool isSceneViewOrPreviewCamera = cameraData.isSceneViewCamera || cameraData.isPreviewCamera;
// This indicates whether the renderer will output a depth texture.
bool requiresDepthTexture = cameraData.requiresDepthTexture || renderPassInputs.requiresDepthTexture || m_DepthPrimingMode == DepthPrimingMode.Forced;
#if UNITY_EDITOR
bool isGizmosEnabled = UnityEditor.Handles.ShouldRenderGizmos();
#else
bool isGizmosEnabled = false;
#endif
bool mainLightShadows = m_MainLightShadowCasterPass.Setup(ref renderingData);
bool additionalLightShadows = m_AdditionalLightsShadowCasterPass.Setup(ref renderingData);
bool transparentsNeedSettingsPass = m_TransparentSettingsPass.Setup();
bool forcePrepass = (m_CopyDepthMode == CopyDepthMode.ForcePrepass);
// Depth prepass is generated in the following cases:
// - If game or offscreen camera requires it we check if we can copy the depth from the rendering opaques pass and use that instead.
// - Scene or preview cameras always require a depth texture. We do a depth pre-pass to simplify it and it shouldn't matter much for editor.
// - Render passes require it
bool requiresDepthPrepass = (requiresDepthTexture || cameraHasPostProcessingWithDepth) && (!CanCopyDepth(ref renderingData.cameraData) || forcePrepass);
requiresDepthPrepass |= isSceneViewOrPreviewCamera;
requiresDepthPrepass |= isGizmosEnabled;
requiresDepthPrepass |= isPreviewCamera;
requiresDepthPrepass |= renderPassInputs.requiresDepthPrepass;
requiresDepthPrepass |= renderPassInputs.requiresNormalsTexture;
// Current aim of depth prepass is to generate a copy of depth buffer, it is NOT to prime depth buffer and reduce overdraw on non-mobile platforms.
// When deferred renderer is enabled, depth buffer is already accessible so depth prepass is not needed.
// The only exception is for generating depth-normal textures: SSAO pass needs it and it must run before forward-only geometry.
// DepthNormal prepass will render:
// - forward-only geometry when deferred renderer is enabled
// - all geometry when forward renderer is enabled
if (requiresDepthPrepass && this.renderingModeActual == RenderingMode.Deferred && !renderPassInputs.requiresNormalsTexture)
requiresDepthPrepass = false;
requiresDepthPrepass |= useDepthPriming;
// If possible try to merge the opaque and skybox passes instead of splitting them when "Depth Texture" is required.
// The copying of depth should normally happen after rendering opaques.
// But if we only require it for post processing or the scene camera then we do it after rendering transparent objects
// Aim to have the most optimized render pass event for Depth Copy (The aim is to minimize the number of render passes)
if (requiresDepthTexture)
{
bool copyDepthAfterTransparents = m_CopyDepthMode == CopyDepthMode.AfterTransparents;
RenderPassEvent copyDepthPassEvent = copyDepthAfterTransparents ? RenderPassEvent.AfterRenderingTransparents : RenderPassEvent.AfterRenderingOpaques;
// RenderPassInputs's requiresDepthTexture is configured through ScriptableRenderPass's ConfigureInput function
if (renderPassInputs.requiresDepthTexture)
{
// Do depth copy before the render pass that requires depth texture as shader read resource
copyDepthPassEvent = (RenderPassEvent)Mathf.Min((int)RenderPassEvent.AfterRenderingTransparents, ((int)renderPassInputs.requiresDepthTextureEarliestEvent) - 1);
}
m_CopyDepthPass.renderPassEvent = copyDepthPassEvent;
// In case we are making the copy depth pass earlier, we need to force set these variables to disable
// depth resolve as the render pass event itself has been moved earlier and it's not possible anymore
if (copyDepthPassEvent < RenderPassEvent.AfterRenderingTransparents)
{
m_CopyDepthPass.m_CopyResolvedDepth = false;
m_CopyDepthMode = CopyDepthMode.AfterOpaques;
}
}
else if (cameraHasPostProcessingWithDepth || isSceneViewOrPreviewCamera || isGizmosEnabled)
{
// If only post process requires depth texture, we can re-use depth buffer from main geometry pass instead of enqueuing a depth copy pass, but no proper API to do that for now, so resort to depth copy pass for now
m_CopyDepthPass.renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
}
createColorTexture |= RequiresIntermediateColorTexture(ref cameraData);
createColorTexture |= renderPassInputs.requiresColorTexture;
createColorTexture |= renderPassInputs.requiresColorTextureCreated;
createColorTexture &= !isPreviewCamera;
// If camera requires depth and there's no depth pre-pass we create a depth texture that can be read later by effect requiring it.
// When deferred renderer is enabled, we must always create a depth texture and CANNOT use BuiltinRenderTextureType.CameraTarget. This is to get
// around a bug where during gbuffer pass (MRT pass), the camera depth attachment is correctly bound, but during
// deferred pass ("camera color" + "camera depth"), the implicit depth surface of "camera color" is used instead of "camera depth",
// because BuiltinRenderTextureType.CameraTarget for depth means there is no explicit depth attachment...
bool createDepthTexture = (requiresDepthTexture || cameraHasPostProcessingWithDepth) && !requiresDepthPrepass;
createDepthTexture |= !cameraData.resolveFinalTarget;
// Deferred renderer always need to access depth buffer.
createDepthTexture |= (this.renderingModeActual == RenderingMode.Deferred && !useRenderPassEnabled);
// Some render cases (e.g. Material previews) have shown we need to create a depth texture when we're forcing a prepass.
createDepthTexture |= useDepthPriming;
// Todo seems like with mrt depth is not taken from first target
createDepthTexture |= (renderingLayerProvidesRenderObjectPass);
#if ENABLE_VR && ENABLE_XR_MODULE
// URP can't handle msaa/size mismatch between depth RT and color RT(for now we create intermediate textures to ensure they match)
if (cameraData.xr.enabled)
createColorTexture |= createDepthTexture;
#endif
#if UNITY_ANDROID || UNITY_WEBGL
// GLES can not use render texture's depth buffer with the color buffer of the backbuffer
// in such case we create a color texture for it too.
// If Vulkan PreTransform is enabled we can't mix backbuffer and intermediate render target due to screen orientation mismatch
if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan || m_VulkanEnablePreTransform)
createColorTexture |= createDepthTexture;
#endif
// If there is any scaling, the color and depth need to be the same resolution and the target texture
// will not be the proper size in this case. Same happens with GameView.
// This introduces the final blit pass.
if (RTHandles.rtHandleProperties.rtHandleScale.x != 1.0f || RTHandles.rtHandleProperties.rtHandleScale.y != 1.0f)
createColorTexture |= createDepthTexture;
if (useRenderPassEnabled || useDepthPriming)
createColorTexture |= createDepthTexture;
//Set rt descriptors so preview camera's have access should it be needed
var colorDescriptor = cameraTargetDescriptor;
colorDescriptor.useMipMap = false;
colorDescriptor.autoGenerateMips = false;
colorDescriptor.depthBufferBits = (int)DepthBits.None;
m_ColorBufferSystem.SetCameraSettings(colorDescriptor, FilterMode.Bilinear);
// Configure all settings require to start a new camera stack (base camera only)
if (cameraData.renderType == CameraRenderType.Base)
{
// Scene filtering redraws the objects on top of the resulting frame. It has to draw directly to the sceneview buffer.
bool sceneViewFilterEnabled = camera.sceneViewFilterMode == Camera.SceneViewFilterMode.ShowFiltered;
bool intermediateRenderTexture = (createColorTexture || createDepthTexture) && !sceneViewFilterEnabled;
// RTHandles do not support combining color and depth in the same texture so we create them separately
// Should be independent from filtered scene view
createDepthTexture |= createColorTexture;
RenderTargetIdentifier targetId = BuiltinRenderTextureType.CameraTarget;
#if ENABLE_VR && ENABLE_XR_MODULE
if (cameraData.xr.enabled)
targetId = cameraData.xr.renderTarget;
#endif
if (m_XRTargetHandleAlias == null)
{
m_XRTargetHandleAlias = RTHandles.Alloc(targetId);
}
else if (m_XRTargetHandleAlias.nameID != targetId)
{
RTHandleStaticHelpers.SetRTHandleUserManagedWrapper(ref m_XRTargetHandleAlias, targetId);
}
// Doesn't create texture for Overlay cameras as they are already overlaying on top of created textures.
if (intermediateRenderTexture)
{
CreateCameraRenderTarget(context, ref cameraTargetDescriptor, useDepthPriming, cmd, ref cameraData);
if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11)
cmd.CopyTexture(m_CameraDepthAttachment, m_CameraDepthAttachment_D3d_11);
}
m_RenderOpaqueForwardPass.m_IsActiveTargetBackBuffer = !intermediateRenderTexture;
m_RenderTransparentForwardPass.m_IsActiveTargetBackBuffer = !intermediateRenderTexture;
m_DrawSkyboxPass.m_IsActiveTargetBackBuffer = !intermediateRenderTexture;
#if ENABLE_VR && ENABLE_XR_MODULE
m_XROcclusionMeshPass.m_IsActiveTargetBackBuffer = !intermediateRenderTexture;
#endif
m_ActiveCameraColorAttachment = createColorTexture ? m_ColorBufferSystem.PeekBackBuffer() : m_XRTargetHandleAlias;
m_ActiveCameraDepthAttachment = createDepthTexture ? m_CameraDepthAttachment : m_XRTargetHandleAlias;
}
else
{
cameraData.baseCamera.TryGetComponent<UniversalAdditionalCameraData>(out var baseCameraData);
var baseRenderer = (UniversalRenderer)baseCameraData.scriptableRenderer;
if (m_ColorBufferSystem != baseRenderer.m_ColorBufferSystem)
{
m_ColorBufferSystem.Dispose();
m_ColorBufferSystem = baseRenderer.m_ColorBufferSystem;
}
m_ActiveCameraColorAttachment = m_ColorBufferSystem.PeekBackBuffer();
m_ActiveCameraDepthAttachment = baseRenderer.m_ActiveCameraDepthAttachment;
m_XRTargetHandleAlias = baseRenderer.m_XRTargetHandleAlias;
}
if (rendererFeatures.Count != 0 && !isPreviewCamera)
ConfigureCameraColorTarget(m_ColorBufferSystem.PeekBackBuffer());
bool copyColorPass = renderingData.cameraData.requiresOpaqueTexture || renderPassInputs.requiresColorTexture;
// Check the createColorTexture logic above: intermediate color texture is not available for preview cameras.
// Because intermediate color is not available and copyColor pass requires it, we disable CopyColor pass here.
copyColorPass &= !isPreviewCamera;
// Assign camera targets (color and depth)
ConfigureCameraTarget(m_ActiveCameraColorAttachment, m_ActiveCameraDepthAttachment);
bool hasPassesAfterPostProcessing = activeRenderPassQueue.Find(x => x.renderPassEvent == RenderPassEvent.AfterRenderingPostProcessing) != null;
if (mainLightShadows)
EnqueuePass(m_MainLightShadowCasterPass);
if (additionalLightShadows)
EnqueuePass(m_AdditionalLightsShadowCasterPass);
bool requiresDepthCopyPass = !requiresDepthPrepass
&& (renderingData.cameraData.requiresDepthTexture || cameraHasPostProcessingWithDepth || renderPassInputs.requiresDepthTexture)
&& createDepthTexture;
if ((DebugHandler != null) && DebugHandler.IsActiveForCamera(ref cameraData))
{
DebugHandler.TryGetFullscreenDebugMode(out var fullScreenMode);
if (fullScreenMode == DebugFullScreenMode.Depth)
{
requiresDepthPrepass = true;
}
if (!DebugHandler.IsLightingActive)
{
mainLightShadows = false;
additionalLightShadows = false;
if (!isSceneViewOrPreviewCamera)
{
requiresDepthPrepass = false;
useDepthPriming = false;
generateColorGradingLUT = false;
copyColorPass = false;
requiresDepthCopyPass = false;
}
}
if (useRenderPassEnabled)
useRenderPassEnabled = DebugHandler.IsRenderPassSupported;
}
cameraData.renderer.useDepthPriming = useDepthPriming;
if (this.renderingModeActual == RenderingMode.Deferred)
{
if (m_DeferredLights.UseRenderPass && (RenderPassEvent.AfterRenderingGbuffer == renderPassInputs.requiresDepthNormalAtEvent || !useRenderPassEnabled))
m_DeferredLights.DisableFramebufferFetchInput();
}
// Allocate m_DepthTexture if used
if ((this.renderingModeActual == RenderingMode.Deferred && !this.useRenderPassEnabled) || requiresDepthPrepass || requiresDepthCopyPass)
{
var depthDescriptor = cameraTargetDescriptor;
if ((requiresDepthPrepass && this.renderingModeActual != RenderingMode.Deferred) || !RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R32_SFloat, FormatUsage.Render))
{
depthDescriptor.graphicsFormat = GraphicsFormat.None;
depthDescriptor.depthStencilFormat = k_DepthStencilFormat;
depthDescriptor.depthBufferBits = k_DepthBufferBits;
}
else
{
depthDescriptor.graphicsFormat = GraphicsFormat.R32_SFloat;
depthDescriptor.depthStencilFormat = GraphicsFormat.None;
depthDescriptor.depthBufferBits = 0;
}
depthDescriptor.msaaSamples = 1;// Depth-Only pass don't use MSAA
RenderingUtils.ReAllocateIfNeeded(ref m_DepthTexture, depthDescriptor, FilterMode.Point, wrapMode: TextureWrapMode.Clamp, name: "_CameraDepthTexture");
cmd.SetGlobalTexture(m_DepthTexture.name, m_DepthTexture.nameID);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
}
if (requiresRenderingLayer || (renderingModeActual == RenderingMode.Deferred && m_DeferredLights.UseRenderingLayers))
{
ref var renderingLayersTexture = ref m_DecalLayersTexture;
string renderingLayersTextureName = "_CameraRenderingLayersTexture";
if (this.renderingModeActual == RenderingMode.Deferred && m_DeferredLights.UseRenderingLayers)
{
renderingLayersTexture = ref m_DeferredLights.GbufferAttachments[(int)m_DeferredLights.GBufferRenderingLayers];
renderingLayersTextureName = renderingLayersTexture.name;
}
var renderingLayersDescriptor = cameraTargetDescriptor;
renderingLayersDescriptor.depthBufferBits = 0;
// Never have MSAA on this depth texture. When doing MSAA depth priming this is the texture that is resolved to and used for post-processing.
if (!renderingLayerProvidesRenderObjectPass)
renderingLayersDescriptor.msaaSamples = 1;// Depth-Only pass don't use MSAA
// Find compatible render-target format for storing normals.
// Shader code outputs normals in signed format to be compatible with deferred gbuffer layout.
// Deferred gbuffer format is signed so that normals can be blended for terrain geometry.
if (this.renderingModeActual == RenderingMode.Deferred && m_DeferredLights.UseRenderingLayers)
renderingLayersDescriptor.graphicsFormat = m_DeferredLights.GetGBufferFormat(m_DeferredLights.GBufferRenderingLayers); // the one used by the gbuffer.
else
renderingLayersDescriptor.graphicsFormat = RenderingLayerUtils.GetFormat(renderingLayerMaskSize);
if (renderingModeActual == RenderingMode.Deferred && m_DeferredLights.UseRenderingLayers)
{
m_DeferredLights.ReAllocateGBufferIfNeeded(renderingLayersDescriptor, (int)m_DeferredLights.GBufferRenderingLayers);
}
else
{
RenderingUtils.ReAllocateIfNeeded(ref renderingLayersTexture, renderingLayersDescriptor, FilterMode.Point, TextureWrapMode.Clamp, name: renderingLayersTextureName);
}
cmd.SetGlobalTexture(renderingLayersTexture.name, renderingLayersTexture.nameID);
RenderingLayerUtils.SetupProperties(cmd, renderingLayerMaskSize);
if (this.renderingModeActual == RenderingMode.Deferred) // As this is requested by render pass we still want to set it
cmd.SetGlobalTexture("_CameraRenderingLayersTexture", renderingLayersTexture.nameID);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
}
// Allocate normal texture if used
if (requiresDepthPrepass && renderPassInputs.requiresNormalsTexture)
{
ref var normalsTexture = ref m_NormalsTexture;
string normalsTextureName = "_CameraNormalsTexture";
if (this.renderingModeActual == RenderingMode.Deferred)
{
normalsTexture = ref m_DeferredLights.GbufferAttachments[(int)m_DeferredLights.GBufferNormalSmoothnessIndex];
normalsTextureName = normalsTexture.name;
}
var normalDescriptor = cameraTargetDescriptor;
normalDescriptor.depthBufferBits = 0;
// Never have MSAA on this depth texture. When doing MSAA depth priming this is the texture that is resolved to and used for post-processing.
normalDescriptor.msaaSamples = useDepthPriming ? cameraTargetDescriptor.msaaSamples : 1;// Depth-Only passes don't use MSAA, unless depth priming is enabled
// Find compatible render-target format for storing normals.
// Shader code outputs normals in signed format to be compatible with deferred gbuffer layout.
// Deferred gbuffer format is signed so that normals can be blended for terrain geometry.
if (this.renderingModeActual == RenderingMode.Deferred)
normalDescriptor.graphicsFormat = m_DeferredLights.GetGBufferFormat(m_DeferredLights.GBufferNormalSmoothnessIndex); // the one used by the gbuffer.
else
normalDescriptor.graphicsFormat = DepthNormalOnlyPass.GetGraphicsFormat();
if (this.renderingModeActual == RenderingMode.Deferred)
{
m_DeferredLights.ReAllocateGBufferIfNeeded(normalDescriptor, (int)m_DeferredLights.GBufferNormalSmoothnessIndex);
}
else
{
RenderingUtils.ReAllocateIfNeeded(ref normalsTexture, normalDescriptor, FilterMode.Point, TextureWrapMode.Clamp, name: normalsTextureName);
}
cmd.SetGlobalTexture(normalsTexture.name, normalsTexture.nameID);
if (this.renderingModeActual == RenderingMode.Deferred) // As this is requested by render pass we still want to set it
cmd.SetGlobalTexture("_CameraNormalsTexture", normalsTexture.nameID);