【问题标题】:WebGL: does OES_texture_float require storage as 32-bit floats? Some devices seem to store only as half floatsWebGL:OES_texture_float 是否需要存储为 32 位浮点数?有些设备似乎只存储半个浮点数
【发布时间】:2016-11-08 01:00:46
【问题描述】:

如果设备支持 OES_texture_float,则 FLOAT 纹理应存储为 32 位浮点值,according to the spec。但是,在某些设备上,纹理似乎存储为半浮点数。

以下代码创建一个包含值 Pi 的 1x1 浮点纹理。片段着色器对纹理进行采样,并将结果与​​ Pi 的 32 位浮点和 16 位浮点(即半浮点)表示进行比较。着色器为 32 位返回绿色,为 16 位返回红色。

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<canvas width="100" height="100"></canvas>
<script>
    "use strict";
    const canvas = document.getElementsByTagName("canvas")[0];
    const gl = canvas.getContext("webgl");
    if (!gl.getExtension("OES_texture_float")) {
        throw new Error("float textures not supported");
    }
    const program = gl.createProgram();
    function addShader(type, source) {
        const shader = gl.createShader(type);
        gl.shaderSource(shader, source);
        gl.compileShader(shader);
        gl.attachShader(program, shader);
    }
    addShader(gl.VERTEX_SHADER, `
        precision highp float;
        attribute vec2 a_Position;
        attribute vec2 a_TexCoord;
        varying vec2 v_TexCoord;
        void main() {
            gl_Position = vec4(a_Position, 0.0, 1.0);
            v_TexCoord = a_TexCoord;
        }
    `);
    addShader(gl.FRAGMENT_SHADER, `
        precision highp float;
        uniform sampler2D u_Tex;
        void main() {
            float v = texture2D(u_Tex, vec2(0.5)).r;
            gl_FragColor = vec4(
                float(v == 3.140625),   // Pi as float16
                float(v == 3.1415927),  // Pi as float32
                0.0,
                1.0);
        }
    `);
    gl.linkProgram(program);
    gl.useProgram(program);

    function createAttrib(name, data) {
        gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
        gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
        const loc = gl.getAttribLocation(program, name);
        gl.enableVertexAttribArray(loc);
        gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
    }
    createAttrib("a_Position", new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]));
    createAttrib("a_TexCoord", new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]));

    gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, 1, 1, 0, gl.LUMINANCE, gl.FLOAT, new Float32Array([Math.PI]));
    gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    gl.uniform1i(gl.getUniformLocation(program, "u_Tex"), 0);

    gl.drawArrays(gl.TRIANGLES, 0, 6);
</script>
</body>
</html>

代码可以在这里运行:https://jsfiddle.net/a6bfL168/2/

在 iPhone 5 和 iPad Air 上测试时,两者都显示为绿色。 在 iPhone 6 和 iPhone SE 上测试时,两者都显示为红色。 (注意:所有运行 iOS 10 的设备。)

如果你用不同的值重复这个测试,比如 100000,那么采样的 texel 的值为 65504,这是半浮点数的最大可表示整数;另一个证据是纹理存储(或采样)为半浮点数。

这是一个错误吗?这会是 GPU 硬件的限制吗?我是misreading the spec吗?似乎规范的意图不仅是接受浮动纹理,而且还要以全精度存储。

【问题讨论】:

    标签: ios opengl-es glsl webgl


    【解决方案1】:

    sampler2D 类型的默认精度是 lowp;尝试更改采样器精度:

    uniform highp sampler2D u_Tex;
    

    请参阅此处的第 4.7.4 节(默认精度限定符):

    【讨论】:

    • 耶!那解决了它。我原以为顶部的precision highp float; 声明也涵盖了sampler2D,但现在你指出了,很明显它们是不同的类型。谢谢!
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2013-04-12
    • 2021-08-05
    相关资源
    最近更新 更多