Advertisement
Guest User

xBR-v3.9-rc1

a guest
Apr 7th, 2013
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. /* COMPATIBILITY
  2. - HLSL compilers
  3. - Cg compilers
  4. */
  5.  
  6. /*
  7. Hyllian's xBR v3.9 Shader - Gamma Corrected
  8.  
  9. Copyright (C) 2011/2012 Hyllian/Jararaca - sergiogdb@gmail.com
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25.  
  26. Incorporates some of the ideas from SABR shader. Thanks to Joshua Street.
  27. */
  28.  
  29. const static float coef = 2.0;
  30. const static float4 eq_threshold = float4(15.0, 15.0, 15.0, 15.0);
  31. const static half y_weight = 48.0;
  32. const static half u_weight = 7.0;
  33. const static half v_weight = 6.0;
  34. const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  35. const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
  36. const static float4 delta = float4(0.4, 0.4, 0.4, 0.4);
  37.  
  38. // Constants used with gamma correction.
  39. #define InputGamma 2.4
  40. #define OutputGamma 2.2
  41.  
  42. #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
  43. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  44.  
  45. float4 df(float4 A, float4 B)
  46. {
  47. return float4(abs(A-B));
  48. }
  49.  
  50. half c_df(half3 c1, half3 c2) {
  51. half3 df = abs(c1 - c2);
  52. return df.r + df.g + df.b;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. bool4 eq(float4 A, float4 B)
  59. {
  60. return (df(A, B) < eq_threshold);
  61. }
  62.  
  63. float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
  64. {
  65. return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  66. }
  67.  
  68.  
  69.  
  70. struct input
  71. {
  72. half2 video_size;
  73. float2 texture_size;
  74. half2 output_size;
  75. };
  76.  
  77.  
  78. struct out_vertex {
  79. float4 position : POSITION;
  80. float4 color : COLOR;
  81. float2 texCoord : TEXCOORD0;
  82. float4 t1;
  83. float4 t2;
  84. float4 t3;
  85. float4 t4;
  86. float4 t5;
  87. float4 t6;
  88. float4 t7;
  89. };
  90.  
  91. /* VERTEX_SHADER */
  92. out_vertex main_vertex
  93. (
  94. float4 position : POSITION,
  95. float4 color : COLOR,
  96. float2 texCoord : TEXCOORD0,
  97.  
  98. uniform half4x4 modelViewProj,
  99. uniform input IN
  100. )
  101. {
  102. out_vertex OUT;
  103.  
  104. OUT.position = mul(modelViewProj, position);
  105. OUT.color = color;
  106.  
  107. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  108. float dx = ps.x;
  109. float dy = ps.y;
  110.  
  111. // A1 B1 C1
  112. // A0 A B C C4
  113. // D0 D E F F4
  114. // G0 G H I I4
  115. // G5 H5 I5
  116.  
  117. OUT.texCoord = texCoord;
  118. OUT.t1 = texCoord.xxxy + half4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  119. OUT.t2 = texCoord.xxxy + half4( -dx, 0, dx, -dy); // A B C
  120. OUT.t3 = texCoord.xxxy + half4( -dx, 0, dx, 0); // D E F
  121. OUT.t4 = texCoord.xxxy + half4( -dx, 0, dx, dy); // G H I
  122. OUT.t5 = texCoord.xxxy + half4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  123. OUT.t6 = texCoord.xyyy + half4(-2.0*dx,-dy, 0, dy); // A0 D0 G0
  124. OUT.t7 = texCoord.xyyy + half4( 2.0*dx,-dy, 0, dy); // C4 F4 I4
  125.  
  126. return OUT;
  127. }
  128.  
  129.  
  130. /* FRAGMENT SHADER */
  131. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  132. {
  133. bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
  134. bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  135. bool4 nc, nc30, nc60, nc45; // new_color
  136. float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
  137. half3 res1, res2, pix1, pix2;
  138. float blend1, blend2;
  139.  
  140. float2 fp = frac(VAR.texCoord*IN.texture_size);
  141.  
  142. half3 A1 = tex2D(decal, VAR.t1.xw).rgb;
  143. half3 B1 = tex2D(decal, VAR.t1.yw).rgb;
  144. half3 C1 = tex2D(decal, VAR.t1.zw).rgb;
  145.  
  146. half3 A = tex2D(decal, VAR.t2.xw).rgb;
  147. half3 B = tex2D(decal, VAR.t2.yw).rgb;
  148. half3 C = tex2D(decal, VAR.t2.zw).rgb;
  149.  
  150. half3 D = tex2D(decal, VAR.t3.xw).rgb;
  151. half3 E = tex2D(decal, VAR.t3.yw).rgb;
  152. half3 F = tex2D(decal, VAR.t3.zw).rgb;
  153.  
  154. half3 G = tex2D(decal, VAR.t4.xw).rgb;
  155. half3 H = tex2D(decal, VAR.t4.yw).rgb;
  156. half3 I = tex2D(decal, VAR.t4.zw).rgb;
  157.  
  158. half3 G5 = tex2D(decal, VAR.t5.xw).rgb;
  159. half3 H5 = tex2D(decal, VAR.t5.yw).rgb;
  160. half3 I5 = tex2D(decal, VAR.t5.zw).rgb;
  161.  
  162. half3 A0 = tex2D(decal, VAR.t6.xy).rgb;
  163. half3 D0 = tex2D(decal, VAR.t6.xz).rgb;
  164. half3 G0 = tex2D(decal, VAR.t6.xw).rgb;
  165.  
  166. half3 C4 = tex2D(decal, VAR.t7.xy).rgb;
  167. half3 F4 = tex2D(decal, VAR.t7.xz).rgb;
  168. half3 I4 = tex2D(decal, VAR.t7.xw).rgb;
  169.  
  170. float4 b = mul( half4x3(B, D, H, F), yuv_weighted[0] );
  171. float4 c = mul( half4x3(C, A, G, I), yuv_weighted[0] );
  172. float4 e = mul( half4x3(E, E, E, E), yuv_weighted[0] );
  173. float4 d = b.yzwx;
  174. float4 f = b.wxyz;
  175. float4 g = c.zwxy;
  176. float4 h = b.zwxy;
  177. float4 i = c.wxyz;
  178.  
  179. float4 i4 = mul( half4x3(I4, C1, A0, G5), yuv_weighted[0] );
  180. float4 i5 = mul( half4x3(I5, C4, A1, G0), yuv_weighted[0] );
  181. float4 h5 = mul( half4x3(H5, F4, B1, D0), yuv_weighted[0] );
  182. float4 f4 = h5.yzwx;
  183.  
  184. float4 c1 = i4.xyzw;
  185. float4 g0 = i5.wxyz;
  186.  
  187. float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
  188. float4 Bo = float4( 1.0, 1.0, -1.0,-1.0 );
  189. float4 Co = float4( 1.5, 0.5, -0.5, 0.5 );
  190. float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
  191. float4 Bx = float4( 0.5, 2.0, -0.5,-2.0 );
  192. float4 Cx = float4( 1.0, 1.0, -0.5, 0.0 );
  193. float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
  194. float4 By = float4( 2.0, 0.5, -2.0,-0.5 );
  195. float4 Cy = float4( 2.0, 0.0, -1.0, 0.5 );
  196.  
  197. // These inequations define the line below which interpolation occurs.
  198. fx = (Ao*fp.y+Bo*fp.x);
  199. fx_left = (Ax*fp.y+Bx*fp.x);
  200. fx_up = (Ay*fp.y+By*fp.x);
  201.  
  202. interp_restriction_lv1 = ((e!=f) && (e!=h) && ( !eq(f,b) && !eq(h,d) || eq(e,i) && !eq(f,i4) && !eq(h,i5) || eq(e,g) || eq(e,c) ) && (f!=f4 && f!=i || h!=h5 && h!=i || h!=g || f!=c || eq(b,c1) && eq(d,g0)));
  203. interp_restriction_lv2_left = ((e!=g) && (d!=g));
  204. interp_restriction_lv2_up = ((e!=c) && (b!=c));
  205.  
  206. float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  207. float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  208. float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  209.  
  210.  
  211. edr = (weighted_distance( e, c, g, i, h5, f4, h, f) < weighted_distance( h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
  212. edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
  213. edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
  214.  
  215. nc45 = ( edr && bool4(fx45));
  216. nc30 = ( edr && edr_left && bool4(fx30));
  217. nc60 = ( edr && edr_up && bool4(fx60));
  218.  
  219. px = (df(e,f) <= df(e,h));
  220.  
  221. nc = (nc30 || nc60 || nc45);
  222.  
  223. float4 final45 = nc45*fx45;
  224. float4 final30 = nc30*fx30;
  225. float4 final60 = nc60*fx60;
  226.  
  227. float4 maximo = max(max(final30, final60), final45);
  228.  
  229. if (nc.x) {pix1 = px.x ? F : H; blend1 = maximo.x;}
  230. else if (nc.y) {pix1 = px.y ? B : F; blend1 = maximo.y;}
  231. else if (nc.z) {pix1 = px.z ? D : B; blend1 = maximo.z;}
  232. else if (nc.w) {pix1 = px.w ? H : D; blend1 = maximo.w;}
  233.  
  234. if (nc.w) {pix2 = px.w ? H : D; blend2 = maximo.w;}
  235. else if (nc.z) {pix2 = px.z ? D : B; blend2 = maximo.z;}
  236. else if (nc.y) {pix2 = px.y ? B : F; blend2 = maximo.y;}
  237. else if (nc.x) {pix2 = px.x ? F : H; blend2 = maximo.x;}
  238.  
  239. E = GAMMA_IN(E);
  240.  
  241. res1 = lerp(E, GAMMA_IN(pix1), blend1);
  242. res2 = lerp(E, GAMMA_IN(pix2), blend2);
  243.  
  244. half3 res = lerp(res1, res2, step(c_df(E, res1), c_df(E, res2)));
  245.  
  246. return float4(clamp( GAMMA_OUT(res), 0.0, 1.0 ), 1.0);
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement