Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 28da5c0

Browse files
committed
Merged final pass into uber when no FXAA is used and no custom "after stack" effects are enabled for the frame
1 parent e0c3267 commit 28da5c0

File tree

5 files changed

+78
-38
lines changed

5 files changed

+78
-38
lines changed

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -496,31 +496,31 @@ public void Render(PostProcessRenderContext context)
496496
context.destination = finalDestination;
497497
}
498498

499+
bool hasBeforeStackEffects = HasActiveEffects(PostProcessEvent.BeforeStack, context);
500+
bool hasAfterStackEffects = HasActiveEffects(PostProcessEvent.AfterStack, context) && !breakBeforeColorGrading;
501+
bool needsFinalPass = (hasAfterStackEffects || antialiasingMode == Antialiasing.FastApproximateAntialiasing) && !breakBeforeColorGrading;
502+
499503
// Right before the builtin stack
500-
lastTarget = RenderInjectionPoint(PostProcessEvent.BeforeStack, context, "BeforeStack", lastTarget);
504+
if (hasBeforeStackEffects)
505+
lastTarget = RenderInjectionPoint(PostProcessEvent.BeforeStack, context, "BeforeStack", lastTarget);
501506

502507
// Builtin stack
503-
lastTarget = RenderBuiltins(context, lastTarget);
508+
lastTarget = RenderBuiltins(context, !needsFinalPass, lastTarget);
504509

505510
// After the builtin stack but before the final pass (before FXAA & Dithering)
506-
if (!breakBeforeColorGrading)
511+
if (hasAfterStackEffects)
507512
lastTarget = RenderInjectionPoint(PostProcessEvent.AfterStack, context, "AfterStack", lastTarget);
508513

509514
// And close with the final pass
510-
RenderFinalPass(context, lastTarget);
515+
if (needsFinalPass)
516+
RenderFinalPass(context, lastTarget);
511517

512518
TextureLerper.instance.EndFrame();
513519
m_SettingsUpdateNeeded = true;
514520
}
515521

