crt.frag 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2017 David (astral) Cravens (decravens@gmail.com)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /*
  20. * crt.frag - a fragment shader that generates scanlines, based on crt-pi from
  21. * libretro and semisoft.frag
  22. */
  23. #version 110
  24. uniform sampler2D baseMap;
  25. varying vec2 vTexcoord;
  26. #define MASK_BRIGHTNESS 0.70
  27. #define SCANLINE_WEIGHT 6.0
  28. #define SCANLINE_GAP_BRIGHTNESS 0.12
  29. #define BLOOM_FACTOR 1.5
  30. #define INPUT_GAMMA 2.4
  31. #define OUTPUT_GAMMA 2.2
  32. #define XS 1024.0
  33. #define YS 512.0
  34. #define TS vec2(XS,YS)
  35. float CalcScanLineWeight(float dist)
  36. {
  37. return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
  38. }
  39. void main()
  40. {
  41. vec2 texcoordInPixels = vTexcoord * TS;
  42. vec2 tempCoord = floor(texcoordInPixels) + 0.5;
  43. vec2 coord = tempCoord / TS;
  44. vec2 deltas = texcoordInPixels - tempCoord;
  45. float scanLineWeight = CalcScanLineWeight(deltas.y);
  46. vec2 tcbase = (tempCoord + 0.5)/TS;
  47. vec2 tcdiff = vTexcoord-tcbase;
  48. vec2 sdiff = sign(tcdiff);
  49. vec2 adiff = pow(abs(tcdiff)*TS, vec2(2.0));
  50. vec3 color = texture2D(baseMap, tcbase + sdiff*adiff/TS).rgb;
  51. color = pow(color, vec3(INPUT_GAMMA));
  52. scanLineWeight *= BLOOM_FACTOR;
  53. color *= scanLineWeight;
  54. color = pow(color, vec3(1.0/OUTPUT_GAMMA));
  55. float whichMask = fract(gl_FragCoord.x * 0.3333333);
  56. vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
  57. if (whichMask < 0.3333333)
  58. mask.x = 1.0;
  59. else if (whichMask < 0.6666666)
  60. mask.y = 1.0;
  61. else
  62. mask.z = 1.0;
  63. gl_FragColor = vec4(color * mask, 1.0) * 1.75;
  64. }