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

Commit 8b303cb

Browse files
authored
Merge pull request #96 from Unity-Technologies/aa-fixes
Antialiasing & grain fixes
2 parents 3ed8511 + 96d20fa commit 8b303cb

File tree

6 files changed

+112
-58
lines changed

6 files changed

+112
-58
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
@@ -288,19 +280,6 @@ Shader "Hidden/Post FX/Uber Shader"
288280

289281
color = saturate(color);
290282

291-
// Grain
292-
#if GRAIN
293-
{
294-
float3 grain = tex2D(_GrainTex, uv * _Grain_Params2.xy + _Grain_Params2.zw).rgb;
295-
296-
// Noisiness response curve based on scene luminance
297-
float lum = 1.0 - sqrt(AcesLuminance(color));
298-
lum = lerp(1.0, lum, _Grain_Params1.x);
299-
300-
color += color * grain * _Grain_Params1.y * lum;
301-
}
302-
#endif
303-
304283
// Back to gamma space if needed
305284
#if UNITY_COLORSPACE_GAMMA
306285
{
@@ -329,16 +308,7 @@ Shader "Hidden/Post FX/Uber Shader"
329308
}
330309
#endif
331310

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/Components/TaaComponent.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using UnityEngine.Rendering;
1+
using System;
22

33
namespace UnityEngine.PostProcessing
44
{
@@ -45,17 +45,25 @@ public void ResetHistory()
4545
m_ResetHistory = true;
4646
}
4747

48-
public void SetProjectionMatrix()
48+
public void SetProjectionMatrix(Func<Vector2, Matrix4x4> jitteredFunc)
4949
{
5050
var settings = model.settings.taaSettings;
5151

5252
var jitter = GenerateRandomOffset();
5353
jitter *= settings.jitterSpread;
5454

5555
context.camera.nonJitteredProjectionMatrix = context.camera.projectionMatrix;
56-
context.camera.projectionMatrix = context.camera.orthographic
57-
? GetOrthographicProjectionMatrix(jitter)
58-
: GetPerspectiveProjectionMatrix(jitter);
56+
57+
if (jitteredFunc != null)
58+
{
59+
context.camera.projectionMatrix = jitteredFunc(jitter);
60+
}
61+
else
62+
{
63+
context.camera.projectionMatrix = context.camera.orthographic
64+
? GetOrthographicProjectionMatrix(jitter)
65+
: GetPerspectiveProjectionMatrix(jitter);
66+
}
5967

6068
#if UNITY_5_5_OR_NEWER
6169
context.camera.useJitteredProjectionMatrixForTransparentRendering = false;
@@ -198,7 +206,7 @@ public override void OnDisable()
198206

199207
m_HistoryTexture = null;
200208
m_SampleIndex = 0;
201-
ResetHistory();
209+
ResetHistory();
202210
}
203211
}
204212
}

PostProcessing/Runtime/PostProcessingBehaviour.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class PostProcessingBehaviour : MonoBehaviour
1616
// Inspector fields
1717
public PostProcessingProfile profile;
1818

19+
public Func<Vector2, Matrix4x4> jitteredMatrixFunc;
20+
Matrix4x4 nonJitteredProjectionMatrix;
21+
1922
// Internal helpers
2023
Dictionary<Type, KeyValuePair<CameraEvent, CommandBuffer>> m_CommandBuffers;
2124
List<PostProcessingComponentBase> m_Components;
@@ -149,7 +152,10 @@ void OnPreCull()
149152

150153
// Temporal antialiasing jittering, needs to happen before culling
151154
if (!m_RenderingInSceneView && m_Taa.active && !profile.debugViews.willInterrupt)
152-
m_Taa.SetProjectionMatrix();
155+
{
156+
nonJitteredProjectionMatrix = m_Context.camera.projectionMatrix;
157+
m_Taa.SetProjectionMatrix(jitteredMatrixFunc);
158+
}
153159
}
154160

155161
void OnPreRender()
@@ -171,7 +177,8 @@ void OnPostRender()
171177
if (profile == null || m_Camera == null)
172178
return;
173179

174-
m_Camera.ResetProjectionMatrix();
180+
if (!m_RenderingInSceneView && m_Taa.active && !profile.debugViews.willInterrupt)
181+
m_Context.camera.projectionMatrix = nonJitteredProjectionMatrix;
175182
}
176183

177184
// Classic render target pipeline for RT-based effects
@@ -234,30 +241,41 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
234241

235242
uberActive |= TryPrepareUberImageEffect(m_ChromaticAberration, uberMaterial);
236243
uberActive |= TryPrepareUberImageEffect(m_ColorGrading, uberMaterial);
237-
uberActive |= TryPrepareUberImageEffect(m_UserLut, uberMaterial);
238-
uberActive |= TryPrepareUberImageEffect(m_Grain, uberMaterial);
239244
uberActive |= TryPrepareUberImageEffect(m_Vignette, uberMaterial);
240-
uberActive |= TryPrepareUberImageEffect(m_Dithering, uberMaterial);
245+
uberActive |= TryPrepareUberImageEffect(m_UserLut, uberMaterial);
241246

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

248-
var input = src;
249-
var output = dst;
250-
if (fxaaActive)
257+
if (uberActive)
251258
{
252-
output = m_RenderTextureFactory.Get(src);
259+
var output = m_RenderTextureFactory.Get(src);
260+
Graphics.Blit(src, output, uberMaterial, 0);
253261
src = output;
254262
}
255263

256-
Graphics.Blit(input, output, uberMaterial, 0);
264+
m_Fxaa.Render(src, dst);
257265
}
266+
else
267+
{
268+
uberActive |= TryPrepareUberImageEffect(m_Grain, uberMaterial);
269+
uberActive |= TryPrepareUberImageEffect(m_Dithering, uberMaterial);
258270

259-
if (fxaaActive)
260-
m_Fxaa.Render(src, dst);
271+
if (uberActive)
272+
{
273+
if (!GraphicsUtils.isLinearColorSpace)
274+
uberMaterial.EnableKeyword("UNITY_COLORSPACE_GAMMA");
275+
276+
Graphics.Blit(src, dst, uberMaterial, 0);
277+
}
278+
}
261279

262280
if (!uberActive && !fxaaActive)
263281
Graphics.Blit(src, dst);

0 commit comments

Comments
 (0)