Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisualShader: Add LinearToSRGB and SRGBToLinear to ColorFunc node #97388

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

tetrapod00
Copy link
Contributor

@tetrapod00 tetrapod00 commented Sep 24, 2024

Implements and closes godotengine/godot-proposals#10818.

Adds LinearToSRGB and SRGBToLinear conversions to the visual shader node ColorFunc.

godot windows editor dev x86_64_5xYhLe22OV

Implementation:
This node automatically switches it's conversion function depending on which renderer is used.

The RenderingDevice (Forward+ and Mobile Renderer) conversion functions:

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)));
}
vec3 srgb_to_linear(vec3 color) {
return mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045)));
}

The Compatibility renderer uses slightly different functions:

// This expects 0-1 range input.
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.
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);
}

If #90187 is merged, and the change in that PR is also made to the GLSL version of the linear_to_srgb and srgb_to_linear functions, then this visual shader node will need to be updated.

@tetrapod00 tetrapod00 requested review from a team as code owners September 24, 2024 01:03
Copy link
Member

@Geometror Geometror left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I wanted to suggest switching the computation based on the current renderer, but after looking at the referenced blog article, I think it would be fine to just use the compatibility version (since it's a very good approximation). Maybe it can be used for RD renderers as well. CC @clayjohn

@tetrapod00
Copy link
Contributor Author

tetrapod00 commented Sep 24, 2024

All else being equal, the visual shader node should be accurate to whatever computation is used in the current renderer (or, if it must make assumptions, favor the Forward+ and Mobile renderers).
But if the Compatibility approximation is both much faster and still very accurate, then it makes sense to use it for visual shdaer nodes, where you might be converting between color spaces repeatedly (if a node is there, it will be used!).

@tetrapod00 tetrapod00 force-pushed the visualshader-linear-srgb branch from 4c382df to 22b86ee Compare September 25, 2024 01:55
@tetrapod00
Copy link
Contributor Author

The computation now depends on the renderer, exactly matching whichever computation is used internally.

@akien-mga akien-mga requested a review from a team October 5, 2024 19:34
@QbieShay QbieShay requested a review from Geometror October 17, 2024 15:40
@tetrapod00 tetrapod00 force-pushed the visualshader-linear-srgb branch from 22b86ee to 2191df0 Compare October 19, 2024 19:18
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming accuracy, the docs are completely fine.

@Repiteo Repiteo merged commit 7faad0c into godotengine:master Nov 22, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 22, 2024

Thanks!

@tetrapod00 tetrapod00 deleted the visualshader-linear-srgb branch November 22, 2024 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VisualShader: Add LinearToSRGB and SRGBToLinear to the ColorFunc node
7 participants