-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add AgX tonemapper #7236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add AgX tonemapper #7236
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b4a418
Add AgX tonemapper
bejado 5daa4b8
Add Java support
bejado 5f85dcf
Switch to Blender matrices, add AgX look
bejado 24a3773
Add Android support for look
bejado d31a5b2
More documentation
bejado 60878a7
Merge branch 'main' into bjd/agx
bejado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,6 +230,106 @@ float3 FilmicToneMapper::operator()(math::float3 x) const noexcept { | |
| return (x * (a * x + b)) / (x * (c * x + d) + e); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // AgX tone mapper | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| AgxToneMapper::AgxToneMapper(AgxToneMapper::AgxLook look) noexcept : look(look) {} | ||
| AgxToneMapper::~AgxToneMapper() noexcept = default; | ||
|
|
||
| // These matrices taken from Blender's implementation of AgX, which works with Rec.2020 primaries. | ||
| // https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBaseRec2020.py | ||
| constexpr mat3f AgXInsetMatrix { | ||
| 0.856627153315983, 0.137318972929847, 0.11189821299995, | ||
| 0.0951212405381588, 0.761241990602591, 0.0767994186031903, | ||
| 0.0482516061458583, 0.101439036467562, 0.811302368396859 | ||
| }; | ||
| constexpr mat3f AgXOutsetMatrixInv { | ||
| 0.899796955911611, 0.11142098895748, 0.11142098895748, | ||
| 0.0871996192028351, 0.875575586156966, 0.0871996192028349, | ||
| 0.013003424885555, 0.0130034248855548, 0.801379391839686 | ||
| }; | ||
| constexpr mat3f AgXOutsetMatrix { inverse(AgXOutsetMatrixInv) }; | ||
|
|
||
| // LOG2_MIN = -10.0 | ||
| // LOG2_MAX = +6.5 | ||
| // MIDDLE_GRAY = 0.18 | ||
| const float AgxMinEv = -12.47393f; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY) | ||
| const float AgxMaxEv = 4.026069f; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY) | ||
|
|
||
| // Adapted from https://iolite-engine.com/blog_posts/minimal_agx_implementation | ||
| float3 agxDefaultContrastApprox(float3 x) { | ||
| float3 x2 = x * x; | ||
| float3 x4 = x2 * x2; | ||
| float3 x6 = x4 * x2; | ||
| return - 17.86 * x6 * x | ||
| + 78.01 * x6 | ||
| - 126.7 * x4 * x | ||
| + 92.06 * x4 | ||
| - 28.72 * x2 * x | ||
| + 4.361 * x2 | ||
| - 0.1718 * x | ||
| + 0.002857; | ||
| } | ||
|
|
||
| // Adapted from https://iolite-engine.com/blog_posts/minimal_agx_implementation | ||
| float3 agxLook(float3 val, AgxToneMapper::AgxLook look) { | ||
| if (look == AgxToneMapper::AgxLook::NONE) { | ||
| return val; | ||
| } | ||
|
|
||
| const float3 lw = float3(0.2126, 0.7152, 0.0722); | ||
| float luma = dot(val, lw); | ||
|
|
||
| // Default | ||
| float3 offset = float3(0.0); | ||
| float3 slope = float3(1.0); | ||
| float3 power = float3(1.0); | ||
| float sat = 1.0; | ||
|
|
||
| if (look == AgxToneMapper::AgxLook::GOLDEN) { | ||
| slope = float3(1.0, 0.9, 0.5); | ||
| power = float3(0.8); | ||
| sat = 1.3; | ||
| } | ||
| if (look == AgxToneMapper::AgxLook::PUNCHY) { | ||
| slope = float3(1.0); | ||
| power = float3(1.35, 1.35, 1.35); | ||
| sat = 1.4; | ||
| } | ||
|
|
||
| // ASC CDL | ||
| val = pow(val * slope + offset, power); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: our existing color grading APIs let you apply a CDL. |
||
| return luma + sat * (val - luma); | ||
| } | ||
|
|
||
| float3 AgxToneMapper::operator()(float3 v) const noexcept { | ||
| // Ensure no negative values | ||
| v = max(float3(0.0), v); | ||
|
|
||
| v = AgXInsetMatrix * v; | ||
|
|
||
| // Log2 encoding | ||
| v = max(v, 1E-10); // avoid 0 or negative numbers for log2 | ||
| v = log2(v); | ||
| v = (v - AgxMinEv) / (AgxMaxEv - AgxMinEv); | ||
|
|
||
| v = clamp(v, 0, 1); | ||
|
|
||
| // Apply sigmoid | ||
| v = agxDefaultContrastApprox(v); | ||
bejado marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Apply AgX look | ||
| v = agxLook(v, look); | ||
|
|
||
| v = AgXOutsetMatrix * v; | ||
|
|
||
| // Linearize | ||
| v = pow(max(float3(0.0), v), 2.2); | ||
|
|
||
| return v; | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Display range tone mapper | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.