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

Commit 6be6d70

Browse files
authored
Merge pull request #1 from Unity-Technologies/master
Updating from base
2 parents 4e56a71 + 4ce8688 commit 6be6d70

18 files changed

+236
-329
lines changed

PostProcessing/Resources/Shaders/AmbientOcclusion.cginc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ VaryingsMultitex VertMultitex(AttributesDefault v)
164164
#endif
165165

166166
VaryingsMultitex o;
167-
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
167+
o.pos = UnityObjectToClipPos(v.vertex);
168168
o.uv = v.texcoord.xy;
169169
o.uv01 = uvAlt;
170170
o.uvSPR = UnityStereoTransformScreenSpaceTex(uvAlt);

PostProcessing/Resources/Shaders/Blit.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Shader "Hidden/Post FX/Blit"
1919
Varyings VertBlit(AttributesDefault v)
2020
{
2121
Varyings o;
22-
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
22+
o.vertex = UnityObjectToClipPos(v.vertex);
2323
o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
2424
return o;
2525
}

PostProcessing/Resources/Shaders/Bloom.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Shader "Hidden/Post FX/Bloom"
6060
VaryingsMultitex VertMultitex(AttributesDefault v)
6161
{
6262
VaryingsMultitex o;
63-
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
63+
o.pos = UnityObjectToClipPos(v.vertex);
6464
o.uvMain = UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST);
6565
o.uvBase = o.uvMain;
6666

PostProcessing/Resources/Shaders/Common.cginc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct VaryingsDefault
3939
VaryingsDefault VertDefault(AttributesDefault v)
4040
{
4141
VaryingsDefault o;
42-
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
42+
o.pos = UnityObjectToClipPos(v.vertex);
4343
o.uv = v.texcoord.xy;
4444
o.uvSPR = UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST);
4545
return o;
@@ -58,6 +58,12 @@ inline half Min3(half x, half y, half z) { return min(x, min(y, z)); }
5858
inline half Max3(half3 x) { return max(x.x, max(x.y, x.z)); }
5959
inline half Max3(half x, half y, half z) { return max(x, max(y, z)); }
6060

61+
inline half Min4(half4 x) { return min(x.x, min(x.y, min(x.z, x.w))); }
62+
inline half Min4(half x, half y, half z, half w) { return min(x, min(y, min(z, w))); }
63+
64+
inline half Max4(half4 x) { return max(x.x, max(x.y, max(x.z, x.w))); }
65+
inline half Max4(half x, half y, half z, half w) { return max(x, max(y, min(z, w))); }
66+
6167
inline half Pow2(half x) { return x * x; }
6268
inline half2 Pow2(half2 x) { return x * x; }
6369
inline half3 Pow2(half3 x) { return x * x; }
@@ -76,9 +82,9 @@ inline half4 Pow4(half4 x) { return x * x * x * x; }
7682
#endif
7783

