LumaAwareBlurGeneric.frag.glsl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Functions.glsl"
  6. #include "shaders/GaussianBlurCommon.glsl"
  7. #include "shaders/Tonemapping.glsl"
  8. // Preprocessor switches sanity checks
  9. #if !defined(VPASS) && !defined(HPASS)
  10. #error See file
  11. #endif
  12. #if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
  13. #error See file
  14. #endif
  15. #if !defined(TEXTURE_SIZE)
  16. #error See file
  17. #endif
  18. // Determine color type
  19. #if defined(COL_RGBA)
  20. #define COL_TYPE vec4
  21. #elif defined(COL_RGB)
  22. #define COL_TYPE vec3
  23. #elif defined(COL_R)
  24. #define COL_TYPE float
  25. #endif
  26. // Determine tex fetch
  27. #if defined(COL_RGBA)
  28. #define TEX_FETCH rgba
  29. #elif defined(COL_RGB)
  30. #define TEX_FETCH rgb
  31. #elif defined(COL_R)
  32. #define TEX_FETCH r
  33. #endif
  34. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_colorTex;
  35. layout(location = 0) in vec2 in_uv;
  36. layout(location = 0) out COL_TYPE out_color;
  37. float computeLumaWeight(float refLuma, COL_TYPE col)
  38. {
  39. float l = computeLuminance(col);
  40. float diff = abs(refLuma - l);
  41. float weight = 1.0 / (EPSILON + diff);
  42. return weight;
  43. }
  44. void main()
  45. {
  46. #if defined(VPASS)
  47. const vec2 TEXEL_SIZE = vec2(0.0, 1.0 / TEXTURE_SIZE.y);
  48. #else
  49. const vec2 TEXEL_SIZE = vec2(1.0 / TEXTURE_SIZE.x, 0.0);
  50. #endif
  51. out_color = COL_TYPE(0.0);
  52. float refLuma = computeLuminance(texture(u_colorTex, in_uv).TEX_FETCH);
  53. float weight = 0.0;
  54. for(uint i = 0u; i < STEP_COUNT; ++i)
  55. {
  56. vec2 texCoordOffset = OFFSETS[i] * TEXEL_SIZE;
  57. vec2 uv = in_uv + texCoordOffset;
  58. COL_TYPE col = texture(u_colorTex, uv).TEX_FETCH;
  59. float w = WEIGHTS[i] * computeLumaWeight(refLuma, col);
  60. out_color += col * w;
  61. weight += w;
  62. uv = in_uv - texCoordOffset;
  63. col = texture(u_colorTex, uv).TEX_FETCH;
  64. w = WEIGHTS[i] * computeLumaWeight(refLuma, col);
  65. out_color += col * w;
  66. weight += w;
  67. }
  68. out_color = out_color / weight;
  69. }