Advertisement
Guest User

crt-hyllian-vbw-v5-fix

a guest
Apr 8th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 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. const static float pi = 3.1415926535897932384626433832795;
  28.  
  29. const static float3x3 yuv = float3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  30.  
  31. #define SCANLINES 0.4
  32.  
  33.  
  34. #define SINC2(X)\
  35. (X<(1E-5)) ? 1.0 : sin(pi*X)*sin(pi*X)/(pi*X*pi*X);
  36.  
  37.  
  38. // Constants used with gamma correction.
  39. #define InputGamma 2.4
  40. #define OutputGamma 2.2
  41.  
  42. #define GAMMA_IN(color) pow(color, float4(InputGamma, InputGamma, InputGamma, InputGamma))
  43. #define GAMMA_OUT(color) pow(color, float4(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  44.  
  45.  
  46. const static float4x4 invX = float4x4(-1.0/6.0, 0.5, -1.0/3.0, 0.0,
  47. 0.5, -1.0, -0.5, 1.0,
  48. -0.5, 0.5, 1.0, 0.0,
  49. 1.0/6.0, 0.0, -1.0/6.0, 0.0);
  50.  
  51.  
  52.  
  53. struct input
  54. {
  55. float2 video_size;
  56. float2 texture_size;
  57. float2 output_size;
  58. float frame_count;
  59. float frame_direction;
  60. float frame_rotation;
  61. };
  62.  
  63.  
  64. struct out_vertex {
  65. float2 texCoord;
  66. float4 t1;
  67. };
  68.  
  69. /* VERTEX_SHADER */
  70. out_vertex main_vertex
  71. (
  72. float4 position : POSITION,
  73. out float4 oPosition : POSITION,
  74. float2 tex : TEXCOORD0,
  75.  
  76. uniform float4x4 modelViewProj,
  77. uniform input IN
  78. )
  79. {
  80. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  81. float dx = ps.x;
  82. float dy = ps.y;
  83.  
  84. oPosition = mul(modelViewProj, position);
  85.  
  86. out_vertex OUT = {
  87. tex,
  88. float4(dx, 0.0, 0.0, dy)
  89. };
  90.  
  91. /*
  92. out_vertex OUT = {
  93. tex,
  94. float4(tex,tex) + float4(-2.0*dx, 0.0, -dx, 0.0),
  95. float4(tex,tex) + float4( dx, 0.0,-2.0*dx, dy),
  96. float4(tex,tex) + float4( -dx, dy, 0.0, dy),
  97. float4(tex,tex) + float4( dx, dy,-2.0*dx, 2.0*dy),
  98. float4(tex,tex) + float4( -dx, 2.0*dy, 0.0, 2.0*dy),
  99. float4(tex,tex) + float4( dx, 2.0*dy,-2.0*dx, 3.0*dy),
  100. float4(tex,tex) + float4( -dx, 3.0*dy, 0.0, 3.0*dy),
  101. tex + float2( dx, 3.0*dy)
  102. };
  103. */
  104.  
  105. return OUT;
  106. }
  107.  
  108.  
  109. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  110. {
  111.  
  112. float2 fp = frac(VAR.texCoord*IN.texture_size);
  113.  
  114. float2 dx = VAR.t1.xy;
  115. float2 dy = VAR.t1.zw;
  116.  
  117. float3 c00 = tex2D(s_p, VAR.texCoord -dx -dy).xyz;
  118. float3 c01 = tex2D(s_p, VAR.texCoord -dy).xyz;
  119. float3 c02 = tex2D(s_p, VAR.texCoord +dx -dy).xyz;
  120. float3 c03 = tex2D(s_p, VAR.texCoord +2.0*dx -dy).xyz;
  121. float3 c10 = tex2D(s_p, VAR.texCoord -dx ).xyz;
  122. float3 c11 = tex2D(s_p, VAR.texCoord ).xyz;
  123. float3 c12 = tex2D(s_p, VAR.texCoord +dx ).xyz;
  124. float3 c13 = tex2D(s_p, VAR.texCoord +2.0*dx ).xyz;
  125. float3 c20 = tex2D(s_p, VAR.texCoord -dx +dy).xyz;
  126. float3 c21 = tex2D(s_p, VAR.texCoord +dy).xyz;
  127. float3 c22 = tex2D(s_p, VAR.texCoord +dx +dy).xyz;
  128. float3 c23 = tex2D(s_p, VAR.texCoord +2.0*dx +dy).xyz;
  129. float3 c30 = tex2D(s_p, VAR.texCoord -dx +2.0*dy).xyz;
  130. float3 c31 = tex2D(s_p, VAR.texCoord +2.0*dy).xyz;
  131. float3 c32 = tex2D(s_p, VAR.texCoord +dx +2.0*dy).xyz;
  132. float3 c33 = tex2D(s_p, VAR.texCoord +2.0*dx +2.0*dy).xyz;
  133.  
  134.  
  135. float4x4 red_matrix = float4x4(c00.x, c01.x, c02.x, c03.x,
  136. c10.x, c11.x, c12.x, c13.x,
  137. c20.x, c21.x, c22.x, c23.x,
  138. c30.x, c31.x, c32.x, c33.x);
  139.  
  140. float4x4 green_matrix = float4x4(c00.y, c01.y, c02.y, c03.y,
  141. c10.y, c11.y, c12.y, c13.y,
  142. c20.y, c21.y, c22.y, c23.y,
  143. c30.y, c31.y, c32.y, c33.y);
  144.  
  145. float4x4 blue_matrix = float4x4(c00.z, c01.z, c02.z, c03.z,
  146. c10.z, c11.z, c12.z, c13.z,
  147. c20.z, c21.z, c22.z, c23.z,
  148. c30.z, c31.z, c32.z, c33.z);
  149.  
  150.  
  151. float4x1 invX_Px = mul(invX, float4x1(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  152.  
  153.  
  154. float red = mul( red_matrix, invX_Px);
  155. float green = mul(green_matrix, invX_Px);
  156. float blue = mul( blue_matrix, invX_Px);
  157.  
  158. float2 pos = abs(fp - float2(0.5));
  159.  
  160. //float3 Y = mul( float3x3(c01, c02, c00), yuv[0] );
  161. //Y = lerp(float3(0.1), float3(1.0), Y);
  162. float2 Y;
  163. Y.x = max(c01.r, max(c01.g, c01.b));
  164. Y.y = max(c02.r, max(c02.g, c02.b));
  165. Y = lerp(float2(0.1), float2(0.5), Y);
  166. //Y.x = (c01.r + c01.g + c01.b)/3;
  167. //Y.y = (c02.r + c02.g + c02.b)/3;
  168. Y=pow(Y,0.5);
  169. //float lum = (Y.x*(1.5-pos.x) + Y.y*(0.5+pos.x) + Y.z*(0.5-pos.x))/(2.5-pos.x);
  170. float lum = lerp(Y.x,Y.y, 1.0-Y.x);
  171.  
  172. float d = clamp(pos.y/lum, 0.0, 1.0);
  173.  
  174. d = smoothstep(0.0, 1.0, 1.0-d);
  175.  
  176. float3 color = float3(red, green, blue);
  177. //float3 color = c11.rgb;
  178.  
  179.  
  180. float color_boost = 2.8;
  181.  
  182. color = clamp(color*d, 0.0, 1.0);
  183.  
  184. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  185.  
  186. float3 dotMaskWeights = lerp(
  187. float3(1.0, 0.7, 1.0),
  188. float3(0.7, 1.0, 0.7),
  189. floor(fmod(mod_factor, 2.0))
  190. );
  191.  
  192.  
  193.  
  194.  
  195. color = GAMMA_IN(color);
  196. color.rgb *= dotMaskWeights;
  197. color = color*color_boost;
  198. color = GAMMA_OUT(color);
  199.  
  200. return float4(color, 1.0);
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement