| 1 |
layout(std140) uniform TonemapData { //ubo:0 |
| 2 |
float exposure; |
| 3 |
int tonemapper; |
| 4 |
int pad; |
| 5 |
int pad2; |
| 6 |
vec4 tonemapper_params; |
| 7 |
float brightness; |
| 8 |
float contrast; |
| 9 |
float saturation; |
| 10 |
int pad3; |
| 11 |
}; |
| 12 |
|
| 13 |
// This expects 0-1 range input. |
| 14 |
vec3 linear_to_srgb(vec3 color) { |
| 15 |
//color = clamp(color, vec3(0.0), vec3(1.0)); |
| 16 |
//const vec3 a = vec3(0.055f); |
| 17 |
//return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); |
| 18 |
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html |
| 19 |
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); |
| 20 |
} |
| 21 |
|
| 22 |
// This expects 0-1 range input, outside that range it behaves poorly. |
| 23 |
vec3 srgb_to_linear(vec3 color) { |
| 24 |
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html |
| 25 |
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); |
| 26 |
} |
| 27 |
|
| 28 |
#ifdef APPLY_TONEMAPPING |
| 29 |
|
| 30 |
// Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt |
| 31 |
vec3 tonemap_reinhard(vec3 color) { |
| 32 |
float white_squared = tonemapper_params.x; |
| 33 |
vec3 white_squared_color = white_squared * color; |
| 34 |
// Equivalent to color * (1 + color / white_squared) / (1 + color) |
| 35 |
return (white_squared_color + color * color) / (white_squared_color + white_squared); |
| 36 |
} |
| 37 |
|
| 38 |
vec3 tonemap_filmic(vec3 color) { |
| 39 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 40 |
// exposure_bias: Input scale (color *= bias, env->white *= bias) to make the brightness consistent with other tonemappers. |
| 41 |
// Also useful to scale the input to the range that the tonemapper is designed for (some require very high input values). |
| 42 |
// Has no effect on the curve's general shape or visual properties. |
| 43 |
const float exposure_bias = 2.0f; |
| 44 |
const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance |
| 45 |
const float B = 0.30f * exposure_bias; |
| 46 |
const float C = 0.10f; |
| 47 |
const float D = 0.20f; |
| 48 |
const float E = 0.01f; |
| 49 |
const float F = 0.30f; |
| 50 |
|
| 51 |
vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; |
| 52 |
|
| 53 |
return color_tonemapped / tonemapper_params.x; |
| 54 |
} |
| 55 |
|
| 56 |
// Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl |
| 57 |
// (MIT License). |
| 58 |
vec3 tonemap_aces(vec3 color) { |
| 59 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 60 |
const float exposure_bias = 1.8f; |
| 61 |
const float A = 0.0245786f; |
| 62 |
const float B = 0.000090537f; |
| 63 |
const float C = 0.983729f; |
| 64 |
const float D = 0.432951f; |
| 65 |
const float E = 0.238081f; |
| 66 |
|
| 67 |
// Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` |
| 68 |
const mat3 rgb_to_rrt = mat3( |
| 69 |
vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), |
| 70 |
vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), |
| 71 |
vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); |
| 72 |
|
| 73 |
const mat3 odt_to_rgb = mat3( |
| 74 |
vec3(1.60475f, -0.53108f, -0.07367f), |
| 75 |
vec3(-0.10208f, 1.10813f, -0.00605f), |
| 76 |
vec3(-0.00327f, -0.07276f, 1.07602f)); |
| 77 |
|
| 78 |
color *= rgb_to_rrt; |
| 79 |
vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); |
| 80 |
color_tonemapped *= odt_to_rgb; |
| 81 |
|
| 82 |
return color_tonemapped / tonemapper_params.x; |
| 83 |
} |
| 84 |
|
| 85 |
// allenwp tonemapping curve; developed for use in the Godot game engine. |
| 86 |
// Source and details: https://allenwp.com/blog/2025/05/29/allenwp-tonemapping-curve/ |
| 87 |
// Input must be a non-negative linear scene value. |
| 88 |
vec3 allenwp_curve(vec3 x) { |
| 89 |
const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0 |
| 90 |
|
| 91 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 92 |
// 18% "middle gray" is perceptually 50% of the brightness of reference white. |
| 93 |
const float awp_crossover_point = 0.18; |
| 94 |
// When output_max_value and/or awp_crossover_point are no longer constant, |
| 95 |
// awp_shoulder_max can be calculated on the CPU and passed in as tonemap_e. |
| 96 |
const float awp_shoulder_max = output_max_value - awp_crossover_point; |
| 97 |
|
| 98 |
float awp_contrast = tonemapper_params.x; |
| 99 |
float awp_toe_a = tonemapper_params.y; |
| 100 |
float awp_slope = tonemapper_params.z; |
| 101 |
float awp_w = tonemapper_params.w; |
| 102 |
|
| 103 |
// Reinhard-like shoulder: |
| 104 |
vec3 s = x - awp_crossover_point; |
| 105 |
vec3 slope_s = awp_slope * s; |
| 106 |
s = slope_s * (1.0 + s / awp_w) / (1.0 + (slope_s / awp_shoulder_max)); |
| 107 |
s += awp_crossover_point; |
| 108 |
|
| 109 |
// Sigmoid power function toe: |
| 110 |
vec3 t = pow(x, vec3(awp_contrast)); |
| 111 |
t = t / (t + awp_toe_a); |
| 112 |
|
| 113 |
return mix(s, t, lessThan(x, vec3(awp_crossover_point))); |
| 114 |
} |
| 115 |
|
| 116 |
// This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender. |
| 117 |
// This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses. |
| 118 |
// Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py |
| 119 |
// Colorspace transformation source: https://www.colour-science.org:8010/apps/rgb_colourspace_transformation_matrix |
| 120 |
vec3 tonemap_agx(vec3 color) { |
| 121 |
// Input color should be non-negative! |
| 122 |
// Large negative values in one channel and large positive values in other |
| 123 |
// channels can result in a colour that appears darker and more saturated than |
| 124 |
// desired after passing it through the inset matrix. For this reason, it is |
| 125 |
// best to prevent negative input values. |
| 126 |
// This is done before the Rec. 2020 transform to allow the Rec. 2020 |
| 127 |
// transform to be combined with the AgX inset matrix. This results in a loss |
| 128 |
// of color information that could be correctly interpreted within the |
| 129 |
// Rec. 2020 color space as positive RGB values, but is often not worth |
| 130 |
// the performance cost of an additional matrix multiplication. |
| 131 |
// |
| 132 |
// Additionally, this AgX configuration was created subjectively based on |
| 133 |
// output appearance in the Rec. 709 color gamut, so it is possible that these |
| 134 |
// matrices will not perform well with non-Rec. 709 output (more testing with |
| 135 |
// future wide-gamut displays is be needed). |
| 136 |
// See this comment from the author on the decisions made to create the matrices: |
| 137 |
// https://github.com/godotengine/godot-proposals/issues/12317#issuecomment-2835824250 |
| 138 |
|
| 139 |
// Combined Rec. 709 to Rec. 2020 and Blender AgX inset matrices: |
| 140 |
const mat3 rec709_to_rec2020_agx_inset_matrix = mat3( |
| 141 |
0.544814746488245, 0.140416948464053, 0.0888104196149096, |
| 142 |
0.373787398372697, 0.754137554567394, 0.178871756420858, |
| 143 |
0.0813978551390581, 0.105445496968552, 0.732317823964232); |
| 144 |
|
| 145 |
// Combined inverse AgX outset matrix and Rec. 2020 to Rec. 709 matrices. |
| 146 |
const mat3 agx_outset_rec2020_to_rec709_matrix = mat3( |
| 147 |
1.96488741169489, -0.299313364904742, -0.164352742528393, |
| 148 |
-0.855988495690215, 1.32639796461980, -0.238183969428088, |
| 149 |
-0.108898916004672, -0.0270845997150571, 1.40253671195648); |
| 150 |
|
| 151 |
const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0 |
| 152 |
|
| 153 |
// Apply inset matrix. |
| 154 |
color = rec709_to_rec2020_agx_inset_matrix * color; |
| 155 |
|
| 156 |
// Use the allenwp tonemapping curve to match the Blender AgX curve while |
| 157 |
// providing stability across all variable dyanimc range (SDR, HDR, EDR). |
| 158 |
color = allenwp_curve(color); |
| 159 |
|
| 160 |
// Clipping to output_max_value is required to address a cyan colour that occurs |
| 161 |
// with very bright inputs. |
| 162 |
color = min(vec3(output_max_value), color); |
| 163 |
|
| 164 |
// Apply outset to make the result more chroma-laden and then go back to Rec. 709. |
| 165 |
color = agx_outset_rec2020_to_rec709_matrix * color; |
| 166 |
|
| 167 |
// Blender's lusRGB.compensate_low_side is too complex for this shader, so |
| 168 |
// simply return the color, even if it has negative components. These negative |
| 169 |
// components may be useful for subsequent color adjustments. |
| 170 |
return color; |
| 171 |
} |
| 172 |
|
| 173 |
#define TONEMAPPER_LINEAR 0 |
| 174 |
#define TONEMAPPER_REINHARD 1 |
| 175 |
#define TONEMAPPER_FILMIC 2 |
| 176 |
#define TONEMAPPER_ACES 3 |
| 177 |
#define TONEMAPPER_AGX 4 |
| 178 |
|
| 179 |
vec3 apply_tonemapping(vec3 color) { // inputs are LINEAR |
| 180 |
if (tonemapper == TONEMAPPER_LINEAR) { |
| 181 |
return color; |
| 182 |
} |
| 183 |
|
| 184 |
// Ensure color values passed to tonemappers are positive. |
| 185 |
// They can be negative in the case of negative lights, which leads to undesired behavior. |
| 186 |
color = max(vec3(0.0), color); |
| 187 |
|
| 188 |
if (tonemapper == TONEMAPPER_REINHARD) { |
| 189 |
return tonemap_reinhard(color); |
| 190 |
} else if (tonemapper == TONEMAPPER_FILMIC) { |
| 191 |
return tonemap_filmic(color); |
| 192 |
} else if (tonemapper == TONEMAPPER_ACES) { |
| 193 |
return tonemap_aces(color); |
| 194 |
} else { // TONEMAPPER_AGX |
| 195 |
return tonemap_agx(color); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
#endif // APPLY_TONEMAPPING |