Skip to content

Commit

Permalink
Added PBR-neutral tone mapping (close #247)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 22, 2024
1 parent 6cc5e6e commit c3f7e73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Components/src/ToneMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool ToneMappingUpdateUI(HLSL::ToneMappingAttribs& Attribs, float* AverageLogLum
{
bool AttribsChanged = false;
{
std::array<const char*, 10> ToneMappingMode{};
std::array<const char*, TONE_MAPPING_MODE_COUNT> ToneMappingMode{};
ToneMappingMode[TONE_MAPPING_MODE_NONE] = "None";
ToneMappingMode[TONE_MAPPING_MODE_EXP] = "Exp";
ToneMappingMode[TONE_MAPPING_MODE_REINHARD] = "Reinhard";
Expand All @@ -97,6 +97,8 @@ bool ToneMappingUpdateUI(HLSL::ToneMappingAttribs& Attribs, float* AverageLogLum
ToneMappingMode[TONE_MAPPING_ADAPTIVE_LOG] = "Adaptive log";
ToneMappingMode[TONE_MAPPING_AGX] = "AgX";
ToneMappingMode[TONE_MAPPING_AGX_CUSTOM] = "AgX Custom";
ToneMappingMode[TONE_MAPPING_PBR_NEUTRAL] = "PBR Neutral";
static_assert(TONE_MAPPING_MODE_COUNT == 11, "Please update ToneMappingMode array");
if (ImGui::Combo("Tone Mapping Mode", &Attribs.iToneMappingMode, ToneMappingMode.data(), static_cast<int>(ToneMappingMode.size())))
AttribsChanged = true;
}
Expand Down
22 changes: 22 additions & 0 deletions Shaders/PostProcess/ToneMapping/public/ToneMapping.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
f3ToneMappedColor = AgXEotf(f3ToneMappedColor);
return f3ToneMappedColor;
}
#elif TONE_MAPPING_MODE == TONE_MAPPING_PBR_NEUTRAL
{
// https://modelviewer.dev/examples/tone-mapping
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/README.md#pbr-neutral-specification
float F90 = 0.04; // Fresnel reflection at normal incidence
float Ks = 0.8 - F90; // Highlight compression start
float Kd = 0.15; // Speed of desaturation

float x = min(f3Color.r, min(f3Color.g, f3Color.b));
float Offset = (x <= 2.0 * F90) ? (x - x * x / (4.0 * F90)) : F90;
float3 f3ToneMappedColor = f3Color - float3(Offset, Offset, Offset);

float Peak = max(f3ToneMappedColor.r, max(f3ToneMappedColor.g, f3ToneMappedColor.b));
if (Peak > Ks)
{
float NewPeak = 1.0 - (1.0 - Ks) * (1.0 - Ks) / (Peak + 1.0 - 2.0 * Ks);
f3ToneMappedColor *= NewPeak / Peak;
float g = 1.0 / (Kd * (Peak - NewPeak) + 1.0);
f3ToneMappedColor = lerp(float3(NewPeak, NewPeak, NewPeak), f3ToneMappedColor, g);
}
return f3ToneMappedColor;
}
#else
{
return f3Color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#define TONE_MAPPING_ADAPTIVE_LOG 7
#define TONE_MAPPING_AGX 8
#define TONE_MAPPING_AGX_CUSTOM 9
#define TONE_MAPPING_PBR_NEUTRAL 10
#define TONE_MAPPING_MODE_COUNT 11


struct AgXAttribs
{
Expand Down

0 comments on commit c3f7e73

Please sign in to comment.