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

Commit 66389e2

Browse files
committed
Removed depth dejittering, do CoC antialiasing instead
1 parent 789d329 commit 66389e2

6 files changed

Lines changed: 108 additions & 129 deletions

File tree

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#pragma target 3.0
1010

1111
sampler2D_float _CameraDepthTexture;
12-
sampler2D_float _DejitteredDepth;
12+
sampler2D_float _HistoryCoC;
1313

1414
// Camera parameters
1515
float _Distance;
@@ -59,18 +59,10 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
5959
half3 c3 = tex2D(_MainTex, i.uv + duv.xy).rgb;
6060

6161
// Sample linear depths.
62-
#if DEJITTER_DEPTH
63-
float d0 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt - duv.xy).x);
64-
float d1 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt - duv.zy).x);
65-
float d2 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt + duv.zy).x);
66-
float d3 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt + duv.xy).x);
67-
#else
6862
float d0 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt - duv.xy));
6963
float d1 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt - duv.zy));
7064
float d2 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt + duv.zy));
7165
float d3 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt + duv.xy));
72-
#endif
73-
7466
float4 depths = float4(d0, d1, d2, d3);
7567

7668
// Calculate the radiuses of CoCs at these sample points.
@@ -105,6 +97,32 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
10597
return half4(avg, coc);
10698
}
10799

100+
// Very simple temporal antialiasing on CoC to reduce jitter (mostly visible on the front plane)
101+
struct Output
102+
{
103+
half4 base : SV_Target0;
104+
half history : SV_Target1;
105+
};
106+
107+
Output FragAntialiasCoC(VaryingsDOF i)
108+
{
109+
half4 base = tex2D(_MainTex, i.uv);
110+
half hCoC = tex2D(_HistoryCoC, i.uv).r;
111+
half CoC = base.a;
112+
half nCoC = (hCoC + CoC) / 2.0; // TODO: Smarter CoC AA
113+
114+
Output output;
115+
output.base = half4(base.rgb, nCoC);
116+
output.history = nCoC;
117+
return output;
118+
}
119+
120+
// CoC history clearing
121+
half4 FragClearCoCHistory(VaryingsDOF i) : SV_Target
122+
{
123+
return tex2D(_MainTex, i.uv).aaaa;
124+
}
125+
108126
// Bokeh filter with disk-shaped kernels
109127
half4 FragBlur(VaryingsDOF i) : SV_Target
110128
{

PostProcessing/Resources/Shaders/DepthOfField.shader

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Shader "Hidden/Post FX/Depth Of Field"
1616
#pragma vertex VertDOF
1717
#pragma fragment FragPrefilter
1818
#pragma target 3.0
19-
#pragma multi_compile __ DEJITTER_DEPTH
2019
#include "DepthOfField.cginc"
2120
ENDCG
2221
}
@@ -61,6 +60,26 @@ Shader "Hidden/Post FX/Depth Of Field"
6160
#include "DepthOfField.cginc"
6261
ENDCG
6362
}
63+
64+
// (5) CoC antialiasing
65+
Pass
66+
{
67+
CGPROGRAM
68+
#pragma vertex VertDOF
69+
#pragma fragment FragAntialiasCoC
70+
#include "DepthOfField.cginc"
71+
ENDCG
72+
}
73+
74+
// (6) CoC history clearing
75+
Pass
76+
{
77+
CGPROGRAM
78+
#pragma vertex VertDOF
79+
#pragma fragment FragClearCoCHistory
80+
#include "DepthOfField.cginc"
81+
ENDCG
82+
}
6483
}
6584

6685
FallBack Off

PostProcessing/Resources/Shaders/TAA.cginc

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ struct OutputSolver
5555
{
5656
float4 first : SV_Target0;
5757
float4 second : SV_Target1;
58-
59-
//#if DEJITTER_DEPTH
60-
float4 third : SV_Target2;
61-
float4 fourth : SV_Target3;
62-
//#endif
6358
};
6459

6560
sampler2D _HistoryTex;
@@ -115,10 +110,10 @@ float GetDepth(float2 uv)
115110
#endif
116111
}
117112