7884
// Returns the largest vector of v1 and v2
79-
inline half2 MaxV(half2 v1, half2 v2) { return lerp(v1, v2, dot(v1, v1) < dot(v2, v2)); }
80-
inline half3 MaxV(half3 v1, half3 v2) { return lerp(v1, v2, dot(v1, v1) < dot(v2, v2)); }
81-
inline half4 MaxV(half4 v1, half4 v2) { return lerp(v1, v2, dot(v1, v1) < dot(v2, v2)); }
85+
inline half2 MaxV(half2 v1, half2 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
86+
inline half3 MaxV(half3 v1, half3 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
87+
inline half4 MaxV(half4 v1, half4 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
8288

8389
// Clamp HDR value within a safe range
8490
inline half SafeHDR(half c) { return min(c, HALF_MAX); }

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 65 additions & 42 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;
@@ -33,21 +34,40 @@ VaryingsDOF VertDOF(AttributesDefault v)
3334
#endif
3435

3536
VaryingsDOF o;
36-
#if defined(UNITY_SINGLE_PASS_STEREO)
3737
o.pos = UnityObjectToClipPos(v.vertex);
38+
39+
#if defined(UNITY_SINGLE_PASS_STEREO)
3840
o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
3941
o.uvAlt = UnityStereoScreenSpaceUVAdjust(uvAlt, _MainTex_ST);
4042
#else
41-
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
4243
o.uv = v.texcoord;
4344
o.uvAlt = uvAlt;
4445
#endif
4546

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

@@ -87,7 +113,9 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
87113
avg /= dot(weights, 1.0);
88114

89115
// Output CoC = average of CoCs
90-
half coc = dot(cocs, 0.25);
116+
half cocmin = Min4(cocs);
117+
half cocmax = Max4(cocs);
118+
half coc = -cocmin > cocmax ? cocmin : cocmax;
91119

92120
// Premultiply CoC again.
93121
avg *= smoothstep(0, _MainTex_TexelSize.y * 2, abs(coc));
@@ -96,33 +124,14 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
96124
avg = GammaToLinearSpace(avg);
97125
#endif
98126

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;
127+
#if defined(PREFILTER_TAA)
128+
PrefilterOutput output;
129+
output.base = half4(avg, coc);
130+
output.history = coc.xxxx;
119131
return output;
120-
}
121-
122-
// CoC history clearing
123-
half4 FragClearCoCHistory(VaryingsDOF i) : SV_Target
124-
{
125-
return tex2D(_MainTex, i.uv).aaaa;
132+
#else
133+
return half4(avg, coc);
134+
#endif
126135
}
127136

128137
// Bokeh filter with disk-shaped kernels
@@ -187,21 +196,35 @@ half4 FragPostBlur(VaryingsDOF i) : SV_Target
187196
{
188197
// 9-tap tent filter
189198
float4 duv = _MainTex_TexelSize.xyxy * float4(1, 1, -1, 0);
190-
half4 acc;
191199

192-
acc = tex2D(_MainTex, i.uv - duv.xy);
193-
acc += tex2D(_MainTex, i.uv - duv.wy) * 2;
194-
acc += tex2D(_MainTex, i.uv - duv.zy);
200+
half4 c0 = tex2D(_MainTex, i.uv - duv.xy);
201+
half4 c1 = tex2D(_MainTex, i.uv - duv.wy);
202+
half4 c2 = tex2D(_MainTex, i.uv - duv.zy);
203+
204+
half4 c3 = tex2D(_MainTex, i.uv + duv.zw);
205+
half4 c4 = tex2D(_MainTex, i.uv );
206+
half4 c5 = tex2D(_MainTex, i.uv + duv.xw);
207+
208+
half4 c6 = tex2D(_MainTex, i.uv + duv.zy);
209+
half4 c7 = tex2D(_MainTex, i.uv + duv.wy);
210+
half4 c8 = tex2D(_MainTex, i.uv + duv.xy);
211+
212+
half4 acc = c0 * 1 + c1 * 2 + c2 * 1 +
213+
c3 * 2 + c4 * 4 + c5 * 2 +
214+
c6 * 1 + c7 * 2 + c8 * 1;
215+
216+
half aa =
217+
c0.a * c0.a * 1 + c1.a * c1.a * 2 + c2.a * c2.a * 1 +
218+
c3.a * c3.a * 2 + c4.a * c4.a * 4 + c5.a * c5.a * 2 +
219+
c6.a * c6.a * 1 + c7.a * c7.a * 2 + c8.a * c8.a * 1;
195220

196-
acc += tex2D(_MainTex, i.uv + duv.zw) * 2;
197-
acc += tex2D(_MainTex, i.uv ) * 4;
198-
acc += tex2D(_MainTex, i.uv + duv.xw) * 2;
221+
half wb = 1.2;
222+
half a = (wb * acc.a - aa) / (wb * 16 - acc.a);
199223

200-
acc += tex2D(_MainTex, i.uv + duv.zy);
201-
acc += tex2D(_MainTex, i.uv + duv.wy) * 2;
202-
acc += tex2D(_MainTex, i.uv + duv.xy);
224+
acc /= 16;
203225

204-
return acc / 16;
226+
half3 rgb = acc.rgb * (1 + saturate(acc.a - a));
227+
return half4(rgb, a);
205228
}
206229

207230
#endif // __DEPTH_OF_FIELD__

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/Resources/Shaders/EyeAdaptation.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Shader "Hidden/Post FX/Eye Adaptation"
134134
VaryingsEditorHisto VertEditorHisto(AttributesDefault v)
135135
{
136136
VaryingsEditorHisto o;
137-
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
137+
o.pos = UnityObjectToClipPos(v.vertex);
138138
o.uv = v.texcoord.xy;
139139
o.maxValue = 1.0 / FindMaxHistogramValue();
140140
o.avgLuminance = GetAverageLuminance(o.maxValue);

0 commit comments

Comments
 (0)