From a97807e2613a82a3b6892145d6c986400ab71a37 Mon Sep 17 00:00:00 2001 From: DanielSnd Date: Tue, 6 Feb 2024 18:36:38 -0400 Subject: [PATCH] Tonemapper AGX --- drivers/gles3/shaders/tonemap_inc.glsl | 83 ++++++++++++++++++- scene/resources/environment.cpp | 4 +- scene/resources/environment.h | 2 + .../renderer_rd/shaders/effects/tonemap.glsl | 83 ++++++++++++++++++- servers/rendering_server.cpp | 2 + servers/rendering_server.h | 4 +- 6 files changed, 170 insertions(+), 8 deletions(-) diff --git a/drivers/gles3/shaders/tonemap_inc.glsl b/drivers/gles3/shaders/tonemap_inc.glsl index f8f12760ece1..8b76fd0e3c21 100644 --- a/drivers/gles3/shaders/tonemap_inc.glsl +++ b/drivers/gles3/shaders/tonemap_inc.glsl @@ -39,6 +39,10 @@ vec3 apply_color_correction(vec3 color) { #endif #endif +vec3 tonemap_reinhard(vec3 color, float p_white) { + return (p_white * color + color) / (color * p_white + p_white); +} + vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) @@ -88,8 +92,75 @@ vec3 tonemap_aces(vec3 color, float p_white) { return color_tonemapped / p_white_tonemapped; } -vec3 tonemap_reinhard(vec3 color, float p_white) { - return (p_white * color + color) / (color * p_white + p_white); +// Mean error^2: 3.6705141e-06 +vec3 agx_default_contrast_approx(vec3 x) { + vec3 x2 = x * x; + vec3 x4 = x2 * x2; + + return +15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232; +} + +vec3 agx(vec3 val, float white) { + const mat3 agx_mat = mat3( + 0.842479062253094, 0.0423282422610123, 0.0423756549057051, + 0.0784335999999992, 0.878468636469772, 0.0784336, + 0.0792237451477643, 0.0791661274605434, 0.879142973793104); + + const float min_ev = -12.47393f; + float max_ev = log2(white); + + // Input transform (inset). + val = agx_mat * val; + + // Log2 space encoding. + val = clamp(log2(val), min_ev, max_ev); + val = (val - min_ev) / (max_ev - min_ev); + + // Apply sigmoid function approximation. + val = agx_default_contrast_approx(val); + + return val; +} + +vec3 agx_eotf(vec3 val) { + const mat3 agx_mat_inv = mat3( + 1.19687900512017, -0.0528968517574562, -0.0529716355144438, + -0.0980208811401368, 1.15190312990417, -0.0980434501171241, + -0.0990297440797205, -0.0989611768448433, 1.15107367264116); + + // Inverse input transform (outset). + val = agx_mat_inv * val; + + // sRGB IEC 61966-2-1 2.2 Exponent Reference EOTF Display + // NOTE: We're linearizing the output here. Comment/adjust when + // *not* using a sRGB render target. + val = pow(val, vec3(2.2)); + + return val; +} + +vec3 agx_look_punchy(vec3 val) { + const vec3 lw = vec3(0.2126, 0.7152, 0.0722); + float luma = dot(val, lw); + + vec3 offset = vec3(0.0); + vec3 slope = vec3(1.0); + vec3 power = vec3(1.35, 1.35, 1.35); + float sat = 1.4; + + // ASC CDL. + val = pow(val * slope + offset, power); + return luma + sat * (val - luma); +} + +// Adapted from https://iolite-engine.com/blog_posts/minimal_agx_implementation +vec3 tonemap_agx(vec3 color, float white, bool punchy) { + color = agx(color, white); + if (punchy) { + color = agx_look_punchy(color); + } + color = agx_eotf(color); + return color; } // This expects 0-1 range input. @@ -111,6 +182,8 @@ vec3 srgb_to_linear(vec3 color) { #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 +#define TONEMAPPER_AGX 4 +#define TONEMAPPER_AGX_PUNCHY 5 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. @@ -121,7 +194,11 @@ vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); - } else { // TONEMAPPER_ACES + } else if (tonemapper == TONEMAPPER_ACES) { return tonemap_aces(max(vec3(0.0f), color), p_white); + } else if (tonemapper == TONEMAPPER_AGX) { + return tonemap_agx(max(vec3(0.0f), color), p_white, false); + } else { // TONEMAPPER_AGX_PUNCHY + return tonemap_agx(max(vec3(0.0f), color), p_white, true); } } diff --git a/scene/resources/environment.cpp b/scene/resources/environment.cpp index b936c2decf85..fad1fdc680fe 100644 --- a/scene/resources/environment.cpp +++ b/scene/resources/environment.cpp @@ -1206,7 +1206,7 @@ void Environment::_bind_methods() { ClassDB::bind_method(D_METHOD("get_tonemap_white"), &Environment::get_tonemap_white); ADD_GROUP("Tonemap", "tonemap_"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "tonemap_mode", PROPERTY_HINT_ENUM, "Linear,Reinhard,Filmic,ACES"), "set_tonemapper", "get_tonemapper"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "tonemap_mode", PROPERTY_HINT_ENUM, "Linear,Reinhard,Filmic,ACES,AgX,AgX Punchy"), "set_tonemapper", "get_tonemapper"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tonemap_exposure", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_tonemap_exposure", "get_tonemap_exposure"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tonemap_white", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_tonemap_white", "get_tonemap_white"); @@ -1497,6 +1497,8 @@ void Environment::_bind_methods() { BIND_ENUM_CONSTANT(TONE_MAPPER_REINHARDT); BIND_ENUM_CONSTANT(TONE_MAPPER_FILMIC); BIND_ENUM_CONSTANT(TONE_MAPPER_ACES); + BIND_ENUM_CONSTANT(TONE_MAPPER_AGX); + BIND_ENUM_CONSTANT(TONE_MAPPER_AGX_PUNCHY); BIND_ENUM_CONSTANT(GLOW_BLEND_MODE_ADDITIVE); BIND_ENUM_CONSTANT(GLOW_BLEND_MODE_SCREEN); diff --git a/scene/resources/environment.h b/scene/resources/environment.h index b01c328b50fd..b489729a6380 100644 --- a/scene/resources/environment.h +++ b/scene/resources/environment.h @@ -67,6 +67,8 @@ class Environment : public Resource { TONE_MAPPER_REINHARDT, TONE_MAPPER_FILMIC, TONE_MAPPER_ACES, + TONE_MAPPER_AGX, + TONE_MAPPER_AGX_PUNCHY, }; enum SDFGIYScale { diff --git a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl index 33ec9911074b..2b4f62d8051a 100644 --- a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl +++ b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl @@ -194,6 +194,10 @@ vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { #endif // !USE_GLOW_FILTER_BICUBIC +vec3 tonemap_reinhard(vec3 color, float white) { + return (white * color + color) / (color * white + white); +} + vec3 tonemap_filmic(vec3 color, float white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) @@ -243,8 +247,75 @@ vec3 tonemap_aces(vec3 color, float white) { return color_tonemapped / white_tonemapped; } -vec3 tonemap_reinhard(vec3 color, float white) { - return (white * color + color) / (color * white + white); +// Mean error^2: 3.6705141e-06 +vec3 agx_default_contrast_approx(vec3 x) { + vec3 x2 = x * x; + vec3 x4 = x2 * x2; + + return +15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232; +} + +vec3 agx(vec3 val, float white) { + const mat3 agx_mat = mat3( + 0.842479062253094, 0.0423282422610123, 0.0423756549057051, + 0.0784335999999992, 0.878468636469772, 0.0784336, + 0.0792237451477643, 0.0791661274605434, 0.879142973793104); + + const float min_ev = -12.47393f; + float max_ev = log2(white); + + // Input transform (inset). + val = agx_mat * val; + + // Log2 space encoding. + val = clamp(log2(val), min_ev, max_ev); + val = (val - min_ev) / (max_ev - min_ev); + + // Apply sigmoid function approximation. + val = agx_default_contrast_approx(val); + + return val; +} + +vec3 agx_eotf(vec3 val) { + const mat3 agx_mat_inv = mat3( + 1.19687900512017, -0.0528968517574562, -0.0529716355144438, + -0.0980208811401368, 1.15190312990417, -0.0980434501171241, + -0.0990297440797205, -0.0989611768448433, 1.15107367264116); + + // Inverse input transform (outset). + val = agx_mat_inv * val; + + // sRGB IEC 61966-2-1 2.2 Exponent Reference EOTF Display + // NOTE: We're linearizing the output here. Comment/adjust when + // *not* using a sRGB render target. + val = pow(val, vec3(2.2)); + + return val; +} + +vec3 agx_look_punchy(vec3 val) { + const vec3 lw = vec3(0.2126, 0.7152, 0.0722); + float luma = dot(val, lw); + + vec3 offset = vec3(0.0); + vec3 slope = vec3(1.0); + vec3 power = vec3(1.35, 1.35, 1.35); + float sat = 1.4; + + // ASC CDL. + val = pow(val * slope + offset, power); + return luma + sat * (val - luma); +} + +// Adapted from https://iolite-engine.com/blog_posts/minimal_agx_implementation +vec3 tonemap_agx(vec3 color, float white, bool punchy) { + color = agx(color, white); + if (punchy) { + color = agx_look_punchy(color); + } + color = agx_eotf(color); + return color; } vec3 linear_to_srgb(vec3 color) { @@ -258,6 +329,8 @@ vec3 linear_to_srgb(vec3 color) { #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 +#define TONEMAPPER_AGX 4 +#define TONEMAPPER_AGX_PUNCHY 5 vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. @@ -268,8 +341,12 @@ vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always o return tonemap_reinhard(max(vec3(0.0f), color), white); } else if (params.tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), white); - } else { // TONEMAPPER_ACES + } else if (params.tonemapper == TONEMAPPER_ACES) { return tonemap_aces(max(vec3(0.0f), color), white); + } else if (params.tonemapper == TONEMAPPER_AGX) { + return tonemap_agx(max(vec3(0.0f), color), white, false); + } else { // TONEMAPPER_AGX_PUNCHY + return tonemap_agx(max(vec3(0.0f), color), white, true); } } diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index 7760d8a870fe..ef4bcf4b4ea6 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -2969,6 +2969,8 @@ void RenderingServer::_bind_methods() { BIND_ENUM_CONSTANT(ENV_TONE_MAPPER_REINHARD); BIND_ENUM_CONSTANT(ENV_TONE_MAPPER_FILMIC); BIND_ENUM_CONSTANT(ENV_TONE_MAPPER_ACES); + BIND_ENUM_CONSTANT(ENV_TONE_MAPPER_AGX); + BIND_ENUM_CONSTANT(ENV_TONE_MAPPER_AGX_PUNCHY); BIND_ENUM_CONSTANT(ENV_SSR_ROUGHNESS_QUALITY_DISABLED); BIND_ENUM_CONSTANT(ENV_SSR_ROUGHNESS_QUALITY_LOW); diff --git a/servers/rendering_server.h b/servers/rendering_server.h index a48a4504cbc4..23ecb43ff7a4 100644 --- a/servers/rendering_server.h +++ b/servers/rendering_server.h @@ -1087,7 +1087,9 @@ class RenderingServer : public Object { ENV_TONE_MAPPER_LINEAR, ENV_TONE_MAPPER_REINHARD, ENV_TONE_MAPPER_FILMIC, - ENV_TONE_MAPPER_ACES + ENV_TONE_MAPPER_ACES, + ENV_TONE_MAPPER_AGX, + ENV_TONE_MAPPER_AGX_PUNCHY, }; virtual void environment_set_tonemap(RID p_env, EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white) = 0;