diff --git a/drivers/gles3/shaders/tonemap_inc.glsl b/drivers/gles3/shaders/tonemap_inc.glsl index ce98a62c23a..dd7df09c38a 100644 --- a/drivers/gles3/shaders/tonemap_inc.glsl +++ b/drivers/gles3/shaders/tonemap_inc.glsl @@ -86,7 +86,7 @@ vec3 tonemap_aces(vec3 color, float p_white) { // Polynomial approximation of EaryChow's AgX sigmoid curve. // x must be within the range [0.0, 1.0] -vec3 agx_default_contrast_approx(vec3 x) { +vec3 agx_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 @@ -96,25 +96,21 @@ vec3 agx_default_contrast_approx(vec3 x) { 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( - vec3(0.6274, 0.0691, 0.0164), - vec3(0.3293, 0.9195, 0.0880), - vec3(0.0433, 0.0113, 0.8956)); - // This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender. // This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses. // Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py vec3 tonemap_agx(vec3 color) { - const mat3 agx_inset_matrix = mat3( - 0.856627153315983, 0.137318972929847, 0.11189821299995, - 0.0951212405381588, 0.761241990602591, 0.0767994186031903, - 0.0482516061458583, 0.101439036467562, 0.811302368396859); + // Combined linear sRGB to linear Rec 2020 and Blender AgX inset matrices: + const mat3 srgb_to_rec2020_agx_inset_matrix = mat3( + 0.54490813676363087053, 0.14044005884001287035, 0.088827411851915368603, + 0.37377945959812267119, 0.75410959864013760045, 0.17887712465043811023, + 0.081384976686407536266, 0.10543358536857773485, 0.73224999956948382528); // Combined inverse AgX outset matrix and linear Rec 2020 to linear sRGB matrices. const mat3 agx_outset_rec2020_to_srgb_matrix = mat3( - 1.9648846919172409596, -0.29937618452442253746, -0.16440106280678278299, - -0.85594737466675834968, 1.3263980951083531115, -0.23819967517076844919, - -0.10883731725048386702, -0.02702191058393112346, 1.4025007379775505276); + 1.9645509602733325934, -0.29932243390911083839, -0.16436833806080403409, + -0.85585845117807513559, 1.3264510741502356555, -0.23822464068860595117, + -0.10886710826831608324, -0.027084020983874825605, 1.402665347143271889); // LOG2_MIN = -10.0 // LOG2_MAX = +6.5 @@ -122,26 +118,32 @@ vec3 tonemap_agx(vec3 color) { const float min_ev = -12.4739311883324; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY) const float max_ev = 4.02606881166759; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY) - // Do AGX in rec2020 to match Blender. - color = LINEAR_SRGB_TO_LINEAR_REC2020 * color; + // Large negative values in one channel and large positive values in other + // channels can result in a colour that appears darker and more saturated than + // desired after passing it through the inset matrix. For this reason, it is + // best to prevent negative input values. + // This is done before the Rec. 2020 transform to allow the Rec. 2020 + // transform to be combined with the AgX inset matrix. This results in a loss + // of color information that could be correctly interpreted within the + // Rec. 2020 color space as positive RGB values, but it is less common for Godot + // to provide this function with negative sRGB values and therefore not worth + // the performance cost of an additional matrix multiplication. + // A value of 2e-10 intentionally introduces insignificant error to prevent + // log2(0.0) after the inset matrix is applied; color will be >= 1e-10 after + // the matrix transform. + color = max(color, 2e-10); - // Preventing negative values is required for the AgX inset matrix to behave correctly. - // This could also be done before the Rec. 2020 transform, allowing the transform to - // be combined with the AgX inset matrix, but doing this causes a loss of color information - // that could be correctly interpreted within the Rec. 2020 color space. - color = max(color, vec3(0.0)); - - color = agx_inset_matrix * color; + // Do AGX in rec2020 to match Blender and then apply inset matrix. + color = srgb_to_rec2020_agx_inset_matrix * 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 + // Must be clamped because agx_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); // Apply sigmoid function approximation. - color = agx_default_contrast_approx(color); + color = agx_contrast_approx(color); // Convert back to linear before applying outset matrix. color = pow(color, vec3(2.4)); @@ -149,9 +151,9 @@ vec3 tonemap_agx(vec3 color) { // Apply outset to make the result more chroma-laden and then go back to linear sRGB. color = agx_outset_rec2020_to_srgb_matrix * color; - // Simply hard clip instead of Blender's complex lusRGB.compensate_low_side. - color = max(color, vec3(0.0)); - + // Blender's lusRGB.compensate_low_side is too complex for this shader, so + // simply return the color, even if it has negative components. These negative + // components may be useful for subsequent color adjustments. return color; } diff --git a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl index c4d2f8f3219..3bb26d29d15 100644 --- a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl +++ b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl @@ -266,7 +266,7 @@ vec3 tonemap_aces(vec3 color, float white) { // Polynomial approximation of EaryChow's AgX sigmoid curve. // x must be within the range [0.0, 1.0] -vec3 agx_default_contrast_approx(vec3 x) { +vec3 agx_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 @@ -276,25 +276,21 @@ vec3 agx_default_contrast_approx(vec3 x) { 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( - vec3(0.6274, 0.0691, 0.0164), - vec3(0.3293, 0.9195, 0.0880), - vec3(0.0433, 0.0113, 0.8956)); - // This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender. // This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses. // Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py vec3 tonemap_agx(vec3 color) { - const mat3 agx_inset_matrix = mat3( - 0.856627153315983, 0.137318972929847, 0.11189821299995, - 0.0951212405381588, 0.761241990602591, 0.0767994186031903, - 0.0482516061458583, 0.101439036467562, 0.811302368396859); + // Combined linear sRGB to linear Rec 2020 and Blender AgX inset matrices: + const mat3 srgb_to_rec2020_agx_inset_matrix = mat3( + 0.54490813676363087053, 0.14044005884001287035, 0.088827411851915368603, + 0.37377945959812267119, 0.75410959864013760045, 0.17887712465043811023, + 0.081384976686407536266, 0.10543358536857773485, 0.73224999956948382528); // Combined inverse AgX outset matrix and linear Rec 2020 to linear sRGB matrices. const mat3 agx_outset_rec2020_to_srgb_matrix = mat3( - 1.9648846919172409596, -0.29937618452442253746, -0.16440106280678278299, - -0.85594737466675834968, 1.3263980951083531115, -0.23819967517076844919, - -0.10883731725048386702, -0.02702191058393112346, 1.4025007379775505276); + 1.9645509602733325934, -0.29932243390911083839, -0.16436833806080403409, + -0.85585845117807513559, 1.3264510741502356555, -0.23822464068860595117, + -0.10886710826831608324, -0.027084020983874825605, 1.402665347143271889); // LOG2_MIN = -10.0 // LOG2_MAX = +6.5 @@ -302,26 +298,32 @@ vec3 tonemap_agx(vec3 color) { const float min_ev = -12.4739311883324; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY) const float max_ev = 4.02606881166759; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY) - // Do AGX in rec2020 to match Blender. - color = LINEAR_SRGB_TO_LINEAR_REC2020 * color; + // Large negative values in one channel and large positive values in other + // channels can result in a colour that appears darker and more saturated than + // desired after passing it through the inset matrix. For this reason, it is + // best to prevent negative input values. + // This is done before the Rec. 2020 transform to allow the Rec. 2020 + // transform to be combined with the AgX inset matrix. This results in a loss + // of color information that could be correctly interpreted within the + // Rec. 2020 color space as positive RGB values, but it is less common for Godot + // to provide this function with negative sRGB values and therefore not worth + // the performance cost of an additional matrix multiplication. + // A value of 2e-10 intentionally introduces insignificant error to prevent + // log2(0.0) after the inset matrix is applied; color will be >= 1e-10 after + // the matrix transform. + color = max(color, 2e-10); - // Preventing negative values is required for the AgX inset matrix to behave correctly. - // This could also be done before the Rec. 2020 transform, allowing the transform to - // be combined with the AgX inset matrix, but doing this causes a loss of color information - // that could be correctly interpreted within the Rec. 2020 color space. - color = max(color, vec3(0.0)); - - color = agx_inset_matrix * color; + // Do AGX in rec2020 to match Blender and then apply inset matrix. + color = srgb_to_rec2020_agx_inset_matrix * 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 + // Must be clamped because agx_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); // Apply sigmoid function approximation. - color = agx_default_contrast_approx(color); + color = agx_contrast_approx(color); // Convert back to linear before applying outset matrix. color = pow(color, vec3(2.4)); @@ -329,9 +331,9 @@ vec3 tonemap_agx(vec3 color) { // Apply outset to make the result more chroma-laden and then go back to linear sRGB. color = agx_outset_rec2020_to_srgb_matrix * color; - // Simply hard clip instead of Blender's complex lusRGB.compensate_low_side. - color = max(color, vec3(0.0)); - + // Blender's lusRGB.compensate_low_side is too complex for this shader, so + // simply return the color, even if it has negative components. These negative + // components may be useful for subsequent color adjustments. return color; }