smaa.glslv 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #version GLSL_VERSION
  2. /**
  3. * Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
  4. * Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com)
  5. * Copyright (C) 2013 Belen Masia (bmasia@unizar.es)
  6. * Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com)
  7. * Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * this software and associated documentation files (the "Software"), to deal in
  11. * the Software without restriction, including without limitation the rights to
  12. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is furnished to
  14. * do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software. As clarification, there
  18. * is no requirement that the copyright notice and permission be included in
  19. * binary distributions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. */
  29. /*==============================================================================
  30. VARS
  31. ==============================================================================*/
  32. #var SMAA_PASS SMAA_RESOLVE
  33. #var AA_METHOD AA_METHOD_SMAA_LOW
  34. /*============================================================================*/
  35. #include <std.glsl>
  36. uniform vec2 u_texel_size;
  37. /*==============================================================================
  38. SHADER INTERFACE
  39. ==============================================================================*/
  40. GLSL_IN vec2 a_position;
  41. //------------------------------------------------------------------------------
  42. GLSL_OUT vec2 v_texcoord;
  43. #if SMAA_PASS == SMAA_NEIGHBORHOOD_BLENDING
  44. GLSL_OUT vec4 v_offset;
  45. #else
  46. GLSL_OUT vec4 v_offset_0;
  47. GLSL_OUT vec4 v_offset_1;
  48. GLSL_OUT vec4 v_offset_2;
  49. #endif
  50. #if SMAA_PASS == SMAA_BLENDING_WEIGHT_CALCULATION
  51. GLSL_OUT vec2 v_pixcoord;
  52. #endif
  53. /*============================================================================*/
  54. //-----------------------------------------------------------------------------
  55. // SMAA Presets
  56. # if AA_METHOD == AA_METHOD_SMAA_LOW
  57. #define SMAA_MAX_SEARCH_STEPS 4
  58. # elif AA_METHOD == AA_METHOD_SMAA_MEDIUM
  59. #define SMAA_MAX_SEARCH_STEPS 8
  60. # elif AA_METHOD == AA_METHOD_SMAA_HIGH
  61. #define SMAA_MAX_SEARCH_STEPS 16
  62. # elif AA_METHOD == AA_METHOD_SMAA_ULTRA
  63. #define SMAA_MAX_SEARCH_STEPS 32
  64. #endif
  65. //-----------------------------------------------------------------------------
  66. // Configurable Defines
  67. /**
  68. * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
  69. * horizontal/vertical pattern searches, at each side of the pixel.
  70. *
  71. * In number of pixels, it's actually the double. So the maximum line length
  72. * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
  73. * longer lines won't look as good, but still antialiased).
  74. *
  75. * Range: [0, 112]
  76. */
  77. #ifndef SMAA_MAX_SEARCH_STEPS
  78. #define SMAA_MAX_SEARCH_STEPS 16
  79. #endif
  80. #if SMAA_PASS == SMAA_EDGE_DETECTION
  81. void smaa_edge_detection(vec2 texcoord) {
  82. v_offset_0 = u_texel_size.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) + texcoord.xyxy;
  83. v_offset_1 = u_texel_size.xyxy * vec4( 1.0, 0.0, 0.0, 1.0) + texcoord.xyxy;
  84. v_offset_2 = u_texel_size.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) + texcoord.xyxy;
  85. }
  86. #elif SMAA_PASS == SMAA_BLENDING_WEIGHT_CALCULATION
  87. void smaa_blending_weight_calculation(vec2 texcoord, out vec2 pixcoord) {
  88. pixcoord = texcoord / u_texel_size;
  89. // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
  90. v_offset_0 = u_texel_size.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) + texcoord.xyxy;
  91. v_offset_1 = u_texel_size.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) + texcoord.xyxy;
  92. // And these for the searches, they indicate the ends of the loops:
  93. v_offset_2 = u_texel_size.xxyy * vec4(-2.0, 2.0, -2.0, 2.0) *
  94. float(SMAA_MAX_SEARCH_STEPS) + vec4(v_offset_0.xz, v_offset_1.yw);
  95. }
  96. #elif SMAA_PASS == SMAA_NEIGHBORHOOD_BLENDING
  97. void smaa_neighborhood_blending(vec2 texcoord) {
  98. v_offset = u_texel_size.xyxy * vec4( 1.0, 0.0, 0.0, 1.0) + texcoord.xyxy;
  99. }
  100. #endif
  101. void main(void) {
  102. v_texcoord = 2.0 * a_position;
  103. #if SMAA_PASS == SMAA_EDGE_DETECTION
  104. smaa_edge_detection(v_texcoord);
  105. #elif SMAA_PASS == SMAA_BLENDING_WEIGHT_CALCULATION
  106. smaa_blending_weight_calculation(v_texcoord, v_pixcoord);
  107. #elif SMAA_PASS == SMAA_NEIGHBORHOOD_BLENDING
  108. smaa_neighborhood_blending(v_texcoord);
  109. #endif
  110. gl_Position = vec4(4.0 * (a_position.xy-0.25), 0.0, 1.0);
  111. }