<p><a href="https://www.khronos.org/registry/OpenGL/specs/es/3.0/es_spec_3.0.pdf" rel="nofollow noreferrer">The spec</a> says depth textures are not filterable.</p>

<blockquote>
  <h3>3.8.13</h3>
  
  <p>...</p>
  
  <p>Using the preceding definitions, a texture is complete unless any of the following conditions hold true:</p>
  
  <ul>
  <li>The effective internal format specified for the texture arrays is a sized internal depth or depth and stencil format (see table 3.14), the value of TEXTURE_COMPARE_MODE is NONE, and either the magnification filter is not NEAREST or the minification filter is neither NEAREST nor NEAREST_-
  MIPMAP_NEAREST</li>
  </ul>
</blockquote>

CC BY-SA 4.0
2020-04-15T00:53:17.873Z
2020-04-15T00:53:17.873Z
gman
<p>With WebGL 1 we can use WEBGL_draw_buffers extension which lets us use a texture as the depth attachment on the frame buffer.</p>

<p>And we can chose to use LINEAR or NEAREST as the filtering method.</p>

<pre><code>gl.getExtension('WEBGL_draw_buffers');
gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
</code></pre>

<p>WebGL 2 doesn't require an extension for this, but as soon as I switched to WebGL 2 I noticed that nothing happens unless I use NEAREST as the filtering method.</p>

<pre><code>gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT24, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); // wont work
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); // has to be NEAREST
</code></pre>

<p>Is there something I have to do to make this possible?</p>

<p>The only workaround for this that I know of would be to draw this depth texture onto another texture, but that seems like a waste of time.</p>

CC BY-SA 4.0
2020-04-14T06:23:41.940Z
If linear is not supported then you could read 4 texels and then do bilinear interpolation of the values yourself in the shader ?
PaulHK
CC BY-SA 4.0
2020-04-14T17:33:44.220Z
@Marvin I have, nothing changed, even after I switched to `DEPTH_COMPONENT32F` it still only works with `NEAREST`. But from your tone it sounds like it should have worked. All examples I managed to find used `NEAREST`, but maybe you know some examples that don't?
zoran404
CC BY-SA 4.0
2020-04-14T17:36:51.953Z
@PaulHK Are you suggesting that 4 reads per fragment would be faster than drawing the depth texture onto another l texture that has a linear filter?
zoran404
<webgl>
Does WebGL 2 support linear depth texture sampling?
zoran404
# Does WebGL 2 support linear depth texture sampling?

(Asked by: username_0 on 2020-04-13)

With WebGL 1 we can use WEBGL\_draw\_buffers extension which lets us use a texture as the depth attachment on the frame buffer.

And we can chose to use LINEAR or NEAREST as the filtering method.

```
gl.getExtension('WEBGL_draw_buffers');
gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);

```

WebGL 2 doesn't require an extension for this, but as soon as I switched to WebGL 2 I noticed that nothing happens unless I use NEAREST as the filtering method.

```
gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT24, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); // wont work
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); // has to be NEAREST

```

Is there something I have to do to make this possible?

The only workaround for this that I know of would be to draw this depth texture onto another texture, but that seems like a waste of time.

**Tags:** webgl

> **Comments:**
> **username_1**: If linear is not supported then you could read 4 texels and then do bilinear interpolation of the values yourself in the shader ?  
> **username_0**: @Marvin I have, nothing changed, even after I switched to `DEPTH_COMPONENT32F` it still only works with `NEAREST`. But from your tone it sounds like it should have worked. All examples I managed to find used `NEAREST`, but maybe you know some examples that don't?  
> **username_0**: @username_1 Are you suggesting that 4 reads per fragment would be faster than drawing the depth texture onto another l texture that has a linear filter?  

---

## 1 answers

### Answer by username_2 on 2020-04-15. Score: 4 | Accepted answer

[The spec](https://www.khronos.org/registry/OpenGL/specs/es/3.0/es_spec_3.0.pdf) says depth textures are not filterable.

> ### 3.8.13
>
> ...
>
> Using the preceding definitions, a texture is complete unless any of the following conditions hold true:
>
> * The effective internal format specified for the texture arrays is a sized internal depth or depth and stencil format (see table 3.14), the value of TEXTURE\_COMPARE\_MODE is NONE, and either the magnification filter is not NEAREST or the minification filter is neither NEAREST nor NEAREST\_-
>   MIPMAP\_NEAREST


---

