Depth test against a previous pass

Hey everybody, is there a way to do depth testing against a previous pass? I need to render a mesh in such a way that backfaces are one color and front faces are the other, i do it in two passes, but passes are drawn on top of each other and they dont do any depth testing, so it causes backfaces, that shouldnt be visible, to be drawn on top of the front faces (and vice versa, if i change the order of passes). Maybe theres some other way of achieving this effect? Tried searching and couldnt find anything regarding that.

You could use stencil.

           // 1st pass
            Stencil {
                Ref 0
                Comp Always
                Pass IncrSat
            }
           Cull Back

           // 2nd pass
            Stencil {
                Ref 1
                Comp Greater
                Pass IncrSat
            }
           Cull Front

Thanks, i tried stencils, but couldnt make it work. Your code seem to do nothing as well :frowning:

I forgot to mention that i displace vertices in the shader (they are displaced exactly the same in both passes), i think thats the reason stencil buffers dont do anything

I’ve used it in a toon shader to do an outline pass after the normal pass, but I’ve not tested with displaced vertices so I can’t confirm it would still work then.

Is the material transparent? I can’t picture how you expect backfaces to be seen unless the displacement make them visible when they normally would not be…

Edit: Past my bedtime correction: Of course the outline pass in my toon shader is a displaced version of the mesh used in 1st pass, so at least in my case, it does still work. I used stencil so only the displaced edges get drawn on the back face outline pass.

Im a goddamn moron. The reason it didnt work was because i was rendering it into a renderTarget without a depth buffer. As soon as i enable it, it actually works like i wanted it by default now, without the need to use stencils. Thanks for the help!