smaa_blending.glsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (C) 2013 Jorge Jimenez ([email protected])
  3. * Copyright (C) 2013 Jose I. Echevarria ([email protected])
  4. * Copyright (C) 2013 Belen Masia ([email protected])
  5. * Copyright (C) 2013 Fernando Navarro ([email protected])
  6. * Copyright (C) 2013 Diego Gutierrez ([email protected])
  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. */
  28. #[vertex]
  29. #version 450
  30. layout(location = 0) out vec2 tex_coord;
  31. layout(location = 1) out vec4 offset;
  32. layout(push_constant, std430) uniform Params {
  33. vec2 inv_size;
  34. vec2 reserved;
  35. }
  36. params;
  37. void main() {
  38. vec2 vertex_base;
  39. if (gl_VertexIndex == 0) {
  40. vertex_base = vec2(-1.0, -1.0);
  41. } else if (gl_VertexIndex == 1) {
  42. vertex_base = vec2(-1.0, 3.0);
  43. } else {
  44. vertex_base = vec2(3.0, -1.0);
  45. }
  46. gl_Position = vec4(vertex_base, 0.0, 1.0);
  47. tex_coord = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  48. offset = fma(params.inv_size.xyxy, vec4(1.0, 0.0, 0.0, 1.0), tex_coord.xyxy);
  49. }
  50. #[fragment]
  51. #version 450
  52. layout(location = 0) in vec2 tex_coord;
  53. layout(location = 1) in vec4 offset;
  54. layout(set = 0, binding = 0) uniform sampler2D color_tex;
  55. layout(set = 1, binding = 0) uniform sampler2D blend_tex;
  56. layout(location = 0) out vec4 out_color;
  57. layout(push_constant, std430) uniform Params {
  58. vec2 inv_size;
  59. vec2 reserved;
  60. }
  61. params;
  62. #define textureLinear(tex, uv) srgb_to_linear(textureLod(tex, uv, 0.0).rgb)
  63. vec3 linear_to_srgb(vec3 color) {
  64. // If going to srgb, clamp from 0 to 1.
  65. color = clamp(color, vec3(0.0), vec3(1.0));
  66. const vec3 a = vec3(0.055f);
  67. return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
  68. }
  69. vec3 srgb_to_linear(vec3 color) {
  70. return mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045)));
  71. }
  72. void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
  73. if (cond.x) {
  74. variable.x = value.x;
  75. }
  76. if (cond.y) {
  77. variable.y = value.y;
  78. }
  79. }
  80. void SMAAMovc(bvec4 cond, inout vec4 variable, vec4 value) {
  81. SMAAMovc(cond.xy, variable.xy, value.xy);
  82. SMAAMovc(cond.zw, variable.zw, value.zw);
  83. }
  84. void main() {
  85. vec4 a;
  86. a.x = texture(blend_tex, offset.xy).a;
  87. a.y = texture(blend_tex, offset.zw).g;
  88. a.wz = texture(blend_tex, tex_coord).xz;
  89. if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
  90. out_color = textureLod(color_tex, tex_coord, 0.0);
  91. } else {
  92. bool h = max(a.x, a.z) > max(a.y, a.w);
  93. vec4 blending_offset = vec4(0.0, a.y, 0.0, a.w);
  94. vec2 blending_weight = a.yw;
  95. SMAAMovc(bvec4(h, h, h, h), blending_offset, vec4(a.x, 0.0, a.z, 0.0));
  96. SMAAMovc(bvec2(h, h), blending_weight, a.xz);
  97. blending_weight /= dot(blending_weight, vec2(1.0, 1.0));
  98. vec4 blending_coord = fma(blending_offset, vec4(params.inv_size.xy, -params.inv_size.xy), tex_coord.xyxy);
  99. out_color.rgb = blending_weight.x * textureLinear(color_tex, blending_coord.xy);
  100. out_color.rgb += blending_weight.y * textureLinear(color_tex, blending_coord.zw);
  101. out_color.rgb = linear_to_srgb(out_color.rgb);
  102. out_color.a = texture(color_tex, tex_coord).a;
  103. }
  104. }