Advertisement
Guest User

2xBR-Hybrid-v3.cg

a guest
Feb 8th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. /*
  2. Hyllian's 2xBR v3.8c+ReverseAA (squared) Shader - v3
  3.  
  4. Copyright (C) 2011/2012 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.  
  23. /*
  24. * ReverseAA part of the code
  25. *
  26. * Copyright (c) 2012, Christoph Feck <christoph@maxiom.de>
  27. * All Rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright notice,
  33. * this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in the
  37. * documentation and/or other materials provided with the distribution.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  43. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  44. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  45. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  46. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49. * POSSIBILITY OF SUCH DAMAGE.
  50. *
  51. */
  52.  
  53.  
  54. const static float coef = 2.0;
  55. const static float4 eq_threshold = float4(15.0);
  56. const static half y_weight = 48.0;
  57. const static half u_weight = 7.0;
  58. const static half v_weight = 6.0;
  59. const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  60. const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
  61. const static float4 delta = float4(0.5);
  62. const static float sharpness = 0.65;
  63.  
  64. float4 df(float4 A, float4 B)
  65. {
  66. return float4(abs(A-B));
  67. }
  68.  
  69. half c_df(half3 c1, half3 c2) {
  70. half3 df = abs(c1 - c2);
  71. return df.r + df.g + df.b;
  72. }
  73.  
  74. bool4 eq(float4 A, float4 B)
  75. {
  76. return (df(A, B) < float4(15.0));
  77. }
  78.  
  79. bool4 eq2(float4 A, float4 B)
  80. {
  81. return (df(A, B) < float4(2.0));
  82. }
  83.  
  84.  
  85. float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
  86. {
  87. return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  88. }
  89.  
  90.  
  91.  
  92. struct input
  93. {
  94. half2 video_size;
  95. float2 texture_size;
  96. half2 output_size;
  97. };
  98.  
  99.  
  100. struct out_vertex {
  101. half4 position : POSITION;
  102. half4 color : COLOR;
  103. float2 texCoord : TEXCOORD0;
  104. float4 t1;
  105. float4 t2;
  106. float4 t3;
  107. float4 t4;
  108. float4 t5;
  109. float4 t6;
  110. float4 t7;
  111. };
  112.  
  113. /* VERTEX_SHADER */
  114. out_vertex main_vertex
  115. (
  116. half4 position : POSITION,
  117. half4 color : COLOR,
  118. float2 texCoord : TEXCOORD0,
  119.  
  120. uniform half4x4 modelViewProj,
  121. uniform input IN
  122. )
  123. {
  124. out_vertex OUT;
  125.  
  126. OUT.position = mul(modelViewProj, position);
  127. OUT.color = color;
  128.  
  129. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  130. float dx = ps.x;
  131. float dy = ps.y;
  132.  
  133. // A1 B1 C1
  134. // A0 A B C C4
  135. // D0 D E F F4
  136. // G0 G H I I4
  137. // G5 H5 I5
  138.  
  139. OUT.texCoord = texCoord;
  140. OUT.t1 = texCoord.xxxy + half4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  141. OUT.t2 = texCoord.xxxy + half4( -dx, 0, dx, -dy); // A B C
  142. OUT.t3 = texCoord.xxxy + half4( -dx, 0, dx, 0); // D E F
  143. OUT.t4 = texCoord.xxxy + half4( -dx, 0, dx, dy); // G H I
  144. OUT.t5 = texCoord.xxxy + half4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  145. OUT.t6 = texCoord.xyyy + half4(-2.0*dx,-dy, 0, dy); // A0 D0 G0
  146. OUT.t7 = texCoord.xyyy + half4( 2.0*dx,-dy, 0, dy); // C4 F4 I4
  147.  
  148. return OUT;
  149. }
  150.  
  151.  
  152. /* FRAGMENT SHADER */
  153. half4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  154. {
  155. bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
  156. bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  157. bool4 nc, nc30, nc60, nc45; // new_color
  158. float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
  159. half3 res1, res2, pix1, pix2;
  160. float blend1, blend2;
  161.  
  162. float2 fp = frac(VAR.texCoord*IN.texture_size);
  163.  
  164. half3 A1 = tex2D(decal, VAR.t1.xw).rgb;
  165. half3 B1 = tex2D(decal, VAR.t1.yw).rgb;
  166. half3 C1 = tex2D(decal, VAR.t1.zw).rgb;
  167.  
  168. half3 A = tex2D(decal, VAR.t2.xw).rgb;
  169. half3 B = tex2D(decal, VAR.t2.yw).rgb;
  170. half3 C = tex2D(decal, VAR.t2.zw).rgb;
  171.  
  172. half3 D = tex2D(decal, VAR.t3.xw).rgb;
  173. half3 E = tex2D(decal, VAR.t3.yw).rgb;
  174. half3 F = tex2D(decal, VAR.t3.zw).rgb;
  175.  
  176. half3 G = tex2D(decal, VAR.t4.xw).rgb;
  177. half3 H = tex2D(decal, VAR.t4.yw).rgb;
  178. half3 I = tex2D(decal, VAR.t4.zw).rgb;
  179.  
  180. half3 G5 = tex2D(decal, VAR.t5.xw).rgb;
  181. half3 H5 = tex2D(decal, VAR.t5.yw).rgb;
  182. half3 I5 = tex2D(decal, VAR.t5.zw).rgb;
  183.  
  184. half3 A0 = tex2D(decal, VAR.t6.xy).rgb;
  185. half3 D0 = tex2D(decal, VAR.t6.xz).rgb;
  186. half3 G0 = tex2D(decal, VAR.t6.xw).rgb;
  187.  
  188. half3 C4 = tex2D(decal, VAR.t7.xy).rgb;
  189. half3 F4 = tex2D(decal, VAR.t7.xz).rgb;
  190. half3 I4 = tex2D(decal, VAR.t7.xw).rgb;
  191.  
  192. float4 b = mul( half4x3(B, D, H, F), yuv_weighted[0] );
  193. float4 c = mul( half4x3(C, A, G, I), yuv_weighted[0] );
  194. float4 e = mul( half4x3(E, E, E, E), yuv_weighted[0] );
  195. float4 a = c.yzwx;
  196. float4 d = b.yzwx;
  197. float4 f = b.wxyz;
  198. float4 g = c.zwxy;
  199. float4 h = b.zwxy;
  200. float4 i = c.wxyz;
  201.  
  202. float4 i4 = mul( half4x3(I4, C1, A0, G5), yuv_weighted[0] );
  203. float4 i5 = mul( half4x3(I5, C4, A1, G0), yuv_weighted[0] );
  204. float4 h5 = mul( half4x3(H5, F4, B1, D0), yuv_weighted[0] );
  205. float4 f4 = h5.yzwx;
  206.  
  207.  
  208. float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
  209. float4 Bo = float4( 1.0, 1.0, -1.0,-1.0 );
  210. float4 Co = float4( 1.5, 0.5, -0.5, 0.5 );
  211. float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
  212. float4 Bx = float4( 0.5, 2.0, -0.5,-2.0 );
  213. float4 Cx = float4( 1.0, 1.0, -0.5, 0.0 );
  214. float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
  215. float4 By = float4( 2.0, 0.5, -2.0,-0.5 );
  216. float4 Cy = float4( 2.0, 0.0, -1.0, 0.5 );
  217.  
  218. // These inequations define the line below which interpolation occurs.
  219. fx = (Ao*fp.y+Bo*fp.x);
  220. fx_left = (Ax*fp.y+Bx*fp.x);
  221. fx_up = (Ay*fp.y+By*fp.x);
  222.  
  223. interp_restriction_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)) );
  224. interp_restriction_lv2_left = ((e!=g) && (d!=g));
  225. interp_restriction_lv2_up = ((e!=c) && (b!=c));
  226.  
  227. float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  228. float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  229. float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  230.  
  231.  
  232. edr = ((weighted_distance( e, c, g, i, h5, f4, h, f) + 3.5) < weighted_distance( h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
  233. edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
  234. edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
  235.  
  236. nc45 = ( edr && bool4(fx45));
  237. nc30 = ( edr && edr_left && bool4(fx30));
  238. nc60 = ( edr && edr_up && bool4(fx60));
  239.  
  240. px = (df(e,f) <= df(e,h));
  241.  
  242. half3 res = E;
  243.  
  244.  
  245. float3 n1, n2, n3, n4, s, aa, bb, cc, dd;
  246.  
  247.  
  248. n1 = B1; n2 = B; s = E; n3 = H; n4 = H5;
  249. aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  250.  
  251. float3 t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  252.  
  253. float3 m = (s < 0.5) ? 2*s : 2*(1.0-s);
  254.  
  255. m = min(m, sharpness*abs(bb));
  256. m = min(m, sharpness*abs(cc));
  257.  
  258. t = clamp(t, -m, m);
  259.  
  260.  
  261. float3 s1 = (2*fp.y-1)*t + s;
  262.  
  263. n1 = D0; n2 = D; s = s1; n3 = F; n4 = F4;
  264. aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  265.  
  266. t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  267.  
  268. m = (s < 0.5) ? 2*s : 2*(1.0-s);
  269.  
  270. m = min(m, sharpness*abs(bb));
  271. m = min(m, sharpness*abs(cc));
  272.  
  273. t = clamp(t, -m, m);
  274.  
  275. float3 s0 = (2*fp.x-1)*t + s;
  276.  
  277.  
  278. nc = (nc30 || nc60 || nc45);
  279.  
  280. blend1 = blend2 = 0.0;
  281.  
  282. float4 final45 = dot(nc45, fx45);
  283. float4 final30 = dot(nc30, fx30);
  284. float4 final60 = dot(nc60, fx60);
  285.  
  286. float4 maximo = max(max(final30, final60), final45);
  287.  
  288. if (nc.x) {pix1 = px.x ? F : H; blend1 = maximo.x;}
  289. else if (nc.y) {pix1 = px.y ? B : F; blend1 = maximo.y;}
  290. else if (nc.z) {pix1 = px.z ? D : B; blend1 = maximo.z;}
  291. else if (nc.w) {pix1 = px.w ? H : D; blend1 = maximo.w;}
  292.  
  293. if (nc.w) {pix2 = px.w ? H : D; blend2 = maximo.w;}
  294. else if (nc.z) {pix2 = px.z ? D : B; blend2 = maximo.z;}
  295. else if (nc.y) {pix2 = px.y ? B : F; blend2 = maximo.y;}
  296. else if (nc.x) {pix2 = px.x ? F : H; blend2 = maximo.x;}
  297.  
  298. res1 = lerp(s0, pix1, blend1);
  299. res2 = lerp(s0, pix2, blend2);
  300.  
  301. res = lerp(res1, res2, step(c_df(E, res1), c_df(E, res2)));
  302.  
  303. return half4(res, 1.0);
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement