-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
WalkthroughThe update to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChat with CodeRabbit Bot (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 2
Configuration used: CodeRabbit UI
Files selected for processing (1)
- ios/graphics/Shader/Metal/RasterShader.metal (1 hunks)
Additional comments: 1
ios/graphics/Shader/Metal/RasterShader.metal (1)
- 65-65: The final color output on line 65 multiplies the
rgb
values bycolor.a
, which is standard for alpha blending. However, it is important to ensure that thergb
values have not been premultiplied by alpha earlier in the shader. If they have been, this operation would incorrectly apply the alpha twice. Please verify that thergb
values are not premultiplied by alpha before this line.Verification successful
The search for any lines where
rgb
is premultiplied bycolor.a
in theRasterShader.metal
file did not yield any results. This suggests that there are no instances ofrgb
being premultiplied by the alpha component in the shader code prior to the line in question.Based on the information available and the results of the script, it appears that the concern raised in the review comment about
rgb
being premultiplied by alpha before the final multiplication on line 65 is unfounded.
Scripts Executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for any premultiplication of rgb values by alpha in the shader file. rg --type metal "rgb \*=.*color.a" ios/graphics/Shader/Metal/RasterShader.metalLength of output: 111
Script:
#!/bin/bash # Search for any premultiplication of rgb values by alpha in the shader file without specifying the file type. rg "rgb \*=.*color.a" ios/graphics/Shader/Metal/RasterShader.metalLength of output: 68
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; |
There was a problem hiding this comment.
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.
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; |
|
||
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; |
There was a problem hiding this comment.
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.
return float4(rgb * color.a, color.a) * styling[0].opacity; | |
return float4(rgb * color.a, color.a); |
Summary by CodeRabbit