xbr-mlv4-pass2.fs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Hyllian's xBR MultiLevel4 Shader - Pass2
  3. Ported by Aliaspider.
  4. Copyright (C) 2011/2013 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. #version 150
  18. #define ROUND(X) floor((X)+0.5)
  19. #define y_weighted (vec3(0.299, 0.587, 0.114)* 48.0)
  20. #define maximo vec4(256.0)
  21. uniform sampler2D source[];
  22. uniform vec4 sourceSize[];
  23. in Vertex {
  24. vec2 texCoord;
  25. };
  26. out vec4 fragColor;
  27. vec4 df(vec4 A, vec4 B)
  28. {
  29. return vec4(abs(A-B));
  30. }
  31. bvec4 rd(vec4 A, vec4 B, vec4 C, vec4 D)
  32. {
  33. return greaterThan((df(C,D)/(df(A,B)+0.000000001)) , vec4(2.0));
  34. }
  35. bvec4 id(vec4 A, vec4 B, vec4 C, vec4 D)
  36. {
  37. return greaterThan(df(C,D) , df(A,B));
  38. }
  39. vec4 remapTo01(vec4 v, vec4 high)
  40. {
  41. return (v/high);
  42. }
  43. vec4 remapFrom01(vec4 v, vec4 high)
  44. {
  45. return ROUND(high*v);
  46. }
  47. #define ORIG_ID 1
  48. #define TEX(dx,dy) texture(source[0], texCoord+vec2((dx),(dy))*sourceSize[0].zw)
  49. #define TEX_ORIG(dx,dy) texture(source[ORIG_ID], texCoord+vec2((dx),(dy))*sourceSize[ORIG_ID].zw).rgb
  50. // A1 B1 C1
  51. // A0 A B C C4
  52. // D0 D E F F4
  53. // G0 G H I I4
  54. // G5 H5 I5
  55. void main(void){
  56. vec4 PA = TEX(-1,-1); vec4 PB = TEX( 0,-1); vec4 PC = TEX( 1,-1);
  57. vec4 PD = TEX(-1, 0); vec4 PE = TEX( 0, 0); vec4 PF = TEX( 1, 0);
  58. vec4 PG = TEX(-1, 1); vec4 PH = TEX( 0, 1); vec4 PI = TEX( 1, 1);
  59. vec3 A1 = TEX_ORIG(-1,-2); vec3 B1 = TEX_ORIG( 0,-2); vec3 C1 = TEX_ORIG( 1,-2);
  60. vec3 A0 = TEX_ORIG(-2,-1); vec3 A = TEX_ORIG(-1,-1); vec3 B = TEX_ORIG( 0,-1); vec3 C = TEX_ORIG( 1,-1); vec3 C4 = TEX_ORIG( 2,-1);
  61. vec3 D0 = TEX_ORIG(-2, 0); vec3 D = TEX_ORIG(-1, 0); vec3 E = TEX_ORIG( 0, 0); vec3 F = TEX_ORIG( 1, 0); vec3 F4 = TEX_ORIG( 2, 0);
  62. vec3 G0 = TEX_ORIG(-2, 1); vec3 G = TEX_ORIG(-1, 1); vec3 H = TEX_ORIG( 0, 1); vec3 I = TEX_ORIG( 1, 1); vec3 I4 = TEX_ORIG( 2, 1);
  63. vec3 G5 = TEX_ORIG(-1, 2); vec3 H5 = TEX_ORIG( 0, 2); vec3 I5 = TEX_ORIG( 1, 2);
  64. vec4 b = (transpose(mat4x3(B, D, H, F))* y_weighted );
  65. vec4 c = (transpose( mat4x3(C, A, G, I))* y_weighted );
  66. vec4 e = (transpose( mat4x3(E, E, E, E))* y_weighted );
  67. vec4 d = b.yzwx;
  68. vec4 f = b.wxyz;
  69. vec4 g = c.zwxy;
  70. vec4 h = b.zwxy;
  71. vec4 i = c.wxyz;
  72. vec4 i4 = (transpose(mat4x3(I4, C1, A0, G5))* y_weighted );
  73. vec4 i5 = (transpose(mat4x3(I5, C4, A1, G0))* y_weighted );
  74. vec4 h5 = (transpose(mat4x3(H5, F4, B1, D0))* y_weighted );
  75. vec4 f4 = h5.yzwx;
  76. vec4 pe = remapFrom01(PE, maximo);
  77. vec4 pf = remapFrom01(PF, maximo);
  78. vec4 ph = remapFrom01(PH, maximo);
  79. vec4 pb = remapFrom01(PB, maximo);
  80. vec4 pd = remapFrom01(PD, maximo);
  81. vec4 f2 = vec4(pf.z, pb.w, pd.x, ph.y);
  82. vec4 h2 = vec4(ph.z, pf.w, pb.x, pd.y);
  83. vec4 f1 = vec4(pf.y, pb.z, pd.w, ph.x);
  84. vec4 h3 = vec4(ph.w, pf.x, pb.y, pd.z);
  85. bvec4 nbrs = (greaterThan(pe.yzwx , vec4(1.0)) || greaterThan(pe.wxyz , vec4(1.0)));
  86. bvec4 jag1 = (greaterThan(f2 , vec4(1.0)) || greaterThan(h2 , vec4(1.0)));
  87. bvec4 jag2 = (greaterThan(f2 , vec4(2.0)) || greaterThan(h2 , vec4(2.0)));
  88. bvec4 jag3 = (greaterThan(f2 , vec4(4.0)) || greaterThan(h2 , vec4(4.0)));
  89. bvec4 cond1=(equal(pe,vec4(7.0)) || equal(pe,vec4(8.0)));
  90. pe.x=cond1.x?jag3.x?pe.x: (pe.x-2.0) : pe.x;
  91. pe.y=cond1.y?jag3.y?pe.y: (pe.y-2.0) : pe.y;
  92. pe.z=cond1.z?jag3.z?pe.z: (pe.z-2.0) : pe.z;
  93. pe.w=cond1.w?jag3.w?pe.w: (pe.w-2.0) : pe.w;
  94. bvec4 cond2=(equal(pe,vec4(5.0)) || equal(pe,vec4(6.0)));
  95. pe.x=cond2.x?jag2.x?pe.x: (pe.x-2.0) : pe.x;
  96. pe.y=cond2.y?jag2.y?pe.y: (pe.y-2.0) : pe.y;
  97. pe.z=cond2.z?jag2.z?pe.z: (pe.z-2.0) : pe.z;
  98. pe.w=cond2.w?jag2.w?pe.w: (pe.w-2.0) : pe.w;
  99. bvec4 jag91 = ((id(h,i,e,h) || id(i4,i,f4,i4)) && greaterThan(f2 ,vec4(1.0)) && greaterThan(f1 ,vec4(1.0)));
  100. bvec4 jag92 = ((id(f,i,e,f) || id(i5,i,h5,i5)) && greaterThan(h2 ,vec4(1.0)) && greaterThan(h3 ,vec4(1.0)));
  101. bvec4 jag93 = ( rd(h,g,e,g));
  102. bvec4 jag94 = ( rd(f,c,e,c));
  103. bvec4 jag9 = (not(jag91 && jag93 || jag92 && jag94));
  104. bvec4 cond3=(equal(pe ,vec4(0.0)) || (not(nbrs) || jag1) && jag9);
  105. pe.x=cond3.x?pe.x:1.0;
  106. pe.y=cond3.y?pe.y:1.0;
  107. pe.z=cond3.z?pe.z:1.0;
  108. pe.w=cond3.w?pe.w:1.0;
  109. fragColor=vec4(remapTo01(pe, maximo));
  110. }