118-
float2 GetClosestFragment(float2 uv, out float depth)
113+
float2 GetClosestFragment(float2 uv)
119114
{
120115
const float2 k = _CameraDepthTexture_TexelSize.xy;
121-
depth = GetDepth(uv);
116+
float depth = GetDepth(uv);
122117

123118
#if TAA_USE_GATHER4_FOR_DEPTH_SAMPLE
124119
const float4 neighborhood = _CameraDepthTexture.Gather(sampler_CameraDepthTexture, uv, int2(1, 1));
@@ -186,16 +181,10 @@ float4 ClipToAABB(float4 color, float p, float3 minimum, float3 maximum)
186181

187182
OutputSolver FragSolver(VaryingsSolver input)
188183
{
189-
float currentDepth;
190-
191184
#if TAA_DILATE_MOTION_VECTOR_SAMPLE
192-
float2 motion = tex2D(_CameraMotionVectorsTexture, GetClosestFragment(input.uv.zw, currentDepth)).xy;
185+
float2 motion = tex2D(_CameraMotionVectorsTexture, GetClosestFragment(input.uv.zw)).xy;
193186
#else
194187
float2 motion = tex2D(_CameraMotionVectorsTexture, input.uv.zw).xy;
195-
196-
//#if DEJITTER_DEPTH
197-
currentDepth = GetDepth(input.uv.zw);
198-
//#endif
199188
#endif
200189

201190
const float2 k = TAA_COLOR_NEIGHBORHOOD_SAMPLE_SPREAD * _MainTex_TexelSize.xy;
@@ -393,19 +382,6 @@ OutputSolver FragSolver(VaryingsSolver input)
393382

394383
output.second = color;
395384

396-
#if DEJITTER_DEPTH
397-
// ALl of this is a big hack... FIXME
398-
399-
float4 history1 = tex2D(_DepthHistory1Tex, input.uv.zw);
400-
float4 history2 = tex2D(_DepthHistory2Tex, input.uv.zw);
401-
float motionLength = length(motion); // `motion` may be dilated... Probably should use raw movec instead ?
402-
float dmin = min(min(min(min(min(min(min(history1.y, history1.z), history1.w), history2.x), history2.y), history2.z), history2.w), currentDepth);
403-
float o = lerp(dmin, currentDepth, motionLength > (1.0 - currentDepth) * 0.0002);
404-
405-
output.third = float4(o, currentDepth, history1.yz);
406-
output.fourth = float4(history1.w, history2.xyz);
407-
#endif
408-
409385
return output;
410386
}
411387

PostProcessing/Runtime/Components/DepthOfFieldComponent.cs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ static class Uniforms
1313
internal static readonly int _RcpMaxCoC = Shader.PropertyToID("_RcpMaxCoC");
1414
internal static readonly int _RcpAspect = Shader.PropertyToID("_RcpAspect");
1515
internal static readonly int _DejitteredDepth = Shader.PropertyToID("_DejitteredDepth");
16+
internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
17+
internal static readonly int _HistoryCoC = Shader.PropertyToID("_HistoryCoC");
1618
}
1719

1820
const string k_ShaderString = "Hidden/Post FX/Depth Of Field";
@@ -32,6 +34,9 @@ public override DepthTextureMode GetCameraFlags()
3234
return DepthTextureMode.Depth;
3335
}
3436

37+
RenderTexture m_CoCHistory;
38+
RenderBuffer[] m_MRT = new RenderBuffer[2];
39+
3540
// Height of the 35mm full-frame format (36mm x 24mm)
3641
const float k_FilmHeight = 0.024f;
3742

@@ -57,7 +62,7 @@ float CalculateMaxCoCRadius(int screenHeight)
5762
return Mathf.Min(0.05f, radiusInPixels / screenHeight);
5863
}
5964

60-
public void Prepare(RenderTexture source, Material uberMaterial, RenderTexture dejitteredDepth)
65+
public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasCoC)
6166
{
6267
var settings = model.settings;
6368

@@ -80,24 +85,59 @@ public void Prepare(RenderTexture source, Material uberMaterial, RenderTexture d
8085
var rcpAspect = (float)source.height / source.width;
8186
material.SetFloat(Uniforms._RcpAspect, rcpAspect);
8287

83-
if (dejitteredDepth != null)
84-
{
85-
material.SetTexture(Uniforms._DejitteredDepth, dejitteredDepth);
86-
material.EnableKeyword("DEJITTER_DEPTH");
87-
}
88+
var rt1 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf, filterMode: FilterMode.Point);
89+
var rt2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf, filterMode: FilterMode.Bilinear);
8890

