SmaaBlend.glsl
1 #version 430 core 2 #define SMAA_GLSL_4 1 3 4 layout (constant_id = 0) const int SMAA_PRESET_LOW = 0; 5 layout (constant_id = 1) const int SMAA_PRESET_MEDIUM = 0; 6 layout (constant_id = 2) const int SMAA_PRESET_HIGH = 0; 7 layout (constant_id = 3) const int SMAA_PRESET_ULTRA = 0; 8 layout (constant_id = 4) const float METRIC_WIDTH = 1920.0; 9 layout (constant_id = 5) const float METRIC_HEIGHT = 1080.0; 10 11 #define SMAA_RT_METRICS float4(1.0 / METRIC_WIDTH, 1.0 / METRIC_HEIGHT, METRIC_WIDTH, METRIC_HEIGHT) 12 13 layout (local_size_x = 16, local_size_y = 16) in; 14 /** 15 * Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com) 16 * Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com) 17 * Copyright (C) 2013 Belen Masia (bmasia@unizar.es) 18 * Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com) 19 * Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es) 20 * 21 * Permission is hereby granted, free of charge, to any person obtaining a copy 22 * this software and associated documentation files (the "Software"), to deal in 23 * the Software without restriction, including without limitation the rights to 24 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 25 * of the Software, and to permit persons to whom the Software is furnished to 26 * do so, subject to the following conditions: 27 * 28 * The above copyright notice and this permission notice shall be included in 29 * all copies or substantial portions of the Software. As clarification, there 30 * is no requirement that the copyright notice and permission be included in 31 * binary distributions of the Software. 32 * 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 39 * SOFTWARE. 40 */ 41 42 43 /** 44 * _______ ___ ___ ___ ___ 45 * / || \/ | / \ / \ 46 * | (---- | \ / | / ^ \ / ^ \ 47 * \ \ | |\/| | / /_\ \ / /_\ \ 48 * ----) | | | | | / _____ \ / _____ \ 49 * |_______/ |__| |__| /__/ \__\ /__/ \__\ 50 * 51 * E N H A N C E D 52 * 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 53 * 54 * http://www.iryoku.com/smaa/ 55 * 56 * Hi, welcome aboard! 57 * 58 * Here you'll find instructions to get the shader up and running as fast as 59 * possible. 60 * 61 * IMPORTANTE NOTICE: when updating, remember to update both this file and the 62 * precomputed textures! They may change from version to version. 63 * 64 * The shader has three passes, chained together as follows: 65 * 66 * |input|------------------ 67 * v | 68 * [ SMAA*EdgeDetection ] | 69 * v | 70 * |edgesTex| | 71 * v | 72 * [ SMAABlendingWeightCalculation ] | 73 * v | 74 * |blendTex| | 75 * v | 76 * [ SMAANeighborhoodBlending ] <------ 77 * v 78 * |output| 79 * 80 * Note that each [pass] has its own vertex and pixel shader. Remember to use 81 * oversized triangles instead of quads to avoid overshading along the 82 * diagonal. 83 * 84 * You've three edge detection methods to choose from: luma, color or depth. 85 * They represent different quality/performance and anti-aliasing/sharpness 86 * tradeoffs, so our recommendation is for you to choose the one that best 87 * suits your particular scenario: 88 * 89 * - Depth edge detection is usually the fastest but it may miss some edges. 90 * 91 * - Luma edge detection is usually more expensive than depth edge detection, 92 * but catches visible edges that depth edge detection can miss. 93 * 94 * - Color edge detection is usually the most expensive one but catches 95 * chroma-only edges. 96 * 97 * For quickstarters: just use luma edge detection. 98 * 99 * The general advice is to not rush the integration process and ensure each 100 * step is done correctly (don't try to integrate SMAA T2x with predicated edge 101 * detection from the start!). Ok then, let's go! 102 * 103 * 1. The first step is to create two RGBA temporal render targets for holding 104 * |edgesTex| and |blendTex|. 105 * 106 * In DX10 or DX11, you can use a RG render target for the edges texture. 107 * In the case of NVIDIA GPUs, using RG render targets seems to actually be 108 * slower. 109 * 110 * On the Xbox 360, you can use the same render target for resolving both 111 * |edgesTex| and |blendTex|, as they aren't needed simultaneously. 112 * 113 * 2. Both temporal render targets |edgesTex| and |blendTex| must be cleared 114 * each frame. Do not forget to clear the alpha channel! 115 * 116 * 3. The next step is loading the two supporting precalculated textures, 117 * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as 118 * C++ headers, and also as regular DDS files. They'll be needed for the 119 * 'SMAABlendingWeightCalculation' pass. 120 * 121 * If you use the C++ headers, be sure to load them in the format specified 122 * inside of them. 123 * 124 * You can also compress 'areaTex' and 'searchTex' using BC5 and BC4 125 * respectively, if you have that option in your content processor pipeline. 126 * When compressing then, you get a non-perceptible quality decrease, and a 127 * marginal performance increase. 128 * 129 * 4. All samplers must be set to linear filtering and clamp. 130 * 131 * After you get the technique working, remember that 64-bit inputs have 132 * half-rate linear filtering on GCN. 133 * 134 * If SMAA is applied to 64-bit color buffers, switching to point filtering 135 * when accesing them will increase the performance. Search for 136 * 'SMAASamplePoint' to see which textures may benefit from point 137 * filtering, and where (which is basically the color input in the edge 138 * detection and resolve passes). 139 * 140 * 5. All texture reads and buffer writes must be non-sRGB, with the exception 141 * of the input read and the output write in 142 * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in 143 * this last pass are not possible, the technique will work anyway, but 144 * will perform antialiasing in gamma space. 145 * 146 * IMPORTANT: for best results the input read for the color/luma edge 147 * detection should *NOT* be sRGB. 148 * 149 * 6. Before including SMAA.h you'll have to setup the render target metrics, 150 * the target and any optional configuration defines. Optionally you can 151 * use a preset. 152 * 153 * You have the following targets available: 154 * SMAA_HLSL_3 155 * SMAA_HLSL_4 156 * SMAA_HLSL_4_1 157 * SMAA_GLSL_3 * 158 * SMAA_GLSL_4 * 159 * 160 * * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below). 161 * 162 * And four presets: 163 * SMAA_PRESET_LOW (%60 of the quality) 164 * SMAA_PRESET_MEDIUM (%80 of the quality) 165 * SMAA_PRESET_HIGH (%95 of the quality) 166 * SMAA_PRESET_ULTRA (%99 of the quality) 167 * 168 * For example: 169 * #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0) 170 * #define SMAA_HLSL_4 171 * #define SMAA_PRESET_HIGH 172 * #include "SMAA.h" 173 * 174 * Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a 175 * uniform variable. The code is designed to minimize the impact of not 176 * using a constant value, but it is still better to hardcode it. 177 * 178 * Depending on how you encoded 'areaTex' and 'searchTex', you may have to 179 * add (and customize) the following defines before including SMAA.h: 180 * #define SMAA_AREATEX_SELECT(sample) sample.rg 181 * #define SMAA_SEARCHTEX_SELECT(sample) sample.r 182 * 183 * If your engine is already using porting macros, you can define 184 * SMAA_CUSTOM_SL, and define the porting functions by yourself. 185 * 186 * 7. Then, you'll have to setup the passes as indicated in the scheme above. 187 * You can take a look into SMAA.fx, to see how we did it for our demo. 188 * Checkout the function wrappers, you may want to copy-paste them! 189 * 190 * 8. It's recommended to validate the produced |edgesTex| and |blendTex|. 191 * You can use a screenshot from your engine to compare the |edgesTex| 192 * and |blendTex| produced inside of the engine with the results obtained 193 * with the reference demo. 194 * 195 * 9. After you get the last pass to work, it's time to optimize. You'll have 196 * to initialize a stencil buffer in the first pass (discard is already in 197 * the code), then mask execution by using it the second pass. The last 198 * pass should be executed in all pixels. 199 * 200 * 201 * After this point you can choose to enable predicated thresholding, 202 * temporal supersampling and motion blur integration: 203 * 204 * a) If you want to use predicated thresholding, take a look into 205 * SMAA_PREDICATION; you'll need to pass an extra texture in the edge 206 * detection pass. 207 * 208 * b) If you want to enable temporal supersampling (SMAA T2x): 209 * 210 * 1. The first step is to render using subpixel jitters. I won't go into 211 * detail, but it's as simple as moving each vertex position in the 212 * vertex shader, you can check how we do it in our DX10 demo. 213 * 214 * 2. Then, you must setup the temporal resolve. You may want to take a look 215 * into SMAAResolve for resolving 2x modes. After you get it working, you'll 216 * probably see ghosting everywhere. But fear not, you can enable the 217 * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro. 218 * Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded. 219 * 220 * 3. The next step is to apply SMAA to each subpixel jittered frame, just as 221 * done for 1x. 222 * 223 * 4. At this point you should already have something usable, but for best 224 * results the proper area textures must be set depending on current jitter. 225 * For this, the parameter 'subsampleIndices' of 226 * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x 227 * mode: 228 * 229 * @SUBSAMPLE_INDICES 230 * 231 * | S# | Camera Jitter | subsampleIndices | 232 * +----+------------------+---------------------+ 233 * | 0 | ( 0.25, -0.25) | float4(1, 1, 1, 0) | 234 * | 1 | (-0.25, 0.25) | float4(2, 2, 2, 0) | 235 * 236 * These jitter positions assume a bottom-to-top y axis. S# stands for the 237 * sample number. 238 * 239 * More information about temporal supersampling here: 240 * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf 241 * 242 * c) If you want to enable spatial multisampling (SMAA S2x): 243 * 244 * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be 245 * created with: 246 * - DX10: see below (*) 247 * - DX10.1: D3D10_STANDARD_MULTISAMPLE_PATTERN or 248 * - DX11: D3D11_STANDARD_MULTISAMPLE_PATTERN 249 * 250 * This allows to ensure that the subsample order matches the table in 251 * @SUBSAMPLE_INDICES. 252 * 253 * (*) In the case of DX10, we refer the reader to: 254 * - SMAA::detectMSAAOrder and 255 * - SMAA::msaaReorder 256 * 257 * These functions allow to match the standard multisample patterns by 258 * detecting the subsample order for a specific GPU, and reordering 259 * them appropriately. 260 * 261 * 2. A shader must be run to output each subsample into a separate buffer 262 * (DX10 is required). You can use SMAASeparate for this purpose, or just do 263 * it in an existing pass (for example, in the tone mapping pass, which has 264 * the advantage of feeding tone mapped subsamples to SMAA, which will yield 265 * better results). 266 * 267 * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing 268 * the results in the final buffer. The second run should alpha blend with 269 * the existing final buffer using a blending factor of 0.5. 270 * 'subsampleIndices' must be adjusted as in the SMAA T2x case (see point 271 * b). 272 * 273 * d) If you want to enable temporal supersampling on top of SMAA S2x 274 * (which actually is SMAA 4x): 275 * 276 * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is 277 * to calculate SMAA S2x for current frame. In this case, 'subsampleIndices' 278 * must be set as follows: 279 * 280 * | F# | S# | Camera Jitter | Net Jitter | subsampleIndices | 281 * +----+----+--------------------+-------------------+----------------------+ 282 * | 0 | 0 | ( 0.125, 0.125) | ( 0.375, -0.125) | float4(5, 3, 1, 3) | 283 * | 0 | 1 | ( 0.125, 0.125) | (-0.125, 0.375) | float4(4, 6, 2, 3) | 284 * +----+----+--------------------+-------------------+----------------------+ 285 * | 1 | 2 | (-0.125, -0.125) | ( 0.125, -0.375) | float4(3, 5, 1, 4) | 286 * | 1 | 3 | (-0.125, -0.125) | (-0.375, 0.125) | float4(6, 4, 2, 4) | 287 * 288 * These jitter positions assume a bottom-to-top y axis. F# stands for the 289 * frame number. S# stands for the sample number. 290 * 291 * 2. After calculating SMAA S2x for current frame (with the new subsample 292 * indices), previous frame must be reprojected as in SMAA T2x mode (see 293 * point b). 294 * 295 * e) If motion blur is used, you may want to do the edge detection pass 296 * together with motion blur. This has two advantages: 297 * 298 * 1. Pixels under heavy motion can be omitted from the edge detection process. 299 * For these pixels we can just store "no edge", as motion blur will take 300 * care of them. 301 * 2. The center pixel tap is reused. 302 * 303 * Note that in this case depth testing should be used instead of stenciling, 304 * as we have to write all the pixels in the motion blur pass. 305 * 306 * That's it! 307 */ 308 309 //----------------------------------------------------------------------------- 310 // SMAA Presets 311 312 /** 313 * Note that if you use one of these presets, the following configuration 314 * macros will be ignored if set in the "Configurable Defines" section. 315 */ 316 317 #if defined(SMAA_PRESET_LOW) 318 #define SMAA_THRESHOLD 0.15 319 #define SMAA_MAX_SEARCH_STEPS 4 320 #define SMAA_DISABLE_DIAG_DETECTION 321 #define SMAA_DISABLE_CORNER_DETECTION 322 #elif defined(SMAA_PRESET_MEDIUM) 323 #define SMAA_THRESHOLD 0.1 324 #define SMAA_MAX_SEARCH_STEPS 8 325 #define SMAA_DISABLE_DIAG_DETECTION 326 #define SMAA_DISABLE_CORNER_DETECTION 327 #elif defined(SMAA_PRESET_HIGH) 328 #define SMAA_THRESHOLD 0.1 329 #define SMAA_MAX_SEARCH_STEPS 16 330 #define SMAA_MAX_SEARCH_STEPS_DIAG 8 331 #define SMAA_CORNER_ROUNDING 25 332 #elif defined(SMAA_PRESET_ULTRA) 333 #define SMAA_THRESHOLD 0.05 334 #define SMAA_MAX_SEARCH_STEPS 32 335 #define SMAA_MAX_SEARCH_STEPS_DIAG 16 336 #define SMAA_CORNER_ROUNDING 25 337 #endif 338 339 //----------------------------------------------------------------------------- 340 // Configurable Defines 341 342 /** 343 * SMAA_THRESHOLD specifies the threshold or sensitivity to edges. 344 * Lowering this value you will be able to detect more edges at the expense of 345 * performance. 346 * 347 * Range: [0, 0.5] 348 * 0.1 is a reasonable value, and allows to catch most visible edges. 349 * 0.05 is a rather overkill value, that allows to catch 'em all. 350 * 351 * If temporal supersampling is used, 0.2 could be a reasonable value, as low 352 * contrast edges are properly filtered by just 2x. 353 */ 354 #ifndef SMAA_THRESHOLD 355 #define SMAA_THRESHOLD 0.1 356 #endif 357 358 /** 359 * SMAA_DEPTH_THRESHOLD specifies the threshold for depth edge detection. 360 * 361 * Range: depends on the depth range of the scene. 362 */ 363 #ifndef SMAA_DEPTH_THRESHOLD 364 #define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD) 365 #endif 366 367 /** 368 * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the 369 * horizontal/vertical pattern searches, at each side of the pixel. 370 * 371 * In number of pixels, it's actually the double. So the maximum line length 372 * perfectly handled by, for example 16, is 64 (by perfectly, we meant that 373 * longer lines won't look as good, but still antialiased). 374 * 375 * Range: [0, 112] 376 */ 377 #ifndef SMAA_MAX_SEARCH_STEPS 378 #define SMAA_MAX_SEARCH_STEPS 16 379 #endif 380 381 /** 382 * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the 383 * diagonal pattern searches, at each side of the pixel. In this case we jump 384 * one pixel at time, instead of two. 385 * 386 * Range: [0, 20] 387 * 388 * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16 389 * steps), but it can have a significant impact on older machines. 390 * 391 * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing. 392 */ 393 #ifndef SMAA_MAX_SEARCH_STEPS_DIAG 394 #define SMAA_MAX_SEARCH_STEPS_DIAG 8 395 #endif 396 397 /** 398 * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded. 399 * 400 * Range: [0, 100] 401 * 402 * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing. 403 */ 404 #ifndef SMAA_CORNER_ROUNDING 405 #define SMAA_CORNER_ROUNDING 25 406 #endif 407 408 /** 409 * If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times 410 * bigger contrast than current edge, current edge will be discarded. 411 * 412 * This allows to eliminate spurious crossing edges, and is based on the fact 413 * that, if there is too much contrast in a direction, that will hide 414 * perceptually contrast in the other neighbors. 415 */ 416 #ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 417 #define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0 418 #endif 419 420 /** 421 * Predicated thresholding allows to better preserve texture details and to 422 * improve performance, by decreasing the number of detected edges using an 423 * additional buffer like the light accumulation buffer, object ids or even the 424 * depth buffer (the depth buffer usage may be limited to indoor or short range 425 * scenes). 426 * 427 * It locally decreases the luma or color threshold if an edge is found in an 428 * additional buffer (so the global threshold can be higher). 429 * 430 * This method was developed by Playstation EDGE MLAA team, and used in 431 * Killzone 3, by using the light accumulation buffer. More information here: 432 * http://iryoku.com/aacourse/downloads/06-MLAA-on-PS3.pptx 433 */ 434 #ifndef SMAA_PREDICATION 435 #define SMAA_PREDICATION 0 436 #endif 437 438 /** 439 * Threshold to be used in the additional predication buffer. 440 * 441 * Range: depends on the input, so you'll have to find the magic number that 442 * works for you. 443 */ 444 #ifndef SMAA_PREDICATION_THRESHOLD 445 #define SMAA_PREDICATION_THRESHOLD 0.01 446 #endif 447 448 /** 449 * How much to scale the global threshold used for luma or color edge 450 * detection when using predication. 451 * 452 * Range: [1, 5] 453 */ 454 #ifndef SMAA_PREDICATION_SCALE 455 #define SMAA_PREDICATION_SCALE 2.0 456 #endif 457 458 /** 459 * How much to locally decrease the threshold. 460 * 461 * Range: [0, 1] 462 */ 463 #ifndef SMAA_PREDICATION_STRENGTH 464 #define SMAA_PREDICATION_STRENGTH 0.4 465 #endif 466 467 /** 468 * Temporal reprojection allows to remove ghosting artifacts when using 469 * temporal supersampling. We use the CryEngine 3 method which also introduces 470 * velocity weighting. This feature is of extreme importance for totally 471 * removing ghosting. More information here: 472 * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf 473 * 474 * Note that you'll need to setup a velocity buffer for enabling reprojection. 475 * For static geometry, saving the previous depth buffer is a viable 476 * alternative. 477 */ 478 #ifndef SMAA_REPROJECTION 479 #define SMAA_REPROJECTION 0 480 #endif 481 482 /** 483 * SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting. It allows to 484 * remove ghosting trails behind the moving object, which are not removed by 485 * just using reprojection. Using low values will exhibit ghosting, while using 486 * high values will disable temporal supersampling under motion. 487 * 488 * Behind the scenes, velocity weighting removes temporal supersampling when 489 * the velocity of the subsamples differs (meaning they are different objects). 490 * 491 * Range: [0, 80] 492 */ 493 #ifndef SMAA_REPROJECTION_WEIGHT_SCALE 494 #define SMAA_REPROJECTION_WEIGHT_SCALE 30.0 495 #endif 496 497 /** 498 * On some compilers, discard cannot be used in vertex shaders. Thus, they need 499 * to be compiled separately. 500 */ 501 #ifndef SMAA_INCLUDE_VS 502 #define SMAA_INCLUDE_VS 1 503 #endif 504 #ifndef SMAA_INCLUDE_PS 505 #define SMAA_INCLUDE_PS 1 506 #endif 507 508 //----------------------------------------------------------------------------- 509 // Texture Access Defines 510 511 #ifndef SMAA_AREATEX_SELECT 512 #if defined(SMAA_HLSL_3) 513 #define SMAA_AREATEX_SELECT(sample) sample.ra 514 #else 515 #define SMAA_AREATEX_SELECT(sample) sample.rg 516 #endif 517 #endif 518 519 #ifndef SMAA_SEARCHTEX_SELECT 520 #define SMAA_SEARCHTEX_SELECT(sample) sample.r 521 #endif 522 523 #ifndef SMAA_DECODE_VELOCITY 524 #define SMAA_DECODE_VELOCITY(sample) sample.rg 525 #endif 526 527 //----------------------------------------------------------------------------- 528 // Non-Configurable Defines 529 530 #define SMAA_AREATEX_MAX_DISTANCE 16 531 #define SMAA_AREATEX_MAX_DISTANCE_DIAG 20 532 #define SMAA_AREATEX_PIXEL_SIZE (1.0 / float2(160.0, 560.0)) 533 #define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0) 534 #define SMAA_SEARCHTEX_SIZE float2(66.0, 33.0) 535 #define SMAA_SEARCHTEX_PACKED_SIZE float2(64.0, 16.0) 536 #define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0) 537 538 //----------------------------------------------------------------------------- 539 // Porting Functions 540 541 #if defined(SMAA_HLSL_3) 542 #define SMAATexture2D(tex) sampler2D tex 543 #define SMAATexturePass2D(tex) tex 544 #define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0)) 545 #define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0)) 546 #define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0)) 547 #define SMAASample(tex, coord) tex2D(tex, coord) 548 #define SMAASamplePoint(tex, coord) tex2D(tex, coord) 549 #define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_RT_METRICS.xy) 550 #define SMAA_FLATTEN [flatten] 551 #define SMAA_BRANCH [branch] 552 #endif 553 #if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1) 554 SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; }; 555 SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; }; 556 #define SMAATexture2D(tex) Texture2D tex 557 #define SMAATexturePass2D(tex) tex 558 #define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0) 559 #define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0) 560 #define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset) 561 #define SMAASample(tex, coord) tex.Sample(LinearSampler, coord) 562 #define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord) 563 #define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset) 564 #define SMAA_FLATTEN [flatten] 565 #define SMAA_BRANCH [branch] 566 #define SMAATexture2DMS2(tex) Texture2DMS<float4, 2> tex 567 #define SMAALoad(tex, pos, sample) tex.Load(pos, sample) 568 #if defined(SMAA_HLSL_4_1) 569 #define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0) 570 #endif 571 #endif 572 #if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4) 573 #define SMAATexture2D(tex) sampler2D tex 574 #define SMAATexturePass2D(tex) tex 575 #define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0) 576 #define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0) 577 #define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset) 578 #define SMAASample(tex, coord) texture(tex, coord) 579 #define SMAASamplePoint(tex, coord) texture(tex, coord) 580 #define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset) 581 #define SMAA_FLATTEN 582 #define SMAA_BRANCH 583 #define lerp(a, b, t) mix(a, b, t) 584 #define saturate(a) clamp(a, 0.0, 1.0) 585 #if defined(SMAA_GLSL_4) 586 #define mad(a, b, c) fma(a, b, c) 587 #define SMAAGather(tex, coord) textureGather(tex, coord) 588 #else 589 #define mad(a, b, c) (a * b + c) 590 #endif 591 #define float2 vec2 592 #define float3 vec3 593 #define float4 vec4 594 #define int2 ivec2 595 #define int3 ivec3 596 #define int4 ivec4 597 #define bool2 bvec2 598 #define bool3 bvec3 599 #define bool4 bvec4 600 #endif 601 602 #if !defined(SMAA_HLSL_3) && !defined(SMAA_HLSL_4) && !defined(SMAA_HLSL_4_1) && !defined(SMAA_GLSL_3) && !defined(SMAA_GLSL_4) && !defined(SMAA_CUSTOM_SL) 603 #error you must define the shading language: SMAA_HLSL_*, SMAA_GLSL_* or SMAA_CUSTOM_SL 604 #endif 605 606 //----------------------------------------------------------------------------- 607 // Misc functions 608 609 /** 610 * Gathers current pixel, and the top-left neighbors. 611 */ 612 float3 SMAAGatherNeighbours(float2 texcoord, 613 float4 offset[3], 614 SMAATexture2D(tex)) { 615 #ifdef SMAAGather 616 return SMAAGather(tex, texcoord + SMAA_RT_METRICS.xy * float2(-0.5, -0.5)).grb; 617 #else 618 float P = SMAASamplePoint(tex, texcoord).r; 619 float Pleft = SMAASamplePoint(tex, offset[0].xy).r; 620 float Ptop = SMAASamplePoint(tex, offset[0].zw).r; 621 return float3(P, Pleft, Ptop); 622 #endif 623 } 624 625 /** 626 * Adjusts the threshold by means of predication. 627 */ 628 float2 SMAACalculatePredicatedThreshold(float2 texcoord, 629 float4 offset[3], 630 SMAATexture2D(predicationTex)) { 631 float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(predicationTex)); 632 float2 delta = abs(neighbours.xx - neighbours.yz); 633 float2 edges = step(SMAA_PREDICATION_THRESHOLD, delta); 634 return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges); 635 } 636 637 /** 638 * Conditional move: 639 */ 640 void SMAAMovc(bool2 cond, inout float2 variable, float2 value) { 641 SMAA_FLATTEN if (cond.x) variable.x = value.x; 642 SMAA_FLATTEN if (cond.y) variable.y = value.y; 643 } 644 645 void SMAAMovc(bool4 cond, inout float4 variable, float4 value) { 646 SMAAMovc(cond.xy, variable.xy, value.xy); 647 SMAAMovc(cond.zw, variable.zw, value.zw); 648 } 649 650 651 #if SMAA_INCLUDE_VS 652 //----------------------------------------------------------------------------- 653 // Vertex Shaders 654 655 /** 656 * Edge Detection Vertex Shader 657 */ 658 void SMAAEdgeDetectionVS(float2 texcoord, 659 out float4 offset[3]) { 660 offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), texcoord.xyxy); 661 offset[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy); 662 offset[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), texcoord.xyxy); 663 } 664 665 /** 666 * Blend Weight Calculation Vertex Shader 667 */ 668 void SMAABlendingWeightCalculationVS(float2 texcoord, 669 out float2 pixcoord, 670 out float4 offset[3]) { 671 pixcoord = texcoord * SMAA_RT_METRICS.zw; 672 673 // We will use these offsets for the searches later on (see @PSEUDO_GATHER4): 674 offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.25, -0.125, 1.25, -0.125), texcoord.xyxy); 675 offset[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.25, -0.125, 1.25), texcoord.xyxy); 676 677 // And these for the searches, they indicate the ends of the loops: 678 offset[2] = mad(SMAA_RT_METRICS.xxyy, 679 float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS), 680 float4(offset[0].xz, offset[1].yw)); 681 } 682 683 /** 684 * Neighborhood Blending Vertex Shader 685 */ 686 void SMAANeighborhoodBlendingVS(float2 texcoord, 687 out float4 offset) { 688 offset = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy); 689 } 690 #endif // SMAA_INCLUDE_VS 691 692 #if SMAA_INCLUDE_PS 693 //----------------------------------------------------------------------------- 694 // Edge Detection Pixel Shaders (First Pass) 695 696 /** 697 * Luma Edge Detection 698 * 699 * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and 700 * thus 'colorTex' should be a non-sRGB texture. 701 */ 702 float2 SMAALumaEdgeDetectionPS(float2 texcoord, 703 float4 offset[3], 704 SMAATexture2D(colorTex) 705 #if SMAA_PREDICATION 706 , SMAATexture2D(predicationTex) 707 #endif 708 ) { 709 // Calculate the threshold: 710 #if SMAA_PREDICATION 711 float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, SMAATexturePass2D(predicationTex)); 712 #else 713 float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD); 714 #endif 715 716 // Calculate lumas: 717 float3 weights = float3(0.2126, 0.7152, 0.0722); 718 float L = dot(SMAASamplePoint(colorTex, texcoord).rgb, weights); 719 720 float Lleft = dot(SMAASamplePoint(colorTex, offset[0].xy).rgb, weights); 721 float Ltop = dot(SMAASamplePoint(colorTex, offset[0].zw).rgb, weights); 722 723 // We do the usual threshold: 724 float4 delta; 725 delta.xy = abs(L - float2(Lleft, Ltop)); 726 float2 edges = step(threshold, delta.xy); 727 728 // Then discard if there is no edge: 729 if (dot(edges, float2(1.0, 1.0)) == 0.0) 730 return float2(-2.0, -2.0); 731 732 // Calculate right and bottom deltas: 733 float Lright = dot(SMAASamplePoint(colorTex, offset[1].xy).rgb, weights); 734 float Lbottom = dot(SMAASamplePoint(colorTex, offset[1].zw).rgb, weights); 735 delta.zw = abs(L - float2(Lright, Lbottom)); 736 737 // Calculate the maximum delta in the direct neighborhood: 738 float2 maxDelta = max(delta.xy, delta.zw); 739 740 // Calculate left-left and top-top deltas: 741 float Lleftleft = dot(SMAASamplePoint(colorTex, offset[2].xy).rgb, weights); 742 float Ltoptop = dot(SMAASamplePoint(colorTex, offset[2].zw).rgb, weights); 743 delta.zw = abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop)); 744 745 // Calculate the final maximum delta: 746 maxDelta = max(maxDelta.xy, delta.zw); 747 float finalDelta = max(maxDelta.x, maxDelta.y); 748 749 // Local contrast adaptation: 750 edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy); 751 752 return edges; 753 } 754 755 /** 756 * Color Edge Detection 757 * 758 * IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and 759 * thus 'colorTex' should be a non-sRGB texture. 760 */ 761 float2 SMAAColorEdgeDetectionPS(float2 texcoord, 762 float4 offset[3], 763 SMAATexture2D(colorTex) 764 #if SMAA_PREDICATION 765 , SMAATexture2D(predicationTex) 766 #endif 767 ) { 768 // Calculate the threshold: 769 #if SMAA_PREDICATION 770 float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, predicationTex); 771 #else 772 float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD); 773 #endif 774 775 // Calculate color deltas: 776 float4 delta; 777 float3 C = SMAASamplePoint(colorTex, texcoord).rgb; 778 779 float3 Cleft = SMAASamplePoint(colorTex, offset[0].xy).rgb; 780 float3 t = abs(C - Cleft); 781 delta.x = max(max(t.r, t.g), t.b); 782 783 float3 Ctop = SMAASamplePoint(colorTex, offset[0].zw).rgb; 784 t = abs(C - Ctop); 785 delta.y = max(max(t.r, t.g), t.b); 786 787 // We do the usual threshold: 788 float2 edges = step(threshold, delta.xy); 789 790 // Then discard if there is no edge: 791 if (dot(edges, float2(1.0, 1.0)) == 0.0) 792 return float2(-2.0, -2.0); 793 794 // Calculate right and bottom deltas: 795 float3 Cright = SMAASamplePoint(colorTex, offset[1].xy).rgb; 796 t = abs(C - Cright); 797 delta.z = max(max(t.r, t.g), t.b); 798 799 float3 Cbottom = SMAASamplePoint(colorTex, offset[1].zw).rgb; 800 t = abs(C - Cbottom); 801 delta.w = max(max(t.r, t.g), t.b); 802 803 // Calculate the maximum delta in the direct neighborhood: 804 float2 maxDelta = max(delta.xy, delta.zw); 805 806 // Calculate left-left and top-top deltas: 807 float3 Cleftleft = SMAASamplePoint(colorTex, offset[2].xy).rgb; 808 t = abs(C - Cleftleft); 809 delta.z = max(max(t.r, t.g), t.b); 810 811 float3 Ctoptop = SMAASamplePoint(colorTex, offset[2].zw).rgb; 812 t = abs(C - Ctoptop); 813 delta.w = max(max(t.r, t.g), t.b); 814 815 // Calculate the final maximum delta: 816 maxDelta = max(maxDelta.xy, delta.zw); 817 float finalDelta = max(maxDelta.x, maxDelta.y); 818 819 // Local contrast adaptation: 820 edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy); 821 822 return edges; 823 } 824 825 /** 826 * Depth Edge Detection 827 */ 828 float2 SMAADepthEdgeDetectionPS(float2 texcoord, 829 float4 offset[3], 830 SMAATexture2D(depthTex)) { 831 float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(depthTex)); 832 float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z)); 833 float2 edges = step(SMAA_DEPTH_THRESHOLD, delta); 834 835 if (dot(edges, float2(1.0, 1.0)) == 0.0) 836 return float2(-2.0, -2.0); 837 838 return edges; 839 } 840 841 //----------------------------------------------------------------------------- 842 // Diagonal Search Functions 843 844 #if !defined(SMAA_DISABLE_DIAG_DETECTION) 845 846 /** 847 * Allows to decode two binary values from a bilinear-filtered access. 848 */ 849 float2 SMAADecodeDiagBilinearAccess(float2 e) { 850 // Bilinear access for fetching 'e' have a 0.25 offset, and we are 851 // interested in the R and G edges: 852 // 853 // +---G---+-------+ 854 // | x o R x | 855 // +-------+-------+ 856 // 857 // Then, if one of these edge is enabled: 858 // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0 859 // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0 860 // 861 // This function will unpack the values (mad + mul + round): 862 // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1 863 e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75); 864 return round(e); 865 } 866 867 float4 SMAADecodeDiagBilinearAccess(float4 e) { 868 e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75); 869 return round(e); 870 } 871 872 /** 873 * These functions allows to perform diagonal pattern searches. 874 */ 875 float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) { 876 float4 coord = float4(texcoord, -1.0, 1.0); 877 float3 t = float3(SMAA_RT_METRICS.xy, 1.0); 878 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && 879 coord.w > 0.9) { 880 coord.xyz = mad(t, float3(dir, 1.0), coord.xyz); 881 e = SMAASampleLevelZero(edgesTex, coord.xy).rg; 882 coord.w = dot(e, float2(0.5, 0.5)); 883 } 884 return coord.zw; 885 } 886 887 float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) { 888 float4 coord = float4(texcoord, -1.0, 1.0); 889 coord.x += 0.25 * SMAA_RT_METRICS.x; // See @SearchDiag2Optimization 890 float3 t = float3(SMAA_RT_METRICS.xy, 1.0); 891 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && 892 coord.w > 0.9) { 893 coord.xyz = mad(t, float3(dir, 1.0), coord.xyz); 894 895 // @SearchDiag2Optimization 896 // Fetch both edges at once using bilinear filtering: 897 e = SMAASampleLevelZero(edgesTex, coord.xy).rg; 898 e = SMAADecodeDiagBilinearAccess(e); 899 900 // Non-optimized version: 901 // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g; 902 // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r; 903 904 coord.w = dot(e, float2(0.5, 0.5)); 905 } 906 return coord.zw; 907 } 908 909 /** 910 * Similar to SMAAArea, this calculates the area corresponding to a certain 911 * diagonal distance and crossing edges 'e'. 912 */ 913 float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset) { 914 float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist); 915 916 // We do a scale and bias for mapping to texel space: 917 texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); 918 919 // Diagonal areas are on the second half of the texture: 920 texcoord.x += 0.5; 921 922 // Move to proper place, according to the subpixel offset: 923 texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset; 924 925 // Do it! 926 return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord)); 927 } 928 929 /** 930 * This searches for diagonal patterns and returns the corresponding weights. 931 */ 932 float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices) { 933 float2 weights = float2(0.0, 0.0); 934 935 // Search for the line ends: 936 float4 d; 937 float2 end; 938 if (e.r > 0.0) { 939 d.xz = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, 1.0), end); 940 d.x += float(end.y > 0.9); 941 } else 942 d.xz = float2(0.0, 0.0); 943 d.yw = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, -1.0), end); 944 945 SMAA_BRANCH 946 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 947 // Fetch the crossing edges: 948 float4 coords = mad(float4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), SMAA_RT_METRICS.xyxy, texcoord.xyxy); 949 float4 c; 950 c.xy = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).rg; 951 c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).rg; 952 c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw); 953 954 // Non-optimized version: 955 // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy); 956 // float4 c; 957 // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g; 958 // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r; 959 // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g; 960 // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r; 961 962 // Merge crossing edges at each side into a single value: 963 float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw); 964 965 // Remove the crossing edge if we didn't found the end of the line: 966 SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0)); 967 968 // Fetch the areas for this line: 969 weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.z); 970 } 971 972 // Search for the line ends: 973 d.xz = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, -1.0), end); 974 if (SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r > 0.0) { 975 d.yw = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, 1.0), end); 976 d.y += float(end.y > 0.9); 977 } else 978 d.yw = float2(0.0, 0.0); 979 980 SMAA_BRANCH 981 if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 982 // Fetch the crossing edges: 983 float4 coords = mad(float4(-d.x, -d.x, d.y, d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy); 984 float4 c; 985 c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g; 986 c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, -1)).r; 987 c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).gr; 988 float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw); 989 990 // Remove the crossing edge if we didn't found the end of the line: 991 SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0)); 992 993 // Fetch the areas for this line: 994 weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.w).gr; 995 } 996 997 return weights; 998 } 999 #endif 1000 1001 //----------------------------------------------------------------------------- 1002 // Horizontal/Vertical Search Functions 1003 1004 /** 1005 * This allows to determine how much length should we add in the last step 1006 * of the searches. It takes the bilinearly interpolated edge (see 1007 * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and 1008 * crossing edges are active. 1009 */ 1010 float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset) { 1011 // The texture is flipped vertically, with left and right cases taking half 1012 // of the space horizontally: 1013 float2 scale = SMAA_SEARCHTEX_SIZE * float2(0.5, -1.0); 1014 float2 bias = SMAA_SEARCHTEX_SIZE * float2(offset, 1.0); 1015 1016 // Scale and bias to access texel centers: 1017 scale += float2(-1.0, 1.0); 1018 bias += float2( 0.5, -0.5); 1019 1020 // Convert from pixel coordinates to texcoords: 1021 // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped) 1022 scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; 1023 bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; 1024 1025 // Lookup the search texture: 1026 return SMAA_SEARCHTEX_SELECT(SMAASampleLevelZero(searchTex, mad(scale, e, bias))); 1027 } 1028 1029 /** 1030 * Horizontal/vertical search functions for the 2nd pass. 1031 */ 1032 float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { 1033 /** 1034 * @PSEUDO_GATHER4 1035 * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to 1036 * sample between edge, thus fetching four edges in a row. 1037 * Sampling with different offsets in each direction allows to disambiguate 1038 * which edges are active from the four fetched ones. 1039 */ 1040 float2 e = float2(0.0, 1.0); 1041 while (texcoord.x > end && 1042 e.g > 0.8281 && // Is there some edge not activated? 1043 e.r == 0.0) { // Or is there a crossing edge that breaks the line? 1044 e = SMAASampleLevelZero(edgesTex, texcoord).rg; 1045 texcoord = mad(-float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord); 1046 } 1047 1048 float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0), 3.25); 1049 return mad(SMAA_RT_METRICS.x, offset, texcoord.x); 1050 1051 // Non-optimized version: 1052 // We correct the previous (-0.25, -0.125) offset we applied: 1053 // texcoord.x += 0.25 * SMAA_RT_METRICS.x; 1054 1055 // The searches are bias by 1, so adjust the coords accordingly: 1056 // texcoord.x += SMAA_RT_METRICS.x; 1057 1058 // Disambiguate the length added by the last step: 1059 // texcoord.x += 2.0 * SMAA_RT_METRICS.x; // Undo last step 1060 // texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0); 1061 // return mad(SMAA_RT_METRICS.x, offset, texcoord.x); 1062 } 1063 1064 float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { 1065 float2 e = float2(0.0, 1.0); 1066 while (texcoord.x < end && 1067 e.g > 0.8281 && // Is there some edge not activated? 1068 e.r == 0.0) { // Or is there a crossing edge that breaks the line? 1069 e = SMAASampleLevelZero(edgesTex, texcoord).rg; 1070 texcoord = mad(float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord); 1071 } 1072 float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.5), 3.25); 1073 return mad(-SMAA_RT_METRICS.x, offset, texcoord.x); 1074 } 1075 1076 float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { 1077 float2 e = float2(1.0, 0.0); 1078 while (texcoord.y > end && 1079 e.r > 0.8281 && // Is there some edge not activated? 1080 e.g == 0.0) { // Or is there a crossing edge that breaks the line? 1081 e = SMAASampleLevelZero(edgesTex, texcoord).rg; 1082 texcoord = mad(-float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord); 1083 } 1084 float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.0), 3.25); 1085 return mad(SMAA_RT_METRICS.y, offset, texcoord.y); 1086 } 1087 1088 float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { 1089 float2 e = float2(1.0, 0.0); 1090 while (texcoord.y < end && 1091 e.r > 0.8281 && // Is there some edge not activated? 1092 e.g == 0.0) { // Or is there a crossing edge that breaks the line? 1093 e = SMAASampleLevelZero(edgesTex, texcoord).rg; 1094 texcoord = mad(float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord); 1095 } 1096 float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.5), 3.25); 1097 return mad(-SMAA_RT_METRICS.y, offset, texcoord.y); 1098 } 1099 1100 /** 1101 * Ok, we have the distance and both crossing edges. So, what are the areas 1102 * at each side of current edge? 1103 */ 1104 float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset) { 1105 // Rounding prevents precision errors of bilinear filtering: 1106 float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * float2(e1, e2)), dist); 1107 1108 // We do a scale and bias for mapping to texel space: 1109 texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); 1110 1111 // Move to proper place, according to the subpixel offset: 1112 texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y); 1113 1114 // Do it! 1115 return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord)); 1116 } 1117 1118 //----------------------------------------------------------------------------- 1119 // Corner Detection Functions 1120 1121 void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) { 1122 #if !defined(SMAA_DISABLE_CORNER_DETECTION) 1123 float2 leftRight = step(d.xy, d.yx); 1124 float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight; 1125 1126 rounding /= leftRight.x + leftRight.y; // Reduce blending for pixels in the center of a line. 1127 1128 float2 factor = float2(1.0, 1.0); 1129 factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, 1)).r; 1130 factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, 1)).r; 1131 factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, -2)).r; 1132 factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, -2)).r; 1133 1134 weights *= saturate(factor); 1135 #endif 1136 } 1137 1138 void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) { 1139 #if !defined(SMAA_DISABLE_CORNER_DETECTION) 1140 float2 leftRight = step(d.xy, d.yx); 1141 float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight; 1142 1143 rounding /= leftRight.x + leftRight.y; 1144 1145 float2 factor = float2(1.0, 1.0); 1146 factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2( 1, 0)).g; 1147 factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2( 1, 1)).g; 1148 factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(-2, 0)).g; 1149 factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(-2, 1)).g; 1150 1151 weights *= saturate(factor); 1152 #endif 1153 } 1154 1155 //----------------------------------------------------------------------------- 1156 // Blending Weight Calculation Pixel Shader (Second Pass) 1157 1158 float4 SMAABlendingWeightCalculationPS(float2 texcoord, 1159 float2 pixcoord, 1160 float4 offset[3], 1161 SMAATexture2D(edgesTex), 1162 SMAATexture2D(areaTex), 1163 SMAATexture2D(searchTex), 1164 float4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES. 1165 float4 weights = float4(0.0, 0.0, 0.0, 0.0); 1166 1167 float2 e = SMAASample(edgesTex, texcoord).rg; 1168 1169 SMAA_BRANCH 1170 if (e.g > 0.0) { // Edge at north 1171 #if !defined(SMAA_DISABLE_DIAG_DETECTION) 1172 // Diagonals have both north and west edges, so searching for them in 1173 // one of the boundaries is enough. 1174 weights.rg = SMAACalculateDiagWeights(SMAATexturePass2D(edgesTex), SMAATexturePass2D(areaTex), texcoord, e, subsampleIndices); 1175 1176 // We give priority to diagonals, so if we find a diagonal we skip 1177 // horizontal/vertical processing. 1178 SMAA_BRANCH 1179 if (weights.r == -weights.g) { // weights.r + weights.g == 0.0 1180 #endif 1181 1182 float2 d; 1183 1184 // Find the distance to the left: 1185 float3 coords; 1186 coords.x = SMAASearchXLeft(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].xy, offset[2].x); 1187 coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET) 1188 d.x = coords.x; 1189 1190 // Now fetch the left crossing edges, two at a time using bilinear 1191 // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to 1192 // discern what value each edge has: 1193 float e1 = SMAASampleLevelZero(edgesTex, coords.xy).r; 1194 1195 // Find the distance to the right: 1196 coords.z = SMAASearchXRight(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].zw, offset[2].y); 1197 d.y = coords.z; 1198 1199 // We want the distances to be in pixel units (doing this here allow to 1200 // better interleave arithmetic and memory accesses): 1201 d = abs(round(mad(SMAA_RT_METRICS.zz, d, -pixcoord.xx))); 1202 1203 // SMAAArea below needs a sqrt, as the areas texture is compressed 1204 // quadratically: 1205 float2 sqrt_d = sqrt(d); 1206 1207 // Fetch the right crossing edges: 1208 float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r; 1209 1210 // Ok, we know how this pattern looks like, now it is time for getting 1211 // the actual area: 1212 weights.rg = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.y); 1213 1214 // Fix corners: 1215 coords.y = texcoord.y; 1216 SMAADetectHorizontalCornerPattern(SMAATexturePass2D(edgesTex), weights.rg, coords.xyzy, d); 1217 1218 #if !defined(SMAA_DISABLE_DIAG_DETECTION) 1219 } else 1220 e.r = 0.0; // Skip vertical processing. 1221 #endif 1222 } 1223 1224 SMAA_BRANCH 1225 if (e.r > 0.0) { // Edge at west 1226 float2 d; 1227 1228 // Find the distance to the top: 1229 float3 coords; 1230 coords.y = SMAASearchYUp(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].xy, offset[2].z); 1231 coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x; 1232 d.x = coords.y; 1233 1234 // Fetch the top crossing edges: 1235 float e1 = SMAASampleLevelZero(edgesTex, coords.xy).g; 1236 1237 // Find the distance to the bottom: 1238 coords.z = SMAASearchYDown(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].zw, offset[2].w); 1239 d.y = coords.z; 1240 1241 // We want the distances to be in pixel units: 1242 d = abs(round(mad(SMAA_RT_METRICS.ww, d, -pixcoord.yy))); 1243 1244 // SMAAArea below needs a sqrt, as the areas texture is compressed 1245 // quadratically: 1246 float2 sqrt_d = sqrt(d); 1247 1248 // Fetch the bottom crossing edges: 1249 float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, int2(0, 1)).g; 1250 1251 // Get the area for this direction: 1252 weights.ba = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.x); 1253 1254 // Fix corners: 1255 coords.x = texcoord.x; 1256 SMAADetectVerticalCornerPattern(SMAATexturePass2D(edgesTex), weights.ba, coords.xyxz, d); 1257 } 1258 1259 return weights; 1260 } 1261 1262 //----------------------------------------------------------------------------- 1263 // Neighborhood Blending Pixel Shader (Third Pass) 1264 1265 float4 SMAANeighborhoodBlendingPS(float2 texcoord, 1266 float4 offset, 1267 SMAATexture2D(colorTex), 1268 SMAATexture2D(blendTex) 1269 #if SMAA_REPROJECTION 1270 , SMAATexture2D(velocityTex) 1271 #endif 1272 ) { 1273 // Fetch the blending weights for current pixel: 1274 float4 a; 1275 a.x = SMAASample(blendTex, offset.xy).a; // Right 1276 a.y = SMAASample(blendTex, offset.zw).g; // Top 1277 a.wz = SMAASample(blendTex, texcoord).xz; // Bottom / Left 1278 1279 // Is there any blending weight with a value greater than 0.0? 1280 SMAA_BRANCH 1281 if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5) { 1282 float4 color = SMAASampleLevelZero(colorTex, texcoord); 1283 1284 #if SMAA_REPROJECTION 1285 float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord)); 1286 1287 // Pack velocity into the alpha channel: 1288 color.a = sqrt(5.0 * length(velocity)); 1289 #endif 1290 1291 return color; 1292 } else { 1293 bool h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical) 1294 1295 // Calculate the blending offsets: 1296 float4 blendingOffset = float4(0.0, a.y, 0.0, a.w); 1297 float2 blendingWeight = a.yw; 1298 SMAAMovc(bool4(h, h, h, h), blendingOffset, float4(a.x, 0.0, a.z, 0.0)); 1299 SMAAMovc(bool2(h, h), blendingWeight, a.xz); 1300 blendingWeight /= dot(blendingWeight, float2(1.0, 1.0)); 1301 1302 // Calculate the texture coordinates: 1303 float4 blendingCoord = mad(blendingOffset, float4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texcoord.xyxy); 1304 1305 // We exploit bilinear filtering to mix current pixel with the chosen 1306 // neighbor: 1307 float4 color = blendingWeight.x * SMAASampleLevelZero(colorTex, blendingCoord.xy); 1308 color += blendingWeight.y * SMAASampleLevelZero(colorTex, blendingCoord.zw); 1309 1310 #if SMAA_REPROJECTION 1311 // Antialias velocity for proper reprojection in a later stage: 1312 float2 velocity = blendingWeight.x * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.xy)); 1313 velocity += blendingWeight.y * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.zw)); 1314 1315 // Pack velocity into the alpha channel: 1316 color.a = sqrt(5.0 * length(velocity)); 1317 #endif 1318 1319 return color; 1320 } 1321 } 1322 1323 //----------------------------------------------------------------------------- 1324 // Temporal Resolve Pixel Shader (Optional Pass) 1325 1326 float4 SMAAResolvePS(float2 texcoord, 1327 SMAATexture2D(currentColorTex), 1328 SMAATexture2D(previousColorTex) 1329 #if SMAA_REPROJECTION 1330 , SMAATexture2D(velocityTex) 1331 #endif 1332 ) { 1333 #if SMAA_REPROJECTION 1334 // Velocity is assumed to be calculated for motion blur, so we need to 1335 // inverse it for reprojection: 1336 float2 velocity = -SMAA_DECODE_VELOCITY(SMAASamplePoint(velocityTex, texcoord).rg); 1337 1338 // Fetch current pixel: 1339 float4 current = SMAASamplePoint(currentColorTex, texcoord); 1340 1341 // Reproject current coordinates and fetch previous pixel: 1342 float4 previous = SMAASamplePoint(previousColorTex, texcoord + velocity); 1343 1344 // Attenuate the previous pixel if the velocity is different: 1345 float delta = abs(current.a * current.a - previous.a * previous.a) / 5.0; 1346 float weight = 0.5 * saturate(1.0 - sqrt(delta) * SMAA_REPROJECTION_WEIGHT_SCALE); 1347 1348 // Blend the pixels according to the calculated weight: 1349 return lerp(current, previous, weight); 1350 #else 1351 // Just blend the pixels: 1352 float4 current = SMAASamplePoint(currentColorTex, texcoord); 1353 float4 previous = SMAASamplePoint(previousColorTex, texcoord); 1354 return lerp(current, previous, 0.5); 1355 #endif 1356 } 1357 1358 //----------------------------------------------------------------------------- 1359 // Separate Multisamples Pixel Shader (Optional Pass) 1360 1361 #ifdef SMAALoad 1362 void SMAASeparatePS(float4 position, 1363 float2 texcoord, 1364 out float4 target0, 1365 out float4 target1, 1366 SMAATexture2DMS2(colorTexMS)) { 1367 int2 pos = int2(position.xy); 1368 target0 = SMAALoad(colorTexMS, pos, 0); 1369 target1 = SMAALoad(colorTexMS, pos, 1); 1370 } 1371 #endif 1372 1373 //----------------------------------------------------------------------------- 1374 #endif // SMAA_INCLUDE_PS 1375 1376 layout(rgba8, binding = 0, set = 3) uniform image2D imgOutput; 1377 1378 layout(binding = 1, set = 2) uniform sampler2D inputImg; 1379 layout(binding = 3, set = 2) uniform sampler2D samplerArea; 1380 layout(binding = 4, set = 2) uniform sampler2D samplerSearch; 1381 layout( binding = 2 ) uniform invResolution 1382 { 1383 vec2 invResolution_data; 1384 }; 1385 1386 void main() { 1387 ivec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4); 1388 for(int i = 0; i < 4; i++) 1389 { 1390 for(int j = 0; j < 4; j++) 1391 { 1392 ivec2 texelCoord = ivec2(loc.x + i, loc.y + j); 1393 vec2 coord = (texelCoord + vec2(0.5)) / invResolution_data; 1394 vec2 pixCoord; 1395 vec4 offset[3]; 1396 1397 SMAABlendingWeightCalculationVS( coord, pixCoord, offset); 1398 1399 vec4 oColor = SMAABlendingWeightCalculationPS(coord, pixCoord, offset, inputImg, samplerArea, samplerSearch, ivec4(0)); 1400 1401 imageStore(imgOutput, texelCoord, oColor); 1402 } 1403 } 1404 }