Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1/**
2 * Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
3 * Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com)
4 * Copyright (C) 2013 Belen Masia (bmasia@unizar.es)
5 * Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com)
6 * Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es)
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is furnished to
13 * do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software. As clarification, there
17 * is no requirement that the copyright notice and permission be included in
18 * binary distributions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
29/**
30 * _______ ___ ___ ___ ___
31 * / || \/ | / \ / \
32 * | (---- | \ / | / ^ \ / ^ \
33 * \ \ | |\/| | / /_\ \ / /_\ \
34 * ----) | | | | | / _____ \ / _____ \
35 * |_______/ |__| |__| /__/ \__\ /__/ \__\
36 *
37 * E N H A N C E D
38 * 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
39 *
40 * http://www.iryoku.com/smaa/
41 *
42 * Hi, welcome aboard!
43 *
44 * Here you'll find instructions to get the shader up and running as fast as
45 * possible.
46 *
47 * IMPORTANT NOTICE: when updating, remember to update both this file and the
48 * precomputed textures! They may change from version to version.
49 *
50 * The shader has three passes, chained together as follows:
51 *
52 * |input|------------------�
53 * v |
54 * [ SMAA*EdgeDetection ] |
55 * v |
56 * |edgesTex| |
57 * v |
58 * [ SMAABlendingWeightCalculation ] |
59 * v |
60 * |blendTex| |
61 * v |
62 * [ SMAANeighborhoodBlending ] <------�
63 * v
64 * |output|
65 *
66 * Note that each [pass] has its own vertex and pixel shader. Remember to use
67 * oversized triangles instead of quads to avoid overshading along the
68 * diagonal.
69 *
70 * You've three edge detection methods to choose from: luma, color or depth.
71 * They represent different quality/performance and anti-aliasing/sharpness
72 * tradeoffs, so our recommendation is for you to choose the one that best
73 * suits your particular scenario:
74 *
75 * - Depth edge detection is usually the fastest but it may miss some edges.
76 *
77 * - Luma edge detection is usually more expensive than depth edge detection,
78 * but catches visible edges that depth edge detection can miss.
79 *
80 * - Color edge detection is usually the most expensive one but catches
81 * chroma-only edges.
82 *
83 * For quickstarters: just use luma edge detection.
84 *
85 * The general advice is to not rush the integration process and ensure each
86 * step is done correctly (don't try to integrate SMAA T2x with predicated edge
87 * detection from the start!). Ok then, let's go!
88 *
89 * 1. The first step is to create two RGBA temporal render targets for holding
90 * |edgesTex| and |blendTex|.
91 *
92 * In DX10 or DX11, you can use a RG render target for the edges texture.
93 * In the case of NVIDIA GPUs, using RG render targets seems to actually be
94 * slower.
95 *
96 * On the Xbox 360, you can use the same render target for resolving both
97 * |edgesTex| and |blendTex|, as they aren't needed simultaneously.
98 *
99 * 2. Both temporal render targets |edgesTex| and |blendTex| must be cleared
100 * each frame. Do not forget to clear the alpha channel!
101 *
102 * 3. The next step is loading the two supporting precalculated textures,
103 * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as
104 * C++ headers, and also as regular DDS files. They'll be needed for the
105 * 'SMAABlendingWeightCalculation' pass.
106 *
107 * If you use the C++ headers, be sure to load them in the format specified
108 * inside of them.
109 *
110 * You can also compress 'areaTex' and 'searchTex' using BC5 and BC4
111 * respectively, if you have that option in your content processor pipeline.
112 * When compressing then, you get a non-perceptible quality decrease, and a
113 * marginal performance increase.
114 *
115 * 4. All samplers must be set to linear filtering and clamp.
116 *
117 * After you get the technique working, remember that 64-bit inputs have
118 * half-rate linear filtering on GCN.
119 *
120 * If SMAA is applied to 64-bit color buffers, switching to point filtering
121 * when accessing them will increase the performance. Search for
122 * 'SMAASamplePoint' to see which textures may benefit from point
123 * filtering, and where (which is basically the color input in the edge
124 * detection and resolve passes).
125 *
126 * 5. All texture reads and buffer writes must be non-sRGB, with the exception
127 * of the input read and the output write in
128 * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in
129 * this last pass are not possible, the technique will work anyway, but
130 * will perform antialiasing in gamma space.
131 *
132 * IMPORTANT: for best results the input read for the color/luma edge
133 * detection should *NOT* be sRGB.
134 *
135 * 6. Before including SMAA.h you'll have to setup the render target metrics,
136 * the target and any optional configuration defines. Optionally you can
137 * use a preset.
138 *
139 * You have the following targets available:
140 * SMAA_HLSL_3
141 * SMAA_HLSL_4
142 * SMAA_HLSL_4_1
143 * SMAA_GLSL_3 *
144 * SMAA_GLSL_4 *
145 *
146 * * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below).
147 *
148 * And four presets:
149 * SMAA_PRESET_LOW (60% of the quality)
150 * SMAA_PRESET_MEDIUM (80% of the quality)
151 * SMAA_PRESET_HIGH (95% of the quality)
152 * SMAA_PRESET_ULTRA (99% of the quality)
153 *
154 * For example:
155 * #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0)
156 * #define SMAA_HLSL_4
157 * #define SMAA_PRESET_HIGH
158 * #include "SMAA.h"
159 *
160 * Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a
161 * uniform variable. The code is designed to minimize the impact of not
162 * using a constant value, but it is still better to hardcode it.
163 *
164 * Depending on how you encoded 'areaTex' and 'searchTex', you may have to
165 * add (and customize) the following defines before including SMAA.h:
166 * #define SMAA_AREATEX_SELECT(sample) sample.rg
167 * #define SMAA_SEARCHTEX_SELECT(sample) sample.r
168 *
169 * If your engine is already using porting macros, you can define
170 * SMAA_CUSTOM_SL, and define the porting functions by yourself.
171 *
172 * 7. Then, you'll have to setup the passes as indicated in the scheme above.
173 * You can take a look into SMAA.fx, to see how we did it for our demo.
174 * Checkout the function wrappers, you may want to copy-paste them!
175 *
176 * 8. It's recommended to validate the produced |edgesTex| and |blendTex|.
177 * You can use a screenshot from your engine to compare the |edgesTex|
178 * and |blendTex| produced inside of the engine with the results obtained
179 * with the reference demo.
180 *
181 * 9. After you get the last pass to work, it's time to optimize. You'll have
182 * to initialize a stencil buffer in the first pass (discard is already in
183 * the code), then mask execution by using it the second pass. The last
184 * pass should be executed in all pixels.
185 *
186 *
187 * After this point you can choose to enable predicated thresholding,
188 * temporal supersampling and motion blur integration:
189 *
190 * a) If you want to use predicated thresholding, take a look into
191 * SMAA_PREDICATION; you'll need to pass an extra texture in the edge
192 * detection pass.
193 *
194 * b) If you want to enable temporal supersampling (SMAA T2x):
195 *
196 * 1. The first step is to render using subpixel jitters. I won't go into
197 * detail, but it's as simple as moving each vertex position in the
198 * vertex shader, you can check how we do it in our DX10 demo.
199 *
200 * 2. Then, you must setup the temporal resolve. You may want to take a look
201 * into SMAAResolve for resolving 2x modes. After you get it working, you'll
202 * probably see ghosting everywhere. But fear not, you can enable the
203 * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro.
204 * Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded.
205 *
206 * 3. The next step is to apply SMAA to each subpixel jittered frame, just as
207 * done for 1x.
208 *
209 * 4. At this point you should already have something usable, but for best
210 * results the proper area textures must be set depending on current jitter.
211 * For this, the parameter 'subsampleIndices' of
212 * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x
213 * mode:
214 *
215 * @SUBSAMPLE_INDICES
216 *
217 * | S# | Camera Jitter | subsampleIndices |
218 * +----+------------------+---------------------+
219 * | 0 | ( 0.25, -0.25) | float4(1, 1, 1, 0) |
220 * | 1 | (-0.25, 0.25) | float4(2, 2, 2, 0) |
221 *
222 * These jitter positions assume a bottom-to-top y axis. S# stands for the
223 * sample number.
224 *
225 * More information about temporal supersampling here:
226 * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
227 *
228 * c) If you want to enable spatial multisampling (SMAA S2x):
229 *
230 * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be
231 * created with:
232 * - DX10: see below (*)
233 * - DX10.1: D3D10_STANDARD_MULTISAMPLE_PATTERN or
234 * - DX11: D3D11_STANDARD_MULTISAMPLE_PATTERN
235 *
236 * This allows to ensure that the subsample order matches the table in
237 * @SUBSAMPLE_INDICES.
238 *
239 * (*) In the case of DX10, we refer the reader to:
240 * - SMAA::detectMSAAOrder and
241 * - SMAA::msaaReorder
242 *
243 * These functions allow to match the standard multisample patterns by
244 * detecting the subsample order for a specific GPU, and reordering
245 * them appropriately.
246 *
247 * 2. A shader must be run to output each subsample into a separate buffer
248 * (DX10 is required). You can use SMAASeparate for this purpose, or just do
249 * it in an existing pass (for example, in the tone mapping pass, which has
250 * the advantage of feeding tone mapped subsamples to SMAA, which will yield
251 * better results).
252 *
253 * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing
254 * the results in the final buffer. The second run should alpha blend with
255 * the existing final buffer using a blending factor of 0.5.
256 * 'subsampleIndices' must be adjusted as in the SMAA T2x case (see point
257 * b).
258 *
259 * d) If you want to enable temporal supersampling on top of SMAA S2x
260 * (which actually is SMAA 4x):
261 *
262 * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is
263 * to calculate SMAA S2x for current frame. In this case, 'subsampleIndices'
264 * must be set as follows:
265 *
266 * | F# | S# | Camera Jitter | Net Jitter | subsampleIndices |
267 * +----+----+--------------------+-------------------+----------------------+
268 * | 0 | 0 | ( 0.125, 0.125) | ( 0.375, -0.125) | float4(5, 3, 1, 3) |
269 * | 0 | 1 | ( 0.125, 0.125) | (-0.125, 0.375) | float4(4, 6, 2, 3) |
270 * +----+----+--------------------+-------------------+----------------------+
271 * | 1 | 2 | (-0.125, -0.125) | ( 0.125, -0.375) | float4(3, 5, 1, 4) |
272 * | 1 | 3 | (-0.125, -0.125) | (-0.375, 0.125) | float4(6, 4, 2, 4) |
273 *
274 * These jitter positions assume a bottom-to-top y axis. F# stands for the
275 * frame number. S# stands for the sample number.
276 *
277 * 2. After calculating SMAA S2x for current frame (with the new subsample
278 * indices), previous frame must be reprojected as in SMAA T2x mode (see
279 * point b).
280 *
281 * e) If motion blur is used, you may want to do the edge detection pass
282 * together with motion blur. This has two advantages:
283 *
284 * 1. Pixels under heavy motion can be omitted from the edge detection process.
285 * For these pixels we can just store "no edge", as motion blur will take
286 * care of them.
287 * 2. The center pixel tap is reused.
288 *
289 * Note that in this case depth testing should be used instead of stenciling,
290 * as we have to write all the pixels in the motion blur pass.
291 *
292 * That's it!
293 */
295struct SmaaInfo {
296 rt_metrics: vec4<f32>,
299struct VertexVaryings {
300 clip_coord: vec2<f32>,
301 tex_coord: vec2<f32>,
304struct EdgeDetectionVaryings {
305 @builtin(position) position: vec4<f32>,
306 @location(0) offset_0: vec4<f32>,
307 @location(1) offset_1: vec4<f32>,
308 @location(2) offset_2: vec4<f32>,
309 @location(3) tex_coord: vec2<f32>,
312struct BlendingWeightCalculationVaryings {
313 @builtin(position) position: vec4<f32>,
314 @location(0) offset_0: vec4<f32>,
315 @location(1) offset_1: vec4<f32>,
316 @location(2) offset_2: vec4<f32>,
317 @location(3) tex_coord: vec2<f32>,
320struct NeighborhoodBlendingVaryings {
321 @builtin(position) position: vec4<f32>,
322 @location(0) offset: vec4<f32>,
323 @location(1) tex_coord: vec2<f32>,
326@group(0) @binding(0) var color_texture: texture_2d<f32>;
327@group(0) @binding(1) var<uniform> smaa_info: SmaaInfo;
329@if(SMAA_EDGE_DETECTION)
330@group(1) @binding(0) var color_sampler: sampler;
332@if(SMAA_BLENDING_WEIGHT_CALCULATION) {
333@group(1) @binding(0) var edges_texture: texture_2d<f32>;
334@group(1) @binding(1) var edges_sampler: sampler;
335@group(1) @binding(2) var search_texture: texture_2d<f32>;
336@group(1) @binding(3) var area_texture: texture_2d<f32>;
339@if(SMAA_NEIGHBORHOOD_BLENDING) {
340@group(1) @binding(0) var blend_texture: texture_2d<f32>;
341@group(1) @binding(1) var blend_sampler: sampler;
344//-----------------------------------------------------------------------------
345// SMAA Presets
347@if(SMAA_PRESET_LOW)
348const SMAA_THRESHOLD: f32 = 0.15;
349@elif(SMAA_PRESET_MEDIUM)
350const SMAA_THRESHOLD: f32 = 0.1;
351@elif(SMAA_PRESET_HIGH)
352const SMAA_THRESHOLD: f32 = 0.1;
353@elif(SMAA_PRESET_ULTRA)
354const SMAA_THRESHOLD: f32 = 0.05;
355@else
356const SMAA_THRESHOLD: f32 = 0.1;
357@if(SMAA_PRESET_LOW)
358const SMAA_MAX_SEARCH_STEPS: u32 = 4u;
359@elif(SMAA_PRESET_MEDIUM)
360const SMAA_MAX_SEARCH_STEPS: u32 = 8u;
361@elif(SMAA_PRESET_HIGH)
362const SMAA_MAX_SEARCH_STEPS: u32 = 16u;
363@elif(SMAA_PRESET_ULTRA)
364const SMAA_MAX_SEARCH_STEPS: u32 = 32u;
365@else
366const SMAA_MAX_SEARCH_STEPS: u32 = 16u;
367@if(SMAA_PRESET_HIGH)
368const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u;
369@elif(SMAA_PRESET_ULTRA)
370const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 16u;
371@elif(!SMAA_PRESET_LOW && !SMAA_PRESET_MEDIUM)
372const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u;
373@if(SMAA_PRESET_HIGH)
374const SMAA_CORNER_ROUNDING: u32 = 25u;
375@elif(SMAA_PRESET_ULTRA)
376const SMAA_CORNER_ROUNDING: u32 = 25u;
377@elif(!SMAA_PRESET_LOW && !SMAA_PRESET_MEDIUM)
378const SMAA_CORNER_ROUNDING: u32 = 25u;
380//-----------------------------------------------------------------------------
381// Configurable Defines
383/**
384 * SMAA_THRESHOLD specifies the threshold or sensitivity to edges.
385 * Lowering this value you will be able to detect more edges at the expense of
386 * performance.
387 *
388 * Range: [0, 0.5]
389 * 0.1 is a reasonable value, and allows to catch most visible edges.
390 * 0.05 is a rather overkill value, that allows to catch 'em all.
391 *
392 * If temporal supersampling is used, 0.2 could be a reasonable value, as low
393 * contrast edges are properly filtered by just 2x.
394 */
395// (In the WGSL version of this shader, `SMAA_THRESHOLD` is set above, in "SMAA
396// Presets".)
398/**
399 * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
400 * horizontal/vertical pattern searches, at each side of the pixel.
401 *
402 * In number of pixels, it's actually the double. So the maximum line length
403 * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
404 * longer lines won't look as good, but still antialiased).
405 *
406 * Range: [0, 112]
407 */
408// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS` is set above, in
409// "SMAA Presets".)
411/**
412 * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
413 * diagonal pattern searches, at each side of the pixel. In this case we jump
414 * one pixel at time, instead of two.
415 *
416 * Range: [0, 20]
417 *
418 * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16
419 * steps), but it can have a significant impact on older machines.
420 *
421 * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing.
422 */
423// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS_DIAG` is set
424// above, in "SMAA Presets".)
426/**
427 * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded.
428 *
429 * Range: [0, 100]
430 *
431 * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing.
432 */
433// (In the WGSL version of this shader, `SMAA_CORNER_ROUNDING` is set above, in
434// "SMAA Presets".)
436/**
437 * If there is a neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times
438 * bigger contrast than current edge, current edge will be discarded.
439 *
440 * This allows to eliminate spurious crossing edges, and is based on the fact
441 * that, if there is too much contrast in a direction, that will hide
442 * perceptually contrast in the other neighbors.
443 */
444const SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR: f32 = 2.0;
446//-----------------------------------------------------------------------------
447// Non-Configurable Defines
449const SMAA_AREATEX_MAX_DISTANCE: f32 = 16.0;
450const SMAA_AREATEX_MAX_DISTANCE_DIAG: f32 = 20.0;
451const SMAA_AREATEX_PIXEL_SIZE: vec2<f32> = (1.0 / vec2<f32>(160.0, 560.0));
452const SMAA_AREATEX_SUBTEX_SIZE: f32 = (1.0 / 7.0);
453const SMAA_SEARCHTEX_SIZE: vec2<f32> = vec2(66.0, 33.0);
454const SMAA_SEARCHTEX_PACKED_SIZE: vec2<f32> = vec2(64.0, 16.0);
456@if(!SMAA_DISABLE_CORNER_DETECTION)
457const SMAA_CORNER_ROUNDING_NORM: f32 = f32(SMAA_CORNER_ROUNDING) / 100.0;
459//-----------------------------------------------------------------------------
460// WGSL-Specific Functions
462/// This vertex shader produces the following, when drawn using indices 0..3:
463///
464/// 1 | 0-----x.....2
465/// 0 | | s | . ´
466/// -1 | x_____x´
467/// -2 | : .´
468/// -3 | 1´
469/// +---------------
470/// -1 0 1 2 3
471///
472/// The axes are clip-space x and y. The region marked s is the visible region.
473/// The digits in the corners of the right-angled triangle are the vertex
474/// indices.
475///
476/// The top-left has UV 0,0, the bottom-left has 0,2, and the top-right has 2,0.
477/// This means that the UV gets interpolated to 1,1 at the bottom-right corner
478/// of the clip-space rectangle that is at 1,-1 in clip space.
479fn calculate_vertex_varyings(vertex_index: u32) -> VertexVaryings {
480 // See the explanation above for how this works
481 let uv = vec2<f32>(f32(vertex_index >> 1u), f32(vertex_index & 1u)) * 2.0;
482 let clip_position = vec2<f32>(uv * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0));
484 return VertexVaryings(clip_position, uv);
487//-----------------------------------------------------------------------------
488// Vertex Shaders
490@if(SMAA_EDGE_DETECTION)
492/// Edge Detection Vertex Shader
493@vertex
494fn edge_detection_vertex_main(@builtin(vertex_index) vertex_index: u32) -> EdgeDetectionVaryings {
495 let varyings = calculate_vertex_varyings(vertex_index);
497 var edge_detection_varyings = EdgeDetectionVaryings();
498 edge_detection_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0);
499 edge_detection_varyings.tex_coord = varyings.tex_coord;
501 edge_detection_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) +
502 varyings.tex_coord.xyxy;
503 edge_detection_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) +
504 varyings.tex_coord.xyxy;
505 edge_detection_varyings.offset_2 = smaa_info.rt_metrics.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) +
506 varyings.tex_coord.xyxy;
508 return edge_detection_varyings;
512@if(SMAA_BLENDING_WEIGHT_CALCULATION)
514/// Blend Weight Calculation Vertex Shader
515@vertex
516fn blending_weight_calculation_vertex_main(@builtin(vertex_index) vertex_index: u32)
517 -> BlendingWeightCalculationVaryings {
518 let varyings = calculate_vertex_varyings(vertex_index);
520 var weight_varyings = BlendingWeightCalculationVaryings();
521 weight_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0);
522 weight_varyings.tex_coord = varyings.tex_coord;
524 // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
525 weight_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) +
526 varyings.tex_coord.xyxy;
527 weight_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) +
528 varyings.tex_coord.xyxy;
530 // And these for the searches, they indicate the ends of the loops:
531 weight_varyings.offset_2 =
532 smaa_info.rt_metrics.xxyy * vec4(-2.0, 2.0, -2.0, 2.0) * f32(SMAA_MAX_SEARCH_STEPS) +
533 vec4(weight_varyings.offset_0.xz, weight_varyings.offset_1.yw);
535 return weight_varyings;
539@if(SMAA_NEIGHBORHOOD_BLENDING)
541/// Neighborhood Blending Vertex Shader
542@vertex
543fn neighborhood_blending_vertex_main(@builtin(vertex_index) vertex_index: u32)
544 -> NeighborhoodBlendingVaryings {
545 let varyings = calculate_vertex_varyings(vertex_index);
546 let offset = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) + varyings.tex_coord.xyxy;
547 return NeighborhoodBlendingVaryings(
548 vec4(varyings.clip_coord, 0.0, 1.0),
549 offset,
550 varyings.tex_coord
551 );
555//-----------------------------------------------------------------------------
556// Edge Detection Pixel Shaders (First Pass)
558@if(SMAA_EDGE_DETECTION)
560/// Luma Edge Detection
561///
562/// IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
563/// thus 'color_texture' should be a non-sRGB texture.
564@fragment
565fn luma_edge_detection_fragment_main(in: EdgeDetectionVaryings) -> @location(0) vec4<f32> {
566 // Calculate the threshold:
567 // TODO: Predication.
568 let threshold = vec2(SMAA_THRESHOLD);
570 // Calculate luma:
571 let weights = vec3(0.2126, 0.7152, 0.0722);
572 let L = dot(textureSample(color_texture, color_sampler, in.tex_coord).rgb, weights);
574 let Lleft = dot(textureSample(color_texture, color_sampler, in.offset_0.xy).rgb, weights);
575 let Ltop = dot(textureSample(color_texture, color_sampler, in.offset_0.zw).rgb, weights);
577 // We do the usual threshold:
578 var delta: vec4<f32> = vec4(abs(L - vec2(Lleft, Ltop)), 0.0, 0.0);
579 var edges = step(threshold, delta.xy);
581 // Then discard if there is no edge:
582 if (dot(edges, vec2(1.0)) == 0.0) {
583 discard;
584 }
586 // Calculate right and bottom deltas:
587 let Lright = dot(textureSample(color_texture, color_sampler, in.offset_1.xy).rgb, weights);
588 let Lbottom = dot(textureSample(color_texture, color_sampler, in.offset_1.zw).rgb, weights);
589 delta = vec4(delta.xy, abs(L - vec2(Lright, Lbottom)));
591 // Calculate the maximum delta in the direct neighborhood:
592 var max_delta = max(delta.xy, delta.zw);
594 // Calculate left-left and top-top deltas:
595 let Lleftleft = dot(textureSample(color_texture, color_sampler, in.offset_2.xy).rgb, weights);
596 let Ltoptop = dot(textureSample(color_texture, color_sampler, in.offset_2.zw).rgb, weights);
597 delta = vec4(delta.xy, abs(vec2(Lleft, Ltop) - vec2(Lleftleft, Ltoptop)));
599 // Calculate the final maximum delta:
600 max_delta = max(max_delta.xy, delta.zw);
601 let final_delta = max(max_delta.x, max_delta.y);
603 // Local contrast adaptation:
604 edges *= step(vec2(final_delta), SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
606 return vec4(edges, 0.0, 1.0);
611//-----------------------------------------------------------------------------
612// Diagonal Search Functions
614@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
616/// Allows to decode two binary values from a bilinear-filtered access.
617fn decode_diag_bilinear_access_2(in_e: vec2<f32>) -> vec2<f32> {
618 // Bilinear access for fetching 'e' have a 0.25 offset, and we are
619 // interested in the R and G edges:
620 //
621 // +---G---+-------+
622 // | x o R x |
623 // +-------+-------+
624 //
625 // Then, if one of these edge is enabled:
626 // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0
627 // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0
628 //
629 // This function will unpack the values (mad + mul + round):
630 // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1
631 var e = in_e;
632 e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
633 return round(e);
636@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
637fn decode_diag_bilinear_access_4(e: vec4<f32>) -> vec4<f32> {
638 let e_rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
639 return round(vec4(e_rb.x, e.g, e_rb.y, e.a));
642/**
643 * These functions allows to perform diagonal pattern searches.
644 */
645@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
646fn search_diag_1(tex_coord: vec2<f32>, dir: vec2<f32>, e: ptr<function, vec2<f32>>) -> vec2<f32> {
647 var coord = vec4(tex_coord, -1.0, 1.0);
648 let t = vec3(smaa_info.rt_metrics.xy, 1.0);
649 while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) {
650 coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w);
651 *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg;
652 coord.w = dot(*e, vec2(0.5));
653 }
654 return coord.zw;
657@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
658fn search_diag_2(tex_coord: vec2<f32>, dir: vec2<f32>, e: ptr<function, vec2<f32>>) -> vec2<f32> {
659 var coord = vec4(tex_coord, -1.0, 1.0);
660 coord.x += 0.25 * smaa_info.rt_metrics.x; // See @SearchDiag2Optimization
661 let t = vec3(smaa_info.rt_metrics.xy, 1.0);
662 while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) {
663 coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w);
665 // @SearchDiag2Optimization
666 // Fetch both edges at once using bilinear filtering:
667 *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg;
668 *e = decode_diag_bilinear_access_2(*e);
670 // Non-optimized version:
671 // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g;
672 // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r;
674 coord.w = dot(*e, vec2(0.5));
675 }
676 return coord.zw;
679/// Similar to SMAAArea, this calculates the area corresponding to a certain
680/// diagonal distance and crossing edges 'e'.
681@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
682fn area_diag(dist: vec2<f32>, e: vec2<f32>, offset: f32) -> vec2<f32> {
683 var tex_coord = vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG) * e + dist;
685 // We do a scale and bias for mapping to texel space:
686 tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
688 // Diagonal areas are on the second half of the texture:
689 tex_coord.x += 0.5;
691 // Move to proper place, according to the subpixel offset:
692 tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
694 // Do it!
695 return textureSampleLevel(area_texture, edges_sampler, tex_coord, 0.0).rg;
698/// This searches for diagonal patterns and returns the corresponding weights.
699@if(SMAA_BLENDING_WEIGHT_CALCULATION && !SMAA_DISABLE_DIAG_DETECTION)
700fn calculate_diag_weights(tex_coord: vec2<f32>, e: vec2<f32>, subsample_indices: vec4<f32>)
701 -> vec2<f32> {
702 var weights = vec2(0.0, 0.0);
704 // Search for the line ends:
705 var d = vec4(0.0);
706 var end = vec2(0.0);
707 if (e.r > 0.0) {
708 let d_xz = search_diag_1(tex_coord, vec2(-1.0, 1.0), &end);
709 d = vec4(d_xz.x, d.y, d_xz.y, d.w);
710 d.x += f32(end.y > 0.9);
711 } else {
712 d = vec4(0.0, d.y, 0.0, d.w);
713 }
714 let d_yw = search_diag_1(tex_coord, vec2(1.0, -1.0), &end);
715 d = vec4(d.x, d_yw.x, d.y, d_yw.y);
717 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
718 // Fetch the crossing edges:
719 let coords = vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25) * smaa_info.rt_metrics.xyxy +
720 tex_coord.xyxy;
721 var c = vec4(
722 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).rg,
723 textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).rg,
724 );
725 let c_yxwz = decode_diag_bilinear_access_4(c.xyzw);
726 c = c_yxwz.yxwz;
728 // Non-optimized version:
729 // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
730 // float4 c;
731 // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
732 // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r;
733 // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g;
734 // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r;
736 // Merge crossing edges at each side into a single value:
737 var cc = vec2(2.0) * c.xz + c.yw;
739 // Remove the crossing edge if we didn't found the end of the line:
740 cc = select(cc, vec2(0.0, 0.0), vec2<bool>(step(vec2(0.9), d.zw)));
742 // Fetch the areas for this line:
743 weights += area_diag(d.xy, cc, subsample_indices.z);
744 }
746 // Search for the line ends:
747 let d_xz = search_diag_2(tex_coord, vec2(-1.0, -1.0), &end);
748 if (textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0, vec2(1, 0)).r > 0.0) {
749 let d_yw = search_diag_2(tex_coord, vec2(1.0, 1.0), &end);
750 d = vec4(d_xz.x, d_yw.x, d_xz.y, d_yw.y);
751 d.y += f32(end.y > 0.9);
752 } else {
753 d = vec4(d_xz.x, 0.0, d_xz.y, 0.0);
754 }
756 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
757 // Fetch the crossing edges:
758 let coords = vec4(-d.x, -d.x, d.y, d.y) * smaa_info.rt_metrics.xyxy + tex_coord.xyxy;
759 let c = vec4(
760 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).g,
761 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2( 0, -1)).r,
762 textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).gr,
763 );
764 var cc = vec2(2.0) * c.xz + c.yw;
766 // Remove the crossing edge if we didn't found the end of the line:
767 cc = select(cc, vec2(0.0, 0.0), vec2<bool>(step(vec2(0.9), d.zw)));
769 // Fetch the areas for this line:
770 weights += area_diag(d.xy, cc, subsample_indices.w).gr;
771 }
773 return weights;
777//-----------------------------------------------------------------------------
778// Horizontal/Vertical Search Functions
780/// This allows to determine how much length should we add in the last step
781/// of the searches. It takes the bilinearly interpolated edge (see
782/// @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
783/// crossing edges are active.
784@if(SMAA_BLENDING_WEIGHT_CALCULATION)
785fn search_length(e: vec2<f32>, offset: f32) -> f32 {
786 // The texture is flipped vertically, with left and right cases taking half
787 // of the space horizontally:
788 var scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0);
789 var bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0);
791 // Scale and bias to access texel centers:
792 scale += vec2(-1.0, 1.0);
793 bias += vec2( 0.5, -0.5);
795 // Convert from pixel coordinates to texcoords:
796 // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped)
797 scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
798 bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
800 // Lookup the search texture:
801 return textureSampleLevel(search_texture, edges_sampler, scale * e + bias, 0.0).r;
804/// Horizontal/vertical search functions for the 2nd pass.
805@if(SMAA_BLENDING_WEIGHT_CALCULATION)
806fn search_x_left(in_tex_coord: vec2<f32>, end: f32) -> f32 {
807 var tex_coord = in_tex_coord;
809 /**
810 * @PSEUDO_GATHER4
811 * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
812 * sample between edge, thus fetching four edges in a row.
813 * Sampling with different offsets in each direction allows to disambiguate
814 * which edges are active from the four fetched ones.
815 */
816 var e = vec2(0.0, 1.0);
817 while (tex_coord.x > end &&
818 e.g > 0.8281 && // Is there some edge not activated?
819 e.r == 0.0) { // Or is there a crossing edge that breaks the line?
820 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
821 tex_coord += -vec2(2.0, 0.0) * smaa_info.rt_metrics.xy;
822 }
823 let offset = -(255.0 / 127.0) * search_length(e, 0.0) + 3.25;
824 return smaa_info.rt_metrics.x * offset + tex_coord.x;
827@if(SMAA_BLENDING_WEIGHT_CALCULATION)
828fn search_x_right(in_tex_coord: vec2<f32>, end: f32) -> f32 {
829 var tex_coord = in_tex_coord;
831 var e = vec2(0.0, 1.0);
832 while (tex_coord.x < end &&
833 e.g > 0.8281 && // Is there some edge not activated?
834 e.r == 0.0) { // Or is there a crossing edge that breaks the line?
835 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
836 tex_coord += vec2(2.0, 0.0) * smaa_info.rt_metrics.xy;
837 }
838 let offset = -(255.0 / 127.0) * search_length(e, 0.5) + 3.25;
839 return -smaa_info.rt_metrics.x * offset + tex_coord.x;
842@if(SMAA_BLENDING_WEIGHT_CALCULATION)
843fn search_y_up(in_tex_coord: vec2<f32>, end: f32) -> f32 {
844 var tex_coord = in_tex_coord;
846 var e = vec2(1.0, 0.0);
847 while (tex_coord.y > end &&
848 e.r > 0.8281 && // Is there some edge not activated?
849 e.g == 0.0) { // Or is there a crossing edge that breaks the line?
850 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
851 tex_coord += -vec2(0.0, 2.0) * smaa_info.rt_metrics.xy;
852 }
853 let offset = -(255.0 / 127.0) * search_length(e.gr, 0.0) + 3.25;
854 return smaa_info.rt_metrics.y * offset + tex_coord.y;
857@if(SMAA_BLENDING_WEIGHT_CALCULATION)
858fn search_y_down(in_tex_coord: vec2<f32>, end: f32) -> f32 {
859 var tex_coord = in_tex_coord;
861 var e = vec2(1.0, 0.0);
862 while (tex_coord.y < end &&
863 e.r > 0.8281 && // Is there some edge not activated?
864 e.g == 0.0) { // Or is there a crossing edge that breaks the line?
865 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
866 tex_coord += vec2(0.0, 2.0) * smaa_info.rt_metrics.xy;
867 }
868 let offset = -(255.0 / 127.0) * search_length(e.gr, 0.5) + 3.25;
869 return -smaa_info.rt_metrics.y * offset + tex_coord.y;
872/// Ok, we have the distance and both crossing edges. So, what are the areas
873/// at each side of current edge?
874@if(SMAA_BLENDING_WEIGHT_CALCULATION)
875fn area(dist: vec2<f32>, e1: f32, e2: f32, offset: f32) -> vec2<f32> {
876 // Rounding prevents precision errors of bilinear filtering:
877 var tex_coord = SMAA_AREATEX_MAX_DISTANCE * round(4.0 * vec2(e1, e2)) + dist;
879 // We do a scale and bias for mapping to texel space:
880 tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
882 // Move to proper place, according to the subpixel offset:
883 tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
885 // Do it!
886 return textureSampleLevel(area_texture, edges_sampler, tex_coord, 0.0).rg;
889//-----------------------------------------------------------------------------
890// Corner Detection Functions
892@if(SMAA_BLENDING_WEIGHT_CALCULATION)
893fn detect_horizontal_corner_pattern(weights: vec2<f32>, tex_coord: vec4<f32>, d: vec2<f32>)
894 -> vec2<f32> {
895@if(!SMAA_DISABLE_CORNER_DETECTION) {
896 let left_right = step(d.xy, d.yx);
897 var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
899 rounding /= left_right.x + left_right.y; // Reduce blending for pixels in the center of a line.
901 var factor = vec2(1.0, 1.0);
902 factor.x -= rounding.x *
903 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, 1)).r;
904 factor.x -= rounding.y *
905 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, 1)).r;
906 factor.y -= rounding.x *
907 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, -2)).r;
908 factor.y -= rounding.y *
909 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, -2)).r;
911 return weights * saturate(factor);
912} @else {
913 return weights;
917@if(SMAA_BLENDING_WEIGHT_CALCULATION)
918fn detect_vertical_corner_pattern(weights: vec2<f32>, tex_coord: vec4<f32>, d: vec2<f32>)
919 -> vec2<f32> {
920@if(!SMAA_DISABLE_CORNER_DETECTION) {
921 let left_right = step(d.xy, d.yx);
922 var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
924 rounding /= left_right.x + left_right.y;
926 var factor = vec2(1.0, 1.0);
927 factor.x -= rounding.x *
928 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2( 1, 0)).g;
929 factor.x -= rounding.y *
930 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2( 1, 1)).g;
931 factor.y -= rounding.x *
932 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(-2, 0)).g;
933 factor.y -= rounding.y *
934 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(-2, 1)).g;
936 return weights * saturate(factor);
937} @else {
938 return weights;
942//-----------------------------------------------------------------------------
943// Blending Weight Calculation Pixel Shader (Second Pass)
945@if(SMAA_BLENDING_WEIGHT_CALCULATION)
946@fragment
947fn blending_weight_calculation_fragment_main(in: BlendingWeightCalculationVaryings)
948 -> @location(0) vec4<f32> {
949 let subsample_indices = vec4(0.0); // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
951 var weights = vec4(0.0);
953 var e = textureSample(edges_texture, edges_sampler, in.tex_coord).rg;
955 if (e.g > 0.0) { // Edge at north
956@if(!SMAA_DISABLE_DIAG_DETECTION) {
957 // Diagonals have both north and west edges, so searching for them in
958 // one of the boundaries is enough.
959 weights = vec4(calculate_diag_weights(in.tex_coord, e, subsample_indices), weights.ba);
961 // We give priority to diagonals, so if we find a diagonal we skip
962 // horizontal/vertical processing.
963 if (weights.r + weights.g != 0.0) {
964 return weights;
965 }
968 var d: vec2<f32>;
970 // Find the distance to the left:
971 var coords: vec3<f32>;
972 coords.x = search_x_left(in.offset_0.xy, in.offset_2.x);
973 // in.offset_1.y = in.tex_coord.y - 0.25 * smaa_info.rt_metrics.y (@CROSSING_OFFSET)
974 coords.y = in.offset_1.y;
975 d.x = coords.x;
977 // Now fetch the left crossing edges, two at a time using bilinear
978 // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
979 // discern what value each edge has:
980 let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).r;
982 // Find the distance to the right:
983 coords.z = search_x_right(in.offset_0.zw, in.offset_2.y);
984 d.y = coords.z;
986 // We want the distances to be in pixel units (doing this here allow to
987 // better interleave arithmetic and memory accesses):
988 d = abs(round(smaa_info.rt_metrics.zz * d - in.position.xx));
990 // SMAAArea below needs a sqrt, as the areas texture is compressed
991 // quadratically:
992 let sqrt_d = sqrt(d);
994 // Fetch the right crossing edges:
995 let e2 = textureSampleLevel(
996 edges_texture, edges_sampler, coords.zy, 0.0, vec2<i32>(1, 0)).r;
998 // Ok, we know how this pattern looks like, now it is time for getting
999 // the actual area:
1000 weights = vec4(area(sqrt_d, e1, e2, subsample_indices.y), weights.ba);
1002 // Fix corners:
1003 coords.y = in.tex_coord.y;
1004 weights = vec4(
1005 detect_horizontal_corner_pattern(weights.rg, coords.xyzy, d),
1006 weights.ba
1007 );
1010 if (e.r > 0.0) { // Edge at west
1011 var d: vec2<f32>;
1013 // Find the distance to the top:
1014 var coords: vec3<f32>;
1015 coords.y = search_y_up(in.offset_1.xy, in.offset_2.z);
1016 // in.offset_1.x = in.tex_coord.x - 0.25 * smaa_info.rt_metrics.x
1017 coords.x = in.offset_0.x;
1018 d.x = coords.y;
1020 // Fetch the top crossing edges:
1021 let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).g;
1023 // Find the distance to the bottom:
1024 coords.z = search_y_down(in.offset_1.zw, in.offset_2.w);
1025 d.y = coords.z;
1027 // We want the distances to be in pixel units:
1028 d = abs(round(smaa_info.rt_metrics.ww * d - in.position.yy));
1030 // SMAAArea below needs a sqrt, as the areas texture is compressed
1031 // quadratically:
1032 let sqrt_d = sqrt(d);
1034 // Fetch the bottom crossing edges:
1035 let e2 = textureSampleLevel(
1036 edges_texture, edges_sampler, coords.xz, 0.0, vec2<i32>(0, 1)).g;
1038 // Get the area for this direction:
1039 weights = vec4(weights.rg, area(sqrt_d, e1, e2, subsample_indices.x));
1041 // Fix corners:
1042 coords.x = in.tex_coord.x;
1043 weights = vec4(weights.rg, detect_vertical_corner_pattern(weights.ba, coords.xyxz, d));
1046 return weights;
1050//-----------------------------------------------------------------------------
1051// Neighborhood Blending Pixel Shader (Third Pass)
1053@if(SMAA_NEIGHBORHOOD_BLENDING)
1054@fragment
1055fn neighborhood_blending_fragment_main(in: NeighborhoodBlendingVaryings) -> @location(0) vec4<f32> {
1056 // Fetch the blending weights for current pixel:
1057 let a = vec4(
1058 textureSample(blend_texture, blend_sampler, in.offset.xy).a, // Right
1059 textureSample(blend_texture, blend_sampler, in.offset.zw).g, // Top
1060 textureSample(blend_texture, blend_sampler, in.tex_coord).zx, // Bottom / Left
1061 );
1063 // Is there any blending weight with a value greater than 0.0?
1064 if (dot(a, vec4(1.0)) < 1.0e-5) {
1065 let color = textureSampleLevel(color_texture, blend_sampler, in.tex_coord, 0.0);
1066 // TODO: Reprojection
1067 return color;
1068 } else {
1069 let h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical)
1071 // Calculate the blending offsets:
1072 var blending_offset = vec4(0.0, a.y, 0.0, a.w);
1073 var blending_weight = a.yw;
1074 blending_offset = select(blending_offset, vec4(a.x, 0.0, a.z, 0.0), h);
1075 blending_weight = select(blending_weight, a.xz, h);
1076 blending_weight /= dot(blending_weight, vec2(1.0));
1078 // Calculate the texture coordinates:
1079 let blending_coord =
1080 blending_offset * vec4(smaa_info.rt_metrics.xy, -smaa_info.rt_metrics.xy) +
1081 in.tex_coord.xyxy;
1083 // We exploit bilinear filtering to mix current pixel with the chosen
1084 // neighbor:
1085 var color = blending_weight.x *
1086 textureSampleLevel(color_texture, blend_sampler, blending_coord.xy, 0.0);
1087 color += blending_weight.y *
1088 textureSampleLevel(color_texture, blend_sampler, blending_coord.zw, 0.0);
1090 // TODO: Reprojection
1092 return color;
Contents