Does depth buffer update between passes?

I have a shader that depends on the depth buffer.

I want to render the back faces and write these to the depth buffer.

Then I want to render the front faces which sample the depth buffer.

Problem is I don’t think the depth buffer is being updated between passes??

e.g. it’s like this:

pass{
    //render back faces with z-depth.
}
pass{
   //render front faces sampling screen depth buffer.
}

Will the first pass update the depth buffer or is it using a cached depth buffer?

If so, is there another way I can make it draw all the back faces, then update then get the depth buffer, then draw all the front faces?

The depth buffer is indeed updated every time something with ZWrite On is drawn. The _CameraDepthTexture does not because it is not the current depth buffer. It is a texture generated during a pre-pass of the scene that renders all of the visible shadow receiving opaque objects using their shadowcaster pass, copying the resulting depth buffer from that into the _CameraDepthTexture before rendering the scene again normally.

If you’re looking to do a volume rendering, you’d have to do that using a command buffer to render the backface depth into a render texture before the main scene renders, or you could render the depth value as the back face pass’s color and use a GrabPass() to sample it.

So there’s no way to render the first passes (back faces) before the _CameraDepthTexture is set and then rendering the second pass afterwards? I can’t think how to tell it to render the object twice.

No.

Well, yes. You can make a shadowcaster pass for your shader that only renders the back faces like your first pass does, but this has a lot of implications. The depth texture is used for the main directional light shadow receiving, so if your object is transparent the objects behind it will receive shadows as if they’re where the back faces of your object is, and any post process effects that use the depth, like SSAO, depth of field, etc. will also be doing it’s stuff using that back face depth. It also means your object will be casting shadows using its back faces, unless you disable it on the renderer component.

Well, it’s an option. I might not necessarily want shadows. Interesting idea.

I did a google search and saw another post of yours! Basically I could use two cameras, and the first camera could render the back faces and the second camera could render the front faces. I’d have to set some flag in the material to tell it which to do. Unless there was some way to get the current camera ID in the shader… probably not.

Also I had a look at this: https://docs.unity3d.com/Manual/SL-ShaderReplacement.html

Use two separate shaders & materials.

Well I got something working.

What I did was clone the mesh object. Then have a script that flips the normals.
Draw it once in the geometry queue so it puts the backfaces on the z-buffer
Then on the other I have my transparent depth-dependent shader.

I hope there’s a better way of doing it! But at least I got it working.

Twice as many meshes in memory, though.

Try a custom shadow caster pass using Cull Front. Copy the example shadow caster from here:

Thanks I’ll try it. I had trouble with Cull Front because it culled drawing the faces but still wrote the front faces to the depth buffer.