Advertisement
Guest User

4xBR-Hybrid-CRT-beta5 for tests

a guest
Feb 23rd, 2013
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.45 KB | None | 0 0
  1. /*
  2.    Hyllian's 4xBR v3.8c+ReverseAA (squared) Shader + CRT - beta5
  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, 15.0, 15.0, 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, 0.4, 0.4, 0.4);
  62. const static float sharpness      = 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, InputGamma, InputGamma))
  71.         #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 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.58
  78.  
  79.         // Used to counteract the desaturation effect of weighting.
  80.         #define COLOR_BOOST 1.45
  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.  
  91. float lum(half3 A, half3 B)
  92. {
  93.     return abs(dot(A-B, yuv_weighted[0]));
  94. }
  95.  
  96. float4 df(float4 A, float4 B)
  97. {
  98.     return float4(abs(A-B));
  99. }
  100.  
  101. bool4 eq(float4 A, float4 B)
  102. {
  103.     return (df(A, B) < float4(15.0, 15.0, 15.0, 15.0));
  104. }
  105.  
  106. bool4 eq2(float4 A, float4 B)
  107. {
  108.     return (df(A, B) < float4(2.0, 2.0, 2.0, 2.0));
  109. }
  110.  
  111.  
  112. float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
  113. {
  114.     return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  115. }
  116.  
  117.  
  118.  
  119. struct input
  120. {
  121.     half2 video_size;
  122.     float2 texture_size;
  123.     half2 output_size;
  124. };
  125.  
  126.  
  127. struct out_vertex {
  128.     half4 position : POSITION;
  129.     half4 color    : COLOR;
  130.     float2 texCoord : TEXCOORD0;
  131.     float4 t1;
  132.     float4 t2;
  133.     float4 t3;
  134.     float4 t4;
  135.     float4 t5;
  136.     float4 t6;
  137.     float4 t7;
  138. };
  139.  
  140. /*    VERTEX_SHADER    */
  141. out_vertex main_vertex
  142. (
  143.     half4 position    : POSITION,
  144.     half4 color    : COLOR,
  145.     float2 texCoord : TEXCOORD0,
  146.  
  147.        uniform half4x4 modelViewProj,
  148.     uniform input IN
  149. )
  150. {
  151.     out_vertex OUT;
  152.  
  153.     OUT.position = mul(modelViewProj, position);
  154.     OUT.color = color;
  155.  
  156.     float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  157.     float dx = ps.x;
  158.     float dy = ps.y;
  159.  
  160.     //    A1 B1 C1
  161.     // A0  A  B  C C4
  162.     // D0  D  E  F F4
  163.     // G0  G  H  I I4
  164.     //    G5 H5 I5
  165.  
  166.     OUT.texCoord = texCoord;
  167.     OUT.t1 = texCoord.xxxy + half4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  168.     OUT.t2 = texCoord.xxxy + half4( -dx, 0, dx,    -dy); //  A  B  C
  169.     OUT.t3 = texCoord.xxxy + half4( -dx, 0, dx,      0); //  D  E  F
  170.     OUT.t4 = texCoord.xxxy + half4( -dx, 0, dx,     dy); //  G  H  I
  171.     OUT.t5 = texCoord.xxxy + half4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  172.     OUT.t6 = texCoord.xyyy + half4(-2.0*dx,-dy, 0,  dy); // A0 D0 G0
  173.     OUT.t7 = texCoord.xyyy + half4( 2.0*dx,-dy, 0,  dy); // C4 F4 I4
  174.  
  175.     return OUT;
  176. }
  177.  
  178.  
  179. /*    FRAGMENT SHADER    */
  180. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  181. {
  182.     bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
  183.     bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  184.     bool4 nc, nc30, nc60, nc45; // new_color
  185.     float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
  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.     float blend = 0.0;
  306.     half3 pix;
  307.  
  308.     float4 r1 = lerp(e, f, edr);
  309.  
  310.     if ( all(eq2(r1,e)) )
  311.     {
  312.         pix = res = s0;
  313.     }
  314.     else
  315.     {
  316.         pix = res = E;
  317.     }
  318.  
  319.  
  320.     float4 final45 = dot(nc45, fx45);
  321.     float4 final30 = dot(nc30, fx30);
  322.     float4 final60 = dot(nc60, fx60);
  323.  
  324.     float4 maximo = max(max(final30, final60), final45);
  325.  
  326.          if (nc.x) {pix = px.x ? F : H; blend = maximo.x;}
  327.     else if (nc.y) {pix = px.y ? B : F; blend = maximo.y;}
  328.     else if (nc.z) {pix = px.z ? D : B; blend = maximo.z;}
  329.     else if (nc.w) {pix = px.w ? H : D; blend = maximo.w;}
  330.  
  331.     res = lerp(res, pix, blend);
  332.     // CRT-caligari - only vertical blend
  333.  
  334.             float3 color = GAMMA_IN(res);
  335.  
  336.             float ddy = fp.y - 0.5;
  337.             float v_weight_00 = ddy / SPOT_HEIGHT;
  338.             WEIGHT(v_weight_00);
  339.             color *= float3( v_weight_00, v_weight_00, v_weight_00 );
  340.  
  341.     // get closest vertical neighbour to blend
  342.     float3 coords10;
  343.             if (ddy>0.0) {
  344.              coords10 = H;
  345.                 ddy = 1.0 - ddy;
  346.             } else {
  347.                 coords10 = B;
  348.                 ddy = 1.0 + ddy;
  349.             }
  350.     float3 colorNB = GAMMA_IN(coords10);
  351.  
  352.             float v_weight_10 = ddy / SPOT_HEIGHT;
  353.             WEIGHT( v_weight_10 );
  354.  
  355.             color += colorNB * float3( v_weight_10, v_weight_10, v_weight_10 );
  356.  
  357.             color *= float3( COLOR_BOOST, COLOR_BOOST, COLOR_BOOST );
  358.  
  359.      return float4(clamp( GAMMA_OUT(color), 0.0, 1.0 ), 1.0);
  360.  
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement