hqscale.frag 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. 4xGLSLHqFilter shader
  3. Copyright (C) 2005 guest(r) - guest.r@gmail.com
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #version 110
  17. uniform sampler2D baseMap;
  18. varying vec2 vTexcoord;
  19. varying vec4 vT1;
  20. varying vec4 vT2;
  21. varying vec4 vT3;
  22. varying vec4 vT4;
  23. varying vec4 vT5;
  24. varying vec4 vT6;
  25. /* Default Vertex shader */
  26. const vec3 dt = vec3(1.0, 1.0, 1.0);
  27. const float mx = 1.0; // start smoothing wt.
  28. const float k = -1.10; // wt. decrease factor
  29. const float max_w = 0.75; // max filter weigth
  30. const float min_w = 0.03; // min filter weigth
  31. const float lum_add = 0.33; // effects smoothing
  32. #define XS 1024.0
  33. #define YS 512.0
  34. #define AX 0.5/XS
  35. #define AY 0.5/YS
  36. void main(void)
  37. {
  38. /* FIXME: Would be faster if we just disabled GL_LINEAR,
  39. * then we don't need this floor() junk..
  40. */
  41. vec3 c = texture2D(baseMap, vec2(floor(vTexcoord.x*XS)/XS+AX, floor(vTexcoord.y*YS)/YS+AY)).xyz;
  42. vec3 i1 = texture2D(baseMap, vec2(floor(vT1.x*XS)/XS+AX, floor(vT1.y*YS)/YS+AY)).xyz;
  43. vec3 i2 = texture2D(baseMap, vec2(floor(vT2.x*XS)/XS+AX, floor(vT2.y*YS)/YS+AY)).xyz;
  44. vec3 i3 = texture2D(baseMap, vec2(floor(vT3.x*XS)/XS+AX, floor(vT3.y*YS)/YS+AY)).xyz;
  45. vec3 i4 = texture2D(baseMap, vec2(floor(vT4.x*XS)/XS+AX, floor(vT4.y*YS)/YS+AY)).xyz;
  46. vec3 o1 = texture2D(baseMap, vec2(floor(vT5.x*XS)/XS+AX, floor(vT5.y*YS)/YS+AY)).xyz;
  47. vec3 o3 = texture2D(baseMap, vec2(floor(vT6.x*XS)/XS+AX, floor(vT6.y*YS)/YS+AY)).xyz;
  48. vec3 o2 = texture2D(baseMap, vec2(floor(vT5.z*XS)/XS+AX, floor(vT5.w*YS)/YS+AY)).xyz;
  49. vec3 o4 = texture2D(baseMap, vec2(floor(vT6.z*XS)/XS+AX, floor(vT6.w*YS)/YS+AY)).xyz;
  50. vec3 s1 = texture2D(baseMap, vec2(floor(vT1.z*XS)/XS+AX, floor(vT1.w*YS)/YS+AY)).xyz;
  51. vec3 s2 = texture2D(baseMap, vec2(floor(vT2.z*XS)/XS+AX, floor(vT2.w*YS)/YS+AY)).xyz;
  52. vec3 s3 = texture2D(baseMap, vec2(floor(vT3.z*XS)/XS+AX, floor(vT3.w*YS)/YS+AY)).xyz;
  53. vec3 s4 = texture2D(baseMap, vec2(floor(vT4.z*XS)/XS+AX, floor(vT4.w*YS)/YS+AY)).xyz;
  54. float ko1=dot(abs(o1-c),dt);
  55. float ko2=dot(abs(o2-c),dt);
  56. float ko3=dot(abs(o3-c),dt);
  57. float ko4=dot(abs(o4-c),dt);
  58. float k1=min(dot(abs(i1-i3),dt),max(ko1,ko3));
  59. float k2=min(dot(abs(i2-i4),dt),max(ko2,ko4));
  60. float w1 = k2; if(ko3<ko1) w1*=ko3/ko1;
  61. float w2 = k1; if(ko4<ko2) w2*=ko4/ko2;
  62. float w3 = k2; if(ko1<ko3) w3*=ko1/ko3;
  63. float w4 = k1; if(ko2<ko4) w4*=ko2/ko4;
  64. c=(w1*o1+w2*o2+w3*o3+w4*o4+0.001*c)/(w1+w2+w3+w4+0.001);
  65. w1 = k*dot(abs(i1-c)+abs(i3-c),dt)/(0.125*dot(i1+i3,dt)+lum_add);
  66. w2 = k*dot(abs(i2-c)+abs(i4-c),dt)/(0.125*dot(i2+i4,dt)+lum_add);
  67. w3 = k*dot(abs(s1-c)+abs(s3-c),dt)/(0.125*dot(s1+s3,dt)+lum_add);
  68. w4 = k*dot(abs(s2-c)+abs(s4-c),dt)/(0.125*dot(s2+s4,dt)+lum_add);
  69. w1 = clamp(w1+mx,min_w,max_w);
  70. w2 = clamp(w2+mx,min_w,max_w);
  71. w3 = clamp(w3+mx,min_w,max_w);
  72. w4 = clamp(w4+mx,min_w,max_w);
  73. gl_FragColor = vec4((w1*(i1+i3)+w2*(i2+i4)+w3*(s1+s3)+w4*(s2+s4)+c)/(2.0*(w1+w2+w3+w4)+1.0), 1.0);
  74. }