【问题标题】:Inaccurate shader precision on android compared to webgl与 webgl 相比,android 上的着色器精度不准确
【发布时间】:2016-12-14 12:39:28
【问题描述】:

我正在尝试在 android 上制作一个单纯形噪声,基于此示例:

https://github.com/ashima/webgl-noise/blob/master/src/noise2D.glsl

转换代码并在设备上编译后,我意识到模式远非随机。我已将代码分解为最基本的部分,我仍然看到桌面(我在 http://glslsandbox.com/e 上测试这些)和移动设备之间存在很大差异,我已经明白了。

#ifdef GL_ES
precision mediump float;
#endif

#extension GL_OES_standard_derivatives : enable

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

float u_time = time;
vec2 u_resolution = resolution;

vec3 mod289(vec3 x) { 
    vec3 t = x * 0.00346020761;
    t = floor(t);
    t = t * 289.0;

    return x - t;

    //return x - floor(x * (1.0 / 289.0)) * 289.0; 
}

vec3 permute(vec3 x) { 
    vec3 t = x * 34.0;
    t = t + 1.0;
    t = t * x;
    return mod289(t);

    //return mod289(((x*34.0)+1.0)*x); 
}


void main( void ) {

    vec2 p = vec2( (gl_FragCoord.xy / u_resolution.xx - 0.5) * 2.0);

    vec3 value = permute( p.xyx * 10000.0 + u_time * 0.1);

    gl_FragColor = vec4(value, 1.0);
    //gl_FragColor = vec4(p + 1.0, 0.0, 1.0);
}

将它们分成几行的目的是我之前也遇到过这种错误。 GLSL ES fragment shader produces very different results on different devices

在设备上,此代码(#ifdef 和 #extension 已删除,统一变量设置正确)会产生白屏。 (在多个设备上进行了测试,使用 #version 300 es 并使用较旧的着色器版本,结果始终相同。)

这个错误是由于 floor() 不准确造成的吗?

或者有没有办法在没有这些 mod289 函数的情况下生成单纯形噪声? (2d 噪声运行得很好)

【问题讨论】:

    标签: android opengl-es fragment-shader simplex-noise


    【解决方案1】:

    很可能您的桌面 GPU 使用 fp32 精度进行计算(即使您在着色器中指定 mediump,它只需要 fp16)。

    如果你设置precision highp float 会更好吗?这将强制进行 fp32 浮点计算。

    但请注意,并非所有移动设备都支持 highp - 它仅在 OpenGL ES 3.x 及更高版本中成为强制要求,因此某些 OpenGL ES 2.x GPU 仅支持 fp16 计算。

    (你在什么设备上运行?)

    【讨论】:

    • 我只在低端设备上测试了highp,它是ARM Mali-400 MP2(双核)。支持es 3.0的Adreno 305更好的也支持highp,这解决了我的问题。请注意对于遇到此问题的其他人,您可以检查对精度的支持:stackoverflow.com/questions/4414041/…glGetShaderPrecisionFormat 如果不支持 highp,则返回零。
    • Mali-400 系列仅支持 OpenGL ES 2.x,不支持 fp32,因此在这种情况下,您将获得 fp16
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2014-03-24
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多