TonemappingAverageLuminance.ankiprog 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. ANKI_SPECIALIZATION_CONSTANT_UVEC2(INPUT_TEX_SIZE, 0u);
  6. #pragma anki start comp
  7. #define LOG_AVG 0
  8. #include <AnKi/Shaders/TonemappingFunctions.glsl>
  9. const UVec2 WORKGROUP_SIZE = UVec2(32u, 16u);
  10. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  11. // Align the tex size to workgroup size
  12. const UVec2 ALIGNED_INPUT_TEX_SIZE = WORKGROUP_SIZE * ((INPUT_TEX_SIZE + WORKGROUP_SIZE - 1u) / WORKGROUP_SIZE);
  13. const UVec2 PIXELS_PER_TILE = ALIGNED_INPUT_TEX_SIZE / WORKGROUP_SIZE;
  14. layout(set = 0, binding = 0) uniform ANKI_RP texture2D u_tex;
  15. #define TONEMAPPING_RESOURCE_AS_WRITE_IMAGE 1
  16. #define TONEMAPPING_SET 0
  17. #define TONEMAPPING_BINDING 1
  18. #include <AnKi/Shaders/TonemappingResources.glsl>
  19. shared F32 s_avgLum[WORKGROUP_SIZE.x * WORKGROUP_SIZE.y];
  20. void main()
  21. {
  22. // Gather the log-average luminance of a tile. It will miss some pixels but not too many
  23. const U32 yStart = gl_LocalInvocationID.y * PIXELS_PER_TILE.y;
  24. const U32 xStart = gl_LocalInvocationID.x * PIXELS_PER_TILE.x;
  25. F32 avgLum = 0.0;
  26. ANKI_UNROLL for(U32 y = 0u; y < PIXELS_PER_TILE.y; ++y)
  27. {
  28. ANKI_UNROLL for(U32 x = 0u; x < PIXELS_PER_TILE.x; ++x)
  29. {
  30. const UVec2 uv = UVec2(xStart, yStart) + UVec2(x, y);
  31. if(uv.x >= INPUT_TEX_SIZE.x || uv.y >= INPUT_TEX_SIZE.y)
  32. {
  33. continue;
  34. }
  35. const Vec3 color = texelFetch(u_tex, IVec2(uv), 0).rgb;
  36. const F32 lum = computeLuminance(color);
  37. #if LOG_AVG
  38. avgLum += log(max(EPSILON, lum));
  39. #else
  40. avgLum += lum;
  41. #endif
  42. }
  43. }
  44. s_avgLum[gl_LocalInvocationIndex] = avgLum;
  45. memoryBarrierShared();
  46. barrier();
  47. // Gather the results into one
  48. ANKI_LOOP for(U32 s = (WORKGROUP_SIZE.x * WORKGROUP_SIZE.y) / 2u; s > 0u; s >>= 1u)
  49. {
  50. if(gl_LocalInvocationIndex < s)
  51. {
  52. s_avgLum[gl_LocalInvocationIndex] += s_avgLum[gl_LocalInvocationIndex + s];
  53. }
  54. #if ANKI_PLATFORM_MOBILE
  55. if(s > 16u)
  56. {
  57. memoryBarrierShared();
  58. barrier();
  59. }
  60. #endif
  61. }
  62. // Write the result
  63. ANKI_BRANCH if(gl_LocalInvocationIndex == 0u)
  64. {
  65. #if LOG_AVG
  66. const F32 crntLum = exp(s_avgLum[0] * (1.0 / F32(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y)));
  67. #else
  68. const F32 crntLum = s_avgLum[0] * (1.0 / F32(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y));
  69. #endif
  70. #if 1
  71. const F32 prevLum = readExposureAndAverageLuminance().y;
  72. // Lerp between previous and new L value
  73. const F32 INTERPOLATION_FACTOR = 0.05;
  74. F32 finalAvgLum = mix(prevLum, crntLum, INTERPOLATION_FACTOR);
  75. #else
  76. F32 finalAvgLum = crntLum;
  77. #endif
  78. // This is a workaround because sometimes the avg lum becomes nan
  79. finalAvgLum = clamp(finalAvgLum, EPSILON, MAX_F32);
  80. writeExposureAndAverageLuminance(computeExposure(finalAvgLum, 0.0), finalAvgLum);
  81. }
  82. }
  83. #pragma anki end