TonemappingAverageLuminance.ankiprog 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!--
  2. Copyright (C) 2009-2017, 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. #include "shaders/TonemappingResources.glsl"
  25. shared float g_avgLum[WORKGROUP_SIZE];
  26. void main()
  27. {
  28. // Gather the log-average luminance of a tile
  29. float avgLum = 0.0;
  30. uint yStart = gl_LocalInvocationID.y * PIXEL_READ_Y;
  31. uint xStart = gl_LocalInvocationID.x * PIXEL_READ_X;
  32. for(uint y = 0; y < PIXEL_READ_Y; ++y)
  33. {
  34. for(uint x = 0; x < PIXEL_READ_X; ++x)
  35. {
  36. ivec2 uv = ivec2(xStart, yStart) + ivec2(x, y);
  37. vec3 color = texelFetch(u_tex, uv, 0).rgb;
  38. float lum = computeLuminance(color);
  39. #if LOG_AVG
  40. avgLum += log(max(EPSILON, lum));
  41. #else
  42. avgLum += lum;
  43. #endif
  44. }
  45. }
  46. g_avgLum[gl_LocalInvocationIndex] = avgLum;
  47. memoryBarrierShared();
  48. barrier();
  49. // Gather the results into one
  50. for(uint s = WORKGROUP_SIZE / 2u; s > 0u; s >>= 1u)
  51. {
  52. if(gl_LocalInvocationIndex < s)
  53. {
  54. g_avgLum[gl_LocalInvocationIndex] += g_avgLum[gl_LocalInvocationIndex + s];
  55. }
  56. memoryBarrierShared();
  57. barrier();
  58. }
  59. // Write the result
  60. if(gl_LocalInvocationIndex == 0)
  61. {
  62. #if LOG_AVG
  63. float crntLum = exp(g_avgLum[0] / float(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y));
  64. #else
  65. float crntLum = g_avgLum[0] / float(INPUT_TEX_SIZE.x * INPUT_TEX_SIZE.y);
  66. #endif
  67. crntLum = max(crntLum, 0.04);
  68. #if 1
  69. float prevLum = u_averageLuminance;
  70. // Lerp between previous and new L value
  71. const float INTERPOLATION_FACTOR = 0.05;
  72. u_averageLuminance = mix(prevLum, crntLum, INTERPOLATION_FACTOR);
  73. #else
  74. u_averageLuminance = crntLum;
  75. #endif
  76. u_exposureThreshold0 = computeExposure(u_averageLuminance, 0.0);
  77. }
  78. }
  79. ]]></source>
  80. </shader>
  81. </shaders>
  82. </shaderProgram>