Guest User

crt-hyllian for low end systems

a guest
Apr 23rd, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 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 - [email protected]
  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. // Uncomment to increase the sharpness of the scanlines.
  28. //#define SHARPER
  29.  
  30. // Control the level of sharpness when SHARPER is enabled.
  31. #define SHARPNESS 2.0
  32.  
  33. // Constants used with gamma correction.
  34. #define InputGamma 2.4
  35. #define OutputGamma 2.2
  36.  
  37. #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
  38. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  39.  
  40.  
  41. const static float4x4 invX = float4x4(-1.0/6.0, 0.5, -1.0/3.0, 0.0,
  42. 0.5, -1.0, -0.5, 1.0,
  43. -0.5, 0.5, 1.0, 0.0,
  44. 1.0/6.0, 0.0, -1.0/6.0, 0.0);
  45.  
  46.  
  47.  
  48. struct input
  49. {
  50. float2 video_size;
  51. float2 texture_size;
  52. float2 output_size;
  53. float frame_count;
  54. float frame_direction;
  55. float frame_rotation;
  56. };
  57.  
  58.  
  59. struct out_vertex {
  60. float2 texCoord;
  61. };
  62.  
  63. /* VERTEX_SHADER */
  64. out_vertex main_vertex
  65. (
  66. float4 position : POSITION,
  67. out float4 oPosition : POSITION,
  68. float2 texCoord : TEXCOORD0,
  69.  
  70. uniform float4x4 modelViewProj,
  71. uniform input IN
  72. )
  73. {
  74. #ifdef SHARPER
  75. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  76. #else
  77. float2 TextureSize = IN.texture_size;
  78. #endif
  79. float2 ps = 1.0/TextureSize;
  80.  
  81. oPosition = mul(modelViewProj, position);
  82.  
  83. out_vertex OUT = {
  84. texCoord + ps*float2(-0.49, 0.0)
  85. };
  86.  
  87. return OUT;
  88. }
  89.  
  90.  
  91.  
  92. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  93. {
  94. #ifdef SHARPER
  95. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  96. #else
  97. float2 TextureSize = IN.texture_size;
  98. #endif
  99.  
  100. float2 dx = float2(1.0/TextureSize.x, 0.0);
  101. float2 dy = float2(0.0, 1.0/TextureSize.y);
  102.  
  103. float2 tc = (floor(VAR.texCoord*TextureSize)+float2(0.5,0.5))/TextureSize;
  104.  
  105. float2 fp = frac(VAR.texCoord*TextureSize);
  106.  
  107. float3 c10 = tex2D(s_p, tc - dx).xyz;
  108. float3 c11 = tex2D(s_p, tc ).xyz;
  109. float3 c12 = tex2D(s_p, tc + dx).xyz;
  110. float3 c13 = tex2D(s_p, tc + 2.0*dx).xyz;
  111.  
  112. float4x3 color_matrix = float4x3(c10, c11, c12, c13);
  113.  
  114. float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  115. float3 color = mul(invX_Px, color_matrix);
  116.  
  117. color = GAMMA_IN(color);
  118.  
  119. float d = smoothstep(0.0, 1.0, 1.0 - abs(fp.y - 0.5));
  120.  
  121. color *=d;
  122.  
  123. return float4(GAMMA_OUT(color), 1.0);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment