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. Dismiss Notice

How do I copy depth to the camera target in Unity's SRP?

Discussion in 'General Graphics' started by ArtyIF, Jul 19, 2019.

  1. ArtyIF

    ArtyIF

    Joined:
    Oct 13, 2014
    Posts:
    36
    (Copied from r/Unity3D because no one replied me there)

    Hello, I'm trying to copy the depth buffer to camera target after post-processing in my render pipeline, but instead it's just black. Thus, the gizmos (that I draw through UnityEditor.Handles.DrawGizmos btw) aren't affected by depth unless I disable post-processing. I render the depth buffer to a different texture for shaders to use, but that's about it. Also there's an odd thing is that when I draw depth of field (which is in second pass btw) the buffer does get copied to that texture according to frame debugger, but when I don't render it it does the same to a different effect. I'm using Unity 2019.1.9f1, by the way. Maybe it was fixed in future releases, but I'm not updating with my current internet, no thanks.

    Things I've already tried:
    1. Rendering depth into BuiltinRenderTextureType.Depth. It does nothing and instead sends a warning in console about target type 3 not existing.
    2. Not using CommandBuffer.Blit and doing it manually (render a fullscreen quad with a material). Same thing minus the second pass mystery.
    3. Copying depth with a pass in the end. No depth buffer at all.
    4. Setting a render target to a destination texture's color buffer and source's depth buffer. As expected, nothing, even though others (including the official documentation) say it works. Might be just a misunderstanding, English isn't my mother tongue.
    5. Same as 4, but instead setting a render target's depth to a camera target. Result is the same.
    6. Copying depth in every pass. Same as blitting it, including the second pass mystery.
    Could someone help me, please? I feel like I'm going crazy because of this.

    P.S.: No, I don't want to use LWRP/HDRP, I don't know how to write custom renderers for the former so I can have toon shaders and I don't know how to access lights in a shader graph so I calculate lighting myself, this is why I decided to make my own RP. If someone tells me how to do that, it would also be very nice and I might move to LWRP.

    P.P.S.: I didn't find any tutorials for post-processing with SRP online and my current system is based on researching LWRP.
     
  2. ArtyIF

    ArtyIF

    Joined:
    Oct 13, 2014
    Posts:
    36
    OK, I will probably have to rewrite the entire thing. I messed up and for whatever reason the entire image slowly turns white, similar how it does in Source games when you go out-of-bounds if you have HDR on. For now I'll restrain from doing any post-processing until this question is solved or someone has a proper tutorial on how to do post-processing in SRP.
     
  3. ArtyIF

    ArtyIF

    Joined:
    Oct 13, 2014
    Posts:
    36
    After doing more research, it turned out I was stupid. ZWrite was set to off in my copy depth shader. Setting it to on did not add a depth buffer in the camera target unfortunately (probably because it doesn't have a depth buffer to begin with?), but gizmos now work as intended, fading out if occluded and everything. This question is now solved.
     
  4. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27
    Hey! I'm facing with the same bloody issue.
    And looks like you've made some progress with it. Can you please provide some details about how exactly did you solve it? If I understand correct you copying depth buffer to BuiltinRenderTextureType.CameraTarget some where before Drawing Gizmos?
     
  5. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    What type of issue do you have? I got similar mechanic working in my srp, may be I can help.
     
  6. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27
    I'm using Custom Renderer pipeline with MRT (so I create a depth buffer to attach as render target) and the problem is that now gizmos aren't affected by depth, as the OP mentioned.

    I'm assuming that I rather need to copy my existing depth buffer to be used later when gizmos rendered, or I need to hook into one of the BuiltinRenderTextureType. When I'm trying to copy/ renderer whatever to BuiltinRenderTextureType.Depth I'm recieving "target type 3 not existing" error in console.

    Have you faced smth similar?
     
  7. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    Here is how it works for me (CustomDraw is just my Blit() analog)
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         if (camera.cameraType == CameraType.SceneView && Handles.ShouldRenderGizmos())
    3.         {
    4.             CustomDraw(BuiltinRenderTextureType.CameraTarget, matFxCopyDepth);
    5.             ProcessBuffer();
    6.  
    7.             context.DrawGizmos(camera, GizmoSubset.PreImageEffects);
    8.             context.DrawGizmos(camera, GizmoSubset.PostImageEffects);
    9.         }
    10. #endif
    Code (CSharp):
    1.         void ProcessBuffer()
    2.         {
    3.             context.ExecuteCommandBuffer(buffer);
    4.             buffer.Clear();
    5.         }
    6.  
    7.         void CustomDraw(RenderTargetIdentifier target, Material material, int pass = 0)
    8.         {
    9.             buffer.SetRenderTarget(target);
    10.             buffer.DrawProcedural(Matrix4x4.identity, material, pass, MeshTopology.Triangles, 3);
    11.         }

    DepthTexture is preliminarily set as global, so shader can use it. You probably should try
    Blit(depthTexture, BuiltinRenderTextureType.CameraTarget, copyDepthMaterial)
    depending on how your shader works.
     
    Last edited: Jan 4, 2021
  8. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27
    Unfortunately is doesn't help. I've been trying similar approach, and it doesn't seems to have any effect, so maybe there is an issue with my CopyDepth shader. Here is it:

    Are you drawing gizmos after resolving the MTR?

    Code (CSharp):
    1. Shader "Hidden/CopyDepth" {
    2.     SubShader {
    3.         Cull Off
    4.         ZWrite Off
    5.         ZTest Always
    6.  
    7.         HLSLINCLUDE
    8.         #include "../ShaderLibrary/Common.hlsl"
    9.         ENDHLSL
    10.  
    11.         Pass {
    12.             Name "Copy Depth"
    13.  
    14.             HLSLPROGRAM
    15.             #pragma target 3.5
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             TEXTURE2D (_MainTex);
    20.             SAMPLER (sampler_linear_clamp);
    21.  
    22.             struct Varyings {
    23.                 float4 vertex : SV_POSITION;
    24.                 float2 uv : VAR_FX_UV;
    25.             };
    26.  
    27.             Varyings vert (uint vertexID : SV_VertexID) {
    28.                 Varyings output;
    29.  
    30.                 output.vertex = float4(
    31.                     vertexID <= 1 ? -1.0 : 3.0,
    32.                     vertexID == 1 ? 3.0 : -1.0,
    33.                     0.0, 1.0
    34.                 );
    35.  
    36.                 output.uv = float2(
    37.                     vertexID <= 1 ? 0.0 : 2.0,
    38.                     vertexID == 1 ? 2.0 : 0.0
    39.                 );
    40.  
    41.                 if (_ProjectionParams.x < 0.0) {
    42.                     output.uv.y = 1.0 - output.uv.y;
    43.                 }
    44.                 return output;
    45.             }
    46.  
    47.             float4 frag (Varyings i) : SV_TARGET {
    48.                 float depth = SAMPLE_DEPTH_TEXTURE(_MainTex, sampler_linear_clamp, i.uv);
    49.                 return float4(depth.xxx, 1);
    50.             }
    51.  
    52.             ENDHLSL
    53.         }
    54.     }
    55. }
    56.  
     
    Last edited: Jan 5, 2021
  9. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    @Egnech It's the shader indeed. First of all, it should write to Z buffer. Frag must output float into SV_DEPTH, try this:

    Code (CSharp):
    1. Shader "Hidden/CopyDepth" {
    2.     SubShader {
    3.         Cull Off
    4.         ColorMask 0
    5.         ZWrite On
    6.         ZTest Always
    7.  
    8.         HLSLINCLUDE
    9.         #include "../ShaderLibrary/Common.hlsl"
    10.         ENDHLSL
    11.  
    12.         Pass {
    13.             Name "Copy Depth"
    14.  
    15.             HLSLPROGRAM
    16.             #pragma target 3.5
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             TEXTURE2D (_MainTex);
    21.             SAMPLER (sampler_linear_clamp);
    22.  
    23.             struct Varyings {
    24.                 float4 vertex : SV_POSITION;
    25.                 float2 uv : VAR_FX_UV;
    26.             };
    27.  
    28.             Varyings vert (uint vertexID : SV_VertexID) {
    29.                 Varyings output;
    30.  
    31.                 output.vertex = float4(
    32.                     vertexID <= 1 ? -1.0 : 3.0,
    33.                     vertexID == 1 ? 3.0 : -1.0,
    34.                     0.0, 1.0
    35.                 );
    36.  
    37.                 output.uv = float2(
    38.                     vertexID <= 1 ? 0.0 : 2.0,
    39.                     vertexID == 1 ? 2.0 : 0.0
    40.                 );
    41.  
    42.                 if (_ProjectionParams.x < 0.0) {
    43.                     output.uv.y = 1.0 - output.uv.y;
    44.                 }
    45.                 return output;
    46.             }
    47.  
    48.             float frag (Varyings i) : SV_DEPTH {
    49.                 float depth = SAMPLE_DEPTH_TEXTURE(_MainTex, sampler_linear_clamp, i.uv);
    50.                 return depth;
    51.             }
    52.  
    53.             ENDHLSL
    54.         }
    55.     }
    56. }
    57.  
     
    Egnech likes this.
  10. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27
    And it work like a charm. Thank you!
     
    AlexTorbin likes this.