Copy depth to a buffer(textur)?

Hey guys! First of all, first post in the forums so nice to meet you! I’m planning on posting more often, specially in the graphics sub forum, so it will not be the last time you see me around here :wink:

Well, onto the question itself. I’m trying to get working some real-time boolean operations based on ray marched volumes, and it’s almost finished, but I’m facing an optimization problem I’m having trouble to solve. Since the ray marching is performed for every pixel, I’m trying to clip unnecessary pixels to keep the GPU ms as low as possible.

To clip the pixels, I compare the depth value of the terrain (previously rendered to the depth buffer) against the depth of the front and back faces of the biggest boolean (which I render by using a command buffer on AfterGBuffer). The problem is that, as I’m using deferred rendering, _CameraDepthTexture is not available until AfterGBuffer camera event has been processed (and RenderDeferred.CopyDepth happens), and when I try to access it from my shader it outputs a clearly incorrect version of it. I think this happens because I’m accessing last frame’s depth values (which were modified by last frame’s booleans), as the depth has not been copied to _CameraDepthTexture yet (right?).

So, knowing this, I was wondering if there’s a way of simulating what RenderDeferred.CopyDepth does and copy the depth buffer to a texture before doing all my other command buffers and use that one instead of _CameraDepthTexture. I tried using a Blit, but the output depth is the one of the full-screen quad used to blit (which makes complete sense). I don’t know how Unity does this, and I thought that some of you guys could maybe give me a hand on this :slight_smile:

PS: Also, when building the Command Buffer MRT on AfterGBuffer, unity gives me a warning saying that it cannot access neither BuiltinRenderTextureType.Depth, nor BuiltinRenderTextureType.ResolvedDepth… Is there a way of pointing to the depth buffer Unity is using when rendering the normal meshes’ depth?

And, just in case anyone suggest the following as a solution, I already tried doing a blit and storing the content from _CameraDepthTexture into a buffer, but it’s not as straight forward. Again, this is the value that gets output from the previous frame RenderDeferred.CopyDepth (as AfterGBuffer gets executed before this), so it’s not the correct terrain depth that I want.

PS: Sorry for the “textur” in the title of the post :stuck_out_tongue: xD

Hey man, did you ever find a solution for this?