Advertisement
Guest User

ddt gamma corrected

a guest
Feb 26th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. /*
  2.    Hyllian's Data Dependent Triangulation Shader + gamma correction
  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 float3 dtt = float3(65536,255,1);
  23.  
  24. float reduce(half3 color)
  25. {
  26.     return dot(color, dtt);
  27. }
  28.  
  29.  
  30. float3 bilinear(float p, float q, half3 A, half3 B, half3 C, half3 D)
  31. {
  32.     return ((1-p)*(1-q)*A + p*(1-q)*B + (1-p)*q*C + p*q*D);
  33. }
  34.  
  35.  
  36. struct input
  37. {
  38.     half2 video_size;
  39.     float2 texture_size;
  40.     half2 output_size;
  41. };
  42.  
  43.  
  44. struct out_vertex {
  45.     half4 position : POSITION;
  46.     half4 color    : COLOR;
  47.     float2 texCoord : TEXCOORD0;
  48.     half4 t1;
  49.     half2 loc;
  50. };
  51.  
  52.  
  53.         // Constants used with gamma correction.
  54.         #define InputGamma 2.4
  55.         #define OutputGamma 2.2
  56.  
  57.         #define GAMMA_IN(color)     pow(color, float3(InputGamma))
  58.         #define GAMMA_OUT(color)    pow(color, float3(1.0 / OutputGamma))
  59.  
  60.     #define TEX2D(coords)   GAMMA_IN( tex2D(decal, coords).rgb )
  61.  
  62.  
  63.  
  64.  
  65. /*    VERTEX_SHADER    */
  66. out_vertex main_vertex
  67. (
  68.     half4 position  : POSITION,
  69.     half4 color : COLOR,
  70.     float2 texCoord : TEXCOORD0,
  71.  
  72.     uniform half4x4 modelViewProj,
  73.     uniform input IN
  74. )
  75. {
  76.     out_vertex OUT;
  77.  
  78.     OUT.position = mul(modelViewProj, position);
  79.     OUT.color = color;
  80.  
  81.     half2 ps = half2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  82.     half dx = ps.x;
  83.     half dy = ps.y;
  84.  
  85.     OUT.texCoord = texCoord;
  86.     OUT.t1.xy = half2( dx,  0); // F
  87.     OUT.t1.zw = half2(  0, dy); // H
  88.     OUT.loc = texCoord*IN.texture_size;
  89.  
  90.     return OUT;
  91. }
  92.  
  93.  
  94.  
  95. /*    FRAGMENT SHADER    */
  96. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  97. {
  98.     float2 pos = frac(VAR.loc)-float2(0.5); // pos = pixel position
  99.     float2 dir = sign(pos); // dir = pixel direction
  100.  
  101.     float2 g1 = dir*VAR.t1.xy;
  102.     float2 g2 = dir*VAR.t1.zw;
  103.  
  104.     half3 A = TEX2D(VAR.texCoord       ).xyz;
  105.     half3 B = TEX2D(VAR.texCoord +g1   ).xyz;
  106.     half3 C = TEX2D(VAR.texCoord    +g2).xyz;
  107.     half3 D = TEX2D(VAR.texCoord +g1+g2).xyz;
  108.  
  109.     float a = reduce(A);
  110.     float b = reduce(B);
  111.     float c = reduce(C);
  112.     float d = reduce(D);
  113.  
  114.     float p = abs(pos.x);
  115.     float q = abs(pos.y);
  116.  
  117.     float k = distance(pos,g1);
  118.     float l = distance(pos,g2);
  119.  
  120.     if (abs(a-d) < abs(b-c))
  121.     {
  122.         if (k < l)
  123.         {
  124.             C = A + D - B;
  125.         }
  126.         else if (k > l)
  127.         {
  128.             B = A + D - C;
  129.         }
  130.     }
  131.     else if (abs(a-d) > abs(b-c))
  132.     {
  133.         D = B + C - A;
  134.     }
  135.  
  136.     float3 color = bilinear(p, q, A, B, C, D);
  137.  
  138.     return float4(clamp( GAMMA_OUT(color), 0.0, 1.0 ), 1.0);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement