Advertisement
Guest User

5xBR-v3.7d

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