GritsScanlines.fs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #version 150
  2. // GritsScanlines by torridgristle
  3. // license: public domain (https://forums.libretro.com/t/lightweight-lut-based-scanline-glow-concept-prototype-glsl/18336/7)
  4. /* Runtime parameters
  5. #in LuminanceDawnbringer
  6. #in LuminanceLUT
  7. #in ScanlinesOpacity
  8. #in GammaCorrection
  9. #in TrinitronColors
  10. */
  11. // static parameters
  12. //#define LuminanceDawnbringer
  13. #define LuminanceLUT
  14. //#define TrinitronColors
  15. #define ScanlinesOpacity 0.9
  16. //#define GammaCorrection 1.2
  17. uniform sampler2D source[];
  18. uniform vec4 sourceSize[];
  19. uniform vec4 targetSize;
  20. uniform sampler2D pixmap[];
  21. in Vertex {
  22. vec2 texCoord;
  23. };
  24. out vec4 fragColor;
  25. #ifdef LuminanceLUT
  26. #define LUT_SizeLum 16.0
  27. // Code taken from RetroArch's LUT shader
  28. float luminancelut(vec4 org)
  29. {
  30. vec4 imgColorLum = org;
  31. float redLum = ( imgColorLum.r * (LUT_SizeLum - 1.0) + 0.4999 ) / (LUT_SizeLum * LUT_SizeLum);
  32. float greenLum = ( imgColorLum.g * (LUT_SizeLum - 1.0) + 0.4999 ) / LUT_SizeLum;
  33. float blue1Lum = (floor( imgColorLum.b * (LUT_SizeLum - 1.0) ) / LUT_SizeLum) + redLum;
  34. float blue2Lum = (ceil( imgColorLum.b * (LUT_SizeLum - 1.0) ) / LUT_SizeLum) + redLum;
  35. float mixerLum = clamp(max((imgColorLum.b - blue1Lum) / (blue2Lum - blue1Lum), 0.0), 0.0, 32.0);
  36. float color1Lum = texture(pixmap[1], vec2( blue1Lum, greenLum )).x;
  37. float color2Lum = texture(pixmap[1], vec2( blue2Lum, greenLum )).x;
  38. return mix(color1Lum, color2Lum, mixerLum);
  39. }
  40. #endif
  41. #ifdef TrinitronColors
  42. #define LUT_SizeTrinitron 32.0
  43. vec4 TrinitronD50(vec4 org)
  44. {
  45. vec4 imgColorTrinitron = org;
  46. float redTrinitron = ( imgColorTrinitron.r * (LUT_SizeTrinitron - 1.0) + 0.4999 ) / (LUT_SizeTrinitron * LUT_SizeTrinitron);
  47. float greenTrinitron = ( imgColorTrinitron.g * (LUT_SizeTrinitron - 1.0) + 0.4999 ) / LUT_SizeTrinitron;
  48. float blue1Trinitron = (floor( imgColorTrinitron.b * (LUT_SizeTrinitron - 1.0) ) / LUT_SizeTrinitron) + redTrinitron;
  49. float blue2Trinitron = (ceil( imgColorTrinitron.b * (LUT_SizeTrinitron - 1.0) ) / LUT_SizeTrinitron) + redTrinitron;
  50. float mixerTrinitron = clamp(max((imgColorTrinitron.b - blue1Trinitron) / (blue2Trinitron - blue1Trinitron), 0.0), 0.0, 32.0);
  51. vec4 color1Trinitron = texture(pixmap[2], vec2( blue1Trinitron, greenTrinitron ));
  52. vec4 color2Trinitron = texture(pixmap[2], vec2( blue2Trinitron, greenTrinitron ));
  53. vec4 fragColorTrinitron = mix(color1Trinitron, color2Trinitron, mixerTrinitron);
  54. return vec4(pow(fragColorTrinitron.rgb,vec3(GammaCorrection,GammaCorrection,GammaCorrection)),1.0);
  55. }
  56. #endif
  57. void main() {
  58. //Source Image
  59. vec4 org = texture(source[0], texCoord);
  60. #ifdef LuminanceLUT
  61. // Use a 3DLUT instead of an equation so that it can use any arbitrary mess you can come up with.
  62. float luminance = luminancelut(org);
  63. #elif defined LuminanceDawnbringer
  64. // Dawnbringer's brightness equation from Dawnbringer's Toolbox scripts for Grafx2
  65. float luminance = sqrt(org.r*org.r*0.0676 + org.g*org.g*0.3025 + org.b*org.b*0.0361) * 1.5690256395005606;
  66. #else
  67. // Plain, standard, fine; slightly faster
  68. float luminance = ((0.299*org.r) + (0.587*org.g) + (0.114*org.b));
  69. #endif
  70. // Don't let it exceed 1.0
  71. luminance = clamp(luminance, 0.0, 1.0);
  72. // Scanline Mapping, based on the Phosphor LUT shader's method of tiling a texture over the screen
  73. vec2 LUTeffectiveCoord = vec2(luminance,fract((texCoord.y*targetSize.y)/4.0));
  74. // Scanline Layer
  75. vec4 screen = texture(pixmap[0], LUTeffectiveCoord);
  76. // Output multiplying the scanlines into the original image, with control over opacity
  77. #ifdef TrinitronColors
  78. org = TrinitronD50(org);
  79. #endif
  80. fragColor = ((screen*ScanlinesOpacity)+(1.0 - ScanlinesOpacity)) * (org);
  81. }