forked from Unity-Technologies/PostProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostProcessLayer.cs
More file actions
981 lines (799 loc) · 39.3 KB
/
PostProcessLayer.cs
File metadata and controls
981 lines (799 loc) · 39.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions;
namespace UnityEngine.Rendering.PostProcessing
{
#if UNITY_2017_2_OR_NEWER
using XRSettings = UnityEngine.XR.XRSettings;
#elif UNITY_5_6_OR_NEWER
using XRSettings = UnityEngine.VR.VRSettings;
#endif
// TODO: XMLDoc everything (?)
[DisallowMultipleComponent, ExecuteInEditMode, ImageEffectAllowedInSceneView]
[AddComponentMenu("Rendering/Post-process Layer", 1000)]
[RequireComponent(typeof(Camera))]
public sealed class PostProcessLayer : MonoBehaviour
{
public enum Antialiasing
{
None,
FastApproximateAntialiasing,
SubpixelMorphologicalAntialiasing,
TemporalAntialiasing
}
// Settings
public Transform volumeTrigger;
public LayerMask volumeLayer;
public bool stopNaNPropagation = true;
// Builtins / hardcoded effects that don't benefit from volume blending
public Antialiasing antialiasingMode = Antialiasing.None;
public TemporalAntialiasing temporalAntialiasing;
public SubpixelMorphologicalAntialiasing subpixelMorphologicalAntialiasing;
public FastApproximateAntialiasing fastApproximateAntialiasing;
public DeferredFog fog;
public Dithering dithering;
public PostProcessDebugLayer debugLayer;
[SerializeField]
PostProcessResources m_Resources;
// UI states
[SerializeField] bool m_ShowToolkit;
[SerializeField] bool m_ShowCustomSorter;
// Will stop applying post-processing effects just before color grading is applied
// Currently used to export to exr without color grading
public bool breakBeforeColorGrading = false;
// Pre-ordered custom user effects
// These are automatically populated and made to work properly with the serialization
// system AND the editor. Modify at your own risk.
[Serializable]
public sealed class SerializedBundleRef
{
// We can't serialize Type so use assemblyQualifiedName instead, we only need this at
// init time anyway so it's fine
public string assemblyQualifiedName;
// Not serialized, is set/reset when deserialization kicks in
public PostProcessBundle bundle;
}
[SerializeField]
List<SerializedBundleRef> m_BeforeTransparentBundles;
[SerializeField]
List<SerializedBundleRef> m_BeforeStackBundles;
[SerializeField]
List<SerializedBundleRef> m_AfterStackBundles;
public Dictionary<PostProcessEvent, List<SerializedBundleRef>> sortedBundles { get; private set; }
// We need to keep track of bundle initialization because for some obscure reason, on
// assembly reload a MonoBehavior's Editor OnEnable will be called BEFORE the MonoBehavior's
// own OnEnable... So we'll use it to pre-init bundles if the layer inspector is opened and
// the component hasn't been enabled yet.
public bool haveBundlesBeenInited { get; private set; }
// Settings/Renderer bundles mapped to settings types
Dictionary<Type, PostProcessBundle> m_Bundles;
PropertySheetFactory m_PropertySheetFactory;
CommandBuffer m_LegacyCmdBufferBeforeReflections;
CommandBuffer m_LegacyCmdBufferBeforeLighting;
CommandBuffer m_LegacyCmdBufferOpaque;
CommandBuffer m_LegacyCmdBuffer;
Camera m_Camera;
PostProcessRenderContext m_CurrentContext;
LogHistogram m_LogHistogram;
bool m_SettingsUpdateNeeded = true;
bool m_IsRenderingInSceneView = false;
TargetPool m_TargetPool;
bool m_NaNKilled = false;
// Recycled list - used to reduce GC stress when gathering active effects in a bundle list
// on each frame
readonly List<PostProcessEffectRenderer> m_ActiveEffects = new List<PostProcessEffectRenderer>();
readonly List<RenderTargetIdentifier> m_Targets = new List<RenderTargetIdentifier>();
void OnEnable()
{
Init(null);
if (!haveBundlesBeenInited)
InitBundles();
m_LogHistogram = new LogHistogram();
m_PropertySheetFactory = new PropertySheetFactory();
m_TargetPool = new TargetPool();
debugLayer.OnEnable();
if (RuntimeUtilities.scriptableRenderPipelineActive)
return;
InitLegacy();
}
void InitLegacy()
{
m_LegacyCmdBufferBeforeReflections = new CommandBuffer { name = "Deferred Ambient Occlusion" };
m_LegacyCmdBufferBeforeLighting = new CommandBuffer { name = "Deferred Ambient Occlusion" };
m_LegacyCmdBufferOpaque = new CommandBuffer { name = "Opaque Only Post-processing" };
m_LegacyCmdBuffer = new CommandBuffer { name = "Post-processing" };
m_Camera = GetComponent<Camera>();
m_Camera.forceIntoRenderTexture = true; // Needed when running Forward / LDR / No MSAA
m_Camera.AddCommandBuffer(CameraEvent.BeforeReflections, m_LegacyCmdBufferBeforeReflections);
m_Camera.AddCommandBuffer(CameraEvent.BeforeLighting, m_LegacyCmdBufferBeforeLighting);
m_Camera.AddCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, m_LegacyCmdBufferOpaque);
m_Camera.AddCommandBuffer(CameraEvent.BeforeImageEffects, m_LegacyCmdBuffer);
// Internal context used if no SRP is set
m_CurrentContext = new PostProcessRenderContext();
}
public void Init(PostProcessResources resources)
{
if (resources != null) m_Resources = resources;
RuntimeUtilities.CreateIfNull(ref temporalAntialiasing);
RuntimeUtilities.CreateIfNull(ref subpixelMorphologicalAntialiasing);
RuntimeUtilities.CreateIfNull(ref fastApproximateAntialiasing);
RuntimeUtilities.CreateIfNull(ref dithering);
RuntimeUtilities.CreateIfNull(ref fog);
RuntimeUtilities.CreateIfNull(ref debugLayer);
}
public void InitBundles()
{
if (haveBundlesBeenInited)
return;
// Create these lists only once, the serialization system will take over after that
RuntimeUtilities.CreateIfNull(ref m_BeforeTransparentBundles);
RuntimeUtilities.CreateIfNull(ref m_BeforeStackBundles);
RuntimeUtilities.CreateIfNull(ref m_AfterStackBundles);
// Create a bundle for each effect type
m_Bundles = new Dictionary<Type, PostProcessBundle>();
foreach (var type in PostProcessManager.instance.settingsTypes.Keys)
{
var settings = (PostProcessEffectSettings)ScriptableObject.CreateInstance(type);
var bundle = new PostProcessBundle(settings);
m_Bundles.Add(type, bundle);
}
// Update sorted lists with newly added or removed effects in the assemblies
UpdateBundleSortList(m_BeforeTransparentBundles, PostProcessEvent.BeforeTransparent);
UpdateBundleSortList(m_BeforeStackBundles, PostProcessEvent.BeforeStack);
UpdateBundleSortList(m_AfterStackBundles, PostProcessEvent.AfterStack);
// Push all sorted lists in a dictionary for easier access
sortedBundles = new Dictionary<PostProcessEvent, List<SerializedBundleRef>>(new PostProcessEventComparer())
{
{ PostProcessEvent.BeforeTransparent, m_BeforeTransparentBundles },
{ PostProcessEvent.BeforeStack, m_BeforeStackBundles },
{ PostProcessEvent.AfterStack, m_AfterStackBundles }
};
// Done
haveBundlesBeenInited = true;
}
void UpdateBundleSortList(List<SerializedBundleRef> sortedList, PostProcessEvent evt)
{
// First get all effects associated with the injection point
var effects = m_Bundles.Where(kvp => kvp.Value.attribute.eventType == evt && !kvp.Value.attribute.builtinEffect)
.Select(kvp => kvp.Value)
.ToList();
// Remove types that don't exist anymore
sortedList.RemoveAll(x =>
{
string searchStr = x.assemblyQualifiedName;
return !effects.Exists(b => b.settings.GetType().AssemblyQualifiedName == searchStr);
});
// Add new ones
foreach (var effect in effects)
{
string typeName = effect.settings.GetType().AssemblyQualifiedName;
if (!sortedList.Exists(b => b.assemblyQualifiedName == typeName))
{
var sbr = new SerializedBundleRef { assemblyQualifiedName = typeName };
sortedList.Add(sbr);
}
}
// Link internal references
foreach (var effect in sortedList)
{
string typeName = effect.assemblyQualifiedName;
var bundle = effects.Find(b => b.settings.GetType().AssemblyQualifiedName == typeName);
effect.bundle = bundle;
}
}
void OnDisable()
{
if (!RuntimeUtilities.scriptableRenderPipelineActive)
{
m_Camera.RemoveCommandBuffer(CameraEvent.BeforeReflections, m_LegacyCmdBufferBeforeReflections);
m_Camera.RemoveCommandBuffer(CameraEvent.BeforeLighting, m_LegacyCmdBufferBeforeLighting);
m_Camera.RemoveCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, m_LegacyCmdBufferOpaque);
m_Camera.RemoveCommandBuffer(CameraEvent.BeforeImageEffects, m_LegacyCmdBuffer);
}
temporalAntialiasing.Release();
m_LogHistogram.Release();
foreach (var bundle in m_Bundles.Values)
bundle.Release();
m_Bundles.Clear();
m_PropertySheetFactory.Release();
if (debugLayer != null)
debugLayer.OnDisable();
// Might be an issue if several layers are blending in the same frame...
TextureLerper.instance.Clear();
haveBundlesBeenInited = false;
}
// Called everytime the user resets the component from the inspector and more importantly
// the first time it's added to a GameObject. As we don't have added/removed event for
// components, this will do fine
void Reset()
{
volumeTrigger = transform;
}
void OnPreCull()
{
// Unused in scriptable render pipelines
if (RuntimeUtilities.scriptableRenderPipelineActive)
return;
if (m_Camera == null || m_CurrentContext == null)
InitLegacy();
// Resets the projection matrix from previous frame in case TAA was enabled.
// We also need to force reset the non-jittered projection matrix here as it's not done
// when ResetProjectionMatrix() is called and will break transparent rendering if TAA
// is switched off and the FOV or any other camera property changes.
m_Camera.ResetProjectionMatrix();
m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
if (m_Camera.stereoEnabled)
{
m_Camera.ResetStereoProjectionMatrices();
Shader.SetGlobalFloat(ShaderIDs.RenderViewportScaleFactor, XRSettings.renderViewportScale);
}
else
{
Shader.SetGlobalFloat(ShaderIDs.RenderViewportScaleFactor, 1.0f);
}
BuildCommandBuffers();
}
void OnPreRender()
{
// Unused in scriptable render pipelines
// Only needed for multi-pass stereo right eye
if (RuntimeUtilities.scriptableRenderPipelineActive ||
(m_Camera.stereoActiveEye != Camera.MonoOrStereoscopicEye.Right))
return;
BuildCommandBuffers();
}
void BuildCommandBuffers()
{
var context = m_CurrentContext;
var sourceFormat = m_Camera.allowHDR ? RuntimeUtilities.defaultHDRRenderTextureFormat : RenderTextureFormat.Default;
if (!RuntimeUtilities.isFloatingPointFormat(sourceFormat))
m_NaNKilled = true;
context.Reset();
context.camera = m_Camera;
context.sourceFormat = sourceFormat;
// TODO: Investigate retaining command buffers on XR multi-pass right eye
m_LegacyCmdBufferBeforeReflections.Clear();
m_LegacyCmdBufferBeforeLighting.Clear();
m_LegacyCmdBufferOpaque.Clear();
m_LegacyCmdBuffer.Clear();
SetupContext(context);
context.command = m_LegacyCmdBufferOpaque;
TextureLerper.instance.BeginFrame(context);
UpdateSettingsIfNeeded(context);
// Fog effect settings
var fogBundle = GetBundle<Fog>();
if (fogBundle != null)
{
fogBundle.CastRenderer<FogRenderer>().ApplySettings(context, ref fog.excludeSkybox);
}
// Lighting & opaque-only effects
var aoBundle = GetBundle<AmbientOcclusion>();
var aoSettings = aoBundle.CastSettings<AmbientOcclusion>();
var aoRenderer = aoBundle.CastRenderer<AmbientOcclusionRenderer>();
bool aoSupported = aoSettings.IsEnabledAndSupported(context);
bool aoAmbientOnly = aoRenderer.IsAmbientOnly(context);
bool isAmbientOcclusionDeferred = aoSupported && aoAmbientOnly;
bool isAmbientOcclusionOpaque = aoSupported && !aoAmbientOnly;
var ssrBundle = GetBundle<ScreenSpaceReflections>();
var ssrSettings = ssrBundle.settings;
var ssrRenderer = ssrBundle.renderer;
bool isScreenSpaceReflectionsActive = ssrSettings.IsEnabledAndSupported(context);
// Ambient-only AO is a special case and has to be done in separate command buffers
if (isAmbientOcclusionDeferred)
{
var ao = aoRenderer.Get();
// Render as soon as possible - should be done async in SRPs when available
context.command = m_LegacyCmdBufferBeforeReflections;
ao.RenderAmbientOnly(context);
// Composite with GBuffer right before the lighting pass
context.command = m_LegacyCmdBufferBeforeLighting;
ao.CompositeAmbientOnly(context);
}
else if (isAmbientOcclusionOpaque)
{
context.command = m_LegacyCmdBufferOpaque;
aoRenderer.Get().RenderAfterOpaque(context);
}
bool isFogActive = fog.IsEnabledAndSupported(context);
bool hasCustomOpaqueOnlyEffects = HasOpaqueOnlyEffects(context);
int opaqueOnlyEffects = 0;
opaqueOnlyEffects += isScreenSpaceReflectionsActive ? 1 : 0;
opaqueOnlyEffects += isFogActive ? 1 : 0;
opaqueOnlyEffects += hasCustomOpaqueOnlyEffects ? 1 : 0;
// This works on right eye because it is resolved/populated at runtime
var cameraTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
if (opaqueOnlyEffects > 0)
{
var cmd = m_LegacyCmdBufferOpaque;
context.command = cmd;
// We need to use the internal Blit method to copy the camera target or it'll fail
// on tiled GPU as it won't be able to resolve
int tempTarget0 = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(cmd, tempTarget0, 0, sourceFormat);
cmd.Blit(cameraTarget, tempTarget0);
context.source = tempTarget0;
int tempTarget1 = -1;
if (opaqueOnlyEffects > 1)
{
tempTarget1 = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(cmd, tempTarget1, 0, sourceFormat);
context.destination = tempTarget1;
}
else context.destination = cameraTarget;
if (isScreenSpaceReflectionsActive)
{
ssrRenderer.Render(context);
opaqueOnlyEffects--;
var prevSource = context.source;
context.source = context.destination;
context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
}
if (isFogActive)
{
fog.Render(context);
opaqueOnlyEffects--;
var prevSource = context.source;
context.source = context.destination;
context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
}
if (hasCustomOpaqueOnlyEffects)
RenderOpaqueOnly(context);
if (opaqueOnlyEffects > 1)
cmd.ReleaseTemporaryRT(tempTarget1);
cmd.ReleaseTemporaryRT(tempTarget0);
}
// Post-transparency stack
// Same as before, first blit needs to use the builtin Blit command to properly handle
// tiled GPUs
int tempRt = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(m_LegacyCmdBuffer, tempRt, 0, sourceFormat, RenderTextureReadWrite.sRGB);
m_LegacyCmdBuffer.Blit(cameraTarget, tempRt, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
if (!m_NaNKilled)
m_NaNKilled = stopNaNPropagation;
context.command = m_LegacyCmdBuffer;
context.source = tempRt;
context.destination = cameraTarget;
Render(context);
m_LegacyCmdBuffer.ReleaseTemporaryRT(tempRt);
}
void OnPostRender()
{
// Unused in scriptable render pipelines
if (RuntimeUtilities.scriptableRenderPipelineActive)
return;
// Restore original fog settings
var fogBundle = GetBundle<Fog>();
if (fogBundle != null)
{
fogBundle.CastRenderer<FogRenderer>().RestoreSettings(ref fog.excludeSkybox);
}
if (m_CurrentContext.IsTemporalAntialiasingActive())
{
m_Camera.ResetProjectionMatrix();
if (m_CurrentContext.stereoActive)
{
if (RuntimeUtilities.isSinglePassStereoEnabled || m_Camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
m_Camera.ResetStereoProjectionMatrices();
}
}
}
public PostProcessBundle GetBundle<T>()
where T : PostProcessEffectSettings
{
return GetBundle(typeof(T));
}
public PostProcessBundle GetBundle(Type settingsType)
{
Assert.IsTrue(m_Bundles.ContainsKey(settingsType), "Invalid type");
return m_Bundles[settingsType];
}
public T GetSettings<T>()
where T : PostProcessEffectSettings
{
return GetBundle<T>().CastSettings<T>();
}
public void BakeMSVOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifier destination, RenderTargetIdentifier? depthMap, bool invert)
{
var bundle = GetBundle<AmbientOcclusion>();
var renderer = bundle.CastRenderer<AmbientOcclusionRenderer>().GetMultiScaleVO();
renderer.SetResources(m_Resources);
renderer.GenerateAOMap(cmd, camera, destination, depthMap, invert);
}
internal void OverrideSettings(List<PostProcessEffectSettings> baseSettings, float interpFactor)
{
// Go through all settings & overriden parameters for the given volume and lerp values
foreach (var settings in baseSettings)
{
if (!settings.active)
continue;
var target = GetBundle(settings.GetType()).settings;
int count = settings.parameters.Count;
for (int i = 0; i < count; i++)
{
var toParam = settings.parameters[i];
if (toParam.overrideState)
{
var fromParam = target.parameters[i];
fromParam.Interp(fromParam, toParam, interpFactor);
}
}
}
}
// In the legacy render loop you have to explicitely set flags on camera to tell that you
// need depth, depth+normals or motion vectors... This won't have any effect with most
// scriptable render pipelines.
void SetLegacyCameraFlags(PostProcessRenderContext context)
{
var flags = context.camera.depthTextureMode;
foreach (var bundle in m_Bundles)
{
if (bundle.Value.settings.IsEnabledAndSupported(context))
flags |= bundle.Value.renderer.GetCameraFlags();
}
// Special case for AA & lighting effects
if (context.IsTemporalAntialiasingActive())
flags |= temporalAntialiasing.GetCameraFlags();
if (fog.IsEnabledAndSupported(context))
flags |= fog.GetCameraFlags();
if (debugLayer.debugOverlay != DebugOverlay.None)
flags |= debugLayer.GetCameraFlags();
context.camera.depthTextureMode = flags;
}
// Call this function whenever you need to reset any temporal effect (TAA, Motion Blur etc).
// Mainly used when doing camera cuts.
public void ResetHistory()
{
foreach (var bundle in m_Bundles)
bundle.Value.ResetHistory();
temporalAntialiasing.ResetHistory();
}
public bool HasOpaqueOnlyEffects(PostProcessRenderContext context)
{
return HasActiveEffects(PostProcessEvent.BeforeTransparent, context);
}
public bool HasActiveEffects(PostProcessEvent evt, PostProcessRenderContext context)
{
var list = sortedBundles[evt];
foreach (var item in list)
{
if (item.bundle.settings.IsEnabledAndSupported(context))
return true;
}
return false;
}
void SetupContext(PostProcessRenderContext context)
{
m_IsRenderingInSceneView = context.camera.cameraType == CameraType.SceneView;
context.isSceneView = m_IsRenderingInSceneView;
context.resources = m_Resources;
context.propertySheets = m_PropertySheetFactory;
context.debugLayer = debugLayer;
context.antialiasing = antialiasingMode;
context.temporalAntialiasing = temporalAntialiasing;
context.logHistogram = m_LogHistogram;
SetLegacyCameraFlags(context);
// Prepare debug overlay
debugLayer.SetFrameSize(context.width, context.height);
// Unsafe to keep this around but we need it for OnGUI events for debug views
// Will be removed eventually
m_CurrentContext = context;
}
void UpdateSettingsIfNeeded(PostProcessRenderContext context)
{
if (m_SettingsUpdateNeeded)
{
context.command.BeginSample("VolumeBlending");
PostProcessManager.instance.UpdateSettings(this, context.camera);
context.command.EndSample("VolumeBlending");
m_TargetPool.Reset();
// TODO: fix me once VR support is in SRP
// Needed in SRP so that _RenderViewportScaleFactor isn't 0
if (RuntimeUtilities.scriptableRenderPipelineActive)
Shader.SetGlobalFloat(ShaderIDs.RenderViewportScaleFactor, 1f);
}
m_SettingsUpdateNeeded = false;
}
// Renders before-transparent effects.
// Make sure you check `HasOpaqueOnlyEffects()` before calling this method as it won't
// automatically blit source into destination if no opaque effects are active.
public void RenderOpaqueOnly(PostProcessRenderContext context)
{
if (RuntimeUtilities.scriptableRenderPipelineActive)
SetupContext(context);
TextureLerper.instance.BeginFrame(context);
// Update & override layer settings first (volume blending), will only be done once per
// frame, either here or in Render() if there isn't any opaque-only effect to render.
UpdateSettingsIfNeeded(context);
RenderList(sortedBundles[PostProcessEvent.BeforeTransparent], context, "OpaqueOnly");
}
// Renders everything not opaque-only
//
// Current order of operation is as following:
// 1. Pre-stack
// 2. Built-in stack
// 3. Post-stack
// 4. Built-in final pass
//
// Final pass should be skipped when outputting to a HDR display.
public void Render(PostProcessRenderContext context)
{
if (RuntimeUtilities.scriptableRenderPipelineActive)
SetupContext(context);
TextureLerper.instance.BeginFrame(context);
var cmd = context.command;
// Update & override layer settings first (volume blending) if the opaque only pass
// hasn't been called this frame.
UpdateSettingsIfNeeded(context);
// Do a NaN killing pass if needed
int lastTarget = -1;
if (stopNaNPropagation && !m_NaNKilled)
{
lastTarget = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(cmd, lastTarget, 0, context.sourceFormat);
cmd.BlitFullscreenTriangle(context.source, lastTarget, RuntimeUtilities.copySheet, 1);
context.source = lastTarget;
m_NaNKilled = true;
}
// Do temporal anti-aliasing first
if (context.IsTemporalAntialiasingActive())
{
if (!RuntimeUtilities.scriptableRenderPipelineActive)
{
if (context.stereoActive)
{
// We only need to configure all of this once for stereo, during OnPreCull
if (context.camera.stereoActiveEye != Camera.MonoOrStereoscopicEye.Right)
temporalAntialiasing.ConfigureStereoJitteredProjectionMatrices(context);
}
else
{
temporalAntialiasing.ConfigureJitteredProjectionMatrix(context);
}
}
var taaTarget = m_TargetPool.Get();
var finalDestination = context.destination;
context.GetScreenSpaceTemporaryRT(cmd, taaTarget, 0, context.sourceFormat);
context.destination = taaTarget;
temporalAntialiasing.Render(context);
context.source = taaTarget;
context.destination = finalDestination;
if (lastTarget > -1)
cmd.ReleaseTemporaryRT(lastTarget);
lastTarget = taaTarget;
}
bool hasBeforeStackEffects = HasActiveEffects(PostProcessEvent.BeforeStack, context);
bool hasAfterStackEffects = HasActiveEffects(PostProcessEvent.AfterStack, context) && !breakBeforeColorGrading;
bool needsFinalPass = (hasAfterStackEffects
|| (antialiasingMode == Antialiasing.FastApproximateAntialiasing) || (antialiasingMode == Antialiasing.SubpixelMorphologicalAntialiasing && subpixelMorphologicalAntialiasing.IsSupported()))
&& !breakBeforeColorGrading;
// Right before the builtin stack
if (hasBeforeStackEffects)
lastTarget = RenderInjectionPoint(PostProcessEvent.BeforeStack, context, "BeforeStack", lastTarget);
// Builtin stack
lastTarget = RenderBuiltins(context, !needsFinalPass, lastTarget);
// After the builtin stack but before the final pass (before FXAA & Dithering)
if (hasAfterStackEffects)
lastTarget = RenderInjectionPoint(PostProcessEvent.AfterStack, context, "AfterStack", lastTarget);
// And close with the final pass
if (needsFinalPass)
RenderFinalPass(context, lastTarget);
// Render debug monitors & overlay if requested
debugLayer.RenderSpecialOverlays(context);
debugLayer.RenderMonitors(context);
// End frame cleanup
TextureLerper.instance.EndFrame();
debugLayer.EndFrame();
m_SettingsUpdateNeeded = true;
m_NaNKilled = false;
}
int RenderInjectionPoint(PostProcessEvent evt, PostProcessRenderContext context, string marker, int releaseTargetAfterUse = -1)
{
int tempTarget = m_TargetPool.Get();
var finalDestination = context.destination;
var cmd = context.command;
context.GetScreenSpaceTemporaryRT(cmd, tempTarget, 0, context.sourceFormat);
context.destination = tempTarget;
RenderList(sortedBundles[evt], context, marker);
context.source = tempTarget;
context.destination = finalDestination;
if (releaseTargetAfterUse > -1)
cmd.ReleaseTemporaryRT(releaseTargetAfterUse);
return tempTarget;
}
void RenderList(List<SerializedBundleRef> list, PostProcessRenderContext context, string marker)
{
var cmd = context.command;
cmd.BeginSample(marker);
// First gather active effects - we need this to manage render targets more efficiently
m_ActiveEffects.Clear();
for (int i = 0; i < list.Count; i++)
{
var effect = list[i].bundle;
if (effect.settings.IsEnabledAndSupported(context))
{
if (!context.isSceneView || (context.isSceneView && effect.attribute.allowInSceneView))
m_ActiveEffects.Add(effect.renderer);
}
}
int count = m_ActiveEffects.Count;
// If there's only one active effect, we can simply execute it and skip the rest
if (count == 1)
{
m_ActiveEffects[0].Render(context);
}
else
{
// Else create the target chain
m_Targets.Clear();
m_Targets.Add(context.source); // First target is always source
int tempTarget1 = m_TargetPool.Get();
int tempTarget2 = m_TargetPool.Get();
for (int i = 0; i < count - 1; i++)
m_Targets.Add(i % 2 == 0 ? tempTarget1 : tempTarget2);
m_Targets.Add(context.destination); // Last target is always destination
// Render
context.GetScreenSpaceTemporaryRT(cmd, tempTarget1, 0, context.sourceFormat);
if (count > 2)
context.GetScreenSpaceTemporaryRT(cmd, tempTarget2, 0, context.sourceFormat);
for (int i = 0; i < count; i++)
{
context.source = m_Targets[i];
context.destination = m_Targets[i + 1];
m_ActiveEffects[i].Render(context);
}
cmd.ReleaseTemporaryRT(tempTarget1);
if (count > 2)
cmd.ReleaseTemporaryRT(tempTarget2);
}
cmd.EndSample(marker);
}
void ApplyFlip(PostProcessRenderContext context, MaterialPropertyBlock properties)
{
if (context.flip && !context.isSceneView)
properties.SetVector(ShaderIDs.UVTransform, new Vector4(1.0f, 1.0f, 0.0f, 0.0f));
else
ApplyDefaultFlip(properties);
}
void ApplyDefaultFlip(MaterialPropertyBlock properties)
{
properties.SetVector(ShaderIDs.UVTransform, SystemInfo.graphicsUVStartsAtTop ? new Vector4(1.0f, -1.0f, 0.0f, 1.0f) : new Vector4(1.0f, 1.0f, 0.0f, 0.0f));
}
int RenderBuiltins(PostProcessRenderContext context, bool isFinalPass, int releaseTargetAfterUse = -1)
{
var uberSheet = context.propertySheets.Get(context.resources.shaders.uber);
uberSheet.ClearKeywords();
uberSheet.properties.Clear();
context.uberSheet = uberSheet;
context.autoExposureTexture = RuntimeUtilities.whiteTexture;
context.bloomBufferNameID = -1;
var cmd = context.command;
cmd.BeginSample("BuiltinStack");
int tempTarget = -1;
var finalDestination = context.destination;
if (!isFinalPass)
{
// Render to an intermediate target as this won't be the final pass
tempTarget = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(cmd, tempTarget, 0, context.sourceFormat);
context.destination = tempTarget;
// Handle FXAA's keep alpha mode
if (antialiasingMode == Antialiasing.FastApproximateAntialiasing && !fastApproximateAntialiasing.keepAlpha)
uberSheet.properties.SetFloat(ShaderIDs.LumaInAlpha, 1f);
}
// Depth of field final combination pass used to be done in Uber which led to artifacts
// when used at the same time as Bloom (because both effects used the same source, so
// the stronger bloom was, the more DoF was eaten away in out of focus areas)
int depthOfFieldTarget = RenderEffect<DepthOfField>(context, true);
// Motion blur is a separate pass - could potentially be done after DoF depending on the
// kind of results you're looking for...
int motionBlurTarget = RenderEffect<MotionBlur>(context, true);
// Prepare exposure histogram if needed
if (ShouldGenerateLogHistogram(context))
m_LogHistogram.Generate(context);
// Uber effects
RenderEffect<AutoExposure>(context);
uberSheet.properties.SetTexture(ShaderIDs.AutoExposureTex, context.autoExposureTexture);
RenderEffect<LensDistortion>(context);
RenderEffect<ChromaticAberration>(context);
RenderEffect<Bloom>(context);
RenderEffect<Vignette>(context);
RenderEffect<Grain>(context);
if (!breakBeforeColorGrading)
RenderEffect<ColorGrading>(context);
if (isFinalPass)
{
uberSheet.EnableKeyword("FINALPASS");
dithering.Render(context);
ApplyFlip(context, uberSheet.properties);
}
else
{
ApplyDefaultFlip(uberSheet.properties);
}
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, 0);
context.source = context.destination;
context.destination = finalDestination;
if (releaseTargetAfterUse > -1) cmd.ReleaseTemporaryRT(releaseTargetAfterUse);
if (motionBlurTarget > -1) cmd.ReleaseTemporaryRT(motionBlurTarget);
if (depthOfFieldTarget > -1) cmd.ReleaseTemporaryRT(depthOfFieldTarget);
if (context.bloomBufferNameID > -1) cmd.ReleaseTemporaryRT(context.bloomBufferNameID);
cmd.EndSample("BuiltinStack");
return tempTarget;
}
// This pass will have to be disabled for HDR screen output as it's an LDR pass
void RenderFinalPass(PostProcessRenderContext context, int releaseTargetAfterUse = -1)
{
var cmd = context.command;
cmd.BeginSample("FinalPass");
if (breakBeforeColorGrading)
{
var sheet = context.propertySheets.Get(context.resources.shaders.discardAlpha);
cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}
else
{
var uberSheet = context.propertySheets.Get(context.resources.shaders.finalPass);
uberSheet.ClearKeywords();
uberSheet.properties.Clear();
context.uberSheet = uberSheet;
int tempTarget = -1;
if (antialiasingMode == Antialiasing.FastApproximateAntialiasing)
{
uberSheet.EnableKeyword(fastApproximateAntialiasing.fastMode
? "FXAA_LOW"
: "FXAA"
);
if (fastApproximateAntialiasing.keepAlpha)
uberSheet.EnableKeyword("FXAA_KEEP_ALPHA");
}
else if (antialiasingMode == Antialiasing.SubpixelMorphologicalAntialiasing && subpixelMorphologicalAntialiasing.IsSupported())
{
tempTarget = m_TargetPool.Get();
var finalDestination = context.destination;
context.GetScreenSpaceTemporaryRT(context.command, tempTarget, 0, context.sourceFormat);
context.destination = tempTarget;
subpixelMorphologicalAntialiasing.Render(context);
context.source = tempTarget;
context.destination = finalDestination;
}
dithering.Render(context);
ApplyFlip(context, uberSheet.properties);
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, 0);
if (tempTarget > -1)
cmd.ReleaseTemporaryRT(tempTarget);
}
if (releaseTargetAfterUse > -1)
cmd.ReleaseTemporaryRT(releaseTargetAfterUse);
cmd.EndSample("FinalPass");
}
int RenderEffect<T>(PostProcessRenderContext context, bool useTempTarget = false)
where T : PostProcessEffectSettings
{
var effect = GetBundle<T>();
if (!effect.settings.IsEnabledAndSupported(context))
return -1;
if (m_IsRenderingInSceneView && !effect.attribute.allowInSceneView)
return -1;
if (!useTempTarget)
{
effect.renderer.Render(context);
return -1;
}
var finalDestination = context.destination;
var tempTarget = m_TargetPool.Get();
context.GetScreenSpaceTemporaryRT(context.command, tempTarget, 0, context.sourceFormat);
context.destination = tempTarget;
effect.renderer.Render(context);
context.source = tempTarget;
context.destination = finalDestination;
return tempTarget;
}
bool ShouldGenerateLogHistogram(PostProcessRenderContext context)
{
bool autoExpo = GetBundle<AutoExposure>().settings.IsEnabledAndSupported(context);
bool lightMeter = debugLayer.lightMeter.IsRequestedAndSupported(context);
return autoExpo || lightMeter;
}
}
}