Skip to content

Commit

Permalink
Simplify linearTosRGB(sRGBToLinear(srgbIn) * k) (#592)
Browse files Browse the repository at this point in the history
* Simplify linearTosRGB(sRGBToLinear(srgbIn) * k)

On Adreno, brings performance improvement (e.g. 5FPS in this case https://mapbox.atlassian.net/browse/MAPS3D-912?focusedCommentId=138071)

* simplify sRGBToLinear(vec3 srgbIn)
  • Loading branch information
astojilj authored Jun 8, 2023
1 parent b121892 commit e2b6df8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/shaders/_prelude.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ vec3 linearTosRGB(vec3 color)
// sRGB to linear approximation
vec3 sRGBToLinear(vec3 srgbIn)
{
return vec3(pow(srgbIn.xyz, vec3(2.2)));
return pow(srgbIn, vec3(2.2));
}

// equivalent to linearTosRGB(sRGBToLinear(srgbIn) * k)
vec3 linearProduct(vec3 srgbIn, vec3 k)
{
return srgbIn * pow(k, vec3(1./2.2));
}

// Apply depth for wireframe overlay to reduce z-fighting
Expand Down
4 changes: 2 additions & 2 deletions src/shaders/_prelude_lighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ vec3 apply_lighting(vec3 color, vec3 normal, float dir_factor) {
float ambient_directional_factor = calculate_ambient_directional_factor(normal);
vec3 ambient_contrib = ambient_directional_factor * u_lighting_ambient_color;
vec3 directional_contrib = u_lighting_directional_color * dir_factor;
return linearTosRGB(sRGBToLinear(color) * (ambient_contrib + directional_contrib));
return linearProduct(color, ambient_contrib + directional_contrib);
}

vec4 apply_lighting(vec4 color, vec3 normal, float dir_factor) {
Expand All @@ -69,7 +69,7 @@ vec3 apply_lighting_ground(vec3 color) {
// Emulate sky being brighter close to the main light source
float ambient_directional_factor = calculate_ambient_directional_factor(vec3(0.0, 0.0, 1.0));
vec3 ambient_contrib = u_lighting_ambient_color * ambient_directional_factor;
return linearTosRGB(sRGBToLinear(color) * (ambient_contrib + u_lighting_directional_color * NdotL));
return linearProduct(color, ambient_contrib + u_lighting_directional_color * NdotL);
}

vec4 apply_lighting_ground(vec4 color) {
Expand Down

0 comments on commit e2b6df8

Please sign in to comment.