Tonemap.ankiprog 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Does tonemapping
  6. #pragma anki technique vert pixel comp
  7. #include <AnKi/Shaders/QuadVert.hlsl>
  8. #if ANKI_COMPUTE_SHADER || ANKI_PIXEL_SHADER
  9. # include <AnKi/Shaders/Functions.hlsl>
  10. # include <AnKi/Shaders/TonemappingFunctions.hlsl>
  11. SamplerState g_nearestAnyClampSampler : register(s0);
  12. SamplerState g_trilinearRepeatSampler : register(s1);
  13. Texture2D<Vec4> g_inputRt : register(t0);
  14. Texture3D<Vec4> g_lut : register(t1);
  15. # define TONEMAPPING_REGISTER u0
  16. # include <AnKi/Shaders/TonemappingResources.hlsl>
  17. # if ANKI_COMPUTE_SHADER
  18. RWTexture2D<Vec4> g_storageTex : register(u1);
  19. # define THREADGROUP_SIZE_SQRT 8
  20. # endif
  21. HVec3 colorGrading(HVec3 color)
  22. {
  23. Vec3 lutSize;
  24. g_lut.GetDimensions(lutSize.x, lutSize.y, lutSize.y);
  25. const HVec3 lutScale = ((lutSize.x - 1.0) / lutSize.x).xxx;
  26. const HVec3 lutOffset = (1.0 / (2.0 * lutSize.x)).xxx;
  27. color = min(color, HVec3(1.0, 1.0, 1.0));
  28. const HVec3 lutCoords = color * lutScale + lutOffset;
  29. return g_lut.SampleLevel(g_trilinearRepeatSampler, lutCoords, 0.0).rgb;
  30. }
  31. # if ANKI_COMPUTE_SHADER
  32. [numthreads(THREADGROUP_SIZE_SQRT, THREADGROUP_SIZE_SQRT, 1)] void main(UVec3 svDispatchThreadId : SV_DISPATCHTHREADID)
  33. # else
  34. Vec3 main(VertOut input) : SV_TARGET0
  35. # endif
  36. {
  37. # if ANKI_COMPUTE_SHADER
  38. Vec2 texSize;
  39. g_storageTex.GetDimensions(texSize.x, texSize.y);
  40. const Vec2 uv = (Vec2(svDispatchThreadId.xy) + 0.5f) / texSize;
  41. # else
  42. const Vec2 uv = input.m_uv;
  43. # endif
  44. const HVec3 hdr = g_inputRt.SampleLevel(g_nearestAnyClampSampler, uv, 0.0f).rgb;
  45. HVec3 tonemapped = linearToSRgb(tonemap<F16>(hdr, readExposureAndAverageLuminance<F16>().x));
  46. tonemapped = colorGrading(tonemapped);
  47. # if ANKI_COMPUTE_SHADER
  48. g_storageTex[svDispatchThreadId.xy] = Vec4(tonemapped, 0.0);
  49. # else
  50. return tonemapped;
  51. # endif
  52. }
  53. #endif // ANKI_COMPUTE_SHADER || ANKI_PIXEL_SHADER