Advertisement
Guest User

5xBR-v3.8c

a guest
Nov 19th, 2012
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.65 KB | None | 0 0
  1. /*
  2.    Hyllian's 5xBR v3.8c (squared) Shader
  3.    
  4.    Copyright (C) 2011/2012 Hyllian/Jararaca - [email protected]
  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.    Incorporates some of the ideas from SABR shader. Thanks to Joshua Street.
  22. */
  23.  
  24. const static float coef           = 2.0;
  25. const static float4 eq_threshold  = float4(15.0);
  26. const static half y_weight        = 48.0;
  27. const static half u_weight        = 7.0;
  28. const static half v_weight        = 6.0;
  29. const static half3x3 yuv          = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  30. const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
  31. const static float4 delta       = float4(0.2);
  32.  
  33. float4 df(float4 A, float4 B)
  34. {
  35.     return float4(abs(A-B));
  36. }
  37.  
  38. bool4 eq(float4 A, float4 B)
  39. {
  40.     return (df(A, B) < float4(15.0));
  41. }
  42.  
  43. float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
  44. {
  45.     return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  46. }
  47.  
  48.  
  49.  
  50. struct input
  51. {
  52.     half2 video_size;
  53.     float2 texture_size;
  54.     half2 output_size;
  55. };
  56.  
  57.  
  58. struct out_vertex {
  59.     half4 position : POSITION;
  60.     half4 color    : COLOR;
  61.     float2 texCoord : TEXCOORD0;
  62.     float4 t1;
  63.     float4 t2;
  64.     float4 t3;
  65.     float4 t4;
  66.     float4 t5;
  67.     float4 t6;
  68.     float4 t7;
  69.     float2 pos;
  70. };
  71.  
  72. /*    VERTEX_SHADER    */
  73. out_vertex main_vertex
  74. (
  75.     half4 position  : POSITION,
  76.     half4 color : COLOR,
  77.     float2 texCoord : TEXCOORD0,
  78.  
  79.     uniform half4x4 modelViewProj,
  80.     uniform input IN
  81. )
  82. {
  83.     out_vertex OUT;
  84.  
  85.     OUT.position = mul(modelViewProj, position);
  86.     OUT.color = color;
  87.  
  88.     float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  89.     float dx = ps.x;
  90.     float dy = ps.y;
  91.  
  92.     //    A1 B1 C1
  93.     // A0  A  B  C C4
  94.     // D0  D  E  F F4
  95.     // G0  G  H  I I4
  96.     //    G5 H5 I5
  97.  
  98.     OUT.texCoord = texCoord;
  99.     OUT.t1 = texCoord.xxxy + half4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  100.     OUT.t2 = texCoord.xxxy + half4( -dx, 0, dx,    -dy); //  A  B  C
  101.     OUT.t3 = texCoord.xxxy + half4( -dx, 0, dx,      0); //  D  E  F
  102.     OUT.t4 = texCoord.xxxy + half4( -dx, 0, dx,     dy); //  G  H  I
  103.     OUT.t5 = texCoord.xxxy + half4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  104.     OUT.t6 = texCoord.xyyy + half4(-2.0*dx,-dy, 0,  dy); // A0 D0 G0
  105.     OUT.t7 = texCoord.xyyy + half4( 2.0*dx,-dy, 0,  dy); // C4 F4 I4
  106.     OUT.pos= texCoord*IN.texture_size;
  107.  
  108.     return OUT;
  109. }
  110.  
  111.  
  112. /*    FRAGMENT SHADER    */
  113. half4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  114. {
  115.     bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
  116.     bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  117.     bool4 nc, nc30, nc60, nc45; // new_color
  118.     float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
  119.  
  120.     float2 fp = frac(VAR.pos);
  121.  
  122.     half3 A1 = tex2D(decal, VAR.t1.xw).rgb;
  123.     half3 B1 = tex2D(decal, VAR.t1.yw).rgb;
  124.     half3 C1 = tex2D(decal, VAR.t1.zw).rgb;
  125.  
  126.     half3 A  = tex2D(decal, VAR.t2.xw).rgb;
  127.     half3 B  = tex2D(decal, VAR.t2.yw).rgb;
  128.     half3 C  = tex2D(decal, VAR.t2.zw).rgb;
  129.  
  130.     half3 D  = tex2D(decal, VAR.t3.xw).rgb;
  131.     half3 E  = tex2D(decal, VAR.t3.yw).rgb;
  132.     half3 F  = tex2D(decal, VAR.t3.zw).rgb;
  133.  
  134.     half3 G  = tex2D(decal, VAR.t4.xw).rgb;
  135.     half3 H  = tex2D(decal, VAR.t4.yw).rgb;
  136.     half3 I  = tex2D(decal, VAR.t4.zw).rgb;
  137.  
  138.     half3 G5 = tex2D(decal, VAR.t5.xw).rgb;
  139.     half3 H5 = tex2D(decal, VAR.t5.yw).rgb;
  140.     half3 I5 = tex2D(decal, VAR.t5.zw).rgb;
  141.  
  142.     half3 A0 = tex2D(decal, VAR.t6.xy).rgb;
  143.     half3 D0 = tex2D(decal, VAR.t6.xz).rgb;
  144.     half3 G0 = tex2D(decal, VAR.t6.xw).rgb;
  145.  
  146.     half3 C4 = tex2D(decal, VAR.t7.xy).rgb;
  147.     half3 F4 = tex2D(decal, VAR.t7.xz).rgb;
  148.     half3 I4 = tex2D(decal, VAR.t7.xw).rgb;
  149.  
  150.     float4 b = mul( half4x3(B, D, H, F), yuv_weighted[0] );
  151.     float4 c = mul( half4x3(C, A, G, I), yuv_weighted[0] );
  152.     float4 e = mul( half4x3(E, E, E, E), yuv_weighted[0] );
  153.     float4 d = b.yzwx;
  154.     float4 f = b.wxyz;
  155.     float4 g = c.zwxy;
  156.     float4 h = b.zwxy;
  157.     float4 i = c.wxyz;
  158.  
  159.     float4 i4 = mul( half4x3(I4, C1, A0, G5), yuv_weighted[0] );
  160.     float4 i5 = mul( half4x3(I5, C4, A1, G0), yuv_weighted[0] );
  161.     float4 h5 = mul( half4x3(H5, F4, B1, D0), yuv_weighted[0] );
  162.     float4 f4 = h5.yzwx;
  163.  
  164.     float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
  165.     float4 Bo = float4( 1.0,  1.0, -1.0,-1.0 );
  166.     float4 Co = float4( 1.5,  0.5, -0.5, 0.5 );
  167.     float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
  168.     float4 Bx = float4( 0.5,  2.0, -0.5,-2.0 );
  169.     float4 Cx = float4( 1.0,  1.0, -0.5, 0.0 );
  170.     float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
  171.     float4 By = float4( 2.0,  0.5, -2.0,-0.5 );
  172.     float4 Cy = float4( 2.0,  0.0, -1.0, 0.5 );
  173.  
  174.     // These inequations define the line below which interpolation occurs.
  175.     fx      = (Ao*fp.y+Bo*fp.x);
  176.     fx_left = (Ax*fp.y+Bx*fp.x);
  177.     fx_up   = (Ay*fp.y+By*fp.x);
  178.  
  179.     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)) );
  180.     interp_restriction_lv2_left = ((e!=g) && (d!=g));
  181.     interp_restriction_lv2_up   = ((e!=c) && (b!=c));
  182.  
  183.     float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  184.     float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  185.     float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  186.  
  187.  
  188.     edr      = (weighted_distance( e, c, g, i, h5, f4, h, f) < weighted_distance( h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
  189.     edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
  190.     edr_up   = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
  191.  
  192.     nc45 = ( edr &&             bool4(fx45));
  193.     nc30 = ( edr && edr_left && bool4(fx30));
  194.     nc60 = ( edr && edr_up   && bool4(fx60));
  195.  
  196.     px = (df(e,f) <= df(e,h));
  197.  
  198.     nc = (nc30 || nc60 || nc45);
  199.  
  200.  
  201.     float final45 = dot(nc45, fx45);
  202.     float final30 = dot(nc30, fx30);
  203.     float final60 = dot(nc60, fx60);
  204.  
  205.     float blend = max(max(final30, final60), final45);
  206.  
  207.     half3 pix = nc.x ? px.x ? F : H : nc.y ? px.y ? B : F : nc.z ? px.z ? D : B : nc.w ? px.w ? H : D : E;
  208.  
  209.     half3 res = lerp(E, pix, blend);
  210.  
  211.     return half4(res, 1.0);
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement