Merge pull request #113956 from allenwp/colour/remove-LinearToSRGB-clamping

Remove clamping from LinearToSRGB visual shader node.
This commit is contained in:
Thaddeus Crews 2026-02-27 08:49:33 -06:00
commit e42575d23f
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
7 changed files with 11 additions and 14 deletions

View file

@ -43,11 +43,10 @@
<constant name="FUNC_LINEAR_TO_SRGB" value="4" enum="Function">
Converts color from linear encoding to nonlinear sRGB encoding using the following formula:
[codeblock]
vec3 c = clamp(c, vec3(0.0), vec3(1.0));
const vec3 a = vec3(0.055f);
return mix((vec3(1.0f) + a) * pow(c.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * c.rgb, lessThan(c.rgb, vec3(0.0031308f)));
[/codeblock]
The Compatibility renderer uses a simpler formula:
The Compatibility renderer uses a simpler formula that may produce undefined behavior with negative input values:
[codeblock]
vec3 c = input;
return max(vec3(1.055) * pow(c, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
@ -59,7 +58,7 @@
vec3 c = input;
return mix(pow((c.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), c.rgb * (1.0 / 12.92), lessThan(c.rgb, vec3(0.04045)));
[/codeblock]
The Compatibility renderer uses a simpler formula:
The Compatibility renderer uses a simpler formula that behaves poorly with negative input values:
[codeblock]
vec3 c = input;
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);

View file

@ -106,13 +106,13 @@ uniform float aspect_ratio;
layout(location = 0) out vec4 frag_color;
// This expects 0-1 range input, outside that range it behaves poorly.
// This approximation expects non-negative input; negative input behaves poorly.
vec3 srgb_to_linear(vec3 color) {
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
}
// This expects 0-1 range input.
// This approximation expects non-negative input; negative input is undefined behavior.
vec3 linear_to_srgb(vec3 color) {
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));

View file

@ -43,10 +43,12 @@ layout(location = 0) out vec4 frag_color;
#define M_PI 3.14159265359
// Don't include tonemap_inc.glsl because all we want is these functions, we don't want the uniforms
// This approximation expects non-negative input; negative input is undefined behavior.
vec3 linear_to_srgb(vec3 color) {
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
}
// This approximation expects non-negative input; negative input behaves poorly.
vec3 srgb_to_linear(vec3 color) {
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
}

View file

@ -10,16 +10,15 @@ layout(std140) uniform TonemapData { //ubo:0
int pad3;
};
// This expects 0-1 range input.
// This approximation expects non-negative input; negative input is undefined behavior.
vec3 linear_to_srgb(vec3 color) {
//color = clamp(color, vec3(0.0), vec3(1.0));
//const vec3 a = vec3(0.055f);
//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)));
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
}
// This expects 0-1 range input, outside that range it behaves poorly.
// This approximation expects non-negative input; negative input behaves poorly.
vec3 srgb_to_linear(vec3 color) {
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);

View file

@ -3263,7 +3263,7 @@ String VisualShaderNodeColorFunc::generate_code(Shader::Mode p_mode, VisualShade
code += " vec3 c = " + p_input_vars[0] + ";\n";
code += " " + p_output_vars[0] + " = max(vec3(1.055) * pow(c, vec3(0.416666667)) - vec3(0.055), vec3(0.0));\n";
} else {
code += " vec3 c = clamp(" + p_input_vars[0] + ", vec3(0.0), vec3(1.0));\n";
code += " vec3 c = " + p_input_vars[0] + ";\n";
code += " const vec3 a = vec3(0.055f);\n";
code += " " + p_output_vars[0] + " = mix((vec3(1.0f) + a) * pow(c.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * c.rgb, lessThan(c.rgb, vec3(0.0031308f)));\n";
}

View file

@ -106,8 +106,6 @@ layout(set = 1, binding = 0) uniform sampler2D source_color2;
layout(location = 0) out vec4 frag_color;
vec3 linear_to_srgb(vec3 color) {
//if going to srgb, clamp from 0 to 1.
color = clamp(color, vec3(0.0), vec3(1.0));
const vec3 a = vec3(0.055f);
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)));
}
@ -179,6 +177,7 @@ void main() {
}
if (bool(params.flags & FLAG_SRGB)) {
color.rgb = linear_to_srgb(color.rgb);
color.rgb = clamp(color.rgb, vec3(0.0), vec3(1.0));
}
if (bool(params.flags & FLAG_ALPHA_TO_ONE)) {
color.a = 1.0;

View file

@ -50,8 +50,6 @@ layout(push_constant, std430) uniform Params {
params;
vec3 linear_to_srgb(vec3 color) {
//if going to srgb, clamp from 0 to 1.
color = clamp(color, vec3(0.0), vec3(1.0));
const vec3 a = vec3(0.055f);
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)));
}
@ -181,5 +179,5 @@ void main() {
#endif
imageStore(screen_buffer, screen_pos, vec4(linear_to_srgb(light), 1.0));
imageStore(screen_buffer, screen_pos, vec4(clamp(linear_to_srgb(light), vec3(0.0), vec3(1.0)), 1.0));
}