# Does WebGL 2 support linear depth texture sampling?

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.

• If linear is not supported then you could read 4 texels and then do bilinear interpolation of the values yourself in the shader ? – PaulHK Apr 14 at 6:23
• have you tried enabling the extensions OES_texture_half_float_linear and OES_texture_float_linear? – Marvin Apr 14 at 14:18
• @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 Apr 14 at 17:33
• @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 Apr 14 at 17:36
• @zoran404 not really, I just came across this question and thought it was worth a quick check before I looked any further (I've not really worked with textures yet, so this is a learning exercise for me too). – Marvin Apr 14 at 18:29