/
FSMAA.fx
370 lines (332 loc) · 10.6 KB
/
FSMAA.fx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* _______ ___ ___ ___ ___
* / || \/ | / \ / \
* | (---- | \ / | / ^ \ / ^ \
* \ \ | |\/| | / /_\ \ / /_\ \
* ----) | | | | | / _____ \ / _____ \
* |_______/ |__| |__| /__/ \__\ /__/ \__\
*
* E N H A N C E D
* S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
*
* for ReShade 3.0+
*/
//------------------- Preprocessor Settings -------------------
#if !defined(SMAA_PRESET_LOW) && !defined(SMAA_PRESET_MEDIUM) && !defined(SMAA_PRESET_HIGH) && !defined(SMAA_PRESET_ULTRA)
#define SMAA_PRESET_CUSTOM // Do not use a quality preset by default
#define SMAA_DISABLE_DIAG_DETECTION
#define SMAA_DISABLE_DEPTH_BUFFER
#endif
//----------------------- UI Variables ------------------------
#include "ReShadeUI.fxh"
#ifndef SMAA_DISABLE_DEPTH_BUFFER
uniform int EdgeDetectionType < __UNIFORM_COMBO_INT1
ui_items = "Luminance edge detection\0Color edge detection\0Depth edge detection\0";
ui_label = "Edge Detection Type";
> = 1;
#else
uniform int EdgeDetectionType < __UNIFORM_COMBO_INT1
ui_items = "Luminance edge detection\0Color edge detection\0";
ui_label = "Edge Detection Type";
> = 1;
#endif
#ifdef SMAA_PRESET_CUSTOM
uniform float EdgeDetectionThreshold < __UNIFORM_DRAG_FLOAT1
ui_min = 0.05; ui_max = 0.20; ui_step = 0.001;
ui_tooltip = "Edge detection threshold. If SMAA misses some edges try lowering this slightly.";
ui_label = "Edge Detection Threshold";
> = 0.10;
#ifndef SMAA_DISABLE_DEPTH_BUFFER
uniform float DepthEdgeDetectionThreshold < __UNIFORM_DRAG_FLOAT1
ui_min = 0.001; ui_max = 0.10; ui_step = 0.001;
ui_tooltip = "Depth Edge detection threshold. If SMAA misses some edges try lowering this slightly.";
ui_label = "Depth Edge Detection Threshold";
> = 0.01;
#endif
uniform int MaxSearchSteps < __UNIFORM_SLIDER_INT1
ui_min = 0; ui_max = 112;
ui_label = "Max Search Steps";
ui_tooltip = "Determines the radius SMAA will search for aliased edges.";
> = 32;
#ifndef SMAA_DISABLE_DIAG_DETECTION
uniform int MaxSearchStepsDiagonal < __UNIFORM_SLIDER_INT1
ui_min = 0; ui_max = 20;
ui_label = "Max Search Steps Diagonal";
ui_tooltip = "Determines the radius SMAA will search for diagonal aliased edges";
> = 16;
#endif
#ifndef SMAA_DISABLE_CORNER_DETECTION
uniform int CornerRounding < __UNIFORM_SLIDER_INT1
ui_min = 0; ui_max = 100;
ui_label = "Corner Rounding";
ui_tooltip = "Determines the percent of anti-aliasing to apply to corners.";
> = 25;
#endif
#ifndef SMAA_DISABLE_DEPTH_BUFFER
uniform bool PredicationEnabled < __UNIFORM_INPUT_BOOL1
ui_label = "Enable Predicated Thresholding";
> = false;
uniform float PredicationThreshold < __UNIFORM_DRAG_FLOAT1
ui_min = 0.005; ui_max = 1.00; ui_step = 0.01;
ui_tooltip = "Threshold to be used in the additional predication buffer.";
ui_label = "Predication Threshold";
> = 0.01;
uniform float PredicationScale < __UNIFORM_SLIDER_FLOAT1
ui_min = 1; ui_max = 8;
ui_tooltip = "How much to scale the global threshold used for luma or color edge.";
ui_label = "Predication Scale";
> = 2.0;
uniform float PredicationStrength < __UNIFORM_SLIDER_FLOAT1
ui_min = 0; ui_max = 10;
ui_tooltip = "How much to locally decrease the threshold.";
ui_label = "Predication Strength";
> = 0.4;
#endif
#endif
uniform int DebugOutput < __UNIFORM_COMBO_INT1
ui_items = "None\0View edges\0View weights\0";
ui_label = "Debug Output";
> = false;
#ifdef SMAA_PRESET_CUSTOM
#define SMAA_THRESHOLD EdgeDetectionThreshold
#define SMAA_MAX_SEARCH_STEPS MaxSearchSteps
#ifndef SMAA_DISABLE_CORNER_DETECTION
#define SMAA_CORNER_ROUNDING CornerRounding
#endif
#ifndef SMAA_DISABLE_DIAG_DETECTION
#define SMAA_MAX_SEARCH_STEPS_DIAG MaxSearchStepsDiagonal
#endif
#ifndef SMAA_DISABLE_DEPTH_BUFFER
#define SMAA_DEPTH_THRESHOLD DepthEdgeDetectionThreshold
#define SMAA_PREDICATION PredicationEnabled
#define SMAA_PREDICATION_THRESHOLD PredicationThreshold
#define SMAA_PREDICATION_SCALE PredicationScale
#define SMAA_PREDICATION_STRENGTH PredicationStrength
#else
#define SMAA_PREDICATION false
#endif
#endif
#define SMAA_RT_METRICS float4(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT, BUFFER_WIDTH, BUFFER_HEIGHT)
#define SMAA_CUSTOM_SL 1
#define SMAATexture2D(tex) sampler tex
#define SMAATexturePass2D(tex) tex
#define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, coord))
#define SMAASampleLevelZeroPoint(tex, coord) SMAASampleLevelZero(tex, coord)
#define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlodoffset(tex, float4(coord, coord), offset)
#define SMAASample(tex, coord) tex2D(tex, coord)
#define SMAASamplePoint(tex, coord) SMAASample(tex, coord)
#define SMAASampleOffset(tex, coord, offset) tex2Doffset(tex, coord, offset)
#define SMAA_BRANCH [branch]
#define SMAA_FLATTEN [flatten]
#if (__RENDERER__ == 0xb000 || __RENDERER__ == 0xb100)
#define SMAAGather(tex, coord) tex2Dgather(tex, coord, 0)
#endif
#include "SMAA.fxh"
#include "ReShade.fxh"
// Textures
#ifndef SMAA_DISABLE_DEPTH_BUFFER
texture depthTex < pooled = true; >
{
Width = BUFFER_WIDTH;
Height = BUFFER_HEIGHT;
Format = R16F;
};
#endif
texture edgesTex < pooled = true; >
{
Width = BUFFER_WIDTH;
Height = BUFFER_HEIGHT;
Format = RG8;
};
texture blendTex < pooled = true; >
{
Width = BUFFER_WIDTH;
Height = BUFFER_HEIGHT;
Format = RGBA8;
};
texture areaTex < source = "AreaTex.png"; >
{
Width = 160;
Height = 560;
Format = RG8;
};
texture searchTex < source = "SearchTex.png"; >
{
Width = 64;
Height = 16;
Format = R8;
};
// Samplers
#ifndef SMAA_DISABLE_DEPTH_BUFFER
sampler depthLinearSampler
{
Texture = depthTex;
};
#endif
sampler colorGammaSampler
{
Texture = ReShade::BackBufferTex;
AddressU = Clamp; AddressV = Clamp;
MipFilter = Point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler colorLinearSampler
{
Texture = ReShade::BackBufferTex;
AddressU = Clamp; AddressV = Clamp;
MipFilter = Point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = true;
};
sampler edgesSampler
{
Texture = edgesTex;
AddressU = Clamp; AddressV = Clamp;
MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler blendSampler
{
Texture = blendTex;
AddressU = Clamp; AddressV = Clamp;
MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler areaSampler
{
Texture = areaTex;
AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler searchSampler
{
Texture = searchTex;
AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
MipFilter = Point; MinFilter = Point; MagFilter = Point;
SRGBTexture = false;
};
// Vertex shaders
void SMAAEdgeDetectionWrapVS(
in uint id : SV_VertexID,
out float4 position : SV_Position,
out float2 texcoord : TEXCOORD0,
out float4 offset[3] : TEXCOORD1)
{
PostProcessVS(id, position, texcoord);
SMAAEdgeDetectionVS(texcoord, offset);
}
void SMAABlendingWeightCalculationWrapVS(
in uint id : SV_VertexID,
out float4 position : SV_Position,
out float2 texcoord : TEXCOORD0,
out float2 pixcoord : TEXCOORD1,
out float4 offset[3] : TEXCOORD2)
{
PostProcessVS(id, position, texcoord);
SMAABlendingWeightCalculationVS(texcoord, pixcoord, offset);
}
void SMAANeighborhoodBlendingWrapVS(
in uint id : SV_VertexID,
out float4 position : SV_Position,
out float2 texcoord : TEXCOORD0,
out float4 offset : TEXCOORD1)
{
PostProcessVS(id, position, texcoord);
SMAANeighborhoodBlendingVS(texcoord, offset);
}
// Pixel shaders
#ifndef SMAA_DISABLE_DEPTH_BUFFER
float SMAADepthLinearizationPS(
float4 position : SV_Position,
float2 texcoord : TEXCOORD) : SV_Target
{
return ReShade::GetLinearizedDepth(texcoord);
}
#endif
float2 SMAAEdgeDetectionWrapPS(
float4 position : SV_Position,
float2 texcoord : TEXCOORD0,
float4 offset[3] : TEXCOORD1) : SV_Target
{
#ifndef SMAA_DISABLE_DEPTH_BUFFER
if (EdgeDetectionType == 0 && SMAA_PREDICATION == true)
return SMAALumaEdgePredicationDetectionPS(texcoord, offset, colorGammaSampler, depthLinearSampler);
else
#endif
if (EdgeDetectionType == 0)
return SMAALumaEdgeDetectionPS(texcoord, offset, colorGammaSampler);
#ifndef SMAA_DISABLE_DEPTH_BUFFER
if (EdgeDetectionType == 2)
return SMAADepthEdgeDetectionPS(texcoord, offset, depthLinearSampler);
if (SMAA_PREDICATION)
return SMAAColorEdgePredicationDetectionPS(texcoord, offset, colorGammaSampler, depthLinearSampler);
else
#endif
return SMAAColorEdgeDetectionPS(texcoord, offset, colorGammaSampler);
}
float4 SMAABlendingWeightCalculationWrapPS(
float4 position : SV_Position,
float2 texcoord : TEXCOORD0,
float2 pixcoord : TEXCOORD1,
float4 offset[3] : TEXCOORD2) : SV_Target
{
return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesSampler, areaSampler, searchSampler, 0.0);
}
float3 SMAANeighborhoodBlendingWrapPS(
float4 position : SV_Position,
float2 texcoord : TEXCOORD0,
float4 offset : TEXCOORD1) : SV_Target
{
if (DebugOutput == 1)
return tex2D(edgesSampler, texcoord).rgb;
if (DebugOutput == 2)
return tex2D(blendSampler, texcoord).rgb;
return SMAANeighborhoodBlendingPS(texcoord, offset, colorLinearSampler, blendSampler).rgb;
}
// Rendering passes
technique SMAA
< ui_tooltip = "Fast SMAA skips most of the enhanced features of the shader in order to\n"
"apply its basic effect to the scene with a very low overhead.\n\n"
"There are no changes to any of the core enhanced SMAA code, only the\n"
"addition of one built-in pre-processor define plus one new one I created.\n"
"This shader can be reverted to full enhanced SMAA by deleting the 2 added\n"
"pre-processor defines if so desired. This UI tooltip is the ONLY new\n"
"piece of code in the shader."; >
{
#ifndef SMAA_DISABLE_DEPTH_BUFFER
pass LinearizeDepthPass
{
VertexShader = PostProcessVS;
PixelShader = SMAADepthLinearizationPS;
RenderTarget = depthTex;
}
#endif
pass EdgeDetectionPass
{
VertexShader = SMAAEdgeDetectionWrapVS;
PixelShader = SMAAEdgeDetectionWrapPS;
RenderTarget = edgesTex;
ClearRenderTargets = true;
StencilEnable = true;
StencilPass = REPLACE;
StencilRef = 1;
}
pass BlendWeightCalculationPass
{
VertexShader = SMAABlendingWeightCalculationWrapVS;
PixelShader = SMAABlendingWeightCalculationWrapPS;
RenderTarget = blendTex;
ClearRenderTargets = true;
StencilEnable = true;
StencilPass = KEEP;
StencilFunc = EQUAL;
StencilRef = 1;
}
pass NeighborhoodBlendingPass
{
VertexShader = SMAANeighborhoodBlendingWrapVS;
PixelShader = SMAANeighborhoodBlendingWrapPS;
StencilEnable = false;
SRGBWriteEnable = true;
}
}