Advertisement
Guest User

4xBR-Hybrid+CRT.cg

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