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#ifdef SMAA_EDGE_DETECTION
330@group(1) @binding(0) var color_sampler: sampler;
331#endif // SMAA_EDGE_DETECTION
333#ifdef SMAA_BLENDING_WEIGHT_CALCULATION
334@group(1) @binding(0) var edges_texture: texture_2d<f32>;
335@group(1) @binding(1) var edges_sampler: sampler;
336@group(1) @binding(2) var search_texture: texture_2d<f32>;
337@group(1) @binding(3) var area_texture: texture_2d<f32>;
338#endif // SMAA_BLENDING_WEIGHT_CALCULATION
340#ifdef SMAA_NEIGHBORHOOD_BLENDING
341@group(1) @binding(0) var blend_texture: texture_2d<f32>;
342@group(1) @binding(1) var blend_sampler: sampler;
343#endif // SMAA_NEIGHBORHOOD_BLENDING
345//-----------------------------------------------------------------------------
346// SMAA Presets
348#ifdef SMAA_PRESET_LOW
349const SMAA_THRESHOLD: f32 = 0.15;
350const SMAA_MAX_SEARCH_STEPS: u32 = 4u;
351#define SMAA_DISABLE_DIAG_DETECTION
352#define SMAA_DISABLE_CORNER_DETECTION
353#else ifdef SMAA_PRESET_MEDIUM // SMAA_PRESET_LOW
354const SMAA_THRESHOLD: f32 = 0.1;
355const SMAA_MAX_SEARCH_STEPS: u32 = 8u;
356#define SMAA_DISABLE_DIAG_DETECTION
357#define SMAA_DISABLE_CORNER_DETECTION
358#else ifdef SMAA_PRESET_HIGH // SMAA_PRESET_MEDIUM
359const SMAA_THRESHOLD: f32 = 0.1;
360const SMAA_MAX_SEARCH_STEPS: u32 = 16u;
361const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u;
362const SMAA_CORNER_ROUNDING: u32 = 25u;
363#else ifdef SMAA_PRESET_ULTRA // SMAA_PRESET_HIGH
364const SMAA_THRESHOLD: f32 = 0.05;
365const SMAA_MAX_SEARCH_STEPS: u32 = 32u;
366const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 16u;
367const SMAA_CORNER_ROUNDING: u32 = 25u;
368#else // SMAA_PRESET_ULTRA
369const SMAA_THRESHOLD: f32 = 0.1;
370const SMAA_MAX_SEARCH_STEPS: u32 = 16u;
371const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u;
372const SMAA_CORNER_ROUNDING: u32 = 25u;
373#endif // SMAA_PRESET_ULTRA
375//-----------------------------------------------------------------------------
376// Configurable Defines
378/**
379 * SMAA_THRESHOLD specifies the threshold or sensitivity to edges.
380 * Lowering this value you will be able to detect more edges at the expense of
381 * performance.
382 *
383 * Range: [0, 0.5]
384 * 0.1 is a reasonable value, and allows to catch most visible edges.
385 * 0.05 is a rather overkill value, that allows to catch 'em all.
386 *
387 * If temporal supersampling is used, 0.2 could be a reasonable value, as low
388 * contrast edges are properly filtered by just 2x.
389 */
390// (In the WGSL version of this shader, `SMAA_THRESHOLD` is set above, in "SMAA
391// Presets".)
393/**
394 * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
395 * horizontal/vertical pattern searches, at each side of the pixel.
396 *
397 * In number of pixels, it's actually the double. So the maximum line length
398 * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
399 * longer lines won't look as good, but still antialiased).
400 *
401 * Range: [0, 112]
402 */
403// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS` is set above, in
404// "SMAA Presets".)
406/**
407 * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
408 * diagonal pattern searches, at each side of the pixel. In this case we jump
409 * one pixel at time, instead of two.
410 *
411 * Range: [0, 20]
412 *
413 * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16
414 * steps), but it can have a significant impact on older machines.
415 *
416 * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing.
417 */
418// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS_DIAG` is set
419// above, in "SMAA Presets".)
421/**
422 * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded.
423 *
424 * Range: [0, 100]
425 *
426 * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing.
427 */
428// (In the WGSL version of this shader, `SMAA_CORNER_ROUNDING` is set above, in
429// "SMAA Presets".)
431/**
432 * If there is a neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times
433 * bigger contrast than current edge, current edge will be discarded.
434 *
435 * This allows to eliminate spurious crossing edges, and is based on the fact
436 * that, if there is too much contrast in a direction, that will hide
437 * perceptually contrast in the other neighbors.
438 */
439const SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR: f32 = 2.0;
441//-----------------------------------------------------------------------------
442// Non-Configurable Defines
444const SMAA_AREATEX_MAX_DISTANCE: f32 = 16.0;
445const SMAA_AREATEX_MAX_DISTANCE_DIAG: f32 = 20.0;
446const SMAA_AREATEX_PIXEL_SIZE: vec2<f32> = (1.0 / vec2<f32>(160.0, 560.0));
447const SMAA_AREATEX_SUBTEX_SIZE: f32 = (1.0 / 7.0);
448const SMAA_SEARCHTEX_SIZE: vec2<f32> = vec2(66.0, 33.0);
449const SMAA_SEARCHTEX_PACKED_SIZE: vec2<f32> = vec2(64.0, 16.0);
451#ifndef SMAA_DISABLE_CORNER_DETECTION
452const SMAA_CORNER_ROUNDING_NORM: f32 = f32(SMAA_CORNER_ROUNDING) / 100.0;
453#endif // SMAA_DISABLE_CORNER_DETECTION
455//-----------------------------------------------------------------------------
456// WGSL-Specific Functions
458// This vertex shader produces the following, when drawn using indices 0..3:
459//
460// 1 | 0-----x.....2
461// 0 | | s | . ´
462// -1 | x_____x´
463// -2 | : .´
464// -3 | 1´
465// +---------------
466// -1 0 1 2 3
467//
468// The axes are clip-space x and y. The region marked s is the visible region.
469// The digits in the corners of the right-angled triangle are the vertex
470// indices.
471//
472// The top-left has UV 0,0, the bottom-left has 0,2, and the top-right has 2,0.
473// This means that the UV gets interpolated to 1,1 at the bottom-right corner
474// of the clip-space rectangle that is at 1,-1 in clip space.
475fn calculate_vertex_varyings(vertex_index: u32) -> VertexVaryings {
476 // See the explanation above for how this works
477 let uv = vec2<f32>(f32(vertex_index >> 1u), f32(vertex_index & 1u)) * 2.0;
478 let clip_position = vec2<f32>(uv * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0));
480 return VertexVaryings(clip_position, uv);
483//-----------------------------------------------------------------------------
484// Vertex Shaders
486#ifdef SMAA_EDGE_DETECTION
488/**
489 * Edge Detection Vertex Shader
490 */
491@vertex
492fn edge_detection_vertex_main(@builtin(vertex_index) vertex_index: u32) -> EdgeDetectionVaryings {
493 let varyings = calculate_vertex_varyings(vertex_index);
495 var edge_detection_varyings = EdgeDetectionVaryings();
496 edge_detection_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0);
497 edge_detection_varyings.tex_coord = varyings.tex_coord;
499 edge_detection_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) +
500 varyings.tex_coord.xyxy;
501 edge_detection_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) +
502 varyings.tex_coord.xyxy;
503 edge_detection_varyings.offset_2 = smaa_info.rt_metrics.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) +
504 varyings.tex_coord.xyxy;
506 return edge_detection_varyings;
509#endif // SMAA_EDGE_DETECTION
511#ifdef SMAA_BLENDING_WEIGHT_CALCULATION
513/**
514 * Blend Weight Calculation Vertex Shader
515 */
516@vertex
517fn blending_weight_calculation_vertex_main(@builtin(vertex_index) vertex_index: u32)
518 -> BlendingWeightCalculationVaryings {
519 let varyings = calculate_vertex_varyings(vertex_index);
521 var weight_varyings = BlendingWeightCalculationVaryings();
522 weight_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0);
523 weight_varyings.tex_coord = varyings.tex_coord;
525 // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
526 weight_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) +
527 varyings.tex_coord.xyxy;
528 weight_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) +
529 varyings.tex_coord.xyxy;
531 // And these for the searches, they indicate the ends of the loops:
532 weight_varyings.offset_2 =
533 smaa_info.rt_metrics.xxyy * vec4(-2.0, 2.0, -2.0, 2.0) * f32(SMAA_MAX_SEARCH_STEPS) +
534 vec4(weight_varyings.offset_0.xz, weight_varyings.offset_1.yw);
536 return weight_varyings;
539#endif // SMAA_BLENDING_WEIGHT_CALCULATION
541#ifdef SMAA_NEIGHBORHOOD_BLENDING
543/**
544 * Neighborhood Blending Vertex Shader
545 */
546@vertex
547fn neighborhood_blending_vertex_main(@builtin(vertex_index) vertex_index: u32)
548 -> NeighborhoodBlendingVaryings {
549 let varyings = calculate_vertex_varyings(vertex_index);
550 let offset = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) + varyings.tex_coord.xyxy;
551 return NeighborhoodBlendingVaryings(
552 vec4(varyings.clip_coord, 0.0, 1.0),
553 offset,
554 varyings.tex_coord
555 );
558#endif // SMAA_NEIGHBORHOOD_BLENDING
560//-----------------------------------------------------------------------------
561// Edge Detection Pixel Shaders (First Pass)
563#ifdef SMAA_EDGE_DETECTION
565/**
566 * Luma Edge Detection
567 *
568 * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
569 * thus 'color_texture' should be a non-sRGB texture.
570 */
571@fragment
572fn luma_edge_detection_fragment_main(in: EdgeDetectionVaryings) -> @location(0) vec4<f32> {
573 // Calculate the threshold:
574 // TODO: Predication.
575 let threshold = vec2(SMAA_THRESHOLD);
577 // Calculate luma:
578 let weights = vec3(0.2126, 0.7152, 0.0722);
579 let L = dot(textureSample(color_texture, color_sampler, in.tex_coord).rgb, weights);
581 let Lleft = dot(textureSample(color_texture, color_sampler, in.offset_0.xy).rgb, weights);
582 let Ltop = dot(textureSample(color_texture, color_sampler, in.offset_0.zw).rgb, weights);
584 // We do the usual threshold:
585 var delta: vec4<f32> = vec4(abs(L - vec2(Lleft, Ltop)), 0.0, 0.0);
586 var edges = step(threshold, delta.xy);
588 // Then discard if there is no edge:
589 if (dot(edges, vec2(1.0)) == 0.0) {
590 discard;
591 }
593 // Calculate right and bottom deltas:
594 let Lright = dot(textureSample(color_texture, color_sampler, in.offset_1.xy).rgb, weights);
595 let Lbottom = dot(textureSample(color_texture, color_sampler, in.offset_1.zw).rgb, weights);
596 delta = vec4(delta.xy, abs(L - vec2(Lright, Lbottom)));
598 // Calculate the maximum delta in the direct neighborhood:
599 var max_delta = max(delta.xy, delta.zw);
601 // Calculate left-left and top-top deltas:
602 let Lleftleft = dot(textureSample(color_texture, color_sampler, in.offset_2.xy).rgb, weights);
603 let Ltoptop = dot(textureSample(color_texture, color_sampler, in.offset_2.zw).rgb, weights);
604 delta = vec4(delta.xy, abs(vec2(Lleft, Ltop) - vec2(Lleftleft, Ltoptop)));
606 // Calculate the final maximum delta:
607 max_delta = max(max_delta.xy, delta.zw);
608 let final_delta = max(max_delta.x, max_delta.y);
610 // Local contrast adaptation:
611 edges *= step(vec2(final_delta), SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
613 return vec4(edges, 0.0, 1.0);
616#endif // SMAA_EDGE_DETECTION
618#ifdef SMAA_BLENDING_WEIGHT_CALCULATION
620//-----------------------------------------------------------------------------
621// Diagonal Search Functions
623#ifndef SMAA_DISABLE_DIAG_DETECTION
625/**
626 * Allows to decode two binary values from a bilinear-filtered access.
627 */
628fn decode_diag_bilinear_access_2(in_e: vec2<f32>) -> vec2<f32> {
629 // Bilinear access for fetching 'e' have a 0.25 offset, and we are
630 // interested in the R and G edges:
631 //
632 // +---G---+-------+
633 // | x o R x |
634 // +-------+-------+
635 //
636 // Then, if one of these edge is enabled:
637 // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0
638 // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0
639 //
640 // This function will unpack the values (mad + mul + round):
641 // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1
642 var e = in_e;
643 e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
644 return round(e);
647fn decode_diag_bilinear_access_4(e: vec4<f32>) -> vec4<f32> {
648 let e_rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
649 return round(vec4(e_rb.x, e.g, e_rb.y, e.a));
652/**
653 * These functions allows to perform diagonal pattern searches.
654 */
655fn search_diag_1(tex_coord: vec2<f32>, dir: vec2<f32>, e: ptr<function, vec2<f32>>) -> vec2<f32> {
656 var coord = vec4(tex_coord, -1.0, 1.0);
657 let t = vec3(smaa_info.rt_metrics.xy, 1.0);
658 while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) {
659 coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w);
660 *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg;
661 coord.w = dot(*e, vec2(0.5));
662 }
663 return coord.zw;
666fn search_diag_2(tex_coord: vec2<f32>, dir: vec2<f32>, e: ptr<function, vec2<f32>>) -> vec2<f32> {
667 var coord = vec4(tex_coord, -1.0, 1.0);
668 coord.x += 0.25 * smaa_info.rt_metrics.x; // See @SearchDiag2Optimization
669 let t = vec3(smaa_info.rt_metrics.xy, 1.0);
670 while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) {
671 coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w);
673 // @SearchDiag2Optimization
674 // Fetch both edges at once using bilinear filtering:
675 *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg;
676 *e = decode_diag_bilinear_access_2(*e);
678 // Non-optimized version:
679 // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g;
680 // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r;
682 coord.w = dot(*e, vec2(0.5));
683 }
684 return coord.zw;
687/**
688 * Similar to SMAAArea, this calculates the area corresponding to a certain
689 * diagonal distance and crossing edges 'e'.
690 */
691fn area_diag(dist: vec2<f32>, e: vec2<f32>, offset: f32) -> vec2<f32> {
692 var tex_coord = vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG) * e + dist;
694 // We do a scale and bias for mapping to texel space:
695 tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
697 // Diagonal areas are on the second half of the texture:
698 tex_coord.x += 0.5;
700 // Move to proper place, according to the subpixel offset:
701 tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
703 // Do it!
704 return textureSampleLevel(area_texture, edges_sampler, tex_coord, 0.0).rg;
707/**
708 * This searches for diagonal patterns and returns the corresponding weights.
709 */
710fn calculate_diag_weights(tex_coord: vec2<f32>, e: vec2<f32>, subsample_indices: vec4<f32>)
711 -> vec2<f32> {
712 var weights = vec2(0.0, 0.0);
714 // Search for the line ends:
715 var d = vec4(0.0);
716 var end = vec2(0.0);
717 if (e.r > 0.0) {
718 let d_xz = search_diag_1(tex_coord, vec2(-1.0, 1.0), &end);
719 d = vec4(d_xz.x, d.y, d_xz.y, d.w);
720 d.x += f32(end.y > 0.9);
721 } else {
722 d = vec4(0.0, d.y, 0.0, d.w);
723 }
724 let d_yw = search_diag_1(tex_coord, vec2(1.0, -1.0), &end);
725 d = vec4(d.x, d_yw.x, d.y, d_yw.y);
727 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
728 // Fetch the crossing edges:
729 let coords = vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25) * smaa_info.rt_metrics.xyxy +
730 tex_coord.xyxy;
731 var c = vec4(
732 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).rg,
733 textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).rg,
734 );
735 let c_yxwz = decode_diag_bilinear_access_4(c.xyzw);
736 c = c_yxwz.yxwz;
738 // Non-optimized version:
739 // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
740 // float4 c;
741 // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
742 // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r;
743 // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g;
744 // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r;
746 // Merge crossing edges at each side into a single value:
747 var cc = vec2(2.0) * c.xz + c.yw;
749 // Remove the crossing edge if we didn't found the end of the line:
750 cc = select(cc, vec2(0.0, 0.0), vec2<bool>(step(vec2(0.9), d.zw)));
752 // Fetch the areas for this line:
753 weights += area_diag(d.xy, cc, subsample_indices.z);
754 }
756 // Search for the line ends:
757 let d_xz = search_diag_2(tex_coord, vec2(-1.0, -1.0), &end);
758 if (textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0, vec2(1, 0)).r > 0.0) {
759 let d_yw = search_diag_2(tex_coord, vec2(1.0, 1.0), &end);
760 d = vec4(d_xz.x, d_yw.x, d_xz.y, d_yw.y);
761 d.y += f32(end.y > 0.9);
762 } else {
763 d = vec4(d_xz.x, 0.0, d_xz.y, 0.0);
764 }
766 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
767 // Fetch the crossing edges:
768 let coords = vec4(-d.x, -d.x, d.y, d.y) * smaa_info.rt_metrics.xyxy + tex_coord.xyxy;
769 let c = vec4(
770 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).g,
771 textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2( 0, -1)).r,
772 textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).gr,
773 );
774 var cc = vec2(2.0) * c.xz + c.yw;
776 // Remove the crossing edge if we didn't found the end of the line:
777 cc = select(cc, vec2(0.0, 0.0), vec2<bool>(step(vec2(0.9), d.zw)));
779 // Fetch the areas for this line:
780 weights += area_diag(d.xy, cc, subsample_indices.w).gr;
781 }
783 return weights;
786#endif // SMAA_DISABLE_DIAG_DETECTION
788//-----------------------------------------------------------------------------
789// Horizontal/Vertical Search Functions
791/**
792 * This allows to determine how much length should we add in the last step
793 * of the searches. It takes the bilinearly interpolated edge (see
794 * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
795 * crossing edges are active.
796 */
797fn search_length(e: vec2<f32>, offset: f32) -> f32 {
798 // The texture is flipped vertically, with left and right cases taking half
799 // of the space horizontally:
800 var scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0);
801 var bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0);
803 // Scale and bias to access texel centers:
804 scale += vec2(-1.0, 1.0);
805 bias += vec2( 0.5, -0.5);
807 // Convert from pixel coordinates to texcoords:
808 // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped)
809 scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
810 bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
812 // Lookup the search texture:
813 return textureSampleLevel(search_texture, edges_sampler, scale * e + bias, 0.0).r;
816/**
817 * Horizontal/vertical search functions for the 2nd pass.
818 */
819fn search_x_left(in_tex_coord: vec2<f32>, end: f32) -> f32 {
820 var tex_coord = in_tex_coord;
822 /**
823 * @PSEUDO_GATHER4
824 * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
825 * sample between edge, thus fetching four edges in a row.
826 * Sampling with different offsets in each direction allows to disambiguate
827 * which edges are active from the four fetched ones.
828 */
829 var e = vec2(0.0, 1.0);
830 while (tex_coord.x > end &&
831 e.g > 0.8281 && // Is there some edge not activated?
832 e.r == 0.0) { // Or is there a crossing edge that breaks the line?
833 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
834 tex_coord += -vec2(2.0, 0.0) * smaa_info.rt_metrics.xy;
835 }
836 let offset = -(255.0 / 127.0) * search_length(e, 0.0) + 3.25;
837 return smaa_info.rt_metrics.x * offset + tex_coord.x;
840fn search_x_right(in_tex_coord: vec2<f32>, end: f32) -> f32 {
841 var tex_coord = in_tex_coord;
843 var e = vec2(0.0, 1.0);
844 while (tex_coord.x < end &&
845 e.g > 0.8281 && // Is there some edge not activated?
846 e.r == 0.0) { // Or is there a crossing edge that breaks the line?
847 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
848 tex_coord += vec2(2.0, 0.0) * smaa_info.rt_metrics.xy;
849 }
850 let offset = -(255.0 / 127.0) * search_length(e, 0.5) + 3.25;
851 return -smaa_info.rt_metrics.x * offset + tex_coord.x;
854fn search_y_up(in_tex_coord: vec2<f32>, end: f32) -> f32 {
855 var tex_coord = in_tex_coord;
857 var e = vec2(1.0, 0.0);
858 while (tex_coord.y > end &&
859 e.r > 0.8281 && // Is there some edge not activated?
860 e.g == 0.0) { // Or is there a crossing edge that breaks the line?
861 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
862 tex_coord += -vec2(0.0, 2.0) * smaa_info.rt_metrics.xy;
863 }
864 let offset = -(255.0 / 127.0) * search_length(e.gr, 0.0) + 3.25;
865 return smaa_info.rt_metrics.y * offset + tex_coord.y;
868fn search_y_down(in_tex_coord: vec2<f32>, end: f32) -> f32 {
869 var tex_coord = in_tex_coord;
871 var e = vec2(1.0, 0.0);
872 while (tex_coord.y < end &&
873 e.r > 0.8281 && // Is there some edge not activated?
874 e.g == 0.0) { // Or is there a crossing edge that breaks the line?
875 e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg;
876 tex_coord += vec2(0.0, 2.0) * smaa_info.rt_metrics.xy;
877 }
878 let offset = -(255.0 / 127.0) * search_length(e.gr, 0.5) + 3.25;
879 return -smaa_info.rt_metrics.y * offset + tex_coord.y;
882/**
883 * Ok, we have the distance and both crossing edges. So, what are the areas
884 * at each side of current edge?
885 */
886fn area(dist: vec2<f32>, e1: f32, e2: f32, offset: f32) -> vec2<f32> {
887 // Rounding prevents precision errors of bilinear filtering:
888 var tex_coord = SMAA_AREATEX_MAX_DISTANCE * round(4.0 * vec2(e1, e2)) + dist;
890 // We do a scale and bias for mapping to texel space:
891 tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
893 // Move to proper place, according to the subpixel offset:
894 tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
896 // Do it!
897 return textureSample(area_texture, edges_sampler, tex_coord).rg;
900//-----------------------------------------------------------------------------
901// Corner Detection Functions
903fn detect_horizontal_corner_pattern(weights: vec2<f32>, tex_coord: vec4<f32>, d: vec2<f32>)
904 -> vec2<f32> {
905#ifndef SMAA_DISABLE_CORNER_DETECTION
906 let left_right = step(d.xy, d.yx);
907 var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
909 rounding /= left_right.x + left_right.y; // Reduce blending for pixels in the center of a line.
911 var factor = vec2(1.0, 1.0);
912 factor.x -= rounding.x *
913 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, 1)).r;
914 factor.x -= rounding.y *
915 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, 1)).r;
916 factor.y -= rounding.x *
917 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, -2)).r;
918 factor.y -= rounding.y *
919 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, -2)).r;
921 return weights * saturate(factor);
922#else // SMAA_DISABLE_CORNER_DETECTION
923 return weights;
924#endif // SMAA_DISABLE_CORNER_DETECTION
927fn detect_vertical_corner_pattern(weights: vec2<f32>, tex_coord: vec4<f32>, d: vec2<f32>)
928 -> vec2<f32> {
929#ifndef SMAA_DISABLE_CORNER_DETECTION
930 let left_right = step(d.xy, d.yx);
931 var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
933 rounding /= left_right.x + left_right.y;
935 var factor = vec2(1.0, 1.0);
936 factor.x -= rounding.x *
937 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2( 1, 0)).g;
938 factor.x -= rounding.y *
939 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2( 1, 1)).g;
940 factor.y -= rounding.x *
941 textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(-2, 0)).g;
942 factor.y -= rounding.y *
943 textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(-2, 1)).g;
945 return weights * saturate(factor);
946#else // SMAA_DISABLE_CORNER_DETECTION
947 return weights;
948#endif // SMAA_DISABLE_CORNER_DETECTION
951//-----------------------------------------------------------------------------
952// Blending Weight Calculation Pixel Shader (Second Pass)
954@fragment
955fn blending_weight_calculation_fragment_main(in: BlendingWeightCalculationVaryings)
956 -> @location(0) vec4<f32> {
957 let subsample_indices = vec4(0.0); // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
959 var weights = vec4(0.0);
961 var e = textureSample(edges_texture, edges_sampler, in.tex_coord).rg;
963 if (e.g > 0.0) { // Edge at north
964#ifndef SMAA_DISABLE_DIAG_DETECTION
965 // Diagonals have both north and west edges, so searching for them in
966 // one of the boundaries is enough.
967 weights = vec4(calculate_diag_weights(in.tex_coord, e, subsample_indices), weights.ba);
969 // We give priority to diagonals, so if we find a diagonal we skip
970 // horizontal/vertical processing.
971 if (weights.r + weights.g != 0.0) {
972 return weights;
973 }
974#endif // SMAA_DISABLE_DIAG_DETECTION
976 var d: vec2<f32>;
978 // Find the distance to the left:
979 var coords: vec3<f32>;
980 coords.x = search_x_left(in.offset_0.xy, in.offset_2.x);
981 // in.offset_1.y = in.tex_coord.y - 0.25 * smaa_info.rt_metrics.y (@CROSSING_OFFSET)
982 coords.y = in.offset_1.y;
983 d.x = coords.x;
985 // Now fetch the left crossing edges, two at a time using bilinear
986 // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
987 // discern what value each edge has:
988 let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).r;
990 // Find the distance to the right:
991 coords.z = search_x_right(in.offset_0.zw, in.offset_2.y);
992 d.y = coords.z;
994 // We want the distances to be in pixel units (doing this here allow to
995 // better interleave arithmetic and memory accesses):
996 d = abs(round(smaa_info.rt_metrics.zz * d - in.position.xx));
998 // SMAAArea below needs a sqrt, as the areas texture is compressed
999 // quadratically:
1000 let sqrt_d = sqrt(d);
1002 // Fetch the right crossing edges:
1003 let e2 = textureSampleLevel(
1004 edges_texture, edges_sampler, coords.zy, 0.0, vec2<i32>(1, 0)).r;
1006 // Ok, we know how this pattern looks like, now it is time for getting
1007 // the actual area:
1008 weights = vec4(area(sqrt_d, e1, e2, subsample_indices.y), weights.ba);
1010 // Fix corners:
1011 coords.y = in.tex_coord.y;
1012 weights = vec4(
1013 detect_horizontal_corner_pattern(weights.rg, coords.xyzy, d),
1014 weights.ba
1015 );
1018 if (e.r > 0.0) { // Edge at west
1019 var d: vec2<f32>;
1021 // Find the distance to the top:
1022 var coords: vec3<f32>;
1023 coords.y = search_y_up(in.offset_1.xy, in.offset_2.z);
1024 // in.offset_1.x = in.tex_coord.x - 0.25 * smaa_info.rt_metrics.x
1025 coords.x = in.offset_0.x;
1026 d.x = coords.y;
1028 // Fetch the top crossing edges:
1029 let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).g;
1031 // Find the distance to the bottom:
1032 coords.z = search_y_down(in.offset_1.zw, in.offset_2.w);
1033 d.y = coords.z;
1035 // We want the distances to be in pixel units:
1036 d = abs(round(smaa_info.rt_metrics.ww * d - in.position.yy));
1038 // SMAAArea below needs a sqrt, as the areas texture is compressed
1039 // quadratically:
1040 let sqrt_d = sqrt(d);
1042 // Fetch the bottom crossing edges:
1043 let e2 = textureSampleLevel(
1044 edges_texture, edges_sampler, coords.xz, 0.0, vec2<i32>(0, 1)).g;
1046 // Get the area for this direction:
1047 weights = vec4(weights.rg, area(sqrt_d, e1, e2, subsample_indices.x));
1049 // Fix corners:
1050 coords.x = in.tex_coord.x;
1051 weights = vec4(weights.rg, detect_vertical_corner_pattern(weights.ba, coords.xyxz, d));
1054 return weights;
1057#endif // SMAA_BLENDING_WEIGHT_CALCULATION
1059#ifdef SMAA_NEIGHBORHOOD_BLENDING
1061//-----------------------------------------------------------------------------
1062// Neighborhood Blending Pixel Shader (Third Pass)
1064@fragment
1065fn neighborhood_blending_fragment_main(in: NeighborhoodBlendingVaryings) -> @location(0) vec4<f32> {
1066 // Fetch the blending weights for current pixel:
1067 let a = vec4(
1068 textureSample(blend_texture, blend_sampler, in.offset.xy).a, // Right
1069 textureSample(blend_texture, blend_sampler, in.offset.zw).g, // Top
1070 textureSample(blend_texture, blend_sampler, in.tex_coord).zx, // Bottom / Left
1071 );
1073 // Is there any blending weight with a value greater than 0.0?
1074 if (dot(a, vec4(1.0)) < 1.0e-5) {
1075 let color = textureSampleLevel(color_texture, blend_sampler, in.tex_coord, 0.0);
1076 // TODO: Reprojection
1077 return color;
1078 } else {
1079 let h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical)
1081 // Calculate the blending offsets:
1082 var blending_offset = vec4(0.0, a.y, 0.0, a.w);
1083 var blending_weight = a.yw;
1084 blending_offset = select(blending_offset, vec4(a.x, 0.0, a.z, 0.0), h);
1085 blending_weight = select(blending_weight, a.xz, h);
1086 blending_weight /= dot(blending_weight, vec2(1.0));
1088 // Calculate the texture coordinates:
1089 let blending_coord =
1090 blending_offset * vec4(smaa_info.rt_metrics.xy, -smaa_info.rt_metrics.xy) +
1091 in.tex_coord.xyxy;
1093 // We exploit bilinear filtering to mix current pixel with the chosen
1094 // neighbor:
1095 var color = blending_weight.x *
1096 textureSampleLevel(color_texture, blend_sampler, blending_coord.xy, 0.0);
1097 color += blending_weight.y *
1098 textureSampleLevel(color_texture, blend_sampler, blending_coord.zw, 0.0);
1100 // TODO: Reprojection
1102 return color;
1106#endif // SMAA_NEIGHBORHOOD_BLENDING
Contents