Advertisement
Guest User

xBR-Hybrid-v5 for tests

a guest
Feb 23rd, 2013
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.72 KB | None | 0 0
  1. /*
  2.    Hyllian's 2xBR v3.8c+ReverseAA (squared) Shader - 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.5, 0.5, 0.5, 0.5);
  62. const static float sharpness      = 0.65;
  63.  
  64. float lum(half3 A, half3 B)
  65. {
  66.     return abs(dot(A-B, yuv_weighted[0]));
  67. }
  68.  
  69. float4 df(float4 A, float4 B)
  70. {
  71.     return float4(abs(A-B));
  72. }
  73.  
  74. bool4 eq(float4 A, float4 B)
  75. {
  76.     return (df(A, B) < float4(15.0, 15.0, 15.0, 15.0));
  77. }
  78.  
  79. bool4 eq2(float4 A, float4 B)
  80. {
  81.     return (df(A, B) < float4(2.0, 2.0, 2.0, 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.  
  160.     float2 fp = frac(VAR.texCoord*IN.texture_size);
  161.  
  162.     half3 A1 = tex2D(decal, VAR.t1.xw).rgb;
  163.     half3 B1 = tex2D(decal, VAR.t1.yw).rgb;
  164.     half3 C1 = tex2D(decal, VAR.t1.zw).rgb;
  165.  
  166.     half3 A  = tex2D(decal, VAR.t2.xw).rgb;
  167.     half3 B  = tex2D(decal, VAR.t2.yw).rgb;
  168.     half3 C  = tex2D(decal, VAR.t2.zw).rgb;
  169.  
  170.     half3 D  = tex2D(decal, VAR.t3.xw).rgb;
  171.     half3 E  = tex2D(decal, VAR.t3.yw).rgb;
  172.     half3 F  = tex2D(decal, VAR.t3.zw).rgb;
  173.  
  174.     half3 G  = tex2D(decal, VAR.t4.xw).rgb;
  175.     half3 H  = tex2D(decal, VAR.t4.yw).rgb;
  176.     half3 I  = tex2D(decal, VAR.t4.zw).rgb;
  177.  
  178.     half3 G5 = tex2D(decal, VAR.t5.xw).rgb;
  179.     half3 H5 = tex2D(decal, VAR.t5.yw).rgb;
  180.     half3 I5 = tex2D(decal, VAR.t5.zw).rgb;
  181.  
  182.     half3 A0 = tex2D(decal, VAR.t6.xy).rgb;
  183.     half3 D0 = tex2D(decal, VAR.t6.xz).rgb;
  184.     half3 G0 = tex2D(decal, VAR.t6.xw).rgb;
  185.  
  186.     half3 C4 = tex2D(decal, VAR.t7.xy).rgb;
  187.     half3 F4 = tex2D(decal, VAR.t7.xz).rgb;
  188.     half3 I4 = tex2D(decal, VAR.t7.xw).rgb;
  189.  
  190.     float4 b = mul( half4x3(B, D, H, F), yuv_weighted[0] );
  191.     float4 c = mul( half4x3(C, A, G, I), yuv_weighted[0] );
  192.     float4 e = mul( half4x3(E, E, E, E), yuv_weighted[0] );
  193.     float4 a = c.yzwx;
  194.     float4 d = b.yzwx;
  195.     float4 f = b.wxyz;
  196.     float4 g = c.zwxy;
  197.     float4 h = b.zwxy;
  198.     float4 i = c.wxyz;
  199.  
  200.     float4 i4 = mul( half4x3(I4, C1, A0, G5), yuv_weighted[0] );
  201.     float4 i5 = mul( half4x3(I5, C4, A1, G0), yuv_weighted[0] );
  202.     float4 h5 = mul( half4x3(H5, F4, B1, D0), yuv_weighted[0] );
  203.     float4 f4 = h5.yzwx;
  204.  
  205.  
  206.     float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
  207.     float4 Bo = float4( 1.0,  1.0, -1.0,-1.0 );
  208.     float4 Co = float4( 1.5,  0.5, -0.5, 0.5 );
  209.     float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
  210.     float4 Bx = float4( 0.5,  2.0, -0.5,-2.0 );
  211.     float4 Cx = float4( 1.0,  1.0, -0.5, 0.0 );
  212.     float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
  213.     float4 By = float4( 2.0,  0.5, -2.0,-0.5 );
  214.     float4 Cy = float4( 2.0,  0.0, -1.0, 0.5 );
  215.  
  216.     // These inequations define the line below which interpolation occurs.
  217.     fx      = (Ao*fp.y+Bo*fp.x);
  218.     fx_left = (Ax*fp.y+Bx*fp.x);
  219.     fx_up   = (Ay*fp.y+By*fp.x);
  220.  
  221.     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)) );
  222.     interp_restriction_lv2_left = ((e!=g) && (d!=g));
  223.     interp_restriction_lv2_up   = ((e!=c) && (b!=c));
  224.  
  225.     float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  226.     float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  227.     float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  228.  
  229.  
  230.     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;
  231.     edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
  232.     edr_up   = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
  233.  
  234.     nc45 = ( edr &&             bool4(fx45));
  235.     nc30 = ( edr && edr_left && bool4(fx30));
  236.     nc60 = ( edr && edr_up   && bool4(fx60));
  237.  
  238.     px = (df(e,f) <= df(e,h));
  239.  
  240.     half3 res = E;
  241.  
  242.  
  243.     float3 n1, n2, n3, n4, s, aa, bb, cc, dd;
  244.  
  245.  
  246.     n1 = B1; n2 = B; s = E; n3 = H; n4 = H5;
  247.     aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  248.  
  249.     float3 t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  250.  
  251.     float3 m = (s < 0.5) ? 2*s : 2*(1.0-s);
  252.  
  253.         m = min(m, sharpness*abs(bb));
  254.         m = min(m, sharpness*abs(cc));
  255.  
  256.     t = clamp(t, -m, m);
  257.  
  258.  
  259.     float3 s1 = (2*fp.y-1)*t + s;
  260.  
  261.     n1 = D0; n2 = D; s = s1; n3 = F; n4 = F4;
  262.     aa = n2-n1; bb = s-n2; cc = n3-s; dd = n4-n3;
  263.  
  264.     t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
  265.  
  266.     m = (s < 0.5) ? 2*s : 2*(1.0-s);
  267.  
  268.         m = min(m, sharpness*abs(bb));
  269.         m = min(m, sharpness*abs(cc));
  270.  
  271.     t = clamp(t, -m, m);
  272.  
  273.     float3 s0 = (2*fp.x-1)*t + s;
  274.  
  275.  
  276.     nc = (nc30 || nc60 || nc45);
  277.  
  278.     float blend = 0.0;
  279.     half3 pix;
  280.  
  281.     float4 r1 = lerp(e, f, edr);
  282.  
  283.     if ( all(eq2(r1,e)) )
  284.     {
  285.         pix = res = s0;
  286.     }
  287.     else
  288.     {
  289.         pix = res = E;
  290.     }
  291.  
  292.  
  293.     float4 final45 = dot(nc45, fx45);
  294.     float4 final30 = dot(nc30, fx30);
  295.     float4 final60 = dot(nc60, fx60);
  296.  
  297.     float4 maximo = max(max(final30, final60), final45);
  298.  
  299.          if (nc.x) {pix = px.x ? F : H; blend = maximo.x;}
  300.     else if (nc.y) {pix = px.y ? B : F; blend = maximo.y;}
  301.     else if (nc.z) {pix = px.z ? D : B; blend = maximo.z;}
  302.     else if (nc.w) {pix = px.w ? H : D; blend = maximo.w;}
  303.  
  304.     res = lerp(res, pix, blend);
  305.  
  306.     return half4(res, 1.0);
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement