Advertisement
Guest User

ddt + xbr edge detection + gamma correction

a guest
Feb 26th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. /*
  2.    Hyllian's xBR+ddt+gamma Shader
  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. const static float coef           = 2.0;
  23. const static float3 dtt           = float3(65536,255,1);
  24. const static half y_weight        = 48.0;
  25. const static half u_weight        = 7.0;
  26. const static half v_weight        = 6.0;
  27. const static half3x3 yuv          = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  28. const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
  29.  
  30.         // Constants used with gamma correction.
  31.         #define InputGamma 2.4
  32.         #define OutputGamma 2.2
  33.  
  34.         #define GAMMA_IN(color)     pow(color, float3(InputGamma))
  35.         #define GAMMA_OUT(color)    pow(color, float3(1.0 / OutputGamma))
  36.  
  37.     #define TEX2D(coords)   GAMMA_IN( tex2D(decal, coords).rgb )
  38.  
  39.  
  40. float reduce(half3 color)
  41. {
  42.     return dot(color, dtt);
  43. }
  44.  
  45.  
  46. half3 bilinear(float p, float q, half3 A, half3 B, half3 C, half3 D)
  47. {
  48.     return ((1-p)*(1-q)*A + p*(1-q)*B + (1-p)*q*C + p*q*D);
  49. }
  50.  
  51. float RGBtoYUV(half3 color)
  52. {
  53.     return dot(color, yuv_weighted[0]);
  54. }
  55.  
  56. float df(float A, float B)
  57. {
  58.     return abs(A-B);
  59. }
  60.  
  61. bool eq(float A, float B)
  62. {
  63.     return (df(A, B) < 15.0);
  64. }
  65.  
  66.  
  67.  
  68. float weighted_distance(float a, float b, float c, float d, float e, float f, float g, float h)
  69. {
  70.     return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  71. }
  72.  
  73.  
  74.  
  75. struct input
  76. {
  77.     half2 video_size;
  78.     float2 texture_size;
  79.     half2 output_size;
  80. };
  81.  
  82.  
  83. struct out_vertex {
  84.     half4 position : POSITION;
  85.     half4 color    : COLOR;
  86.     float2 texCoord : TEXCOORD0;
  87.     half4 t1;
  88. };
  89.  
  90. /*    VERTEX_SHADER    */
  91. out_vertex main_vertex
  92. (
  93.     half4 position  : POSITION,
  94.     half4 color : COLOR,
  95.     float2 texCoord : TEXCOORD0,
  96.  
  97.     uniform half4x4 modelViewProj,
  98.     uniform input IN
  99. )
  100. {
  101.     out_vertex OUT;
  102.  
  103.     OUT.position = mul(modelViewProj, position);
  104.     OUT.color = color;
  105.  
  106.     half2 ps = half2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  107.     half dx = ps.x;
  108.     half dy = ps.y;
  109.  
  110.     OUT.texCoord = texCoord;
  111.     OUT.t1.xy = half2(  0,-dy); // B
  112.     OUT.t1.zw = half2(-dx,  0); // D
  113.  
  114.     return OUT;
  115. }
  116.  
  117. /*
  118.     xBR LVL1 works over the pixels below:
  119.  
  120.         |B |C |
  121.      |D |E |F |F4|
  122.      |G |H |I |I4|
  123.         |H5|I5|
  124.  
  125.     Consider E as the central pixel. xBR LVL1 needs only to look at 12 texture pixels.
  126. */
  127.  
  128. /*    FRAGMENT SHADER    */
  129. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  130. {
  131.     bool edr, px; // px = pixel, edr = edge detection rule
  132.  
  133.     float2 fx  = frac(VAR.texCoord*IN.texture_size);
  134.     float2 pos = fx-float2(0.5); // pos = pixel position
  135.     float2 dir = sign(pos); // dir = pixel direction
  136.  
  137.     float2 g1 = dir*VAR.t1.xy;
  138.     float2 g2 = dir*VAR.t1.zw;
  139.  
  140.     half3 B = TEX2D(VAR.texCoord +g1   );
  141.     half3 C = TEX2D(VAR.texCoord +g1-g2);
  142.     half3 D = TEX2D(VAR.texCoord    +g2);
  143.     half3 E = TEX2D(VAR.texCoord       );
  144.     half3 F = TEX2D(VAR.texCoord    -g2);
  145.     half3 G = TEX2D(VAR.texCoord -g1+g2);
  146.     half3 H = TEX2D(VAR.texCoord -g1   );
  147.     half3 I = TEX2D(VAR.texCoord -g1-g2);
  148.  
  149.     half3 F4 = TEX2D(VAR.texCoord    -2.0*g2   );
  150.     half3 I4 = TEX2D(VAR.texCoord -g1-2.0*g2   );
  151.     half3 H5 = TEX2D(VAR.texCoord -2.0*g1      );
  152.     half3 I5 = TEX2D(VAR.texCoord -2.0*g1-g2   );
  153.  
  154.  
  155.     float b = RGBtoYUV( B );
  156.     float c = RGBtoYUV( C );
  157.     float d = RGBtoYUV( D );
  158.     float e = RGBtoYUV( E );
  159.     float f = RGBtoYUV( F );
  160.     float g = RGBtoYUV( G );
  161.     float h = RGBtoYUV( H );
  162.     float i = RGBtoYUV( I );
  163.  
  164.     float i4 = RGBtoYUV( I4 );
  165.     float i5 = RGBtoYUV( I5 );
  166.     float h5 = RGBtoYUV( H5 );
  167.     float f4 = RGBtoYUV( F4 );
  168.  
  169.     edr = (weighted_distance( e, c, g, i, h5, f4, h, f) < weighted_distance( h, d, i5, f, i4, b, e, i));
  170.  
  171.     float p = abs(pos.x);
  172.     float q = abs(pos.y);
  173.  
  174.     float k = distance(pos,g1);
  175.     float l = distance(pos,g2);
  176.  
  177.     if ((abs(e-i) <= abs(f-h)) && !edr)
  178.     {
  179.         if (k < l)
  180.         {
  181.             H = E + I - F;
  182.         }
  183.         else if (k > l)
  184.         {
  185.             F = E + I - H;
  186.         }
  187.     }
  188.     else if ((abs(e-i) >= abs(f-h)) && edr)
  189.     {
  190.         I = F + H - E;
  191.     }
  192.  
  193.     float3 color = bilinear(p, q, E, F, H, I);
  194.  
  195.     return float4(clamp( GAMMA_OUT(color), 0.0, 1.0 ), 1.0);
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement