GaussianBlur.glsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #pragma anki mutator ORIENTATION 0 1 2 // 0: VERTICAL, 1: HORIZONTAL, 2: BOX
  7. #pragma anki mutator KERNEL_SIZE 3 5 7 9 11
  8. #pragma anki mutator COLOR_COMPONENTS 4 3 1
  9. ANKI_SPECIALIZATION_CONSTANT_UVEC2(TEXTURE_SIZE, 0, UVec2(1));
  10. #include <shaders/GaussianBlurCommon.glsl>
  11. #if defined(ANKI_COMPUTE_SHADER)
  12. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  13. # define USE_COMPUTE 1
  14. #else
  15. # define USE_COMPUTE 0
  16. #endif
  17. #if ORIENTATION == 0
  18. # define VERTICAL 1
  19. #elif ORIENTATION == 1
  20. # define HORIZONTAL 1
  21. #else
  22. # define BOX 1
  23. #endif
  24. // Determine color type
  25. #if COLOR_COMPONENTS == 4
  26. # define COL_TYPE Vec4
  27. # define TEX_FETCH rgba
  28. # define TO_VEC4(x_) x_
  29. #elif COLOR_COMPONENTS == 3
  30. # define COL_TYPE Vec3
  31. # define TEX_FETCH rgb
  32. # define TO_VEC4(x_) Vec4(x_, 0.0)
  33. #elif COLOR_COMPONENTS == 1
  34. # define COL_TYPE F32
  35. # define TEX_FETCH r
  36. # define TO_VEC4(x_) Vec4(x_, 0.0, 0.0, 0.0)
  37. #else
  38. # error See file
  39. #endif
  40. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  41. layout(set = 0, binding = 1) uniform texture2D u_tex; ///< Input texture
  42. #if USE_COMPUTE
  43. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  44. layout(set = 0, binding = 2) writeonly uniform image2D u_outImg;
  45. #else
  46. layout(location = 0) in Vec2 in_uv;
  47. layout(location = 0) out COL_TYPE out_color;
  48. #endif
  49. void main()
  50. {
  51. // Set UVs
  52. #if USE_COMPUTE
  53. ANKI_BRANCH if(gl_GlobalInvocationID.x >= TEXTURE_SIZE.x || gl_GlobalInvocationID.y >= TEXTURE_SIZE.y)
  54. {
  55. // Out of bounds
  56. return;
  57. }
  58. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(TEXTURE_SIZE);
  59. #else
  60. const Vec2 uv = in_uv;
  61. #endif
  62. const Vec2 TEXEL_SIZE = 1.0 / Vec2(TEXTURE_SIZE);
  63. #if !defined(BOX)
  64. // Do seperable
  65. # if defined(HORIZONTAL)
  66. # define X_OR_Y x
  67. # else
  68. # define X_OR_Y y
  69. # endif
  70. COL_TYPE color = textureLod(u_tex, u_linearAnyClampSampler, uv, 0.0).TEX_FETCH * WEIGHTS[0u];
  71. Vec2 uvOffset = Vec2(0.0);
  72. uvOffset.X_OR_Y = 1.5 * TEXEL_SIZE.X_OR_Y;
  73. ANKI_UNROLL for(U32 i = 0u; i < STEP_COUNT; ++i)
  74. {
  75. COL_TYPE col = textureLod(u_tex, u_linearAnyClampSampler, uv + uvOffset, 0.0).TEX_FETCH;
  76. col += textureLod(u_tex, u_linearAnyClampSampler, uv - uvOffset, 0.0).TEX_FETCH;
  77. color += WEIGHTS[i + 1u] * col;
  78. uvOffset.X_OR_Y += 2.0 * TEXEL_SIZE.X_OR_Y;
  79. }
  80. #else
  81. // Do box
  82. const Vec2 OFFSET = 1.5 * TEXEL_SIZE;
  83. COL_TYPE color = textureLod(u_tex, u_linearAnyClampSampler, uv, 0.0).TEX_FETCH * BOX_WEIGHTS[0u];
  84. COL_TYPE col;
  85. col = textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(OFFSET.x, 0.0), 0.0).TEX_FETCH;
  86. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(0.0, OFFSET.y), 0.0).TEX_FETCH;
  87. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(-OFFSET.x, 0.0), 0.0).TEX_FETCH;
  88. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(0.0, -OFFSET.y), 0.0).TEX_FETCH;
  89. color += col * BOX_WEIGHTS[1u];
  90. col = textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(+OFFSET.x, +OFFSET.y), 0.0).TEX_FETCH;
  91. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(+OFFSET.x, -OFFSET.y), 0.0).TEX_FETCH;
  92. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(-OFFSET.x, +OFFSET.y), 0.0).TEX_FETCH;
  93. col += textureLod(u_tex, u_linearAnyClampSampler, uv + Vec2(-OFFSET.x, -OFFSET.y), 0.0).TEX_FETCH;
  94. color += col * BOX_WEIGHTS[2u];
  95. #endif
  96. // Write value
  97. #if USE_COMPUTE
  98. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), TO_VEC4(color));
  99. #else
  100. out_color = color;
  101. #endif
  102. }