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

[Android]The same fragment code has a significant precision difference when executed on iOS and Android. #3329

Open
BobliiExp opened this issue Jul 27, 2024 · 0 comments

Comments

@BobliiExp
Copy link

Describe the bug
My bgfx api version: #define BGFX_API_VERSION UINT32_C(98)
This year, I used the fragment code from the 2019 iOS-bgfx project in an Android project and discovered some anomalies. These included aliasing issues when offsetting textures and precision issues with linear interpolation during image convolution operations.

Here are the issues and effects observed on Android devices.

  • aliasing issues
    Effect result on iOS is:
image

Effect result on Android is:
image

The fragment code adapts the input texture to the specified area of the output texture, involving only UV calculations. I suspect the issue is due to insufficient precision of the UV values (it seems bgfx only supports medium precision). It could also be due to anomalies in texture linear interpolation.

Here is my UV handling code.

vec4 calCoord_Normal(vec2 p, vec4 viewSize, vec4 layerSize, vec4 canvasSize, float angle, vec4 center)
{
    vec4 ret = vec4(p.x, p.y, 0., 0.);
    // Normalized Local Rendering Experiment
    // i_texcoord0 is calculated relative to the width and height of viewRect, so it needs to be converted to a normalized ratio in canvasRect.
    vec2 normal_ratio = canvasSize.zw / viewSize.zw;
    // Convert the UV values to the aspect ratio of canvasRect.
    vec2 uv = p * normal_ratio;
    uv += viewSize.xy;
    float insideCanvas = step(0., uv.x) * step(uv.x, 1.) * step(0., uv.y) * step(uv.y, 1.);
    ret.z = insideCanvas;
    // Convert the UV coordinates to the layer coordinate system.
    vec2 layer_origin = layerSize.xy;
    uv = uv - layer_origin;
    
    // Rotation
    uv = rotate_Normal(uv, angle, center.xy, layerSize.w/layerSize.z);
    float insideLayer = step(0., uv.x) * step(uv.x, 1.) * step(0., uv.y) * step(uv.y, 1.);
    ret.w = insideLayer;
    ret.xy = uv;
    return ret;
}
  • Image convolution issues:
    Effect result on iOS is:
image

Effect result on Android is:
image

I tried adding precision highp float; to the fragment code to increase float precision, but shaderc reported a compilation error. I also attempted to specify precision in the fragment value type with , but neither approach worked.

I also tried setting texture sampling interpolation flags in the “bgfx.setTexture” 、“bgfx::createTexture2D” functions, such as BGFX_SAMPLER_MAG_ANISOTROPIC and BGFX_SAMPLER_MIN_ANISOTROPIC. However, I did not find any flags for bilinear or trilinear interpolation. None of these attempts resolved the anomaly in the results.

I urgently need your help and look forward to your response. @bkaradzic

@BobliiExp BobliiExp changed the title The same fragment code has a significant precision difference when executed on iOS and Android. [Android]The same fragment code has a significant precision difference when executed on iOS and Android. Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant