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

Commit 88ef4c9

Browse files
committed
Added quality settings to SMAA
1 parent e949822 commit 88ef4c9

File tree

5 files changed

+195
-116
lines changed

5 files changed

+195
-116
lines changed

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public sealed class PostProcessLayerEditor : BaseEditor<PostProcessLayer>
2323
SerializedProperty m_TaaSharpness;
2424
SerializedProperty m_TaaStationaryBlending;
2525
SerializedProperty m_TaaMotionBlending;
26+
SerializedProperty m_SmaaQuality;
2627
SerializedProperty m_FxaaMobileOptimized;
2728
SerializedProperty m_FxaaKeepAlpha;
2829

@@ -61,6 +62,7 @@ void OnEnable()
6162
m_TaaSharpness = FindProperty(x => x.temporalAntialiasing.sharpness);
6263
m_TaaStationaryBlending = FindProperty(x => x.temporalAntialiasing.stationaryBlending);
6364
m_TaaMotionBlending = FindProperty(x => x.temporalAntialiasing.motionBlending);
65+
m_SmaaQuality = FindProperty(x => x.subpixelMorphologicalAntialiasing.quality);
6466
m_FxaaMobileOptimized = FindProperty(x => x.fastApproximateAntialiasing.fastMode);
6567
m_FxaaKeepAlpha = FindProperty(x => x.fastApproximateAntialiasing.keepAlpha);
6668

@@ -160,6 +162,8 @@ void DoAntialiasing()
160162
{
161163
if (RuntimeUtilities.isSinglePassStereoSelected)
162164
EditorGUILayout.HelpBox("SMAA doesn't work with Single-pass stereo rendering.", MessageType.Warning);
165+
166+
EditorGUILayout.PropertyField(m_SmaaQuality);
163167
}
164168
else if (m_AntialiasingMode.intValue == (int)PostProcessLayer.Antialiasing.FastApproximateAntialiasing)
165169
{

PostProcessing/Runtime/Effects/SubpixelMorphologicalAntialiasing.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ public sealed class SubpixelMorphologicalAntialiasing
77
{
88
enum Pass
99
{
10-
EdgeDetection,
11-
BlendWeights,
12-
NeighborhoodBlending
10+
EdgeDetection = 0,
11+
BlendWeights = 3,
12+
NeighborhoodBlending = 6
1313
}
1414

15+
public enum Quality
16+
{
17+
Low = 0,
18+
Medium = 1,
19+
High = 2
20+
}
21+
22+
[Tooltip("Lower quality is faster at the expense of visual quality (Low = ~60%, Medium = ~80%).")]
23+
public Quality quality = Quality.High;
24+
1525
public bool IsSupported()
1626
{
1727
return !RuntimeUtilities.isSinglePassStereoEnabled;
@@ -29,8 +39,8 @@ internal void Render(PostProcessRenderContext context)
2939
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flip, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
3040
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flop, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
3141

32-
cmd.BlitFullscreenTriangle(context.source, ShaderIDs.SMAA_Flip, sheet, (int)Pass.EdgeDetection, true);
33-
cmd.BlitFullscreenTriangle(ShaderIDs.SMAA_Flip, ShaderIDs.SMAA_Flop, sheet, (int)Pass.BlendWeights);
42+
cmd.BlitFullscreenTriangle(context.source, ShaderIDs.SMAA_Flip, sheet, (int)Pass.EdgeDetection + (int)quality, true);
43+
cmd.BlitFullscreenTriangle(ShaderIDs.SMAA_Flip, ShaderIDs.SMAA_Flop, sheet, (int)Pass.BlendWeights + (int)quality);
3444
cmd.SetGlobalTexture("_BlendTex", ShaderIDs.SMAA_Flop);
3545
cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, (int)Pass.NeighborhoodBlending);
3646

PostProcessing/Shaders/Builtins/SubpixelMorphologicalAntialiasing.shader

Lines changed: 47 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,163 +3,99 @@ Shader "Hidden/PostProcessing/SubpixelMorphologicalAntialiasing"
33
HLSLINCLUDE
44

55
#pragma exclude_renderers d3d11_9x
6-
#include "../StdLib.hlsl"
76

8-
// High/Very-high settings
9-
#define SMAA_THRESHOLD 0.08
10-
#define SMAA_MAX_SEARCH_STEPS 16
11-
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
12-
#define SMAA_CORNER_ROUNDING 25
13-
14-
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
15-
TEXTURE2D_SAMPLER2D(_BlendTex, sampler_BlendTex);
16-
TEXTURE2D_SAMPLER2D(_AreaTex, sampler_AreaTex);
17-
TEXTURE2D_SAMPLER2D(_SearchTex, sampler_SearchTex);
18-
float4 _MainTex_TexelSize;
19-
20-
#define SMAA_RT_METRICS _MainTex_TexelSize
21-
#define SMAA_AREATEX_SELECT(s) s.rg
22-
#define SMAA_SEARCHTEX_SELECT(s) s.a
23-
#define LinearSampler sampler_MainTex
24-
#define PointSampler sampler_MainTex
25-
26-
#include "SubpixelMorphologicalAntialiasing.hlsl"
27-
28-
// ----------------------------------------------------------------------------------------
29-
// Edge Detection
7+
ENDHLSL
308

31-
struct VaryingsEdge
32-
{
33-
float4 vertex : SV_POSITION;
34-
float2 texcoord : TEXCOORD0;
35-
float4 offsets[3] : TEXCOORD1;
36-
};
9+
SubShader
10+
{
11+
Cull Off ZWrite Off ZTest Always
3712

38-
VaryingsEdge VertEdge(AttributesDefault v)
13+
// 0 - Edge detection (Low)
14+
Pass
3915
{
40-
VaryingsEdge o;
41-
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
42-
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
43-
44-
#if UNITY_UV_STARTS_AT_TOP
45-
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
46-
#endif
47-
48-
o.offsets[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), o.texcoord.xyxy);
49-
o.offsets[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), o.texcoord.xyxy);
50-
o.offsets[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), o.texcoord.xyxy);
16+
HLSLPROGRAM
5117

52-
return o;
53-
}
18+
#pragma vertex VertEdge
19+
#pragma fragment FragEdge
20+
#define SMAA_PRESET_LOW
21+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
5422

