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

Commit e5f01e9

Browse files
committed
Split the uber shader in 2 when FXAA is in use so that FXAA doesn't affect grain & dithering
1 parent 1bb1e30 commit e5f01e9

File tree

5 files changed

+87
-50
lines changed

5 files changed

+87
-50
lines changed

PostProcessing/Resources/Shaders/FXAA.shader

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Shader "Hidden/Post FX/FXAA"
99

1010
#include "UnityCG.cginc"
1111
#include "Common.cginc"
12+
#include "UberSecondPass.cginc"
13+
#pragma multi_compile __ GRAIN
14+
#pragma multi_compile __ DITHERING
1215

1316
#if defined(SHADER_API_PS3)
1417
#define FXAA_PS3 1
@@ -35,7 +38,7 @@ Shader "Hidden/Post FX/FXAA"
3538
float3 _QualitySettings;
3639
float4 _ConsoleSettings;
3740

38-
fixed4 Frag(VaryingsDefault i) : SV_Target
41+
half4 Frag(VaryingsDefault i) : SV_Target
3942
{
4043
const float4 consoleUV = i.uv.xyxy + 0.5 * float4(-_MainTex_TexelSize.xy, _MainTex_TexelSize.xy);
4144
const float4 consoleSubpixelFrame = _ConsoleSettings.x * float4(-1.0, -1.0, 1.0, 1.0) *
@@ -50,10 +53,17 @@ Shader "Hidden/Post FX/FXAA"
5053
const float4 consoleConstants = float4(0.0, 0.0, 0.0, 0.0);
5154
#endif
5255

53-
return FxaaPixelShader(UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST), UnityStereoScreenSpaceUVAdjust(consoleUV, _MainTex_ST), _MainTex, _MainTex, _MainTex, _MainTex_TexelSize.xy,
56+
half4 color = FxaaPixelShader(
57+
UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST),
58+
UnityStereoScreenSpaceUVAdjust(consoleUV, _MainTex_ST),
59+
_MainTex, _MainTex, _MainTex, _MainTex_TexelSize.xy,
5460
consoleSubpixelFrame, consoleSubpixelFramePS3, consoleSubpixelFrameXBOX,
55-
_QualitySettings.x, _QualitySettings.y, _QualitySettings.z, _ConsoleSettings.y, _ConsoleSettings.z,
56-
_ConsoleSettings.w, consoleConstants);
61+
_QualitySettings.x, _QualitySettings.y, _QualitySettings.z,
62+
_ConsoleSettings.y, _ConsoleSettings.z, _ConsoleSettings.w, consoleConstants);
63+
64+
color.rgb = UberSecondPass(color.rgb, i.uv);
65+
66+
return color;
5767
}
5868

5969
ENDCG

PostProcessing/Resources/Shaders/Uber.shader

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Shader "Hidden/Post FX/Uber Shader"
3333
#include "UnityCG.cginc"
3434
#include "Bloom.cginc"
3535
#include "ColorGrading.cginc"
36+
#include "UberSecondPass.cginc"
3637

3738
// Auto exposure / eye adaptation
3839
sampler2D _AutoExposure;
@@ -64,15 +65,6 @@ Shader "Hidden/Post FX/Uber Shader"
6465
sampler2D _UserLut;
6566
half4 _UserLut_Params; // @see _LogLut_Params
6667

67-
// Grain
68-
half2 _Grain_Params1; // x: lum_contrib, y: intensity
69-
half4 _Grain_Params2; // x: xscale, h: yscale, z: xoffset, w: yoffset
70-
sampler2D _GrainTex;
71-
72-
// Dithering
73-
sampler2D _DitheringTex;
74-
float4 _DitheringCoords;
75-
7668
// Vignette
7769
half3 _Vignette_Color;
7870
half2 _Vignette_Center; // UV space
@@ -316,29 +308,7 @@ Shader "Hidden/Post FX/Uber Shader"
316308
}
317309
#endif
318310

319-
// Grain
320-
#if GRAIN
321-
{
322-
float3 grain = tex2D(_GrainTex, uv * _Grain_Params2.xy + _Grain_Params2.zw).rgb;
323-
324-
// Noisiness response curve based on scene luminance
325-
float lum = 1.0 - sqrt(AcesLuminance(color));
326-
lum = lerp(1.0, lum, _Grain_Params1.x);
327-
328-
color += color * grain * _Grain_Params1.y * lum;
329-
}
330-
#endif
331-
332-
// Blue noise dithering
333-
#if DITHERING
334-
{
335-
// Symmetric triangular distribution on [-1,1] with maximal density at 0
336-
float noise = tex2D(_DitheringTex, uv * _DitheringCoords.xy + _DitheringCoords.zw).a * 2.0 - 1.0;
337-
noise = sign(noise) * (1.0 - sqrt(1.0 - abs(noise))) / 255.0;
338-
339-
color += noise;
340-
}
341-
#endif
311+
color = UberSecondPass(color, uv);
342312