516522
int RenderInjectionPoint(PostProcessEvent evt, PostProcessRenderContext context, string marker, int releaseTargetAfterUse = -1)
517523
{
518-
// Make sure we have active effects in this injection point, skip it otherwise
519-
bool hasActiveEffects = HasActiveEffects(evt, context);
520-
521-
if (!hasActiveEffects)
522-
return releaseTargetAfterUse;
523-
524524
int tempTarget = m_TargetPool.Get();
525525
var finalDestination = context.destination;
526526

@@ -595,7 +595,7 @@ void RenderList(List<SerializedBundleRef> list, PostProcessRenderContext context
595595
cmd.EndSample(marker);
596596
}
597597

598-
int RenderBuiltins(PostProcessRenderContext context, int releaseTargetAfterUse = -1)
598+
int RenderBuiltins(PostProcessRenderContext context, bool isFinalPass, int releaseTargetAfterUse = -1)
599599
{
600600
var uberSheet = context.propertySheets.Get(context.resources.shaders.uber);
601601
uberSheet.ClearKeywords();
@@ -606,11 +606,16 @@ int RenderBuiltins(PostProcessRenderContext context, int releaseTargetAfterUse =
606606
var cmd = context.command;
607607
cmd.BeginSample("BuiltinStack");
608608

609-
// Render to an intermediate target as this won't be the final pass
610-
int tempTarget = m_TargetPool.Get();
611-
cmd.GetTemporaryRT(tempTarget, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
609+
int tempTarget = -1;
612610
var finalDestination = context.destination;
613-
context.destination = tempTarget;
611+
612+
if (!isFinalPass)
613+
{
614+
// Render to an intermediate target as this won't be the final pass
615+
tempTarget = m_TargetPool.Get();
616+
cmd.GetTemporaryRT(tempTarget, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
617+
context.destination = tempTarget;
618+
}
614619

615620
// Depth of field final combination pass used to be done in Uber which led to artifacts
616621
// when used at the same time as Bloom (because both effects used the same source, so
@@ -636,7 +641,13 @@ int RenderBuiltins(PostProcessRenderContext context, int releaseTargetAfterUse =
636641

637642
if (!breakBeforeColorGrading)
638643
RenderEffect<ColorGrading>(context);
639-
644+
645+
if (isFinalPass)
646+
{
647+
uberSheet.EnableKeyword("FINALPASS");
648+
dithering.Render(context);
649+
}
650+
640651
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, 0);
641652

642653
context.source = context.destination;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef UNITY_POSTFX_DITHERING
2+
#define UNITY_POSTFX_DITHERING
3+
4+
TEXTURE2D_SAMPLER2D(_DitheringTex, sampler_DitheringTex);
5+
float4 _Dithering_Coords;
6+
7+
float3 Dither(float3 color, float2 uv)
8+
{
9+
// Final pass dithering
10+
// Symmetric triangular distribution on [-1,1] with maximal density at 0
11+
float noise = SAMPLE_TEXTURE2D(_DitheringTex, sampler_DitheringTex, uv * _Dithering_Coords.xy + _Dithering_Coords.zw).a * 2.0 - 1.0;
12+
noise = FastSign(noise) * (1.0 - sqrt(1.0 - abs(noise)));
13+
14+
#if UNITY_COLORSPACE_GAMMA
15+
color += noise / 255.0;
16+
#else
17+
color = SRGBToLinear(LinearToSRGB(color) + noise / 255.0);
18+
#endif
19+
20+
return color;
21+
}
22+
23+
#endif // UNITY_POSTFX_DITHERING

PostProcessing/Shaders/Builtins/Dithering.hlsl.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PostProcessing/Shaders/Builtins/FinalPass.shader

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Shader "Hidden/PostProcessing/FinalPass"
66
#pragma multi_compile __ FXAA FXAA_LOW
77
#include "../StdLib.hlsl"
88
#include "../Colors.hlsl"
9+
#include "Dithering.hlsl"
910

1011
// PS3 and XBOX360 aren't supported in Unity anymore, only use the PC variant
1112
#define FXAA_PC 1
@@ -30,10 +31,6 @@ Shader "Hidden/PostProcessing/FinalPass"
3031
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
3132
float4 _MainTex_TexelSize;
3233

33-
// Dithering
34-
TEXTURE2D_SAMPLER2D(_DitheringTex, sampler_DitheringTex);
35-
float4 _Dithering_Coords;
36-
3734
float4 Frag(VaryingsDefault i) : SV_Target
3835
{
3936
half4 color = 0.0;
@@ -75,19 +72,7 @@ Shader "Hidden/PostProcessing/FinalPass"
7572
}
7673
#endif
7774

78-
{
79-
// Final dithering
80-
// Symmetric triangular distribution on [-1,1] with maximal density at 0
81-
float noise = SAMPLE_TEXTURE2D(_DitheringTex, sampler_DitheringTex, i.texcoord * _Dithering_Coords.xy + _Dithering_Coords.zw).a * 2.0 - 1.0;
82-
noise = FastSign(noise) * (1.0 - sqrt(1.0 - abs(noise)));
83-
84-
#if UNITY_COLORSPACE_GAMMA
85-
color.rgb += noise / 255.0;
86-
#else
87-
color.rgb = SRGBToLinear(LinearToSRGB(color.rgb) + noise / 255.0);
88-
#endif
89-
}
90-
75+
color.rgb = Dither(color.rgb, i.texcoord);
9176
return float4(color.rgb, 1.0);
9277
}
9378

PostProcessing/Shaders/Builtins/Uber.shader

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ Shader "Hidden/PostProcessing/Uber"
1010
#pragma multi_compile __ COLOR_GRADING_LDR COLOR_GRADING_HDR
1111
#pragma multi_compile __ VIGNETTE
1212
#pragma multi_compile __ GRAIN
13+
#pragma multi_compile __ FINALPASS
1314

1415
#include "../StdLib.hlsl"
1516
#include "../Colors.hlsl"
1617
#include "../Sampling.hlsl"
18+
#include "Dithering.hlsl"
1719

1820
#define MAX_CHROMATIC_SAMPLES 16
1921

@@ -191,10 +193,21 @@ Shader "Hidden/PostProcessing/Uber"
191193
}
192194
#endif
193195

194-
// Put saturated luma in alpha for FXAA - higher quality than "green as luma" and
195-
// necessary as RGB values will potentially still be HDR for the FXAA pass
196-
half luma = Luminance(saturate(color));
197-
half4 output = half4(color, luma);
196+
half4 output;
197+
198+
#if FINALPASS
199+
{
200+
color.rgb = Dither(color.rgb, i.texcoord);
201+
output = half4(color, 1.0);
202+
}
203+
#else
204+
{
205+
// Put saturated luma in alpha for FXAA - higher quality than "green as luma" and
206+
// necessary as RGB values will potentially still be HDR for the FXAA pass
207+
half luma = Luminance(saturate(color));
208+
output = half4(color, luma);
209+
}
210+
#endif
198211

199212
#if UNITY_COLORSPACE_GAMMA
200213
{
@@ -203,7 +216,6 @@ Shader "Hidden/PostProcessing/Uber"
203216
#endif
204217

205218
// Output RGB is still HDR at that point (unless range was crunched by a tonemapper)
206-
// Alpha is luminance of saturate(rgb)
207219
return output;
208220
}
209221

0 commit comments

Comments
 (0)