gb-pass-0.fs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // This is a port of the original CG shader to the quark format
  2. // the original shader can be found here :
  3. // https://github.com/libretro/common-shaders/tree/master/handheld/gameboy
  4. ///////////////////////////////////////////////////////////////////////////
  5. // //
  6. // Gameboy Classic Shader v0.2.2 //
  7. // //
  8. // Copyright (C) 2013 Harlequin : unknown92835@gmail.com //
  9. // //
  10. // This program is free software: you can redistribute it and/or modify //
  11. // it under the terms of the GNU General Public License as published by //
  12. // the Free Software Foundation, either version 3 of the License, or //
  13. // (at your option) any later version. //
  14. // //
  15. // This program is distributed in the hope that it will be useful, //
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  18. // GNU General Public License for more details. //
  19. // //
  20. // You should have received a copy of the GNU General Public License //
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. #version 150
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. //config //
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. #define baseline_alpha 0.10 //the alpha value of dots in their "off" state, does not affect the border region of the screen - [0, 1]
  29. #define response_time 0.333 //simulate response time, higher values result in longer color transition periods - [0, 1]
  30. //#define HIGAN_FORCE_MONOCHROME_GB //uncomment only when running gb games in higan since the shader expects black and white input.
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. //fragment definitions //
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. #define foreground_color texture(pixmap[0], vec2(0.75, 0.5)).rgb //hardcoded to look up the foreground color from the right half of the palette image
  35. #define rgb_to_alpha(rgb) ( ((rgb.r + rgb.g + rgb.b) / 3.0) + (is_on_dot * baseline_alpha) ) //averages rgb values (allows it to work with color games), modified for contrast and base alpha
  36. #ifdef HIGAN_FORCE_MONOCHROME_GB
  37. #define TEX(src,coord) vec4(vec3(clamp(dot(texture(src, coord).rgb,vec3(21,-16.73,30)),0.0,1.0)),1.0)
  38. #else
  39. #define TEX(src,coord) texture(src, coord)
  40. #endif
  41. #define curr_rgb abs(1 - TEX(source[0], texCoord).rgb)
  42. #define prev0_rgb abs(1 - TEX(frame[0], texCoord).rgb)
  43. #define prev1_rgb abs(1 - TEX(frame[1], texCoord).rgb)
  44. #define prev2_rgb abs(1 - TEX(frame[2], texCoord).rgb)
  45. #define prev3_rgb abs(1 - TEX(frame[3], texCoord).rgb)
  46. #define prev4_rgb abs(1 - TEX(frame[4], texCoord).rgb)
  47. #define prev5_rgb abs(1 - TEX(frame[5], texCoord).rgb)
  48. #define prev6_rgb abs(1 - TEX(frame[6], texCoord).rgb)
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. //fragment shader //
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. uniform sampler2D source[];
  53. uniform sampler2D frame[];
  54. uniform sampler2D pixmap[];
  55. uniform vec4 sourceSize[];
  56. uniform vec4 targetSize;
  57. in Vertex {
  58. vec2 texCoord;
  59. vec2 one_texel;
  60. };
  61. out vec4 fragColor;
  62. void main(void) {
  63. //determine if the corrent texel lies on a dot or in the space between dots
  64. float is_on_dot = float( mod(texCoord.x, sourceSize[0].z) > one_texel.x)* //returns 1 if fragment lies on a dot, 0 otherwise
  65. float(mod(texCoord.y, sourceSize[0].w) > one_texel.y )*
  66. float(texCoord.x>=0.0)*
  67. float(texCoord.x<=1.0)*
  68. float(texCoord.y>=0.0)*
  69. float(texCoord.y<=1.0);
  70. //sample color from the current and previous frames, apply response time modifier
  71. //response time effect implmented through an exponential dropoff algorithm
  72. vec3 input_rgb = curr_rgb;
  73. input_rgb += (prev0_rgb - input_rgb) * response_time;
  74. input_rgb += (prev1_rgb - input_rgb) * pow(response_time, 2.0);
  75. input_rgb += (prev2_rgb - input_rgb) * pow(response_time, 3.0);
  76. input_rgb += (prev3_rgb - input_rgb) * pow(response_time, 4.0);
  77. input_rgb += (prev4_rgb - input_rgb) * pow(response_time, 5.0);
  78. input_rgb += (prev5_rgb - input_rgb) * pow(response_time, 6.0);
  79. input_rgb += (prev6_rgb - input_rgb) * pow(response_time, 7.0);
  80. //apply foreground color and assign alpha value
  81. vec4 out_color = vec4(foreground_color, rgb_to_alpha(input_rgb)); //apply the foreground color to all texels (the color will be modified by alpha later) and assign alpha based on rgb input
  82. //overlay the matrix
  83. out_color.a *= is_on_dot; //if the fragment is not on a dot, set its alpha value to 0
  84. //return fragment color
  85. fragColor= out_color;
  86. }