title: WebGL: does OES_texture_float require storage as 32-bit floats? Some devices seem to store only as half floats
body: <p>If a device supports OES_texture_float, then FLOAT textures should be stored as 32-bit float values, <a href="https://www.khronos.org/registry/gles/extensions/OES/OES_texture_float.txt" rel="nofollow noreferrer">according to the spec</a>. However, on some devices, it appears textures are stored as half floats.</p>
  
  <p>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.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;canvas width="100" height="100"&gt;&lt;/canvas&gt;
  &lt;script&gt;
      "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);
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Code can be run here: <a href="https://jsfiddle.net/a6bfL168/2/" rel="nofollow noreferrer">https://jsfiddle.net/a6bfL168/2/</a></p>
  
  <p>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.)</p>
  
  <p>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.</p>
  
  <p>Is this a bug? Would this be a limitation of the GPU hardware? Am I <a href="https://www.khronos.org/registry/webgl/extensions/OES_texture_float/" rel="nofollow noreferrer">misreading the spec</a>? Seems like the intention of the spec is to not just accept float textures, but store than with full precision as well.</p>