Advertisement
Guest User

4xBR-Hybrid+CRT2.cg

a guest
Feb 8th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. /*
  2. Hyllian's 4xBR v3.8c+ReverseAA + CRT-caligari (squared) Shader - beta2
  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.4);
  62. const static float sharpness = 0.65;
  63.  
  64.  
  65. // Constants used with gamma correction.
  66. #define InputGamma 2.4
  67. #define OutputGamma 2.2
  68.  
  69. #define GAMMA_IN(color) pow(color, float3(InputGamma))
  70. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma))
  71.  
  72. #define TEX2D(coords) GAMMA_IN( tex2D(decal, coords).xyz )
  73.  
  74. // 0.5 = the spot stays inside the original pixel
  75. // 1.0 = the spot bleeds up to the center of next pixel
  76. #define SPOT_HEIGHT 0.58
  77.  
  78. // Used to counteract the desaturation effect of weighting.
  79. #define COLOR_BOOST 1.45
  80.  
  81. // Macro for weights computing
  82. #define WEIGHT(w) \
  83. if(w>1.0) w=1.0; \
  84. w = 1.0 - w * w; \
  85. w = w * w;\
  86.  
  87.  
  88.  
  89. float4 df(float4 A, float4 B)
  90. {
  91. return float4(abs(A-B));
  92. }
  93.  
  94. half c_df(half3 c1, half3 c2) {
  95. half3 df = abs(c1 - c2);
  96. return df.r + df.g + df.b;
  97. }
  98.  
  99. bool4 eq(float4 A, float4 B)
  100. {
  101. return (df(A, B) < float4(15.0));
  102. }
  103.  
  104. bool4 eq2(float4 A, float4 B)
  105. {
  106. return (df(A, B) < float4(2.0));
  107. }
  108.  
  109.  
  110. float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
  111. {
  112. return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  113. }
  114.  
  115.  
  116.  
  117. struct input
  118. {
  119. half2 video_size;
  120. float2 texture_size;
  121. half2 output_size;
  122. };
  123.  
  124.  
  125. struct out_vertex {
  126. half4 position : POSITION;
  127. half4 color : COLOR;
  128. float2 texCoord : TEXCOORD0;
  129. float4 t1;
  130. float4 t2;
  131. float4 t3;
  132. float4 t4;
  133. float4 t5;
  134. float4 t6;
  135. float4 t7;
  136. };
  137.  
  138. /* VERTEX_SHADER */
  139. out_vertex main_vertex
  140. (
  141. half4 position : POSITION,
  142. half4 color : COLOR,
  143. float2 texCoord : TEXCOORD0,
  144.  
  145. uniform half4x4 modelViewProj,
  146. uniform input IN
  147. )
  148. {
  149. out_vertex OUT;
  150.  
  151. OUT.position = mul(modelViewProj, position);
  152. OUT.color = color;
  153.  
  154. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  155. float dx = ps.x;
  156. float dy = ps.y;
  157.  
  158. // A1 B1 C1
  159. // A0 A B C C4
  160. // D0 D E F F4
  161. // G0 G H I I4
  162. // G5 H5 I5
  163.  
  164. OUT.texCoord = texCoord;
  165. OUT.t1 = texCoord.xxxy + half4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  166. OUT.t2 = texCoord.xxxy + half4( -dx, 0, dx, -dy); // A B C
  167. OUT.t3 = texCoord.xxxy + half4( -dx, 0, dx, 0); // D E F
  168. OUT.t4 = texCoord.xxxy + half4( -dx, 0, dx, dy); // G H I
  169. OUT.t5 = texCoord.xxxy + half4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  170. OUT.t6 = texCoord.xyyy + half4(-2.0*dx,-dy, 0, dy); // A0 D0 G0
  171. OUT.t7 = texCoord.xyyy + half4( 2.0*dx,-dy, 0, dy); // C4 F4 I4
  172.  
  173. return OUT;
  174. }
  175.  
  176.  
  177. /* FRAGMENT SHADER */
  178. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  179. {
  180. bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
  181. bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  182. bool4 nc, nc30, nc60, nc45; // new_color
  183. float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
  184. half3 res1, res2, pix1, pix2;
  185. float blend1, blend2;
  186.  
  187. float2 fp = frac(VAR.texCoord*IN.texture_size);
  188.  
  189. half3 A1 = tex2D(decal, VAR.t1.xw).rgb;
  190. half3 B1 = tex2D(decal, VAR.t1.yw).rgb;
  191. half3 C1 = tex2D(decal, VAR.t1.zw).rgb;
  192.  
  193. half3 A = tex2D(decal, VAR.t2.xw).rgb;
  194. half3 B = tex2D(decal, VAR.t2.yw).rgb;
  195. half3 C = tex2D(decal, VAR.t2.zw).rgb;
  196.  
  197. half3 D = tex2D(decal, VAR.t3.xw).rgb;
  198. half3 E = tex2D(decal, VAR.t3.yw).rgb;
  199. half3 F = tex2D(decal, VAR.t3.zw).rgb;
  200.  
  201. half3 G = tex2D(decal, VAR.t4.xw).rgb;
  202. half3 H = tex2D(decal, VAR.t4.yw).rgb;
  203. half3 I = tex2D(decal, VAR.t4.zw).rgb;
  204.  
  205. half3 G5 = tex2D(decal, VAR.t5.xw).rgb;
  206. half3 H5 = tex2D(decal, VAR.t5.yw).rgb;
  207. half3 I5 = tex2D(decal, VAR.t5.zw).rgb;
  208.  
  209. half3 A0 = tex2D(decal, VAR.t6.xy).rgb;
  210. half3 D0 = tex2D(decal, VAR.t6.xz).rgb;
  211. half3 G0 = tex2D(decal, VAR.t6.xw).rgb;
  212.  
  213. half3 C4 = tex2D(decal, VAR.t7.xy).rgb;
  214. half3 F4 = tex2D(decal, VAR.t7.xz).rgb;
  215. half3 I4 = tex2D(decal, VAR.t7.xw).rgb;
  216.  
  217. float4 b = mul( half4x3(B, D, H, F), yuv_weighted[0] );
  218. float4 c = mul( half4x3(C, A, G, I), yuv_weighted[0] );
  219. float4 e = mul( half4x3(E, E, E, E), yuv_weighted[0] );
  220. float4 a = c.yzwx;
  221. float4 d = b.yzwx;
  222. float4 f = b.wxyz;
  223. float4 g = c.zwxy;
  224. float4 h = b.zwxy;
  225. float4 i = c.wxyz;
  226.  
  227. float4 i4 = mul( half4x3(I4, C1, A0, G5), yuv_weighted[0] );
  228. float4 i5 = mul( half4x3(I5, C4, A1, G0), yuv_weighted[0] );
  229. float4 h5 = mul( half4x3(H5, F4, B1, D0), yuv_weighted[0] );
  230. float4 f4 = h5.yzwx;
  231.  
  232.  
  233. float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
  234. float4 Bo = float4( 1.0, 1.0, -1.0,-1.0 );
  235. float4 Co = float4( 1.5, 0.5, -0.5, 0.5 );
  236. float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
  237. float4 Bx = float4( 0.5, 2.0, -0.5,-2.0 );
  238. float4 Cx = float4( 1.0, 1.0, -0.5, 0.0 );
  239. float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
  240. float4 By = float4( 2.0, 0.5, -2.0,-0.5 );
  241. float4 Cy = float4( 2.0, 0.0, -1.0, 0.5 );
  242.  
  243. // These inequations define the line below which interpolation occurs.
  244. fx = (Ao*fp.y+Bo*fp.x);
  245. fx_left = (Ax*fp.y+Bx*fp.x);
  246. fx_up = (Ay*fp.y+By*fp.x);
  247.  
  248. interp_restriction_lv1 = ((e!=f) && (e!=h) && ((eq2(e,b) || eq2(e,d) || !eq2(e,a)) && (eq2(f,f4) || eq2(f,c) || eq2(h,h5) || eq2(h,g))) && ( !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)) );
  249. interp_restriction_lv2_left = ((e!=g) && (d!=g));
  250. interp_restriction_lv2_up = ((e!=c) && (b!=c));
  251.  
  252. float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  253. float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  254. float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  255.  
  256.  
  257. 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;
  258. edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
  259. edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
  260.  
  261. nc45 = ( edr && bool4(fx45));
  262. nc30 = ( edr && edr_left && bool4(fx30));
  263. nc60 = ( edr && edr_up && bool4(fx60));
  264.  
  265. px = (df(e,f) <= df(e,h));
  266.  
  267. half3 res = E;
  268.  
  269.  
  270. float3 n1, n2, n3, n4, s, aa, bb, cc, dd;
  271.  
  272.  
  273. n1 = B1; n2 = B; s = E; n3 = H; n4 = H5;
  274. aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  275.  
  276. float3 t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  277.  
  278. float3 m = (s < 0.5) ? 2*s : 2*(1.0-s);
  279.  
  280. m = min(m, sharpness*abs(bb));
  281. m = min(m, sharpness*abs(cc));
  282.  
  283. t = clamp(t, -m, m);
  284.  
  285.  
  286. float3 s1 = (2*fp.y-1)*t + s;
  287.  
  288. n1 = D0; n2 = D; s = s1; n3 = F; n4 = F4;
  289. aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  290.  
  291. t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  292.  
  293. m = (s < 0.5) ? 2*s : 2*(1.0-s);
  294.  
  295. m = min(m, sharpness*abs(bb));
  296. m = min(m, sharpness*abs(cc));
  297.  
  298. t = clamp(t, -m, m);
  299.  
  300. float3 s0 = (2*fp.x-1)*t + s;
  301.  
  302.  
  303. nc = (nc30 || nc60 || nc45);
  304.  
  305. blend1 = blend2 = 0.0;
  306.  
  307. float4 final45 = dot(nc45, fx45);
  308. float4 final30 = dot(nc30, fx30);
  309. float4 final60 = dot(nc60, fx60);
  310.  
  311. float4 maximo = max(max(final30, final60), final45);
  312.  
  313. if (nc.x) {pix1 = px.x ? F : H; blend1 = maximo.x;}
  314. else if (nc.y) {pix1 = px.y ? B : F; blend1 = maximo.y;}
  315. else if (nc.z) {pix1 = px.z ? D : B; blend1 = maximo.z;}
  316. else if (nc.w) {pix1 = px.w ? H : D; blend1 = maximo.w;}
  317.  
  318. if (nc.w) {pix2 = px.w ? H : D; blend2 = maximo.w;}
  319. else if (nc.z) {pix2 = px.z ? D : B; blend2 = maximo.z;}
  320. else if (nc.y) {pix2 = px.y ? B : F; blend2 = maximo.y;}
  321. else if (nc.x) {pix2 = px.x ? F : H; blend2 = maximo.x;}
  322.  
  323. res1 = lerp(s0, pix1, blend1);
  324. res2 = lerp(s0, pix2, blend2);
  325.  
  326. res = lerp(res1, res2, step(c_df(E, res1), c_df(E, res2)));
  327.  
  328. // CRT-caligari - only vertical blend
  329.  
  330. float3 color = GAMMA_IN(res);
  331.  
  332. float ddy = fp.y - 0.5;
  333. float v_weight_00 = ddy / SPOT_HEIGHT;
  334. WEIGHT(v_weight_00);
  335. color *= float3( v_weight_00 );
  336.  
  337. // get closest vertical neighbour to blend
  338. float3 coords10;
  339. if (ddy>0.0) {
  340. coords10 = H;
  341. ddy = 1.0 - ddy;
  342. } else {
  343. coords10 = B;
  344. ddy = 1.0 + ddy;
  345. }
  346. float3 colorNB = GAMMA_IN(coords10);
  347.  
  348. float v_weight_10 = ddy / SPOT_HEIGHT;
  349. WEIGHT( v_weight_10 );
  350.  
  351. color += colorNB * float3( v_weight_10 );
  352.  
  353. color *= float3( COLOR_BOOST );
  354.  
  355. return float4(clamp( GAMMA_OUT(color), 0.0, 1.0 ), 1.0);
  356.  
  357.  
  358. return float4(res, 1.0);
  359.  
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement