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

Commit da16cfb

Browse files
authored
Merge pull request #43 from keijiro/fix-issue26-2
Fix for issue #26 (reopened)
2 parents d38e24f + 494dc9f commit da16cfb

File tree

3 files changed

+61
-71
lines changed

3 files changed

+61
-71
lines changed

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
sampler2D_float _CameraDepthTexture;
1111
sampler2D_float _HistoryCoC;
12+
float _HistoryWeight;
1213

1314
// Camera parameters
1415
float _Distance;
@@ -46,8 +47,27 @@ VaryingsDOF VertDOF(AttributesDefault v)
4647
return o;
4748
}
4849

49-
// Downsampling, prefiltering and CoC calculation
50-
half4 FragPrefilter(VaryingsDOF i) : SV_Target
50+
// Prefilter: CoC calculation, downsampling and premultiplying.
51+
52+
#if defined(PREFILTER_TAA)
53+
54+
// TAA enabled: use MRT to update the history buffer in the same pass.
55+
struct PrefilterOutput
56+
{
57+
half4 base : SV_Target0;
58+
half4 history : SV_Target1;
59+
};
60+
#define PrefilterSemantics
61+
62+
#else
63+
64+
// No TAA
65+
#define PrefilterOutput half4
66+
#define PrefilterSemantics :SV_Target
67+
68+
#endif
69+
70+
PrefilterOutput FragPrefilter(VaryingsDOF i) PrefilterSemantics
5171
{
5272
float3 duv = _MainTex_TexelSize.xyx * float3(0.5, 0.5, -0.5);
5373

@@ -68,6 +88,12 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
6888
float4 cocs = (depths - _Distance) * _LensCoeff / depths;
6989
cocs = clamp(cocs, -_MaxCoC, _MaxCoC);
7090

91+
#if defined(PREFILTER_TAA)
92+
// Get the average with the history to avoid temporal aliasing.
93+
half hcoc = tex2D(_HistoryCoC, i.uv).r;
94+
cocs = lerp(cocs, hcoc, _HistoryWeight);
95+
#endif
96+
7197
// Premultiply CoC to reduce background bleeding.
7298
float4 weights = saturate(abs(cocs) * _RcpMaxCoC);
7399

@@ -96,33 +122,14 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
96122
avg = GammaToLinearSpace(avg);
97123
#endif
98124

99-
return half4(avg, coc);
100-
}
101-
102-
// Very simple temporal antialiasing on CoC to reduce jitter (mostly visible on the front plane)
103-
struct Output
104-
{
105-
half4 base : SV_Target0;
106-
half4 history : SV_Target1;
107-
};
108-
109-
Output FragAntialiasCoC(VaryingsDOF i)
110-
{
111-
half4 base = tex2D(_MainTex, i.uv);
112-
half hCoC = tex2D(_HistoryCoC, i.uv).r;
113-
half CoC = base.a;
114-
half nCoC = hCoC * CoC < 0.0 ? CoC : (hCoC + CoC) / 2.0; // TODO: Smarter CoC AA
115-
116-
Output output;
117-
output.base = half4(base.rgb, nCoC);
118-
output.history = nCoC.xxxx;
125+
#if defined(PREFILTER_TAA)
126+
PrefilterOutput output;
127+
output.base = half4(avg, coc);
128+
output.history = coc.xxxx;
119129
return output;
120-
}
121-
122-
// CoC history clearing
123-
half4 FragClearCoCHistory(VaryingsDOF i) : SV_Target
124-
{
125-
return tex2D(_MainTex, i.uv).aaaa;
130+
#else
131+
return half4(avg, coc);
132+
#endif
126133
}
127134

128135
// Bokeh filter with disk-shaped kernels

PostProcessing/Resources/Shaders/DepthOfField.shader

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ Shader "Hidden/Post FX/Depth Of Field"
2525
ENDCG
2626
}
2727

28-
// (1-4) Bokeh filter with disk-shaped kernels
28+
// (1) Pass 0 + temporal antialiasing
2929
Pass
3030
{
3131
CGPROGRAM
3232
#pragma vertex VertDOF
33-
#pragma fragment FragBlur
34-
#define KERNEL_SMALL
33+
#pragma fragment FragPrefilter
34+
#define PREFILTER_TAA
3535
#include "DepthOfField.cginc"
3636
ENDCG
3737
}
3838

39+
// (2-5) Bokeh filter with disk-shaped kernels
3940
Pass
4041
{
4142
CGPROGRAM
4243
#pragma vertex VertDOF
4344
#pragma fragment FragBlur
44-
#define KERNEL_MEDIUM
45+
#define KERNEL_SMALL
4546
#include "DepthOfField.cginc"
4647
ENDCG
4748
}
@@ -51,7 +52,7 @@ Shader "Hidden/Post FX/Depth Of Field"
5152
CGPROGRAM
5253
#pragma vertex VertDOF
5354
#pragma fragment FragBlur
54-
#define KERNEL_LARGE
55+
#define KERNEL_MEDIUM
5556
#include "DepthOfField.cginc"
5657
ENDCG
5758
}
@@ -61,32 +62,22 @@ Shader "Hidden/Post FX/Depth Of Field"
6162
CGPROGRAM
6263
#pragma vertex VertDOF
6364
#pragma fragment FragBlur
64-
#define KERNEL_VERYLARGE
65-
#include "DepthOfField.cginc"
66-
ENDCG
67-
}
68-
69-
// (5) CoC antialiasing
70-
Pass
71-
{
72-
CGPROGRAM
73-
#pragma vertex VertDOF
74-
#pragma fragment FragAntialiasCoC
65+
#define KERNEL_LARGE
7566
#include "DepthOfField.cginc"
7667
ENDCG
7768
}
7869

