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

Commit 027d8e2

Browse files
committed
Shader cleanups
Make sure coordinates are clamped where appropriate for TAA samples
1 parent 0991239 commit 027d8e2

3 files changed

Lines changed: 15 additions & 32 deletions

File tree

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public bool IsSupported()
5858
return SystemInfo.supportedRenderTargetCount >= 2
5959
&& SystemInfo.supportsMotionVectors
6060
&& SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2;
61-
//&& !RuntimeUtilities.isSinglePassStereoEnabled;
6261
}
6362

6463
internal DepthTextureMode GetCameraFlags()

PostProcessing/Shaders/Builtins/TemporalAntialiasing.shader

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
1414

1515
TEXTURE2D_SAMPLER2D(_MainTex, _MainTexSampler);
1616
float4 _MainTex_TexelSize;
17-
float4 _MainTex_ST;
1817

1918
TEXTURE2D_SAMPLER2D(_HistoryTex, sampler_HistoryTex);
20-
float4 _HistoryTex_ST;
2119

2220
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
2321
float4 _CameraDepthTexture_TexelSize;
24-
float4 _CameraDepthTexture_ST;
2522

2623
TEXTURE2D_SAMPLER2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture);
27-
float4 _CameraMotionVectorsTexture_ST;
2824

2925
float2 _Jitter;
3026
float4 _FinalBlendParameters; // x: static, y: dynamic, z: motion amplification
@@ -33,14 +29,12 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
3329
float2 GetClosestFragment(float2 uv)
3430
{
3531
const float2 k = _CameraDepthTexture_TexelSize.xy;
36-
// TODO: All the neighborhood sample addresses need to run through UnityStereoClamp
37-
// probably do something like UnityStereoClamp(bleh, unity_StereoScaleOffset[unity_StereoEyeIndex])
38-
// or UnityStereoClamp(bleh, _CameraDepthTexture_ST);
32+
3933
const float4 neighborhood = float4(
40-
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv - k),
41-
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv + float2(k.x, -k.y)),
42-
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv + float2(-k.x, k.y)),
43-
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv + k)
34+
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoClamp(uv - k)),
35+
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoClamp(uv + float2(k.x, -k.y))),
36+
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoClamp(uv + float2(-k.x, k.y))),
37+
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoClamp(uv + k))
4438
);
4539

4640
#if defined(UNITY_REVERSED_Z)
@@ -55,7 +49,6 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
5549
result = lerp(result, float3(-1.0, 1.0, neighborhood.z), COMPARE_DEPTH(neighborhood.z, result.z));
5650
result = lerp(result, float3( 1.0, 1.0, neighborhood.w), COMPARE_DEPTH(neighborhood.w, result.z));
5751

58-
// TODO: do I need to clamp this? Should be lerping between clamped coords...
5952
return (uv + result.xy * k);
6053
}
6154

@@ -83,13 +76,12 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
8376
OutputSolver Solve(float2 motion, float2 texcoord)
8477
{
8578
const float2 k = _MainTex_TexelSize.xy;
86-
float2 uv = texcoord - _Jitter;
79+
float2 uv = UnityStereoClamp(texcoord - _Jitter);
8780

8881
float4 color = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv);
8982

90-
// TODO: clamp the coords here
91-
float4 topLeft = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv - k * 0.5);
92-
float4 bottomRight = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv + k * 0.5);
83+
float4 topLeft = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, UnityStereoClamp(uv - k * 0.5));
84+
float4 bottomRight = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, UnityStereoClamp(uv + k * 0.5));
9385

9486
float4 corners = 4.0 * (topLeft + bottomRight) - 2.0 * color;
9587

@@ -105,8 +97,7 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
10597

10698
color = FastTonemap(color);
10799

108-
// TODO: clamp the coords here
109-
float4 history = SAMPLE_TEXTURE2D(_HistoryTex, sampler_HistoryTex, texcoord - motion);
100+
float4 history = SAMPLE_TEXTURE2D(_HistoryTex, sampler_HistoryTex, UnityStereoClamp(texcoord - motion));
110101

111102
float motionLength = length(motion);
112103
float2 luma = float2(Luminance(average), Luminance(color));
@@ -139,15 +130,8 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
139130
{
140131
float2 adjustedTexCoord = UnityStereoTransformScreenSpaceTex(i.texcoord);
141132

142-
// TODO: either use TRANSFORM_TEX or UnityStereoTransformScreenSpaceTex on i.texcoord
143-
//float2 closest = GetClosestFragment(i.texcoord);
144133
float2 closest = GetClosestFragment(adjustedTexCoord);
145-
146-
// TODO: since closest is based off the transformed tex coord, we should be ok
147134
float2 motion = SAMPLE_TEXTURE2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture, closest).xy;
148-
149-
// TODO: again, either use TRANSFORM_TEX or UnityStereoTransformScreenSpaceTex on i.texcoord
150-
//return Solve(motion, i.texcoord);
151135
return Solve(motion, adjustedTexCoord);
152136
}
153137

@@ -156,12 +140,6 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
156140
float2 adjustedTexCoord = UnityStereoTransformScreenSpaceTex(i.texcoord);
157141

158142
// Don't dilate in ortho !
159-
// TODO: either use TRANSFORM_TEX or UnityStereoTransformScreenSpaceTex on i.texcoord
160-
//float2 motion = SAMPLE_TEXTURE2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture, i.texcoord).xy;
161-
162-
// TODO: again, either use TRANSFORM_TEX or UnityStereoTransformScreenSpaceTex on i.texcoord
163-
//return Solve(motion, i.texcoord);
164-
165143
float2 motion = SAMPLE_TEXTURE2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture, adjustedTexCoord).xy;
166144
return Solve(motion, adjustedTexCoord);
167145
}

PostProcessing/Shaders/xRLib.hlsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ float2 UnityStereoClamp(float2 uv, float4 scaleAndOffset)
5555
{
5656
return float2(clamp(uv.x, scaleAndOffset.z, scaleAndOffset.z + scaleAndOffset.x), uv.y);
5757
}
58+
59+
float2 UnityStereoClamp(float2 uv)
60+
{
61+
return UnityStereoClamp(uv, unity_StereoScaleOffset[unity_StereoEyeIndex]);
62+
}
5863
#else
5964
#define TransformStereoScreenSpaceTex(uv, w) uv
6065
#define UnityStereoTransformScreenSpaceTex(uv) uv
6166
#define UnityStereoClamp(uv, scaleAndOffset) uv
67+
#define UnityStereoClamp(uv) uv
6268
#endif
6369

6470
#endif // UNITY_POSTFX_XRLIB

0 commit comments

Comments
 (0)