AutoHDR.glsl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Based on https://github.com/Filoppi/PumboAutoHDR
  2. /*
  3. [configuration]
  4. [OptionRangeFloat]
  5. GUIName = HDR Display Max Nits
  6. OptionName = HDR_DISPLAY_MAX_NITS
  7. MinValue = 80
  8. MaxValue = 2000
  9. StepAmount = 1
  10. DefaultValue = 400
  11. [OptionRangeFloat]
  12. GUIName = Shoulder Start Alpha
  13. OptionName = AUTO_HDR_SHOULDER_START_ALPHA
  14. MinValue = 0
  15. MaxValue = 1
  16. StepAmount = 0.01
  17. DefaultValue = 0
  18. [OptionRangeFloat]
  19. GUIName = Shoulder Pow
  20. OptionName = AUTO_HDR_SHOULDER_POW
  21. MinValue = 1
  22. MaxValue = 10
  23. StepAmount = 0.05
  24. DefaultValue = 2.5
  25. [/configuration]
  26. */
  27. float luminance(float3 color)
  28. {
  29. return dot(color, float3(0.2126f, 0.7152f, 0.0722f));
  30. }
  31. void main()
  32. {
  33. float4 color = Sample();
  34. // Nothing to do here, we are in SDR
  35. if (!OptionEnabled(hdr_output) || !OptionEnabled(linear_space_output))
  36. {
  37. SetOutput(color);
  38. return;
  39. }
  40. const float hdr_paper_white = hdr_paper_white_nits / hdr_sdr_white_nits;
  41. // Restore the original SDR (0-1) brightness (we might or might not restore it later)
  42. color.rgb /= hdr_paper_white;
  43. // Find the color luminance (it works better than average)
  44. float sdr_ratio = luminance(color.rgb);
  45. const float auto_hdr_max_white = max(HDR_DISPLAY_MAX_NITS / (hdr_paper_white_nits / hdr_sdr_white_nits), hdr_sdr_white_nits) / hdr_sdr_white_nits;
  46. if (sdr_ratio > AUTO_HDR_SHOULDER_START_ALPHA && AUTO_HDR_SHOULDER_START_ALPHA < 1.0)
  47. {
  48. const float auto_hdr_shoulder_ratio = 1.0 - (max(1.0 - sdr_ratio, 0.0) / (1.0 - AUTO_HDR_SHOULDER_START_ALPHA));
  49. const float auto_hdr_extra_ratio = pow(auto_hdr_shoulder_ratio, AUTO_HDR_SHOULDER_POW) * (auto_hdr_max_white - 1.0);
  50. const float auto_hdr_total_ratio = sdr_ratio + auto_hdr_extra_ratio;
  51. color.rgb *= auto_hdr_total_ratio / sdr_ratio;
  52. }
  53. color.rgb *= hdr_paper_white;
  54. SetOutput(color);
  55. }