TonemappingAverageLuminance.ankiprog 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2009-2021, 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 texture2D u_tex;
  15. #define TONEMAPPING_RESOURCE_AS_BUFFER 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. memoryBarrierShared();
  55. barrier();
  56. }
  57. // Write the result
  58. ANKI_BRANCH if(gl_LocalInvocationIndex == 0u)
  59. {
  60. #if LOG_AVG
  61. const F32 crntLum = exp(s_avgLum[0] * (1.0 / F32(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y)));
  62. #else
  63. const F32 crntLum = s_avgLum[0] * (1.0 / F32(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y));
  64. #endif
  65. #if 1
  66. const F32 prevLum = u_averageLuminance;
  67. // Lerp between previous and new L value
  68. const F32 INTERPOLATION_FACTOR = 0.05;
  69. F32 finalAvgLum = mix(prevLum, crntLum, INTERPOLATION_FACTOR);
  70. #else
  71. F32 finalAvgLum = crntLum;
  72. #endif
  73. // This is a workaround because sometimes the avg lum becomes nan
  74. finalAvgLum = clamp(finalAvgLum, EPSILON, FLT_MAX);
  75. u_averageLuminance = finalAvgLum;
  76. u_exposureThreshold0 = computeExposure(u_averageLuminance, 0.0);
  77. }
  78. }
  79. #pragma anki end