8991
// Pass #1 - Downsampling, prefiltering and CoC calculation
90-
var rt1 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf, filterMode: FilterMode.Point);
9192
Graphics.Blit(source, rt1, material, 0);
9293

93-
// Pass #2 - Bokeh simulation
94-
var rt2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf, filterMode: FilterMode.Bilinear);
95-
Graphics.Blit(rt1, rt2, material, 1 + (int)settings.kernelSize);
94+
// Pass #2 - CoC Antialiasing
95+
var pass = rt1;
96+
if (antialiasCoC)
97+
{
98+
pass = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf, filterMode: FilterMode.Point);
99+
100+
if (m_CoCHistory == null || !m_CoCHistory.IsCreated() || m_CoCHistory.width != context.width / 2 || m_CoCHistory.height != context.height / 2)
101+
{
102+
m_CoCHistory = RenderTexture.GetTemporary(context.width / 2, context.height / 2, 0, RenderTextureFormat.RHalf);
103+
m_CoCHistory.filterMode = FilterMode.Point;
104+
m_CoCHistory.name = "CoC History";
105+
Graphics.Blit(rt1, m_CoCHistory, material, 6);
106+
}
107+
108+
var tempCoCHistory = RenderTexture.GetTemporary(context.width / 2, context.height / 2, 0, RenderTextureFormat.RHalf);
109+
tempCoCHistory.filterMode = FilterMode.Point;
110+
tempCoCHistory.name = "CoC History";
111+
112+
m_MRT[0] = pass.colorBuffer;
113+
m_MRT[1] = tempCoCHistory.colorBuffer;
114+
material.SetTexture(Uniforms._MainTex, rt1);
115+
material.SetTexture(Uniforms._HistoryCoC, m_CoCHistory);
116+
Graphics.SetRenderTarget(m_MRT, rt1.depthBuffer);
117+
GraphicsUtils.Blit(material, 5);
118+
119+
RenderTexture.ReleaseTemporary(m_CoCHistory);
120+
m_CoCHistory = tempCoCHistory;
121+
}
122+
123+
// Pass #3 - Bokeh simulation
124+
Graphics.Blit(pass, rt2, material, 1 + (int)settings.kernelSize);
96125

97126
context.renderTextureFactory.Release(rt1);
98127

128+
if (antialiasCoC)
129+
context.renderTextureFactory.Release(pass);
130+
99131
uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, rt2);
100132
uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
101133
}
134+
135+
public override void OnDisable()
136+
{
137+
if (m_CoCHistory != null)
138+
RenderTexture.ReleaseTemporary(m_CoCHistory);
139+
140+
m_CoCHistory = null;
141+
}
102142
}
103143
}

PostProcessing/Runtime/Components/TaaComponent.cs

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,15 @@ static class Uniforms
1616
}
1717

1818
const string k_ShaderString = "Hidden/Post FX/Temporal Anti-aliasing";
19-
const int k_SampleCount = 7; // Don't touch this as it'll break depth dejittering
19+
const int k_SampleCount = 8;
2020

21-
readonly RenderBuffer[] m_MRT2 = new RenderBuffer[2];
22-
readonly RenderBuffer[] m_MRT4 = new RenderBuffer[4];
21+
readonly RenderBuffer[] m_MRT = new RenderBuffer[2];
2322

2423
int m_SampleIndex;
2524
bool m_ResetHistory;
2625

2726
RenderTexture m_HistoryTexture;
2827

29-
RenderTexture m_DepthHistory1;
30-
RenderTexture m_DepthHistory2;
31-
32-
public RenderTexture dejitteredDepth { get { return m_DepthHistory1; } }
33-
3428
public override bool active
3529
{
3630
get
@@ -76,7 +70,7 @@ public void SetProjectionMatrix()
7670
material.SetVector(Uniforms._Jitter, jitter);
7771
}
7872

