custom-jinc2-sharper.fs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #version 150
  2. /*
  3. Hyllian's jinc windowed-jinc 2-lobe sharper with anti-ringing Shader
  4. Copyright (C) 2011-2014 Hyllian/Jararaca - sergiogdb@gmail.com
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. uniform sampler2D source[];
  18. uniform sampler2D frame[];
  19. uniform sampler2D pixmap[];
  20. uniform vec4 sourceSize[];
  21. uniform vec4 targetSize;
  22. in Vertex {
  23. vec2 texCoord;
  24. };
  25. out vec4 fragColor;
  26. #define JINC2_WINDOW_SINC 0.42
  27. #define JINC2_SINC 0.92
  28. #define JINC2_AR_STRENGTH 1.0
  29. #define mul(a,b) (b*a)
  30. /*
  31. This is an approximation of Jinc(x)*Jinc(x*r1/r2) for x < 2.5,
  32. where r1 and r2 are the first two zeros of jinc function.
  33. For a jinc 2-lobe best approximation, use A=0.5 and B=0.825.
  34. */
  35. // A=0.5, B=0.825 is the best jinc approximation for x<2.5. if B=1.0, it's a lanczos filter.
  36. // Increase A to get more blur. Decrease it to get a sharper picture.
  37. // B = 0.825 to get rid of dithering. Increase B to get a fine sharpness, though dithering returns.
  38. #define halfpi 1.5707963267948966192313216916398
  39. #define pi 3.1415926535897932384626433832795
  40. #define wa (JINC2_WINDOW_SINC*pi)
  41. #define wb (JINC2_SINC*pi)
  42. const vec3 Y = vec3(0.299, 0.587, 0.114);
  43. float df(float A, float B)
  44. {
  45. return abs(A-B);
  46. }
  47. // Calculates the distance between two points
  48. float d(vec2 pt1, vec2 pt2)
  49. {
  50. vec2 v = pt2 - pt1;
  51. return sqrt(dot(v,v));
  52. }
  53. vec3 min4(vec3 a, vec3 b, vec3 c, vec3 d)
  54. {
  55. return min(a, min(b, min(c, d)));
  56. }
  57. vec3 max4(vec3 a, vec3 b, vec3 c, vec3 d)
  58. {
  59. return max(a, max(b, max(c, d)));
  60. }
  61. vec4 resampler(vec4 x)
  62. {
  63. vec4 res;
  64. res = (x == vec4(0.0, 0.0, 0.0, 0.0)) ? vec4(wa*wb) : sin(x*wa)*sin(x*wb)/(x*x);
  65. return res;
  66. }
  67. #define vTexCoord (texCoord * 1.0001)
  68. #define SourceSize sourceSize[0]
  69. #define Source source[0]
  70. #define Original source[6]
  71. #define FragColor fragColor
  72. void main(void) {
  73. vec3 color;
  74. mat4x4 weights;
  75. vec2 dx = vec2(1.0, 0.0);
  76. vec2 dy = vec2(0.0, 1.0);
  77. vec2 pc = vTexCoord * SourceSize.xy;
  78. vec2 tc = (floor(pc-vec2(0.5,0.5))+vec2(0.5,0.5));
  79. weights[0] = resampler(vec4(d(pc, tc -dx -dy), d(pc, tc -dy), d(pc, tc +dx -dy), d(pc, tc+2.0*dx -dy)));
  80. weights[1] = resampler(vec4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
  81. weights[2] = resampler(vec4(d(pc, tc -dx +dy), d(pc, tc +dy), d(pc, tc +dx +dy), d(pc, tc+2.0*dx +dy)));
  82. weights[3] = resampler(vec4(d(pc, tc -dx+2.0*dy), d(pc, tc +2.0*dy), d(pc, tc +dx+2.0*dy), d(pc, tc+2.0*dx+2.0*dy)));
  83. //weights[0][0] = weights[0][3] = weights[3][0] = weights[3][3] = 0.0;
  84. dx = dx / SourceSize.xy;
  85. dy = dy / SourceSize.xy;
  86. tc = tc / SourceSize.xy;
  87. // reading the texels
  88. vec3 c00 = texture(Source, tc -dx -dy).xyz;
  89. vec3 c10 = texture(Source, tc -dy).xyz;
  90. vec3 c20 = texture(Source, tc +dx -dy).xyz;
  91. vec3 c30 = texture(Source, tc+2.0*dx -dy).xyz;
  92. vec3 c01 = texture(Source, tc -dx ).xyz;
  93. vec3 c11 = texture(Source, tc ).xyz;
  94. vec3 c21 = texture(Source, tc +dx ).xyz;
  95. vec3 c31 = texture(Source, tc+2.0*dx ).xyz;
  96. vec3 c02 = texture(Source, tc -dx +dy).xyz;
  97. vec3 c12 = texture(Source, tc +dy).xyz;
  98. vec3 c22 = texture(Source, tc +dx +dy).xyz;
  99. vec3 c32 = texture(Source, tc+2.0*dx +dy).xyz;
  100. vec3 c03 = texture(Source, tc -dx+2.0*dy).xyz;
  101. vec3 c13 = texture(Source, tc +2.0*dy).xyz;
  102. vec3 c23 = texture(Source, tc +dx+2.0*dy).xyz;
  103. vec3 c33 = texture(Source, tc+2.0*dx+2.0*dy).xyz;
  104. color = mul(weights[0], mat4x3(c00, c10, c20, c30));
  105. color+= mul(weights[1], mat4x3(c01, c11, c21, c31));
  106. color+= mul(weights[2], mat4x3(c02, c12, c22, c32));
  107. color+= mul(weights[3], mat4x3(c03, c13, c23, c33));
  108. color = color/(dot(mul(weights, vec4(1.)), vec4(1.)));
  109. // Anti-ringing
  110. // Get min/max samples
  111. pc = vTexCoord;
  112. c00 = texture(Source, pc ).xyz;
  113. c11 = texture(Source, pc +dx ).xyz;
  114. c21 = texture(Source, pc -dx ).xyz;
  115. c12 = texture(Source, pc +dy).xyz;
  116. c22 = texture(Source, pc -dy).xyz;
  117. vec3 min_sample = min4(c11, c21, c12, c22);
  118. vec3 max_sample = max4(c11, c21, c12, c22);
  119. min_sample = min(min_sample, c00);
  120. max_sample = max(max_sample, c00);
  121. vec3 aux = color;
  122. color = clamp(color, min_sample, max_sample);
  123. color = mix(aux, color, JINC2_AR_STRENGTH);
  124. FragColor = vec4(color, 1.);
  125. }