xbr-mlv4-pass3.fs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Hyllian's xBR MultiLevel4 Shader - Pass3
  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 ORIG_ID 2
  20. #define y_weighted (vec3(0.299, 0.587, 0.114)* 48.0)
  21. #define maximo vec4(256.0)
  22. #define low vec4(-64.0)
  23. #define high vec4( 64.0)
  24. #define sym_vectors transpose(mat2x4(1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0))
  25. // Bx, Ay, C
  26. vec3 lines[13] = vec3 [](
  27. vec3(4.0, 4.0, 4.0), // 0 NL
  28. vec3(4.0, 4.0, 3.0), // 1 LV0
  29. vec3(4.0, 4.0, 2.0), // 2 LV1
  30. vec3(8.0, 4.0, 2.0), // 3 LV2u
  31. vec3(4.0, 8.0, 2.0), // 4 LV2l
  32. vec3(12.0, 4.0, 2.0), // 5 LV3u
  33. vec3(4.0,12.0, 2.0), // 6 LV3l
  34. vec3(16.0, 4.0, 2.0), // 7 LV4u
  35. vec3(4.0,16.0, 2.0), // 8 LV4l
  36. vec3(12.0, 4.0, 6.0), // 9 LV3u
  37. vec3(4.0,12.0, 6.0), // 10 LV3l
  38. vec3(16.0, 4.0, 6.0), // 11 LV4u
  39. vec3(4.0,16.0, 6.0) // 12 LV4l
  40. );
  41. uniform sampler2D source[];
  42. uniform vec4 sourceSize[];
  43. uniform vec4 targetSize;
  44. in Vertex {
  45. vec2 texCoord;
  46. };
  47. out vec4 fragColor;
  48. vec4 remapTo01(vec4 v, vec4 _low, vec4 _high)
  49. {
  50. return clamp((v - _low)/(_high-_low),0.0,1.0);
  51. }
  52. float remapFrom01(float v, float _high)
  53. {
  54. return ROUND(_high*v);
  55. }
  56. float df(float A, float B)
  57. {
  58. return abs(A-B);
  59. }
  60. // A3 B3 C3
  61. // A1 B1 C1
  62. //A2 A0 A B C C4 C6
  63. //D2 D0 D E F F4 F6
  64. //G2 G0 G H I I4 I6
  65. // G5 H5 I5
  66. // G7 H7 I7
  67. void main(void){
  68. float px;
  69. vec2 pos = fract(texCoord*sourceSize[ORIG_ID].xy)-vec2(0.5, 0.5); // pos = pixel position
  70. vec2 dir = sign(pos); // dir = pixel direction
  71. vec2 g1 = dir* vec2(max( dir.y*dir.x,0.0), max(-dir.y*dir.x,0.0))*sourceSize[ORIG_ID].zw;
  72. vec2 g2 = dir* vec2(max(-dir.y*dir.x,0.0), max( dir.y*dir.x,0.0))*sourceSize[ORIG_ID].zw;
  73. vec3 E = texture(source[ORIG_ID], texCoord ).rgb;
  74. vec3 F = texture(source[ORIG_ID], texCoord +g1).rgb;
  75. vec3 H = texture(source[ORIG_ID], texCoord +g2).rgb;
  76. vec3 I = texture(source[ORIG_ID], texCoord +g1+g2).rgb;
  77. vec3 F4 = texture(source[ORIG_ID], texCoord +2.0*g1).rgb;
  78. vec3 H5 = texture(source[ORIG_ID], texCoord +2.0*g2).rgb;
  79. float e = dot(E, y_weighted);
  80. float f = dot(F, y_weighted);
  81. float h = dot(H, y_weighted);
  82. float i = dot(I, y_weighted);
  83. float f4= dot(F4, y_weighted);
  84. float h5= dot(H5, y_weighted);
  85. vec4 icomp = (clamp((dir* sym_vectors),0.0,1.0)); // choose info component
  86. float infoE = remapFrom01(dot(texture(source[0], texCoord ), icomp), 256.0f); // retrieve 1st pass info
  87. float infoF = remapFrom01(dot(texture(source[0], texCoord+g1), icomp), 256.0f); // 1st pass info from neighbor r
  88. float infoH = remapFrom01(dot(texture(source[0], texCoord+g2), icomp), 256.0f); // 1st pass info from neighbor d
  89. vec4 lparam;
  90. vec2 addr;
  91. if (infoF == 8.0)
  92. {
  93. lparam.xyz = lines[12];
  94. px = float(df(f,f4) <= df(f,i));
  95. addr.x = 2*px + clamp(1.0-px,0.0,1.0);
  96. addr.y = clamp(1.0-px,0.0,1.0);
  97. }
  98. else if (infoH == 7.0)
  99. {
  100. lparam.xyz = lines[11];
  101. px = float(df(h,h5) <= df(h,i));
  102. addr.x = clamp(1.0-px,0.0,1.0);
  103. addr.y = 2*px + clamp(1.0-px,0.0,1.0);
  104. }
  105. else if (infoF == 6.0)
  106. {
  107. lparam.xyz = lines[10];
  108. px = float(df(f,f4) <= df(f,i));
  109. addr.x = 2*px + clamp(1.0-px,0.0,1.0);
  110. addr.y = clamp(1.0-px,0.0,1.0);
  111. }
  112. else if (infoH == 5.0)
  113. {
  114. lparam.xyz = lines[9];
  115. px = float(df(h,h5) <= df(h,i));
  116. addr.x = clamp(1.0-px,0.0,1.0);
  117. addr.y = 2*px + clamp(1.0-px,0.0,1.0);
  118. }
  119. else
  120. {
  121. px = float(df(e,f) <= df(e,h));
  122. addr.x = px;
  123. addr.y = clamp(1.0-px,0.0,1.0);
  124. lparam.xyz = (infoE == 8.0) ? lines[8] : ((infoE == 7.0) ? lines[7] : ((infoE == 6.0) ? lines[6] : ((infoE == 5.0) ? lines[5] : ((infoE == 4.0) ? lines[4] : ((infoE == 3.0) ? lines[3] : ((infoE == 2.0) ? lines[2] : ((infoE == 1.0) ? lines[1] : lines[0])))))));
  125. }
  126. bool inv = (dir.x*dir.y) < 0.0 ? true : false;
  127. // Rotate address from relative to absolute.
  128. addr = addr*dir.yx;
  129. addr = inv ? addr.yx : addr;
  130. // Rotate straight line equation from relative to absolute.
  131. lparam.xy = lparam.xy*dir.yx;
  132. lparam.xy = inv ? lparam.yx : lparam.xy;
  133. addr+=vec2(2.0);
  134. lparam.w = addr.x*8.0 + addr.y;
  135. fragColor=remapTo01(lparam, low, high);
  136. }
  137. /*
  138. 19 1
  139. 9 1
  140. 4 0
  141. 2 0
  142. 1 1
  143. 0 0
  144. 0 0000 ND
  145. 1 0001 EDR0
  146. 2 0010 EDR
  147. 3 0011 EDRU
  148. 4 0100 EDRL
  149. 5 0101 EDRU3
  150. 6 0110 EDRL3
  151. 0 1 2 3 4
  152. -2 -1 0 1 2
  153. */