Skip to content

Commit

Permalink
Merge pull request godotengine#101406 from allenwp/fix-agx-contrast-c…
Browse files Browse the repository at this point in the history
…urve

Fix AgX sigmoid contrast curve approximation
  • Loading branch information
akien-mga committed Jan 11, 2025
2 parents 65af5ae + 77ddaaa commit 62a5ea6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions drivers/gles3/shaders/tonemap_inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ vec3 tonemap_aces(vec3 color, float p_white) {
}

// Polynomial approximation of EaryChow's AgX sigmoid curve.
// In Blender's implementation, numbers could go a little bit over 1.0, so it's best to ensure
// this behaves the same as Blender's with values up to 1.1. Input values cannot be lower than 0.
// x must be within the range [0.0, 1.0]
vec3 agx_default_contrast_approx(vec3 x) {
// Generated with Excel trendline
// Input data: Generated using python sigmoid with EaryChow's configuration and 57 steps
// Additional padding values were added to give correct intersections at 0.0 and 1.0
// 6th order, intercept of 0.0 to remove an operation and ensure intersection at 0.0
vec3 x2 = x * x;
vec3 x4 = x2 * x2;
return -0.20687445 * x + 6.80888933 * x2 - 37.60519607 * x2 * x + 93.32681938 * x4 - 95.2780858 * x4 * x + 33.96372259 * x4 * x2;
return 0.021 * x + 4.0111 * x2 - 25.682 * x2 * x + 70.359 * x4 - 74.778 * x4 * x + 27.069 * x4 * x2;
}

const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(
Expand Down Expand Up @@ -135,7 +135,8 @@ vec3 tonemap_agx(vec3 color) {

// Log2 space encoding.
color = max(color, 1e-10); // Prevent log2(0.0). Possibly unnecessary.
// Must be clamped because agx_blender_default_contrast_approx may not work well with values above 1.0
// Must be clamped because agx_blender_default_contrast_approx may not work
// well with values outside of the range [0.0, 1.0]
color = clamp(log2(color), min_ev, max_ev);
color = (color - min_ev) / (max_ev - min_ev);

Expand Down
9 changes: 5 additions & 4 deletions servers/rendering/renderer_rd/shaders/effects/tonemap.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ vec3 tonemap_aces(vec3 color, float white) {
}

// Polynomial approximation of EaryChow's AgX sigmoid curve.
// In Blender's implementation, numbers could go a little bit over 1.0, so it's best to ensure
// this behaves the same as Blender's with values up to 1.1. Input values cannot be lower than 0.
// x must be within the range [0.0, 1.0]
vec3 agx_default_contrast_approx(vec3 x) {
// Generated with Excel trendline
// Input data: Generated using python sigmoid with EaryChow's configuration and 57 steps
// Additional padding values were added to give correct intersections at 0.0 and 1.0
// 6th order, intercept of 0.0 to remove an operation and ensure intersection at 0.0
vec3 x2 = x * x;
vec3 x4 = x2 * x2;
return -0.20687445 * x + 6.80888933 * x2 - 37.60519607 * x2 * x + 93.32681938 * x4 - 95.2780858 * x4 * x + 33.96372259 * x4 * x2;
return 0.021 * x + 4.0111 * x2 - 25.682 * x2 * x + 70.359 * x4 - 74.778 * x4 * x + 27.069 * x4 * x2;
}

const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(
Expand Down Expand Up @@ -315,7 +315,8 @@ vec3 tonemap_agx(vec3 color) {

// Log2 space encoding.
color = max(color, 1e-10); // Prevent log2(0.0). Possibly unnecessary.
// Must be clamped because agx_blender_default_contrast_approx may not work well with values above 1.0
// Must be clamped because agx_blender_default_contrast_approx may not work
// well with values outside of the range [0.0, 1.0]
color = clamp(log2(color), min_ev, max_ev);
color = (color - min_ev) / (max_ev - min_ev);

Expand Down

0 comments on commit 62a5ea6

Please sign in to comment.