Sample depthBuffer in Eevee?

I’m making a line shader node, got it working on Cycles but I don’t know how to sample the depth prepass in EEVEE.

I was expecting this to work:

vec2 uv = get_uvs_from_view(viewPosition);
float sampled_depth = texture(depthBuffer, uv).r;

But it always returns 0.

Could someone familiar with Eevee point me in the right direction ? @fclem ?

This is complicated if you want to do it from the mesh shader. You can use maxzBuffer to read the prepass depth but this will not contain the transparent objects and will be half resolution. Moreover, your node gpu C code would need to tag the GPUMaterial with one of these:

GPU_MATFLAG_DIFFUSE
GPU_MATFLAG_GLOSSY
GPU_MATFLAG_REFRACT

If you want to do a fullscreen pass after perpass and shading you will have to setup a render pass yourself in C. This would be a much cleaner option but then you cannot have per shader parameter tweaks.

1 Like

Thank you Clèment.

I got it somewhat working, although I cheated a bit (I disabled the half resolution on the maxzbuffer).
Hopefully if I interpolate manually the half res buffer in the shader I can get good enough results.

By the way, just to be sure. Assuming a full resolution buffer and no floating point errors, in the following code reconstructed and viewPosition should be equal, right?

vec2 uv = get_uvs_from_view(viewPosition);
float sampled_depth = texture(maxzBuffer, uv).r;
vec3 reconstructed = get_view_space_from_depth(uv, sampled_depth);

float delta = abs(length(reconstructed) - length(viewPosition));

If I output delta to an emission shader I get this:

Having a fullscreen pass would be too limiting for artists IMO, and would no longer be compatible with cycles.
I’ll upload a patch to phabricator soon anyway, so we can see there what’s needed to get this into master. For now I was just focused in getting it into a working state.

Yes they should be equal. This must be float precision issues.

1 Like