79-
// (6) CoC history clearing
8070
Pass
8171
{
8272
CGPROGRAM
8373
#pragma vertex VertDOF
84-
#pragma fragment FragClearCoCHistory
74+
#pragma fragment FragBlur
75+
#define KERNEL_VERYLARGE
8576
#include "DepthOfField.cginc"
8677
ENDCG
8778
}
8879

89-
// (7) Postfilter blur
80+
// (6) Postfilter blur
9081
Pass
9182
{
9283
CGPROGRAM

PostProcessing/Runtime/Components/DepthOfFieldComponent.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static class Uniforms
1616
internal static readonly int _RcpAspect = Shader.PropertyToID("_RcpAspect");
1717
internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
1818
internal static readonly int _HistoryCoC = Shader.PropertyToID("_HistoryCoC");
19+
internal static readonly int _HistoryWeight = Shader.PropertyToID("_HistoryWeight");
1920
internal static readonly int _DepthOfFieldParams = Shader.PropertyToID("_DepthOfFieldParams");
2021
}
2122

@@ -92,43 +93,36 @@ public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasC
9293
source.filterMode = FilterMode.Point;
9394

9495
// Pass #1 - Downsampling, prefiltering and CoC calculation
95-
Graphics.Blit(source, rt1, material, 0);
96-
97-
// Pass #2 - CoC Antialiasing
98-
var pass = rt1;
99-
if (antialiasCoC)
96+
if (!antialiasCoC)
10097
{
101-
pass = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf);
102-
103-
if (m_CoCHistory == null || !m_CoCHistory.IsCreated() || m_CoCHistory.width != context.width / 2 || m_CoCHistory.height != context.height / 2)
104-
{
105-
m_CoCHistory = RenderTexture.GetTemporary(context.width / 2, context.height / 2, 0, RenderTextureFormat.RHalf);
106-
m_CoCHistory.filterMode = FilterMode.Point;
107-
m_CoCHistory.name = "CoC History";
108-
Graphics.Blit(rt1, m_CoCHistory, material, 6);
109-
}
98+
Graphics.Blit(source, rt1, material, 0);
99+
}
100+
else
101+
{
102+
var initial = m_CoCHistory == null || !m_CoCHistory.IsCreated() || m_CoCHistory.width != context.width / 2 || m_CoCHistory.height != context.height / 2;
110103

111104
var tempCoCHistory = RenderTexture.GetTemporary(context.width / 2, context.height / 2, 0, RenderTextureFormat.RHalf);
112105
tempCoCHistory.filterMode = FilterMode.Point;
113106
tempCoCHistory.name = "CoC History";
114107

115-
m_MRT[0] = pass.colorBuffer;
108+
m_MRT[0] = rt1.colorBuffer;
116109
m_MRT[1] = tempCoCHistory.colorBuffer;
117-
material.SetTexture(Uniforms._MainTex, rt1);
110+
material.SetTexture(Uniforms._MainTex, source);
118111
material.SetTexture(Uniforms._HistoryCoC, m_CoCHistory);
112+
material.SetFloat(Uniforms._HistoryWeight, initial ? 0 : 0.5f);
119113
Graphics.SetRenderTarget(m_MRT, rt1.depthBuffer);
120-
GraphicsUtils.Blit(material, 5);
114+
GraphicsUtils.Blit(material, 1);
121115

122116
RenderTexture.ReleaseTemporary(m_CoCHistory);
123117
m_CoCHistory = tempCoCHistory;
124118
}
125119

126-
// Pass #3 - Bokeh simulation
120+
// Pass #2 - Bokeh simulation
127121
var rt2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf);
128-
Graphics.Blit(pass, rt2, material, 1 + (int)settings.kernelSize);
122+
Graphics.Blit(rt1, rt2, material, 2 + (int)settings.kernelSize);
129123

130-
// Pass #4 - Postfilter blur
131-
Graphics.Blit(rt2, rt1, material, 7);
124+
// Pass #3 - Postfilter blur
125+
Graphics.Blit(rt2, rt1, material, 6);
132126

133127
if (context.profile.debugViews.IsModeActive(DebugMode.FocusPlane))
134128
{
@@ -142,9 +136,7 @@ public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasC
142136
uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
143137
}
144138

145-
if (antialiasCoC)
146-
context.renderTextureFactory.Release(pass);
147-
139+
context.renderTextureFactory.Release(rt1);
148140
context.renderTextureFactory.Release(rt2);
149141
source.filterMode = FilterMode.Bilinear;
150142
}

0 commit comments

Comments
 (0)