Guest User

crt-hyllian-blueblack-pow (for testing)

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