AsyncComputeTonemapping.azsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/Features/SrgSemantics.azsli>
  9. ShaderResourceGroup TexturesSrg : SRG_PerObject
  10. {
  11. RWTexture2D<float4> m_inOutTexture;
  12. Texture2D<float> m_luminanceTexture;
  13. };
  14. float3 ToneMapFilmicALU(float3 color)
  15. {
  16. color = max(0, color - 0.004f);
  17. color = (color * (6.2f * color + 0.5f)) / (color * (6.2f * color + 1.7f)+ 0.06f);
  18. return pow(color, 2.2f);
  19. }
  20. #define ThreadGroupSize 16
  21. [numthreads(ThreadGroupSize, ThreadGroupSize, 1)]
  22. void MainCS(uint3 groupID : SV_GroupID, uint3 groupThreadID : SV_GroupThreadID)
  23. {
  24. uint2 samplePos = groupID.xy * ThreadGroupSize + groupThreadID.xy;
  25. uint2 dimensions;
  26. TexturesSrg::m_inOutTexture.GetDimensions(dimensions.x, dimensions.y);
  27. if (samplePos.x >= dimensions.x || samplePos.y >= dimensions.y)
  28. {
  29. return;
  30. }
  31. const float epsilon = 0.0001f;
  32. float avgLuminance = max(exp(TexturesSrg::m_luminanceTexture[uint2(0,0)]), epsilon);
  33. float4 inputColor = TexturesSrg::m_inOutTexture[samplePos];
  34. // Calculate color based on exposure settings
  35. float keyValue = 0.4f;
  36. float linearExposure = (keyValue / avgLuminance);
  37. float exposure = max(linearExposure, epsilon);
  38. float3 color = exposure * inputColor.rgb;
  39. // Applies the filmic curve
  40. color = max(0, color - 0.004f);
  41. color = (color * (6.2f * color + 0.5f)) / (color * (6.2f * color + 1.7f)+ 0.06f);
  42. color = pow(color, 2.2f);
  43. TexturesSrg::m_inOutTexture[samplePos] = float4(color, inputColor.a);
  44. }