OutputTransform.azsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <viewsrg_all.srgi>
  9. #include <Atom/Features/PostProcessing/FullscreenPixelInfo.azsli>
  10. #include <Atom/Features/PostProcessing/FullscreenVertex.azsli>
  11. #include <Atom/Features/PostProcessing/PostProcessUtil.azsli>
  12. #include <Atom/Features/PostProcessing/Aces.azsli>
  13. #include <Atom/Features/PostProcessing/Tonemap.azsli>
  14. #include <Atom/Features/PostProcessing/Shapers.azsli>
  15. #include <Atom/Features/ColorManagement/TransformColor.azsli>
  16. ShaderResourceGroup PassSrg : SRG_PerPass_WithFallback
  17. {
  18. [[input_attachment_index(0)]]
  19. SubpassInput m_framebuffer;
  20. Texture3D<float4> m_lut;
  21. Sampler LinearSampler
  22. {
  23. MinFilter = Linear;
  24. MagFilter = Linear;
  25. MipFilter = Linear;
  26. AddressU = Clamp;
  27. AddressV = Clamp;
  28. AddressW = Clamp;
  29. };
  30. // Reference white and black luminance values
  31. float2 m_cinemaLimits;
  32. // LDR color grading
  33. int m_shaperType;
  34. float m_shaperBias;
  35. float m_shaperScale;
  36. }
  37. // Option shader variable to select tone mapper feature.
  38. option enum class ToneMapperType {None, Reinhard, AcesFitted, AcesFilmic, Filmic} o_tonemapperType = ToneMapperType::None;
  39. // Option shader variable to select transfer function.
  40. option enum class TransferFunctionType {None, Gamma22, PerceptualQuantizer} o_transferFunctionType = TransferFunctionType::None;
  41. // Option to enable color grading
  42. option bool o_colorGradingEnabled = false;
  43. float3 NormalizedToCinemaLimits(const float3 inputColor)
  44. {
  45. float3 linearColor;
  46. // Scale from normalized range to the proper range
  47. linearColor.r = LinearCVToY(inputColor.r, PassSrg::m_cinemaLimits.y, PassSrg::m_cinemaLimits.x);
  48. linearColor.g = LinearCVToY(inputColor.g, PassSrg::m_cinemaLimits.y, PassSrg::m_cinemaLimits.x);
  49. linearColor.b = LinearCVToY(inputColor.b, PassSrg::m_cinemaLimits.y, PassSrg::m_cinemaLimits.x);
  50. linearColor = max(linearColor, 0.);
  51. return linearColor;
  52. }
  53. PSOutput MainPS(VSOutput IN)
  54. {
  55. PSOutput OUT;
  56. // Fetch the pixel color from the input texture
  57. float3 color = PassSrg::m_framebuffer.SubpassLoad(int3(IN.m_position.x, IN.m_position.y, 0)).rgb;
  58. #if MERGE_MANUAL_EXPOSURE
  59. // Apply manual exposure compensation
  60. color = ApplyManualExposure(color, float(ViewSrg::GetExposureValueCompensation()));
  61. #endif
  62. // Applying tone mapper.
  63. switch (o_tonemapperType)
  64. {
  65. case ToneMapperType::Reinhard:
  66. color = TonemapReinhard(AcesCg_To_LinearSrgb(color));
  67. break;
  68. case ToneMapperType::AcesFitted:
  69. color = AcesCg_To_LinearSrgb(TonemapAcesFitted(color));
  70. break;
  71. case ToneMapperType::AcesFilmic:
  72. color = TonemapAcesFilmic(AcesCg_To_LinearSrgb(color));
  73. break;
  74. case ToneMapperType::Filmic:
  75. color = TonemapFilmic(AcesCg_To_LinearSrgb(color));
  76. break;
  77. default:
  78. break;
  79. }
  80. // Aplying transfer function to go from linear srgb to gamma srgb
  81. switch (o_transferFunctionType)
  82. {
  83. case TransferFunctionType::Gamma22:
  84. color = pow(color, 1.0 / 2.2);
  85. break;
  86. case TransferFunctionType::PerceptualQuantizer:
  87. if (o_tonemapperType == ToneMapperType::Reinhard ||
  88. o_tonemapperType == ToneMapperType::AcesFitted ||
  89. o_tonemapperType == ToneMapperType::AcesFilmic)
  90. {
  91. color = NormalizedToCinemaLimits(color);
  92. }
  93. // Encode with PQ transfer function
  94. color = PerceptualQuantizerRevF3(color);
  95. break;
  96. default:
  97. break;
  98. }
  99. if(o_colorGradingEnabled)
  100. {
  101. color.rgb = ApplyShaperLookupTable(ShaperType::ShaperLinear, color, PassSrg::m_shaperBias, PassSrg::m_shaperScale, PassSrg::m_lut, PassSrg::LinearSampler);
  102. }
  103. OUT.m_color.rgb = color;
  104. OUT.m_color.w = 1;
  105. return OUT;
  106. }