55-
float4 FragEdge(VaryingsEdge i) : SV_Target
56-
{
57-
return float4(SMAAColorEdgeDetectionPS(i.texcoord, i.offsets, _MainTex), 0.0, 0.0);
23+
ENDHLSL
5824
}
5925

60-
// ----------------------------------------------------------------------------------------
61-
// Blend Weights Calculation
62-
63-
struct VaryingsBlend
64-
{
65-
float4 vertex : SV_POSITION;
66-
float2 texcoord : TEXCOORD0;
67-
float2 pixcoord : TEXCOORD1;
68-
float4 offsets[3] : TEXCOORD2;
69-
};
70-
71-
VaryingsBlend VertBlend(AttributesDefault v)
26+
// 1 - Edge detection (Medium)
27+
Pass
7228
{
73-
VaryingsBlend o;
74-
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
75-
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
76-
77-
#if UNITY_UV_STARTS_AT_TOP
78-
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
79-
#endif
80-
81-
o.pixcoord = o.texcoord * SMAA_RT_METRICS.zw;
82-
83-
// We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
84-
o.offsets[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.250, -0.125, 1.250, -0.125), o.texcoord.xyxy);
85-
o.offsets[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.250, -0.125, 1.250), o.texcoord.xyxy);
86-
87-
// And these for the searches, they indicate the ends of the loops:
88-
o.offsets[2] = mad(SMAA_RT_METRICS.xxyy, float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS),
89-
float4(o.offsets[0].xz, o.offsets[1].yw));
29+
HLSLPROGRAM
9030

