gb-pass-2.fs 6.2 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. //fragment declarations //
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. uniform sampler2D source[];
  29. uniform sampler2D frame[];
  30. uniform sampler2D pixmap[];
  31. uniform vec4 sourceSize[];
  32. uniform vec4 targetSize;
  33. in Vertex {
  34. vec2 texCoord;
  35. vec2 lower_bound;
  36. vec2 upper_bound;
  37. };
  38. out vec4 fragColor;
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //fragment functions //
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. //sigma = 4.0, normalized for 5 pixel offset sigma = 4.0, normalized for 4 pixel offset
  43. //Raw Gaussian weights: Raw Gaussian weights:
  44. // 0.09973561222190091607086808117254 @position0 0.09973561222190091607086808117254 @position0
  45. // 0.09666707205829167156101677393475 @position1 0.09666707205829167156101677393475 @position1
  46. // 0.08801637626376240452162358324964 @position2 0.08801637626376240452162358324964 @position2
  47. // 0.07528440407628116669052257071979 @position3 0.07528440407628116669052257071979 @position3
  48. // 0.06049272702308815188099267447364 @position4 0.06049272702308815188099267447364 @position4
  49. // 0.04566231462789672460813692086928 @position5
  50. //
  51. //sum [p0 + 2(p1 + p2 + p3 + p4 + p5)]: sum [p0 + 2(p1 + p2 + p3 + p4)]:
  52. // 0.83198140032054115459545312766674 0.74065677106474770537917928592818
  53. //
  54. //Normalizing factor [1 / sum]: Normalizing factor [1 / sum]:
  55. // 1.2019499469756482251051310195171 1.350153052084338115052273748029
  56. //
  57. //Normalized Gaussian weights: Normalized Gaussian weights:
  58. // 0.11987721382169761913280166382392 @position0 0.13465834124289953661305802732548 @position0
  59. // 0.11618898213475484076479592086597 @position1 0.13051534237555914090930704141833 @position1
  60. // 0.10579127878321792515352079488329 @position2 0.11883557904592230273554609080014 @position2
  61. // 0.09048808548757942339961181362524 @position3 0.10164546793794160274995705611009 @position3
  62. // 0.07270923003781316665844409497651 @position4 0.08167444001912718529866079800870 @position4
  63. // 0.05488381664578583445722654373702 @position5
  64. vec4 gaussian_blur(void)
  65. {
  66. //define offsets and weights - change this for both the X and Y passes if you change the sigma value or number of texels sampled
  67. float offsets[5] = float[](0.0, 1.0, 2.0, 3.0, 4.0);
  68. float weights[5] = float[]( 0.13465834124289953661305802732548, //precalculated using the Gaussian function:
  69. 0.13051534237555914090930704141833, // G(x) = (1 / sqrt(2 * pi * sigma^2)) * e^(-x^2 / (2 * sigma^2))
  70. 0.11883557904592230273554609080014, //where sigma = 4.0 and x = offset in range [0, 5]
  71. 0.10164546793794160274995705611009, //normalized to 1 to prevent image darkening by multiplying each weight by:
  72. 0.08167444001912718529866079800870 ); // 1 / sum(all weights)
  73. //sample the current fragment and apply its weight
  74. vec4 out_color = texture(source[0], clamp(texCoord, lower_bound, upper_bound)) * weights[0];
  75. //iterate across the offsets in both directions sampling texels and adding their weighted alpha values to the total
  76. for (int i = 1; i < 5; i++)
  77. {
  78. out_color.a += texture(source[0], clamp(texCoord + vec2(offsets[i] * sourceSize[0].z, 0.0), lower_bound, upper_bound)).a * weights[i];
  79. out_color.a += texture(source[0], clamp(texCoord - vec2(offsets[i] * sourceSize[0].z, 0.0), lower_bound, upper_bound)).a * weights[i];
  80. }
  81. //return the new value
  82. return out_color;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. //fragment shader //
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. void main(void) {
  88. //apply the Gaussian blur along the x axis and return the result
  89. fragColor= gaussian_blur();
  90. }