343313
// Done !
344314
return half4(color, 1.0);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "ColorGrading.cginc"
2+
3+
// Grain
4+
half2 _Grain_Params1; // x: lum_contrib, y: intensity
5+
half4 _Grain_Params2; // x: xscale, h: yscale, z: xoffset, w: yoffset
6+
sampler2D _GrainTex;
7+
8+
// Dithering
9+
sampler2D _DitheringTex;
10+
float4 _DitheringCoords;
11+
12+
float3 UberSecondPass(half3 color, float2 uv)
13+
{
14+
// Grain
15+
#if GRAIN
16+
{
17+
float3 grain = tex2D(_GrainTex, uv * _Grain_Params2.xy + _Grain_Params2.zw).rgb;
18+
19+
// Noisiness response curve based on scene luminance
20+
float lum = 1.0 - sqrt(AcesLuminance(color));
21+
lum = lerp(1.0, lum, _Grain_Params1.x);
22+
23+
color += color * grain * _Grain_Params1.y * lum;
24+
}
25+
#endif
26+
27+
// Blue noise dithering
28+
#if DITHERING
29+
{
30+
// Symmetric triangular distribution on [-1,1] with maximal density at 0
31+
float noise = tex2D(_DitheringTex, uv * _DitheringCoords.xy + _DitheringCoords.zw).a * 2.0 - 1.0;
32+
noise = sign(noise) * (1.0 - sqrt(1.0 - abs(noise))) / 255.0;
33+
34+
color += noise;
35+
}
36+
#endif
37+
38+
return color;
39+
}

PostProcessing/Resources/Shaders/UberSecondPass.cginc.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/Runtime/PostProcessingBehaviour.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
195195
var uberMaterial = m_MaterialFactory.Get("Hidden/Post FX/Uber Shader");
196196
uberMaterial.shaderKeywords = null;
197197

198+
if (!GraphicsUtils.isLinearColorSpace)
199+
uberMaterial.EnableKeyword("UNITY_COLORSPACE_GAMMA");
200+
198201
var src = source;
199202
var dst = destination;
200203

@@ -235,30 +238,36 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
235238

236239
uberActive |= TryPrepareUberImageEffect(m_ChromaticAberration, uberMaterial);
237240
uberActive |= TryPrepareUberImageEffect(m_ColorGrading, uberMaterial);
238-
uberActive |= TryPrepareUberImageEffect(m_UserLut, uberMaterial);
239-
uberActive |= TryPrepareUberImageEffect(m_Grain, uberMaterial);
240241
uberActive |= TryPrepareUberImageEffect(m_Vignette, uberMaterial);
241-
uberActive |= TryPrepareUberImageEffect(m_Dithering, uberMaterial);
242+
uberActive |= TryPrepareUberImageEffect(m_UserLut, uberMaterial);
242243

243-
// Render to destination
244-
if (uberActive)
244+
var fxaaMaterial = fxaaActive
245+
? m_MaterialFactory.Get("Hidden/Post FX/FXAA")
246+
: null;
247+
248+
if (fxaaActive)
245249
{
246-
if (!GraphicsUtils.isLinearColorSpace)
247-
uberMaterial.EnableKeyword("UNITY_COLORSPACE_GAMMA");
250+
fxaaMaterial.shaderKeywords = null;
251+
TryPrepareUberImageEffect(m_Grain, fxaaMaterial);
252+
TryPrepareUberImageEffect(m_Dithering, fxaaMaterial);
248253

249-
var input = src;
250-
var output = dst;
251-
if (fxaaActive)
254+
if (uberActive)
252255
{
253-
output = m_RenderTextureFactory.Get(src);
256+
var output = m_RenderTextureFactory.Get(src);
257+
Graphics.Blit(src, output, uberMaterial, 0);
254258
src = output;
255259
}
256260

257-
Graphics.Blit(input, output, uberMaterial, 0);
261+
m_Fxaa.Render(src, dst);
258262
}
263+
else
264+
{
265+
uberActive |= TryPrepareUberImageEffect(m_Grain, uberMaterial);
266+
uberActive |= TryPrepareUberImageEffect(m_Dithering, uberMaterial);
259267

260-
if (fxaaActive)
261-
m_Fxaa.Render(src, dst);
268+
if (uberActive)
269+
Graphics.Blit(src, dst, uberMaterial, 0);
270+
}
262271

263272
if (!uberActive && !fxaaActive)
264273
Graphics.Blit(src, dst);

0 commit comments

Comments
 (0)