Guest User

phosphor_test

a guest
Feb 26th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 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 enable anti-ringing to horizontal filter.
  28. #define ANTI_RINGING
  29.  
  30. // Uncomment to dilate bright pixels horizontally.
  31. #define DILATION
  32.  
  33. // Control the dilation intensity by changing this param below when DILATION is enabled. Use always values between 0.0 and 1.0.
  34. #define DILATION_STRENGTH 0.5
  35.  
  36. // Uncomment to increase the sharpness of the scanlines.
  37. //#define SHARPER
  38.  
  39. // Control the level of sharpness when SHARPER is enabled.
  40. #define SHARPNESS 2.0
  41.  
  42. // Comment next line if you don't desire the phosphor effect.
  43. #define PHOSPHOR
  44.  
  45. #define PHOSPHOR_STRENGTH 0.8
  46.  
  47. // Uncomment to enable adjustment of red and green saturation.
  48. //#define RED_GREEN_CONTROL
  49.  
  50. // Control red and green saturation when RED_GREEN_CONTROL is enabled.
  51. #define RED_BOOST 1.0
  52. #define GREEN_BOOST 1.0
  53.  
  54. // Control scanlines transparency by changing this param below. Use always values between 0.0 and 1.0.
  55. #define SCANLINES_STRENGTH 1.0
  56.  
  57. // Control the beam width range by changing these two params below. Use always values between 0.0 and 1.0.
  58. #define BEAM_MIN_WIDTH 0.1
  59. #define BEAM_MAX_WIDTH 1.25
  60.  
  61. // You can saturate all colors using this parameter.
  62. #define COLOR_BOOST 1.4
  63.  
  64. // This param change the threshold from where the beam gets thicker.
  65. #define BEAM_WIDTH_SENSITIVITY 0.65
  66.  
  67. // Cool tint. Controls the blue level. CRT TVs from the 90's had a blue tint.
  68. // To turn OFF this effect, use 1.0 value.
  69. #define CRT_TV_BLUE_TINT 1.06
  70.  
  71. // Constants used with gamma correction.
  72. #define InputGamma 2.4
  73. #define OutputGamma 2.4
  74.  
  75. #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
  76. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  77.  
  78.  
  79. // Horizontal cubic filter.
  80.  
  81. // Some known filters use these values:
  82.  
  83. // B = 0.0, C = 0.0 => Hermite cubic filter.
  84. // B = 1.0, C = 0.0 => Cubic B-Spline filter.
  85. // B = 0.0, C = 0.5 => Catmull-Rom Spline filter. This is the default used in this shader.
  86. // B = C = 1.0/3.0 => Mitchell-Netravali cubic filter.
  87. // B = 0.3782, C = 0.3109 => Robidoux filter.
  88. // B = 0.2620, C = 0.3690 => Robidoux Sharp filter.
  89. // B = 0.36, C = 0.28 => My best config for ringing elimination in pixel art (Hyllian).
  90.  
  91.  
  92. // For more info, see: http://www.imagemagick.org/Usage/img_diagrams/cubic_survey.gif
  93.  
  94. // Change these params to configure the horizontal filter.
  95. const static float B = 0.0;
  96. const static float C = 0.5;
  97.  
  98. const static float4x4 invX = float4x4( (-B - 6.0*C)/6.0, (3.0*B + 12.0*C)/6.0, (-3.0*B - 6.0*C)/6.0, B/6.0,
  99. (12.0 - 9.0*B - 6.0*C)/6.0, (-18.0 + 12.0*B + 6.0*C)/6.0, 0.0, (6.0 - 2.0*B)/6.0,
  100. -(12.0 - 9.0*B - 6.0*C)/6.0, (18.0 - 15.0*B - 12.0*C)/6.0, (3.0*B + 6.0*C)/6.0, B/6.0,
  101. (B + 6.0*C)/6.0, -C, 0.0, 0.0);
  102.  
  103.  
  104. struct input
  105. {
  106. float2 video_size;
  107. float2 texture_size;
  108. float2 output_size;
  109. float frame_count;
  110. float frame_direction;
  111. float frame_rotation;
  112. };
  113.  
  114.  
  115. struct out_vertex {
  116. float2 texCoord;
  117. };
  118.  
  119. /* VERTEX_SHADER */
  120. out_vertex main_vertex
  121. (
  122. float4 position : POSITION,
  123. out float4 oPosition : POSITION,
  124. float2 texCoord : TEXCOORD0,
  125.  
  126. uniform float4x4 modelViewProj,
  127. uniform input IN
  128. )
  129. {
  130. oPosition = mul(modelViewProj, position);
  131.  
  132. out_vertex OUT = {
  133. texCoord
  134. };
  135.  
  136. return OUT;
  137. }
  138.  
  139.  
  140. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  141. {
  142. #ifdef SHARPER
  143. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  144. #else
  145. float2 TextureSize = IN.texture_size;
  146. #endif
  147.  
  148. float2 dx = float2(1.0/TextureSize.x, 0.0);
  149. float2 dy = float2(0.0, 1.0/TextureSize.y);
  150. float2 pix_coord = VAR.texCoord*TextureSize-float2(0.5,0.0);
  151.  
  152. float2 tc = (floor(pix_coord)+float2(0.5,0.5))/TextureSize;
  153.  
  154. float2 fp = frac(pix_coord);
  155.  
  156. float3 c10 = tex2D(s_p, tc - dx).xyz;
  157. float3 c11 = tex2D(s_p, tc ).xyz;
  158. float3 c12 = tex2D(s_p, tc + dx).xyz;
  159. float3 c13 = tex2D(s_p, tc + 2.0*dx).xyz;
  160.  
  161. #ifdef ANTI_RINGING
  162. // Get min/max samples
  163. float3 min_sample = min(c10, min(c11, c12));
  164. float3 max_sample = max(c10, max(c11, c12));
  165. #endif
  166.  
  167. float4x3 color_matrix = float4x3(c10, c11, c12, c13);
  168.  
  169. float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  170. float3 color = mul(invX_Px, color_matrix);
  171.  
  172. #ifdef DILATION
  173. float dz = DILATION_STRENGTH*IN.video_size.x/(IN.output_size.x);
  174.  
  175. float2 fpf = float2(fp.x + dz, fp.y);
  176. float2 fpd = float2(fp.x - dz, fp.y);
  177.  
  178. invX_Px = mul(invX, float4(fpf.x*fpf.x*fpf.x, fpf.x*fpf.x, fpf.x, 1.0));
  179. float3 colorf = mul(invX_Px, color_matrix);
  180.  
  181. invX_Px = mul(invX, float4(fpd.x*fpd.x*fpd.x, fpd.x*fpd.x, fpd.x, 1.0));
  182. float3 colord = mul(invX_Px, color_matrix);
  183.  
  184. color = max(color, max(colorf, colord));
  185. #endif
  186.  
  187. #ifdef ANTI_RINGING
  188. // Anti-ringing
  189. color = clamp(color, min_sample, max_sample);
  190. #endif
  191.  
  192. float pos = abs(fp.y - 0.5);
  193.  
  194. float3 lum = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), color);
  195. lum = pow(lum,float3(BEAM_WIDTH_SENSITIVITY));
  196.  
  197. float3 d = clamp(pos/lum, 0.0, 1.0);
  198.  
  199. d = smoothstep(0.0, 1.0, 1.0-d);
  200.  
  201. d = SCANLINES_STRENGTH*(d-1.0)+1.0;
  202.  
  203. #ifdef RED_GREEN_CONTROL
  204. color.rgb *= float3(RED_BOOST, GREEN_BOOST, CRT_TV_BLUE_TINT);
  205. #else
  206. color.b *= CRT_TV_BLUE_TINT;
  207. #endif
  208.  
  209. // Boost medium brightness and add blue tint to dark pixels //
  210. float colsum = (color.r + color.g + color.b)/3;
  211. float colpow = pow(1 - colsum, 2);
  212.  
  213. color.r = pow(color.r, 0.9) + 0.05 * colpow;
  214. color.g = pow(color.g, 0.9) + 0.05 * colpow;
  215. color.b = pow(color.b, 0.9) + 0.18 * colpow;
  216.  
  217. color = clamp(color*d, 0.0, 1.0);
  218.  
  219. #ifdef PHOSPHOR
  220. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  221. float col_factor = (PHOSPHOR_STRENGTH*colsum - PHOSPHOR_STRENGTH + 1)*0.9;
  222. float3 dotMaskWeights = lerp(
  223. float3(1.0, col_factor, 1.0),
  224. float3(col_factor, 1.0, col_factor),
  225. floor(fmod(mod_factor, 2.0))
  226. );
  227. #endif
  228.  
  229. color = GAMMA_IN(color);
  230.  
  231. #ifdef PHOSPHOR
  232. color.rgb *= dotMaskWeights;
  233. #endif
  234.  
  235. color *= COLOR_BOOST;
  236. color = GAMMA_OUT(color);
  237.  
  238. return float4(color, 1.0);
  239. }
Advertisement
Add Comment
Please, Sign In to add comment