91-
return o;
92-
}
31+
#pragma vertex VertEdge
32+
#pragma fragment FragEdge
33+
#define SMAA_PRESET_MEDIUM
34+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
9335

94-
float4 FragBlend(VaryingsBlend i) : SV_Target
95-
{
96-
return SMAABlendingWeightCalculationPS(i.texcoord, i.pixcoord, i.offsets, _MainTex, _AreaTex, _SearchTex, 0);
36+
ENDHLSL
9737
}
9838

99-
// ----------------------------------------------------------------------------------------
100-
// Neighborhood Blending
101-
102-
struct VaryingsNeighbor
103-
{
104-
float4 vertex : SV_POSITION;
105-
float2 texcoord : TEXCOORD0;
106-
float4 offset : TEXCOORD1;
107-
};
108-
109-
VaryingsNeighbor VertNeighbor(AttributesDefault v)
39+
// 2 - Edge detection (High)
40+
Pass
11041
{
111-
VaryingsNeighbor o;
112-
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
113-
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
42+
HLSLPROGRAM
11443

115-
#if UNITY_UV_STARTS_AT_TOP
116-
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
117-
#endif
44+
#pragma vertex VertEdge
45+
#pragma fragment FragEdge
46+
#define SMAA_PRESET_HIGH
47+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
11848

119-
o.offset = mad(SMAA_RT_METRICS.xyxy, float4(1.0, 0.0, 0.0, 1.0), o.texcoord.xyxy);
120-
return o;
49+
ENDHLSL
12150
}
12251

123-
float4 FragNeighbor(VaryingsNeighbor i) : SV_Target
52+
// 3 - Blend Weights Calculation (Low)
53+
Pass
12454
{
125-
return SMAANeighborhoodBlendingPS(i.texcoord, i.offset, _MainTex, _BlendTex);
126-
}
55+
HLSLPROGRAM
12756

128-
ENDHLSL
57+
#pragma vertex VertBlend
58+
#pragma fragment FragBlend
59+
#define SMAA_PRESET_LOW
60+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
12961

130-
SubShader
131-
{
132-
Cull Off ZWrite Off ZTest Always
62+
ENDHLSL
63+
}
13364

134-
// 0 - Edge detection
65+
// 4 - Blend Weights Calculation (Medium)
13566
Pass
13667
{
13768
HLSLPROGRAM
13869

139-
#pragma vertex VertEdge
140-
#pragma fragment FragEdge
70+
#pragma vertex VertBlend
71+
#pragma fragment FragBlend
72+
#define SMAA_PRESET_MEDIUM
73+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
14174

14275
ENDHLSL
14376
}
14477

145-
// 1 - Blend Weights Calculation
78+
// 5 - Blend Weights Calculation (High)
14679
Pass
14780
{
14881
HLSLPROGRAM
14982

15083
#pragma vertex VertBlend
15184
#pragma fragment FragBlend
85+
#define SMAA_PRESET_HIGH
86+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
15287

15388
ENDHLSL
15489
}
15590