79-
public void Render(RenderTexture source, RenderTexture destination, bool dejitterDepth)
73+
public void Render(RenderTexture source, RenderTexture destination)
8074
{
8175
var material = context.materialFactory.Get(k_ShaderString);
8276
material.shaderKeywords = null;
@@ -103,74 +97,15 @@ public void Render(RenderTexture source, RenderTexture destination, bool dejitte
10397
var tempHistory = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
10498
tempHistory.name = "TAA History";
10599

106-
var mrt = dejitterDepth ? m_MRT4 : m_MRT2;
107-
108-
mrt[0] = destination.colorBuffer;
109-
mrt[1] = tempHistory.colorBuffer;
110-
111-
RenderTexture tempDepthHistory1 = null, tempDepthHistory2 = null;
112-
if (dejitterDepth)
113-
{
114-
if (m_ResetHistory || m_DepthHistory1 == null || m_DepthHistory1.width != source.width || m_DepthHistory1.height != source.height)
115-
{
116-
if (m_DepthHistory1)
117-
RenderTexture.ReleaseTemporary(m_DepthHistory1);
118-
119-
m_DepthHistory1 = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
120-
m_DepthHistory1.name = "Depth History 2";
121-
122-
Graphics.Blit(source, m_DepthHistory1, material, 3);
123-
}
124-
125-
if (m_ResetHistory || m_DepthHistory2 == null || m_DepthHistory2.width != source.width || m_DepthHistory2.height != source.height)
126-
{
127-
if (m_DepthHistory2)
128-
RenderTexture.ReleaseTemporary(m_DepthHistory2);
129-
130-
m_DepthHistory2 = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
131-
m_DepthHistory2.name = "Depth History 1";
132-
133-
Graphics.Blit(source, m_DepthHistory2, material, 3);
134-
}
135-
136-
material.SetTexture(Uniforms._DepthHistory1Tex, m_DepthHistory1);
137-
material.SetTexture(Uniforms._DepthHistory2Tex, m_DepthHistory2);
138-
material.EnableKeyword("DEJITTER_DEPTH");
139-
140-
tempDepthHistory1 = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.ARGBHalf);
141-
tempDepthHistory1.name = "Depth History 1";
142-
tempDepthHistory2 = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.ARGBHalf);
143-
tempDepthHistory2.name = "Depth History 2";
100+
m_MRT[0] = destination.colorBuffer;
101+
m_MRT[1] = tempHistory.colorBuffer;
144102

145-
mrt[2] = tempDepthHistory1.colorBuffer;
146-
mrt[3] = tempDepthHistory2.colorBuffer;
147-
}
148-
149-
Graphics.SetRenderTarget(mrt, destination.depthBuffer);
103+
Graphics.SetRenderTarget(m_MRT, destination.depthBuffer);
150104
GraphicsUtils.Blit(material, context.camera.orthographic ? 1 : 0);
151105

152106
RenderTexture.ReleaseTemporary(m_HistoryTexture);
153107
m_HistoryTexture = tempHistory;
154108

155-
if (dejitterDepth)
156-
{
157-
RenderTexture.ReleaseTemporary(m_DepthHistory1);
158-
RenderTexture.ReleaseTemporary(m_DepthHistory2);
159-
m_DepthHistory1 = tempDepthHistory1;
160-
m_DepthHistory2 = tempDepthHistory2;
161-
}
162-
163-
if (!dejitterDepth)
164-
{
165-
if (m_DepthHistory1)
166-
RenderTexture.ReleaseTemporary(m_DepthHistory1);
167-
if (m_DepthHistory2)
168-
RenderTexture.ReleaseTemporary(m_DepthHistory2);
169-
170-
m_DepthHistory1 = null;
171-
m_DepthHistory2 = null;
172-
}
173-
174109
m_ResetHistory = false;
175110
}
176111

@@ -263,15 +198,7 @@ public override void OnDisable()
263198
if (m_HistoryTexture != null)
264199
RenderTexture.ReleaseTemporary(m_HistoryTexture);
265200

266-
if (m_DepthHistory1 != null)
267-
RenderTexture.ReleaseTemporary(m_DepthHistory1);
268-
269-
if (m_DepthHistory2 != null)
270-
RenderTexture.ReleaseTemporary(m_DepthHistory2);
271-
272201
m_HistoryTexture = null;
273-
m_DepthHistory1 = null;
274-
m_DepthHistory2 = null;
275202
m_SampleIndex = 0;
276203
}
277204
}

0 commit comments

Comments
 (0)