2
0

TonemappingAverageLuminance.ankiprog 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <!--
  2. Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  3. All rights reserved.
  4. Code licensed under the BSD License.
  5. http://www.anki3d.org/LICENSE
  6. -->
  7. <shaderProgram>
  8. <shaders>
  9. <shader type="comp">
  10. <inputs>
  11. <input name="INPUT_TEX_SIZE" type="uvec2" const="1"/>
  12. </inputs>
  13. <source><![CDATA[
  14. #define LOG_AVG 0
  15. #include "shaders/Common.glsl"
  16. #include "shaders/Tonemapping.glsl"
  17. const uint WORKGROUP_SIZE_X = 16u;
  18. const uint WORKGROUP_SIZE_Y = 16u;
  19. const uint WORKGROUP_SIZE = WORKGROUP_SIZE_X * WORKGROUP_SIZE_Y;
  20. layout(local_size_x = WORKGROUP_SIZE_X, local_size_y = WORKGROUP_SIZE_Y, local_size_z = 1) in;
  21. const uint PIXEL_READ_X = INPUT_TEX_SIZE.x / WORKGROUP_SIZE_X;
  22. const uint PIXEL_READ_Y = INPUT_TEX_SIZE.y / WORKGROUP_SIZE_Y;
  23. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_tex;
  24. #define TONEMAPPING_RESOURCE_AS_BUFFER 1
  25. #include "shaders/TonemappingResources.glsl"
  26. shared float s_avgLum[WORKGROUP_SIZE];
  27. void main()
  28. {
  29. // Gather the log-average luminance of a tile
  30. float avgLum = 0.0;
  31. uint yStart = gl_LocalInvocationID.y * PIXEL_READ_Y;
  32. uint xStart = gl_LocalInvocationID.x * PIXEL_READ_X;
  33. for(uint y = 0; y < PIXEL_READ_Y; ++y)
  34. {
  35. for(uint x = 0; x < PIXEL_READ_X; ++x)
  36. {
  37. ivec2 uv = ivec2(xStart, yStart) + ivec2(x, y);
  38. vec3 color = texelFetch(u_tex, uv, 0).rgb;
  39. float lum = computeLuminance(color);
  40. #if LOG_AVG
  41. avgLum += log(max(EPSILON, lum));
  42. #else
  43. avgLum += lum;
  44. #endif
  45. }
  46. }
  47. s_avgLum[gl_LocalInvocationIndex] = avgLum;
  48. memoryBarrierShared();
  49. barrier();
  50. // Gather the results into one
  51. for(uint s = WORKGROUP_SIZE / 2u; s > 0u; s >>= 1u)
  52. {
  53. if(gl_LocalInvocationIndex < s)
  54. {
  55. s_avgLum[gl_LocalInvocationIndex] += s_avgLum[gl_LocalInvocationIndex + s];
  56. }
  57. memoryBarrierShared();
  58. barrier();
  59. }
  60. // Write the result
  61. if(gl_LocalInvocationIndex == 0)
  62. {
  63. #if LOG_AVG
  64. float crntLum = exp(s_avgLum[0] / float(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y));
  65. #else
  66. float crntLum = s_avgLum[0] / float(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y);
  67. #endif
  68. #if 1
  69. float prevLum = u_averageLuminance;
  70. // Lerp between previous and new L value
  71. const float INTERPOLATION_FACTOR = 0.05;
  72. float finalAvgLum = mix(prevLum, crntLum, INTERPOLATION_FACTOR);
  73. #else
  74. float finalAvgLum = crntLum;
  75. #endif
  76. // This is a workaround because sometimes the avg lum becomes nan
  77. finalAvgLum = clamp(finalAvgLum, EPSILON, FLT_MAX);
  78. u_averageLuminance = finalAvgLum;
  79. u_exposureThreshold0 = computeExposure(u_averageLuminance, 0.0);
  80. }
  81. }
  82. ]]></source>
  83. </shader>
  84. </shaders>
  85. </shaderProgram>