Advertisement
Guest User

crt-hyllian-v2

a guest
Mar 28th, 2014
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.80 KB | None | 0 0
  1. /* COMPATIBILITY
  2.    - HLSL compilers
  3.    - Cg   compilers
  4. */
  5.  
  6. /*
  7.    Hyllian's CRT Shader
  8.    
  9.    Copyright (C) 2011-2014 Hyllian/Jararaca - sergiogdb@gmail.com
  10.  
  11.    This program is free software; you can redistribute it and/or
  12.    modify it under the terms of the GNU General Public License
  13.    as published by the Free Software Foundation; either version 2
  14.    of the License, or (at your option) any later version.
  15.  
  16.    This program is distributed in the hope that it will be useful,
  17.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.    GNU General Public License for more details.
  20.  
  21.    You should have received a copy of the GNU General Public License
  22.    along with this program; if not, write to the Free Software
  23.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27.         // Constants used with gamma correction.
  28.         #define InputGamma 2.4
  29.         #define OutputGamma 2.2
  30.  
  31.         #define GAMMA_IN(color)     pow(color, float4(InputGamma, InputGamma, InputGamma, InputGamma))
  32.         #define GAMMA_OUT(color)    pow(color, float4(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  33.  
  34.  
  35. const static float4x4 invX = float4x4(-1.0/6.0,  0.5, -1.0/3.0, 0.0,
  36.                                            0.5, -1.0,     -0.5, 1.0,
  37.                                           -0.5,  0.5,      1.0, 0.0,
  38.                                        1.0/6.0,  0.0, -1.0/6.0, 0.0);
  39.  
  40.  
  41.  
  42. struct input
  43. {
  44.     float2 video_size;
  45.     float2 texture_size;
  46.     float2 output_size;
  47.     float  frame_count;
  48.     float  frame_direction;
  49.     float frame_rotation;
  50. };
  51.  
  52.  
  53. struct out_vertex {
  54.     float2 texCoord;
  55.     float4 t1;
  56.     float4 t2;
  57.     float4 t3;
  58.     float4 t4;
  59.     float4 t5;
  60.     float4 t6;
  61.     float4 t7;
  62.     float2 t8;
  63. };
  64.  
  65. /*    VERTEX_SHADER    */
  66. out_vertex main_vertex
  67. (
  68.     float4 position      : POSITION,
  69.     out float4 oPosition : POSITION,
  70.     float2 tex           : TEXCOORD0,
  71.  
  72.     uniform float4x4 modelViewProj,
  73.     uniform input IN
  74. )
  75. {
  76.     float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  77.     float dx = ps.x;
  78.     float dy = ps.y;
  79.  
  80.     oPosition = mul(modelViewProj, position);
  81.  
  82.     out_vertex OUT = {
  83.         tex,
  84.         float4(tex,tex) + float4(   -dx,    -dy,    0.0,    -dy),
  85.         float4(tex,tex) + float4(    dx,    -dy, 2.0*dx,    -dy),
  86.         float4(tex,tex) + float4(   -dx,    0.0,     dx,    0.0),
  87.         float4(tex,tex) + float4(2.0*dx,    0.0,    -dx,     dy),
  88.         float4(tex,tex) + float4(   0.0,     dy,     dx,     dy),
  89.         float4(tex,tex) + float4(2.0*dx,     dy,    -dx, 2.0*dy),
  90.         float4(tex,tex) + float4(   0.0, 2.0*dy,     dx, 2.0*dy),
  91.         tex             + float2(2.0*dx, 2.0*dy)
  92.     };
  93.  
  94.  
  95.     return OUT;
  96. }
  97.  
  98.  
  99. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  100. {
  101.  
  102.   float2 fp = frac(VAR.texCoord*IN.texture_size);
  103.  
  104.   float3 c00 = tex2D(s_p, VAR.t1.xy).xyz;
  105.   float3 c01 = tex2D(s_p, VAR.t1.zw).xyz;
  106.   float3 c02 = tex2D(s_p, VAR.t2.xy).xyz;
  107.   float3 c03 = tex2D(s_p, VAR.t2.zw).xyz;
  108.   float3 c10 = tex2D(s_p, VAR.t3.xy).xyz;
  109.   float3 c11 = tex2D(s_p, VAR.texCoord).xyz;
  110.   float3 c12 = tex2D(s_p, VAR.t3.zw).xyz;
  111.   float3 c13 = tex2D(s_p, VAR.t4.xy).xyz;
  112.   float3 c20 = tex2D(s_p, VAR.t4.zw).xyz;
  113.   float3 c21 = tex2D(s_p, VAR.t5.xy).xyz;
  114.   float3 c22 = tex2D(s_p, VAR.t5.zw).xyz;
  115.   float3 c23 = tex2D(s_p, VAR.t6.xy).xyz;
  116.   float3 c30 = tex2D(s_p, VAR.t6.zw).xyz;
  117.   float3 c31 = tex2D(s_p, VAR.t7.xy).xyz;
  118.   float3 c32 = tex2D(s_p, VAR.t7.zw).xyz;
  119.   float3 c33 = tex2D(s_p, VAR.t8.xy).xyz;
  120.  
  121.  
  122.   float4x4   red_matrix = float4x4(c00.x, c01.x, c02.x, c03.x,
  123.                                    c10.x, c11.x, c12.x, c13.x,
  124.                                    c20.x, c21.x, c22.x, c23.x,
  125.                                    c30.x, c31.x, c32.x, c33.x);
  126.  
  127.   float4x4 green_matrix = float4x4(c00.y, c01.y, c02.y, c03.y,
  128.                                    c10.y, c11.y, c12.y, c13.y,
  129.                                    c20.y, c21.y, c22.y, c23.y,
  130.                                    c30.y, c31.y, c32.y, c33.y);
  131.  
  132.   float4x4  blue_matrix = float4x4(c00.z, c01.z, c02.z, c03.z,
  133.                                    c10.z, c11.z, c12.z, c13.z,
  134.                                    c20.z, c21.z, c22.z, c23.z,
  135.                                    c30.z, c31.z, c32.z, c33.z);
  136.  
  137.  
  138.   float4x1 invX_Px = mul(invX, float4x1(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  139.  
  140.   float red   = mul(  red_matrix, invX_Px);
  141.   float green = mul(green_matrix, invX_Px);
  142.   float blue  = mul( blue_matrix, invX_Px);
  143.  
  144.   float d = smoothstep(0.0, 1.0, 1.0 - abs(fp.y - 0.5));
  145.  
  146.   float3 color = GAMMA_IN(float3(red, green, blue)*d);
  147.  
  148.   return float4(GAMMA_OUT(color), 1.0);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement