cambecc
cambecc

Reputation: 4184

WebGL: does OES_texture_float require storage as 32-bit floats? Some devices seem to store only as half floats

If a device supports OES_texture_float, then FLOAT textures should be stored as 32-bit float values, according to the spec. However, on some devices, it appears textures are stored as half floats.

The following code creates a 1x1 float texture containing the value Pi. The fragment shader samples the texture and compares the result with 32-bit float and 16-bit float (i.e., half float) representations of Pi. The shader returns green for 32-bit and red for 16-bit.

<!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>

Code can be run here: https://jsfiddle.net/a6bfL168/2/

When tested on an iPhone 5 and iPad Air, both show green. When tested on an iPhone 6 and iPhone SE, both show red. (Note: all devices running iOS 10.)

If you repeat this test with a different value, such as 100000, then the sampled texel has a value of 65504, which is the largest representable integer for half floats; another piece of evidence the textures are stored (or sampled) as half floats.

Is this a bug? Would this be a limitation of the GPU hardware? Am I misreading the spec? Seems like the intention of the spec is to not just accept float textures, but store than with full precision as well.

Upvotes: 2

Views: 516

Answers (1)

solidpixel
solidpixel

Reputation: 12099

The default precision for a sampler2D type is lowp; try changing your sampler precision:

uniform highp sampler2D u_Tex;

See section 4.7.4 (Default Precision Qualifiers) here:

Upvotes: 2

Related Questions