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

fixes iOS alpha blending #565

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions ios/graphics/Shader/Metal/RasterShader.metal
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ rasterFragmentShader(VertexOut in [[stage_in]],
float3 brightnessMax = float3(styling[0].brightnessMax, styling[0].brightnessMax, styling[0].brightnessMax);

float gamma = styling[0].gamma;
rgb = pow(rgb, (1.0 / (gamma) ));

return float4(mix(brightnessMin, brightnessMax, rgb) * color.a, color.a);

rgb = pow(rgb, (1.0 / (gamma)));
rgb = mix(brightnessMin, brightnessMax, min(rgb / color.a, float3(1.0)));
return float4(rgb * color.a, color.a) * styling[0].opacity;
Comment on lines +63 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

The gamma correction logic on line 63 appears to be correct, as it inverses the gamma to adjust the color's luminance. However, the use of min(rgb / color.a, float3(1.0)) on line 64 is potentially problematic. Dividing rgb by color.a could lead to incorrect results when color.a is not equal to 1. This is because the RGB values are being scaled by an amount inversely proportional to the alpha value, which is not a standard approach in alpha blending and could lead to colors being incorrectly brightened when the alpha is low.

A standard approach would be to premultiply the RGB values by the alpha before applying any blending or gamma correction, ensuring that the transparency affects the color intensity appropriately.

- rgb = mix(brightnessMin, brightnessMax, min(rgb / color.a, float3(1.0)));
+ rgb = mix(brightnessMin, brightnessMax, min(rgb, float3(1.0))); // Assuming rgb is already premultiplied by alpha

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
rgb = pow(rgb, (1.0 / (gamma)));
rgb = mix(brightnessMin, brightnessMax, min(rgb / color.a, float3(1.0)));
return float4(rgb * color.a, color.a) * styling[0].opacity;
rgb = pow(rgb, (1.0 / (gamma)));
rgb = mix(brightnessMin, brightnessMax, min(rgb, float3(1.0))); // Assuming rgb is already premultiplied by alpha
return float4(rgb * color.a, color.a) * styling[0].opacity;

Copy link
Contributor

Choose a reason for hiding this comment

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

The final color is multiplied by styling[0].opacity again on line 65, which seems redundant since the alpha component color.a is already scaled by styling[0].opacity at the beginning of the function. This could result in the opacity being applied twice, which is likely not the intended effect. Consider removing the redundant multiplication by opacity.

- return float4(rgb * color.a, color.a) * styling[0].opacity;
+ return float4(rgb * color.a, color.a);

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
return float4(rgb * color.a, color.a) * styling[0].opacity;
return float4(rgb * color.a, color.a);

}
Loading