156-
// 2 - Neighborhood Blending
91+
// 6 - Neighborhood Blending
15792
Pass
15893
{
15994
HLSLPROGRAM
16095

16196
#pragma vertex VertNeighbor
16297
#pragma fragment FragNeighbor
98+
#include "SubpixelMorphologicalAntialiasingBridge.hlsl"
16399

164100
ENDHLSL
165101
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#ifndef UNITY_POSTFX_SMAA_BRIDGE
2+
#define UNITY_POSTFX_SMAA_BRIDGE
3+
4+
#include "../StdLib.hlsl"
5+
6+
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
7+
TEXTURE2D_SAMPLER2D(_BlendTex, sampler_BlendTex);
8+
TEXTURE2D_SAMPLER2D(_AreaTex, sampler_AreaTex);
9+
TEXTURE2D_SAMPLER2D(_SearchTex, sampler_SearchTex);
10+
float4 _MainTex_TexelSize;
11+
12+
#define SMAA_RT_METRICS _MainTex_TexelSize
13+
#define SMAA_AREATEX_SELECT(s) s.rg
14+
#define SMAA_SEARCHTEX_SELECT(s) s.a
15+
#define LinearSampler sampler_MainTex
16+
#define PointSampler sampler_MainTex
17+
18+
#include "SubpixelMorphologicalAntialiasing.hlsl"
19+
20+
// ----------------------------------------------------------------------------------------
21+
// Edge Detection
22+
23+
struct VaryingsEdge
24+
{
25+
float4 vertex : SV_POSITION;
26+
float2 texcoord : TEXCOORD0;
27+
float4 offsets[3] : TEXCOORD1;
28+
};
29+
30+
VaryingsEdge VertEdge(AttributesDefault v)
31+
{
32+
VaryingsEdge o;
33+
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
34+
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
35+
36+
#if UNITY_UV_STARTS_AT_TOP
37+
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
38+
#endif
39+
40+
o.offsets[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), o.texcoord.xyxy);
41+
o.offsets[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), o.texcoord.xyxy);
42+
o.offsets[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), o.texcoord.xyxy);
43+
44+
return o;
45+
}
46+
47+
float4 FragEdge(VaryingsEdge i) : SV_Target
48+
{
49+
return float4(SMAAColorEdgeDetectionPS(i.texcoord, i.offsets, _MainTex), 0.0, 0.0);
50+
}
51+
52+
// ----------------------------------------------------------------------------------------
53+
// Blend Weights Calculation
54+
55+
struct VaryingsBlend
56+
{
57+
float4 vertex : SV_POSITION;
58+
float2 texcoord : TEXCOORD0;
59+
float2 pixcoord : TEXCOORD1;
60+
float4 offsets[3] : TEXCOORD2;
61+
};
62+
63+
VaryingsBlend VertBlend(AttributesDefault v)
64+
{
65+
VaryingsBlend o;
66+
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
67+
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
68+
69+
#if UNITY_UV_STARTS_AT_TOP
70+
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
71+
#endif
72+
73+
o.pixcoord = o.texcoord * SMAA_RT_METRICS.zw;
74+
75+
// We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
76+
o.offsets[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.250, -0.125, 1.250, -0.125), o.texcoord.xyxy);
77+
o.offsets[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.250, -0.125, 1.250), o.texcoord.xyxy);
78+
79+
// And these for the searches, they indicate the ends of the loops:
80+
o.offsets[2] = mad(SMAA_RT_METRICS.xxyy, float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS),
81+
float4(o.offsets[0].xz, o.offsets[1].yw));
82+
83+
return o;
84+
}
85+
86+
float4 FragBlend(VaryingsBlend i) : SV_Target
87+
{
88+
return SMAABlendingWeightCalculationPS(i.texcoord, i.pixcoord, i.offsets, _MainTex, _AreaTex, _SearchTex, 0);
89+
}
90+
91+
// ----------------------------------------------------------------------------------------
92+
// Neighborhood Blending
93+
94+
struct VaryingsNeighbor
95+
{
96+
float4 vertex : SV_POSITION;
97+
float2 texcoord : TEXCOORD0;
98+
float4 offset : TEXCOORD1;
99+
};
100+
101+
VaryingsNeighbor VertNeighbor(AttributesDefault v)
102+
{
103+
VaryingsNeighbor o;
104+
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
105+
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
106+
107+
#if UNITY_UV_STARTS_AT_TOP
108+
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
109+
#endif
110+
111+
o.offset = mad(SMAA_RT_METRICS.xyxy, float4(1.0, 0.0, 0.0, 1.0), o.texcoord.xyxy);
112+
return o;
113+
}
114+
115+
float4 FragNeighbor(VaryingsNeighbor i) : SV_Target
116+
{
117+
return SMAANeighborhoodBlendingPS(i.texcoord, i.offset, _MainTex, _BlendTex);
118+
}
119+
120+
#endif

0 commit comments

Comments
 (0)