replace depth of fragment by depth texture value

It seems that in the context of the ARB_depth_texture extension, some important functionality is missing: It is not possible to simply replace the current depth value of a fragment by the depth value in a currently bound depth texture.

Right now, to achieve this, I see two possibilities:

  • Use glReadPixels and glDrawPixels instead of a texture. This is slow.
  • Use DOT_PRODUT_DEPTH_REPLACE_NV mode of the NV_texture_shader extension. The first texture unit would be bound to the depth texture, which would generate luminance values. A dot product with (1,0,0) would result in the depth value, that then would be divided by the hardcoded result 1.0 of a dot product in the second texture unit.
    I have not tried the latter yet, but I suspect the generated depths would suffer from inacurracies. Apart, a second texture unit is wasted unnecessarily.

I would propose an extension ARB_depth_replace allowing the following:

At least, I would expect from NVidia’s texture shaders to contain a similar functionality, since they allow to modify a fragment’s depth anyway.


NV_texture_shader_3 has DOT_PRODUCT_AFFINE_DEPTH_REPLACE_NV which does what you are asking (i think) - it doesn’t do the divide by a second dot-product, and thus doesn’t waste a texture unit. only on gf4, though.

I want to second this suggestion. When I noticed the term depth texture
as something new in 1.4, I thought surely that it would enable a means
to efficiently “blit” my own depth values into the frame buffer, and then
was disappointed to find on a closer look that it was for some other
specialized purpose and didn’t appear to provide that functionality.
Our application combines rendered 3d objects into a terrain photo for
which we have precomputed and stored a per-pixel depth map.
glDrawPixels(xsize, ysize, GL_DEPTH_COMPONENT,
GL_UNSIGNED_SHORT, range.pix);
is too slow.

Stephen Dow

This :
is a good idea, but I would rather see something like :

Anyway I think there are some problems to take care for the sake of orthogonality.

Perhaps you can just draw your polys (via vertex programs) with an inital z-value of 0. That way, the precise depth you’re looking for will be written.