lanczos16.fs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // This is a port of the original CG shader to the quark format
  2. // the original shader can be found here :
  3. // https://github.com/libretro/common-shaders/tree/master/lanczos
  4. /*
  5. Copyright (C) 2007 guest(r) - guest.r@gmail.com
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #version 150
  19. #define floatpi 1.5707963267948966192313216916398
  20. #define pi 3.1415926535897932384626433832795
  21. uniform sampler2D source[];
  22. uniform vec4 sourceSize[];
  23. uniform vec4 targetSize;
  24. in Vertex {
  25. vec2 texCoord;
  26. vec4 t1;
  27. vec4 t2;
  28. vec4 t3;
  29. vec4 t4;
  30. };
  31. #define SI(X,f1,f2) ((X)==0)?((f1)*(f2)):(sin((f1)*(X))*sin((f2)*(X))/((X)*(X)))
  32. out vec4 fragColor;
  33. vec4 l(vec4 x) {
  34. // vec4 res;
  35. // res =all(equal(x,vec4(0.0, 0.0, 0.0, 0.0)))?vec4(pi*floatpi):sin(x*floatpi)* sin(x*pi)/(x*x);
  36. return vec4(SI(x.x,pi,floatpi),SI(x.y,pi,floatpi),SI(x.z,pi,floatpi),SI(x.w,pi,floatpi));
  37. }
  38. void main(void) {
  39. mat4x3 pix;
  40. vec2 frac = fract(texCoord*sourceSize[0].xy);
  41. vec2 offset = fract(texCoord * sourceSize[0].xy) - 0.5;
  42. offset /= sourceSize[0].xy;
  43. // calculating texel weights
  44. vec4 abcd, pqrs;
  45. abcd = l(vec4(1+frac.x, frac.x, 1-frac.x, 2-frac.x));
  46. pqrs = l(vec4(1+frac.y, frac.y, 1-frac.y, 2-frac.y));
  47. // reading the texels
  48. vec3 c00 = texture(source[0], t1.xz-offset).xyz;
  49. vec3 c10 = texture(source[0], t1.yz-offset).xyz;
  50. vec3 c20 = texture(source[0], t2.xz-offset).xyz;
  51. vec3 c30 = texture(source[0], t2.yz-offset).xyz;
  52. vec3 c01 = texture(source[0], t1.xw-offset).xyz;
  53. vec3 c11 = texture(source[0], texCoord-offset).xyz;
  54. vec3 c21 = texture(source[0], t2.xw-offset).xyz;
  55. vec3 c31 = texture(source[0], t2.yw-offset).xyz;
  56. vec3 c02 = texture(source[0], t3.xz-offset).xyz;
  57. vec3 c12 = texture(source[0], t3.yz-offset).xyz;
  58. vec3 c22 = texture(source[0], t4.xz-offset).xyz;
  59. vec3 c32 = texture(source[0], t4.yz-offset).xyz;
  60. vec3 c03 = texture(source[0], t3.xw-offset).xyz;
  61. vec3 c13 = texture(source[0], t3.yw-offset).xyz;
  62. vec3 c23 = texture(source[0], t4.xw-offset).xyz;
  63. vec3 c33 = texture(source[0], t4.yw-offset).xyz;
  64. pix[0] = abcd* transpose(mat4x3(-c00, c10, c20, -c30));
  65. pix[1] = abcd* transpose(mat4x3( c01, c11, c21, c31));
  66. pix[2] = abcd* transpose(mat4x3( c02, c12, c22, c32));
  67. pix[3] = abcd* transpose(mat4x3(-c03, c13, c23, -c33));
  68. // final sum and weight normalization
  69. fragColor=vec4((pqrs*transpose(pix))/vec3((dot(abcd, vec4(1.0))* dot(pqrs, vec4(1.0)))-2.0*(abcd.x+abcd.w)*(pqrs.x+pqrs.w)),1.0);
  70. }