GaussianBlurGeneric.glsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /// @file
  2. /// Generic shader program for Gaussian blur inspired by Daniel Rakos' article
  3. /// http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
  4. ///
  5. /// Switches: VPASS or HPASS, COL_RGBA or COL_RGB or COL_R
  6. ///
  7. /// This is an optimized version. See the clean one at rev213
  8. #pragma anki vertShaderBegins
  9. #pragma anki attribute position 0
  10. attribute vec2 position;
  11. uniform float imgDimension = 0.0; ///< the img width for hspass or the img height for vpass
  12. out vec2 vTexCoords;
  13. out float vOffsets[2]; ///< For side pixels
  14. const float _offset_[2] = float[](1.3846153846, 3.2307692308); ///< The offset of side pixels
  15. void main()
  16. {
  17. vTexCoords = position;
  18. vOffsets[0] = _offset_[0] / imgDimension;
  19. vOffsets[1] = _offset_[1] / imgDimension;
  20. gl_Position = vec4(position * 2.0 - 1.0, 0.0, 1.0);
  21. }
  22. #pragma anki fragShaderBegins
  23. // Preprocessor switches sanity checks
  24. #if !defined(VPASS) && !defined(HPASS)
  25. #error "See file"
  26. #endif
  27. #if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
  28. #error "See file"
  29. #endif
  30. uniform sampler2D img; ///< Input FAI
  31. uniform float blurringDist = 0.0;
  32. in vec2 vTexCoords;
  33. in float vOffsets[2];
  34. // Determine color type
  35. #if defined(COL_RGBA)
  36. #define COL_TYPE vec4
  37. #elif defined(COL_RGB)
  38. #define COL_TYPE vec3
  39. #elif defined(COL_R)
  40. #define COL_TYPE float
  41. #endif
  42. // Determine tex fetch
  43. #if defined(COL_RGBA)
  44. #define TEX_FETCH rgba
  45. #elif defined(COL_RGB)
  46. #define TEX_FETCH rgb
  47. #elif defined(COL_R)
  48. #define TEX_FETCH r
  49. #endif
  50. layout(location = 0) out COL_TYPE fFragColor;
  51. const float FIRST_WEIGHT = 0.2255859375;
  52. const float WEIGHTS[2] = float[](0.314208984375, 0.06982421875);
  53. void main()
  54. {
  55. // the center (0,0) pixel
  56. COL_TYPE _col_ = texture2D(img, vTexCoords).TEX_FETCH * FIRST_WEIGHT;
  57. // side pixels
  58. for(int i = 0; i < 2; i++)
  59. {
  60. #if defined(HPASS)
  61. vec2 _texCoords_ = vec2(vTexCoords.x + blurringDist + vOffsets[i], vTexCoords.y);
  62. _col_ += texture2D(img, _texCoords_).TEX_FETCH * WEIGHTS[i];
  63. _texCoords_.x = vTexCoords.x - blurringDist - vOffsets[i];
  64. _col_ += texture2D(img, _texCoords_).TEX_FETCH * WEIGHTS[i];
  65. #elif defined(VPASS)
  66. vec2 _texCoords_ = vec2(vTexCoords.x, vTexCoords.y + blurringDist + vOffsets[i]);
  67. _col_ += texture2D(img, _texCoords_).TEX_FETCH * WEIGHTS[i];
  68. _texCoords_.y = vTexCoords.y - blurringDist - vOffsets[i];
  69. _col_ += texture2D(img, _texCoords_).TEX_FETCH * WEIGHTS[i];
  70. #endif
  71. }
  72. fFragColor = _col_;
  73. }