super-8xbr-3d-pass3.fs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #version 150
  2. /*
  3. ******* Super 8XBR 3D Shader - pass3 *******
  4. Copyright (c) 2017 Hyllian - sergiogdb@gmail.com
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. uniform sampler2D source[];
  22. uniform sampler2D frame[];
  23. uniform sampler2D pixmap[];
  24. uniform vec4 sourceSize[];
  25. uniform vec4 targetSize;
  26. in Vertex {
  27. vec2 texCoord;
  28. };
  29. out vec4 fragColor;
  30. #define wp1 8.0
  31. #define wp2 0.0
  32. #define wp3 0.0
  33. #define wp4 0.0
  34. #define wp5 0.0
  35. #define wp6 0.0
  36. #define XBR_RES2 4.0
  37. const float XBR_EDGE_STR = 2.0;
  38. const float XBR_WEIGHT = 1.0;
  39. const float XBR_ANTI_RINGING = 1.0;
  40. #define mul(a,b) (b*a)
  41. #define weight1 (XBR_WEIGHT*1.75068/10.0)
  42. #define weight2 (XBR_WEIGHT*1.29633/10.0/2.0)
  43. const vec3 Y = vec3(.2126, .7152, .0722);
  44. const vec3 dtt = vec3(65536.,255.,1.);
  45. vec4 reduce4(vec3 A, vec3 B, vec3 C, vec3 D)
  46. {
  47. return mul(mat4x3(A, B, C, D), dtt);
  48. }
  49. float RGBtoYUV(vec3 color)
  50. {
  51. return dot(color, Y);
  52. }
  53. float df(float A, float B)
  54. {
  55. return abs(A-B);
  56. }
  57. bool eq(float A, float B)
  58. {
  59. return (df(A, B) < 15.0);
  60. }
  61. /*
  62. P1
  63. |P0|B |C |P1| C F4 |a0|b1|c2|d3|
  64. |D |E |F |F4| B F I4 |b0|c1|d2|e3| |e1|i1|i2|e2|
  65. |G |H |I |I4| P0 E A I P3 |c0|d1|e2|f3| |e3|i3|i4|e4|
  66. |P2|H5|I5|P3| D H I5 |d0|e1|f2|g3|
  67. G H5
  68. P2
  69. */
  70. float d_wd(float b0, float b1, float c0, float c1, float c2, float d0, float d1, float d2, float d3, float e1, float e2, float e3, float f2, float f3)
  71. {
  72. return (wp1*(df(c1,c2) + df(c1,c0) + df(e2,e1) + df(e2,e3)) + wp2*(df(d2,d3) + df(d0,d1)) + wp3*(df(d1,d3) + df(d0,d2)) + wp4*df(d1,d2) + wp5*(df(c0,c2) + df(e1,e3)) + wp6*(df(b0,b1) + df(f2,f3)));
  73. }
  74. float hv_wd(float i1, float i2, float i3, float i4, float e1, float e2, float e3, float e4)
  75. {
  76. return ( wp4*(df(i1,i2)+df(i3,i4)) + wp1*(df(i1,e1)+df(i2,e2)+df(i3,e3)+df(i4,e4)) );
  77. }
  78. vec3 min4(vec3 a, vec3 b, vec3 c, vec3 d)
  79. {
  80. return min(a, min(b, min(c, d)));
  81. }
  82. vec3 max4(vec3 a, vec3 b, vec3 c, vec3 d)
  83. {
  84. return max(a, max(b, max(c, d)));
  85. }
  86. #define vTexCoord (texCoord * 1.0001)
  87. #define SourceSize sourceSize[0]
  88. #define Source source[0]
  89. #define Original source[3]
  90. #define FragColor fragColor
  91. void main(void) {
  92. //Skip pixels on wrong grid
  93. vec2 dir = fract(vTexCoord * SourceSize.xy / XBR_RES2) - vec2(0.5, 0.5);
  94. if ((dir.x*dir.y) > 0.0)
  95. {
  96. FragColor = texture(Source, vTexCoord);
  97. }
  98. else
  99. {
  100. vec2 tex = (floor(vTexCoord * SourceSize.xy / XBR_RES2) + vec2(0.5, 0.5)) * XBR_RES2 * SourceSize.zw;
  101. vec2 g1 = vec2((XBR_RES2 / 2.0) / SourceSize.x, 0.0);
  102. vec2 g2 = vec2(0.0, (XBR_RES2 / 2.0) / SourceSize.y);
  103. vec3 P0 = texture(Source, vTexCoord -3.0*g1 ).xyz;
  104. vec3 P1 = texture(Source, vTexCoord -3.0*g2).xyz;
  105. vec3 P2 = texture(Source, vTexCoord +3.0*g2).xyz;
  106. vec3 P3 = texture(Source, vTexCoord +3.0*g1 ).xyz;
  107. vec3 B = texture(Source, vTexCoord -2.0*g1 -g2).xyz;
  108. vec3 C = texture(Source, vTexCoord -g1 -2.0*g2).xyz;
  109. vec3 D = texture(Source, vTexCoord -2.0*g1 +g2).xyz;
  110. vec3 E = texture(Source, vTexCoord -g1 ).xyz;
  111. vec3 F = texture(Source, vTexCoord -g2).xyz;
  112. vec3 G = texture(Source, vTexCoord -g1 +2.0*g2).xyz;
  113. vec3 H = texture(Source, vTexCoord +g2).xyz;
  114. vec3 I = texture(Source, vTexCoord +g1 ).xyz;
  115. vec3 F4 = texture(Source, vTexCoord +g1 -2.0*g2).xyz;
  116. vec3 I4 = texture(Source, vTexCoord +2.0*g1 -g2).xyz;
  117. vec3 H5 = texture(Source, vTexCoord +g1 +2.0*g2).xyz;
  118. vec3 I5 = texture(Source, vTexCoord +2.0*g1 +g2).xyz;
  119. vec3 A = texture(Source, vTexCoord).xyz;
  120. g1 *= 2.0;
  121. g2 *= 2.0;
  122. vec3 F6 = texture(Original, tex +g1+0.25*g1+0.25*g2).xyz;
  123. vec3 F7 = texture(Original, tex +g1+0.25*g1-0.25*g2).xyz;
  124. vec3 F8 = texture(Original, tex +g1-0.25*g1-0.25*g2).xyz;
  125. vec3 F9 = texture(Original, tex +g1-0.25*g1+0.25*g2).xyz;
  126. vec3 H6 = texture(Original, tex +0.25*g1+0.25*g2+g2).xyz;
  127. vec3 H7 = texture(Original, tex +0.25*g1-0.25*g2+g2).xyz;
  128. vec3 H8 = texture(Original, tex -0.25*g1-0.25*g2+g2).xyz;
  129. vec3 H9 = texture(Original, tex -0.25*g1+0.25*g2+g2).xyz;
  130. vec4 f0 = reduce4(F6, F7, F8, F9);
  131. vec4 h0 = reduce4(H6, H7, H8, H9);
  132. bool block_3d = ((f0.xyz == f0.yzw) && (h0.xyz == h0.yzw));
  133. float b = RGBtoYUV( B );
  134. float c = RGBtoYUV( C );
  135. float d = RGBtoYUV( D );
  136. float e = RGBtoYUV( E );
  137. float f = RGBtoYUV( F );
  138. float g = RGBtoYUV( G );
  139. float h = RGBtoYUV( H );
  140. float i = RGBtoYUV( I );
  141. float i4 = RGBtoYUV( I4 ); float p0 = RGBtoYUV( P0 );
  142. float i5 = RGBtoYUV( I5 ); float p1 = RGBtoYUV( P1 );
  143. float h5 = RGBtoYUV( H5 ); float p2 = RGBtoYUV( P2 );
  144. float f4 = RGBtoYUV( F4 ); float p3 = RGBtoYUV( P3 );
  145. /* Calc edgeness in diagonal directions. */
  146. float d_edge = (d_wd( d, b, g, e, c, p2, h, f, p1, h5, i, f4, i5, i4 ) - d_wd( c, f4, b, f, i4, p0, e, i, p3, d, h, i5, g, h5 ));
  147. /* Calc edgeness in horizontal/vertical directions. */
  148. float hv_edge = (hv_wd(f, i, e, h, c, i5, b, h5) - hv_wd(e, f, h, i, d, f4, g, i4));
  149. float limits = XBR_EDGE_STR + 0.000001;
  150. float edge_strength = smoothstep(0.0, limits, abs(d_edge));
  151. /* Filter weights. Two taps only. */
  152. vec4 w1 = vec4(-weight1, weight1+0.5, weight1+0.5, -weight1);
  153. vec4 w2 = vec4(-weight2, weight2+0.25, weight2+0.25, -weight2);
  154. /* Filtering and normalization in four direction generating four colors. */
  155. vec3 c1 = mul(w1, mat4x3( P2, H, F, P1 ));
  156. vec3 c2 = mul(w1, mat4x3( P0, E, I, P3 ));
  157. vec3 c3 = mul(w2, mat4x3(D+G, E+H, F+I, F4+I4));
  158. vec3 c4 = mul(w2, mat4x3(C+B, F+E, I+H, I5+H5));
  159. bool ir_lv1 = (((e!=f) && (e!=h)) && ( !eq(f,b) && !eq(f,c) || !eq(h,d) && !eq(h,g) || eq(e,i) && (!eq(f,f4) && !eq(f,i4) || !eq(h,h5) && !eq(h,i5)) || eq(e,g) || eq(e,c)) );
  160. /* Smoothly blends the two strongest directions (one in diagonal and the other in vert/horiz direction). */
  161. vec3 color = mix(mix(c1, c2, step(0.0, d_edge)), mix(c3, c4, step(0.0, hv_edge)), 1. - edge_strength);
  162. /* Anti-ringing code. */
  163. vec3 min_sample = min4( E, F, H, I ) + (1.-XBR_ANTI_RINGING)*mix((P2-H)*(F-P1), (P0-E)*(I-P3), step(0.0, d_edge));
  164. vec3 max_sample = max4( E, F, H, I ) - (1.-XBR_ANTI_RINGING)*mix((P2-H)*(F-P1), (P0-E)*(I-P3), step(0.0, d_edge));
  165. color = clamp(color, min_sample, max_sample);
  166. color = block_3d ? color : A;
  167. FragColor = vec4(color, 1.0);
  168. }
  169. }