crt-caligari.fs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #version 150
  2. /*
  3. Phosphor shader - Copyright (C) 2011 caligari.
  4. Ported by Hyllian.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. // 0.5 = the spot stays inside the original pixel
  18. // 1.0 = the spot bleeds up to the center of next pixel
  19. #define SPOT_WIDTH 0.9
  20. #define SPOT_HEIGHT 0.65
  21. // Used to counteract the desaturation effect of weighting.
  22. #define COLOR_BOOST 1.45
  23. // Constants used with gamma correction.
  24. #define InputGamma 2.4
  25. #define OutputGamma 2.2
  26. #define GAMMA_IN(color) pow(color, vec4(InputGamma, InputGamma, InputGamma, InputGamma))
  27. #define GAMMA_OUT(color) pow(color, vec4(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  28. #define TEX2D(coords) GAMMA_IN(texture(source[0],coords))
  29. // Macro for weights computing
  30. #define WEIGHT(w) \
  31. if(w>1.0) w=1.0; \
  32. w = 1.0 - w * w; \
  33. w = w * w;
  34. uniform sampler2D source[];
  35. uniform vec4 sourceSize[];
  36. in Vertex{
  37. vec2 texCoord;
  38. };
  39. out vec4 fragColor;
  40. void main(void) {
  41. vec2 coords = ( texCoord * sourceSize[0].xy );
  42. vec2 pixel_center = floor( coords ) + vec2(0.5, 0.5);
  43. vec2 texture_coords = pixel_center / sourceSize[0].xy;
  44. vec4 color = TEX2D( texture_coords );
  45. float dx = coords.x - pixel_center.x;
  46. float h_weight_00 = dx / SPOT_WIDTH;
  47. WEIGHT(h_weight_00);
  48. color *= vec4( h_weight_00, h_weight_00, h_weight_00, h_weight_00 );
  49. // get closest horizontal neighbour to blend
  50. vec2 coords01;
  51. if (dx>0.0) {
  52. coords01 = vec2(sourceSize[0].z,0.0);
  53. dx = 1.0 - dx;
  54. } else {
  55. coords01 = -vec2(sourceSize[0].z,0.0);
  56. dx = 1.0 + dx;
  57. }
  58. vec4 colorNB = TEX2D( texture_coords + coords01 );
  59. float h_weight_01 = dx / SPOT_WIDTH;
  60. WEIGHT( h_weight_01 );
  61. color = color + colorNB * vec4( h_weight_01, h_weight_01, h_weight_01, h_weight_01 );
  62. //////////////////////////////////////////////////////
  63. // Vertical Blending
  64. float dy = coords.y - pixel_center.y;
  65. float v_weight_00 = dy / SPOT_HEIGHT;
  66. WEIGHT(v_weight_00);
  67. color *= vec4( v_weight_00, v_weight_00, v_weight_00, v_weight_00 );
  68. // get closest vertical neighbour to blend
  69. vec2 coords10;
  70. if (dy>0.0) {
  71. coords10 = vec2(0.0,sourceSize[0].w);
  72. dy = 1.0 - dy;
  73. } else {
  74. coords10 = -vec2(0.0,sourceSize[0].w);
  75. dy = 1.0 + dy;
  76. }
  77. colorNB = TEX2D( texture_coords + coords10 );
  78. float v_weight_10 = dy / SPOT_HEIGHT;
  79. WEIGHT( v_weight_10 );
  80. color = color + colorNB * vec4( v_weight_10 * h_weight_00, v_weight_10 * h_weight_00, v_weight_10 * h_weight_00, v_weight_10 * h_weight_00 );
  81. colorNB = TEX2D( texture_coords + coords01 + coords10 );
  82. color = color + colorNB * vec4( v_weight_10 * h_weight_01, v_weight_10 * h_weight_01, v_weight_10 * h_weight_01, v_weight_10 * h_weight_01 );
  83. color *= vec4( COLOR_BOOST, COLOR_BOOST, COLOR_BOOST, COLOR_BOOST );
  84. fragColor=clamp( GAMMA_OUT(color), 0.0, 1.0 );
  85. }