VariableSamplingBlurGeneric.frag.glsl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. /// Defines:
  6. /// - VPASS or HPASS
  7. /// - COL_RGBA or COL_RGB or COL_R
  8. /// - SAMPLES is a number of 3 or 5 or 7 or 9
  9. /// - BLURRING_DIST is optional and it's extra pixels to move the blurring
  10. #pragma anki type frag
  11. #pragma anki include "shaders/Common.glsl"
  12. // Preprocessor switches sanity checks
  13. #if !defined(VPASS) && !defined(HPASS)
  14. # error See file
  15. #endif
  16. #if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
  17. # error See file
  18. #endif
  19. #if !defined(IMG_DIMENSION)
  20. # error See file
  21. #endif
  22. #if !defined(SAMPLES)
  23. # error See file
  24. #endif
  25. // Input RT with different binding for less GL API calls
  26. layout(binding = 0) uniform mediump sampler2D uTex;
  27. layout(location = 0) in vec2 in_texCoord;
  28. #if !defined(BLURRING_DIST)
  29. # define BLURRING_DIST 0.0
  30. #endif
  31. // Determine color type
  32. #if defined(COL_RGBA)
  33. # define COL_TYPE vec4
  34. #elif defined(COL_RGB)
  35. # define COL_TYPE vec3
  36. #elif defined(COL_R)
  37. # define COL_TYPE float
  38. #endif
  39. // Determine tex fetch
  40. #if defined(COL_RGBA)
  41. # define TEX_FETCH rgba
  42. #elif defined(COL_RGB)
  43. # define TEX_FETCH rgb
  44. #elif defined(COL_R)
  45. # define TEX_FETCH r
  46. #endif
  47. const float OFFSET = 1.0 / (2.0 * IMG_DIMENSION);
  48. // Calc the kernel. Use offsets of 3 to take advantage of bilinear filtering
  49. #define BLURRING(val, sign_) ((float(val) * (float(BLURRING_DIST) + 1.0) / float(IMG_DIMENSION)) * float(sign_) + OFFSET)
  50. #if defined(VPASS)
  51. # define BLURRING_OFFSET_X(val, sign_) BLURRING(val, sign_)
  52. # define BLURRING_OFFSET_Y(val, sign_) 0.0
  53. #else
  54. # define BLURRING_OFFSET_X(val, sign_) 0.0
  55. # define BLURRING_OFFSET_Y(val, sign_) BLURRING(val, sign_)
  56. #endif
  57. #define BLURRING_OFFSET(v, s) vec2(BLURRING_OFFSET_X(v, s), BLURRING_OFFSET_Y(v, s))
  58. const vec2 KERNEL[SAMPLES - 1] = vec2[](
  59. BLURRING_OFFSET(1, -1),
  60. BLURRING_OFFSET(1, 1)
  61. #if SAMPLES > 3
  62. ,BLURRING_OFFSET(2, -1),
  63. BLURRING_OFFSET(2, 1)
  64. #endif
  65. #if SAMPLES > 5
  66. ,BLURRING_OFFSET(3, -1),
  67. BLURRING_OFFSET(3, 1)
  68. #endif
  69. #if SAMPLES > 7
  70. ,BLURRING_OFFSET(4, -1),
  71. BLURRING_OFFSET(4, 1)
  72. #endif
  73. #if SAMPLES > 9
  74. ,BLURRING_OFFSET(5, -1),
  75. BLURRING_OFFSET(5, 1)
  76. #endif
  77. #if SAMPLES > 11
  78. ,BLURRING_OFFSET(6, -1),
  79. BLURRING_OFFSET(6, 1)
  80. #endif
  81. #if SAMPLES > 13
  82. ,BLURRING_OFFSET(7, -1),
  83. BLURRING_OFFSET(7, 1)
  84. #endif
  85. #if SAMPLES > 15
  86. ,BLURRING_OFFSET(8, -1),
  87. BLURRING_OFFSET(8, 1)
  88. #endif
  89. #if SAMPLES > 17
  90. ,BLURRING_OFFSET(9, -1),
  91. BLURRING_OFFSET(9, 1)
  92. #endif
  93. );
  94. layout(location = 0) out COL_TYPE out_fragColor;
  95. void main()
  96. {
  97. // Get the first
  98. COL_TYPE col = textureRt(uTex, in_texCoord).TEX_FETCH;
  99. // Get the rest of the samples
  100. for(uint i = 0; i < SAMPLES - 1; i++)
  101. {
  102. col += textureRt(uTex, in_texCoord + KERNEL[i]).TEX_FETCH;
  103. }
  104. out_fragColor = col / float(SAMPLES);
  105. }