Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Write to Depth buffer using SV_DEPTH

Discussion in 'High Definition Render Pipeline' started by Okewii, Nov 9, 2020.

  1. Okewii

    Okewii

    Joined:
    Aug 16, 2020
    Posts:
    6
    Hi,

    I'm making a custom post-process effect that needs to overwrite the depthBuffer with another depth texture using some computations. The problem is that i cannot figure how to write to depth without using the drawRenderers command that writes to depth automaticaly. I'm using a fullscreen shader to compose my effect and i tried to output there an SV_DEPTH parameter from the fragment function with no effect on the depth buffer :
    Code (ShaderLab):
    1.  
    2. half4 frag(Varyings input, out float depth : SV_Depth) : SV_Target
    3. {
    4.      ...
    5.      depth = lerp(sobelData.y, SampleDepth(uv), s);
    6.      ...
    7. }
    8.  
    The buffer doesn't change even when binding it as the current depth Buffer in DrawFullScreen and the transparants are not renderer correctly.
    I tried to blit to it too and i couldn't either. Is there a way to write to the depthbuffer using a shader in any way ??
    This is my drawFull screen code :
    Code (CSharp):
    1. GetCameraBuffers(out source, out depthSource);
    2. HDUtils.DrawFullScreen(cmd, pixMat, source, depthSource,shaderPassId: 0,properties:compositingProperties);
     
  2. Okewii

    Okewii

    Joined:
    Aug 16, 2020
    Posts:
    6
    Still haven't found a way to write to depth in a shader if anyone has any idea i'm up for tryinf anything because the extra drawRenderers pass for depth destroys the performance.
     
  3. Armageddon104

    Armageddon104

    Joined:
    Sep 14, 2014
    Posts:
    18
    I'm also having the same issue, can't find any documentation on rendering to the depth buffer. If you want to read it and then write to it you have to do it in two passes though; so you'd have a pass in your FullScreen shader to copy depth, and then a pass to write to it, but I'm unsure how you're supposed to put that written one back to the screen/render pipeline.
     
    Okewii likes this.
  4. Okewii

    Okewii

    Joined:
    Aug 16, 2020
    Posts:
    6
    Thanks for the help ! How did you manage to write to depth in the second shader though ? Just with a normal SV_Depth like the one up there ? Or you put the depth buffer as the color buffer and go with that ?
     
  5. Okewii

    Okewii

    Joined:
    Aug 16, 2020
    Posts:
    6
    DUDE I just managed to do it just now with your advices that i think understood wrongly ! I managed to write to depth by just using two DrawFullScreen like you said but with the same shader and without outputting any useless color data (i think !) Here's my code :
    Code (CSharp):
    1.  
    2. RTHandle source;
    3. RTHandle depthSource;
    4. GetCameraBuffers(out source, out depthSource);
    5. HDUtils.DrawFullScreen(cmd, pixMat, source, shaderPassId: 0, properties: compositingProperties);
    6. HDUtils.DrawFullScreen(cmd, pixMat, depthSource,shaderPassId: 0,properties:compositingProperties);
    7.  
    and the fragment shader :
    Code (CSharp):
    1.  
    2. Blend SrcAlpha OneMinusSrcAlpha
    3. ZWrite On
    4.  
    5. half4 frag(Varyings input, out float depth : SV_Depth) : SV_Target
    6.     {
    7.         ...... computations  .....
    8.         s = lerp(1.0, s, ceil(sobelData.y - SampleDepthCam(uv)));
    9.         depth = lerp(sobelData.y, SampleDepth(uv), s);
    10.         return col;
    11.     }
    12.  
    I just tested a few things and when called with the depthBuffer as the render target the shader won't do anything as col changes and will render only the depth variable to the depth texture ! And for the normal fullscreen pass the col variable will be used to render to the screen or whatever RTHandle.
     
    Last edited: Nov 24, 2020
    sirleto and Olmi like this.