title: How to use a depth texture in webgl2
body: <p>When you only want the <strong>color</strong> part of the frame, you can store <strong>depth</strong> in a <strong>render buffer</strong>.<br>
  This works fine for me on both <strong>webgl1</strong> and <strong>webgl2</strong>.</p>
  
  <p>When you also need to use the <strong>depth</strong> part you can't use a <strong>render buffer</strong>, you need to store it in a <strong>texture</strong>.<br>
  In <strong>webgl1</strong> you need to get an extension to support it.<br>
  This also works fine for me in <strong>webgl1</strong>.</p>
  
  <pre><code>    const gl = canvas.getContext('webgl');
      const depthTextureExtension = gl.getExtension('WEBGL_depth_texture');
      if (!depthTextureExtension) {
          alert('Depth textures not supported');
      }
  </code></pre>
  
  <p>This feature should be available by default in <strong>webgl2</strong>.<br>
  Yet when I replace the code above with this:</p>
  
  <pre><code>    const gl = canvas.getContext('webgl2');
  </code></pre>
  
  <p>My code fails to render and I can not find any explanation why.</p>
  
  <pre><code>    this.texture = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, this.texture);
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  
  
      this.depth = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, this.depth);
      checkGLErrors(gl); // OK
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);
      checkGLErrors(gl); // for webgl2: 1281 INVALID_VALUE
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  
  
      this.framebuffer = gl.createFramebuffer();
      gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
      gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
      gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, this.depth, 0);
  </code></pre>