Advertisement
Guest User

crt-hyllian - variable beam width test - v6b

a guest
Apr 12th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.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. // You can change these two params below. Use always values between 0.0 and 1.0.
  32. #define SCANLINES_MIN_WIDTH 0.1
  33. #define SCANLINES_MAX_WIDTH 0.7
  34.  
  35. // You can saturate colors using this parameter.
  36. #define COLOR_BOOST 2.2
  37.  
  38. // This param change the threshold from where the beam gets thicker.
  39. #define BEAM_WIDTH_SENSITIVITY 0.5
  40.  
  41.  
  42.  
  43.  
  44. #define SINC2(X)\
  45. (X<(1E-5)) ? 1.0 : sin(pi*X)*sin(pi*X)/(pi*X*pi*X);
  46.  
  47.  
  48. // Constants used with gamma correction.
  49. #define InputGamma 2.4
  50. #define OutputGamma 2.2
  51.  
  52. #define GAMMA_IN(color) pow(color, float4(InputGamma, InputGamma, InputGamma, InputGamma))
  53. #define GAMMA_OUT(color) pow(color, float4(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  54.  
  55.  
  56. const static float4x4 invX = float4x4(-1.0/6.0, 0.5, -1.0/3.0, 0.0,
  57. 0.5, -1.0, -0.5, 1.0,
  58. -0.5, 0.5, 1.0, 0.0,
  59. 1.0/6.0, 0.0, -1.0/6.0, 0.0);
  60.  
  61.  
  62.  
  63. struct input
  64. {
  65. float2 video_size;
  66. float2 texture_size;
  67. float2 output_size;
  68. float frame_count;
  69. float frame_direction;
  70. float frame_rotation;
  71. };
  72.  
  73.  
  74. struct out_vertex {
  75. float2 texCoord;
  76. float4 t1;
  77. float4 t2;
  78. float4 t3;
  79. float4 t4;
  80. float4 t5;
  81. float4 t6;
  82. float4 t7;
  83. float2 t8;
  84. };
  85.  
  86. /* VERTEX_SHADER */
  87. out_vertex main_vertex
  88. (
  89. float4 position : POSITION,
  90. out float4 oPosition : POSITION,
  91. float2 tex1 : TEXCOORD0,
  92.  
  93. uniform float4x4 modelViewProj,
  94. uniform input IN
  95. )
  96. {
  97. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  98. float dx = ps.x;
  99. float dy = ps.y;
  100.  
  101. float2 tex = tex1+float2(0.0000001, 0.0000001);
  102. oPosition = mul(modelViewProj, position);
  103. /*
  104. out_vertex OUT = {
  105. tex,
  106. float4(tex,tex) + float4( -dx, -dy, 0.0, -dy),
  107. float4(tex,tex) + float4( dx, -dy, 2.0*dx, -dy),
  108. float4(tex,tex) + float4( -dx, 0.0, dx, 0.0),
  109. float4(tex,tex) + float4(2.0*dx, 0.0, -dx, dy),
  110. float4(tex,tex) + float4( 0.0, dy, dx, dy),
  111. float4(tex,tex) + float4(2.0*dx, dy, -dx, 2.0*dy),
  112. float4(tex,tex) + float4( 0.0, 2.0*dy, dx, 2.0*dy),
  113. tex + float2(2.0*dx, 2.0*dy)
  114. };
  115. */
  116. out_vertex OUT = {
  117. tex,
  118. float4(tex,tex) + float4(-2.0*dx, 0.0, -dx, 0.0),
  119. float4(tex,tex) + float4( dx, 0.0,-2.0*dx, dy),
  120. float4(tex,tex) + float4( -dx, dy, 0.0, dy),
  121. float4(tex,tex) + float4( dx, dy,-2.0*dx, 2.0*dy),
  122. float4(tex,tex) + float4( -dx, 2.0*dy, 0.0, 2.0*dy),
  123. float4(tex,tex) + float4( dx, 2.0*dy,-2.0*dx, 3.0*dy),
  124. float4(tex,tex) + float4( -dx, 3.0*dy, 0.0, 3.0*dy),
  125. tex + float2( dx, 3.0*dy)
  126. };
  127.  
  128. return OUT;
  129. }
  130.  
  131.  
  132. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  133. {
  134.  
  135. float2 fp = frac(VAR.texCoord*IN.texture_size);
  136.  
  137. // float2 fps = 0.0;
  138.  
  139. float3 c00 = tex2D(s_p, VAR.t1.xy).xyz;
  140. float3 c01 = tex2D(s_p, VAR.t1.zw).xyz;
  141. float3 c02 = tex2D(s_p, VAR.texCoord).xyz;
  142. float3 c03 = tex2D(s_p, VAR.t2.xy).xyz;
  143. float3 c10 = tex2D(s_p, VAR.t2.zw).xyz;
  144. float3 c11 = tex2D(s_p, VAR.t3.xy).xyz;
  145. float3 c12 = tex2D(s_p, VAR.t3.zw).xyz;
  146. float3 c13 = tex2D(s_p, VAR.t4.xy).xyz;
  147. float3 c20 = tex2D(s_p, VAR.t4.zw).xyz;
  148. float3 c21 = tex2D(s_p, VAR.t5.xy).xyz;
  149. float3 c22 = tex2D(s_p, VAR.t5.zw).xyz;
  150. float3 c23 = tex2D(s_p, VAR.t6.xy).xyz;
  151. float3 c30 = tex2D(s_p, VAR.t6.zw).xyz;
  152. float3 c31 = tex2D(s_p, VAR.t7.xy).xyz;
  153. float3 c32 = tex2D(s_p, VAR.t7.zw).xyz;
  154. float3 c33 = tex2D(s_p, VAR.t8.xy).xyz;
  155.  
  156.  
  157. float4x4 red_matrix = float4x4(c00.x, c01.x, c02.x, c03.x,
  158. c10.x, c11.x, c12.x, c13.x,
  159. c20.x, c21.x, c22.x, c23.x,
  160. c30.x, c31.x, c32.x, c33.x);
  161.  
  162. float4x4 green_matrix = float4x4(c00.y, c01.y, c02.y, c03.y,
  163. c10.y, c11.y, c12.y, c13.y,
  164. c20.y, c21.y, c22.y, c23.y,
  165. c30.y, c31.y, c32.y, c33.y);
  166.  
  167. float4x4 blue_matrix = float4x4(c00.z, c01.z, c02.z, c03.z,
  168. c10.z, c11.z, c12.z, c13.z,
  169. c20.z, c21.z, c22.z, c23.z,
  170. c30.z, c31.z, c32.z, c33.z);
  171.  
  172.  
  173. float4x1 invX_Px = mul(invX, float4x1(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  174.  
  175.  
  176. float red = mul( red_matrix, invX_Px);
  177. float green = mul(green_matrix, invX_Px);
  178. float blue = mul( blue_matrix, invX_Px);
  179.  
  180. float2 pos = abs(fp - float2(0.5));
  181.  
  182. //float3 Y = mul( float3x3(c01, c02, c00), yuv[0] );
  183. //Y = lerp(float3(SCANLINES_MIN_WIDTH), float3(SCANLINES_MAX_WIDTH), Y);
  184. //float2 Y;
  185. //Y.x = max(c01.r, max(c01.g, c01.b));
  186. //Y.y = max(c02.r, max(c02.g, c02.b));
  187. //Y = lerp(float2(SCANLINES_MIN_WIDTH), float2(SCANLINES_MAX_WIDTH), Y);
  188. //Y.x = (c01.r + c01.g + c01.b)/3;
  189. //Y.y = (c02.r + c02.g + c02.b)/3;
  190. //Y=pow(Y,BEAM_WIDTH_SENSITIVITY);
  191. //float lum = (Y.x*(1.5-pos.x) + Y.y*(0.5+pos.x) + Y.z*(0.5-pos.x))/(2.5-pos.x);
  192. //float lum = lerp(Y.x,Y.y, 1.0-Y.x);
  193.  
  194. //float d = clamp(pos.y/lum, 0.0, 1.0);
  195. c01 = lerp(float3(SCANLINES_MIN_WIDTH), float3(SCANLINES_MAX_WIDTH), c01);
  196. c02 = lerp(float3(SCANLINES_MIN_WIDTH), float3(SCANLINES_MAX_WIDTH), c02);
  197. c01=pow(c01,BEAM_WIDTH_SENSITIVITY);
  198. c02=pow(c02,BEAM_WIDTH_SENSITIVITY);
  199. float3 lum = lerp(c01,c02, 1.0-c01);
  200. float3 d = clamp(pos.y/lum, 0.0, 1.0);
  201.  
  202. d = smoothstep(0.0, 1.0, 1.0-d);
  203.  
  204. float3 color = float3(red, green, blue);
  205. //float3 color = c11.rgb;
  206.  
  207.  
  208.  
  209. color = clamp(color*d, 0.0, 1.0);
  210.  
  211. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  212.  
  213. float3 dotMaskWeights = lerp(
  214. float3(1.0, 0.7, 1.0),
  215. float3(0.7, 1.0, 0.7),
  216. floor(fmod(mod_factor, 2.0))
  217. );
  218.  
  219.  
  220.  
  221.  
  222. color = GAMMA_IN(color);
  223. color.rgb *= dotMaskWeights;
  224. color = color*COLOR_BOOST;
  225. color = GAMMA_OUT(color);
  226.  
  227. return float4(color, 1.0);
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement