Search Unity

No Zwrite in dx11?

Discussion in 'Shaders' started by chingwa, Feb 25, 2015.

  1. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    I'm trying to create a shader that, utimately, I can use to mask off the depth rendering of my scene below a certain y-height. I have this working exactly as expected in dx9 mode by using a horizontal plane with the following shader...
    Code (CSharp):
    1. Tags {"RenderType"="Opaque" "Queue"= "Geometry"}
    2. Fog { Mode Off }
    3. Cull Back
    4. ColorMask 0
    5. ZWrite On
    6.  
    7.  
    8. CGPROGRAM
    9. #pragma vertex vert
    10. #pragma fragment frag
    11. #include "UnityCG.cginc"
    12.  
    13.     struct v2f {
    14.         float4 pos : SV_POSITION;
    15.         float2 depth : TEXCOORD0;
    16.     };
    17.  
    18.     v2f vert (appdata_base v) {
    19.         v2f o;
    20.         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    21.         UNITY_TRANSFER_DEPTH(o.depth);
    22.         return o;
    23.     }
    24.  
    25.     half4 frag(v2f i) : COLOR {
    26.         return fixed4(0,0,0,1);
    27.     }
    28. ENDCG
    However when I use the same shader in the same scene after enabling dx11 rendering, absolutley nothing happens. dx11 isn't picking up any depth-write info from this object/shader. Does anyone here have any wisdom they can share as to why this would be the case?
     
  2. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Are you sure the depth parameter is even doing anything?
    in your vertex function
    Code (csharp):
    1. UNITY_TRANSFER_DEPTH(o.depth);
    should be used in conjunction with
    Code (csharp):
    1. UNITY_OUTPUT_DEPTH(i.depth);
    in the fragment function to write something to the depth buffer

    with colormask 0 you are writing nothing to the color channels which means that in order for that shader to even do something you must write to the depth buffer, which you aren't because you frag function does nothing, to be honest, i'm not sure how it's working in dx9

    also, shouldn't this be enough for you?
    http://wiki.unity3d.com/index.php/DepthMask
     
  3. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    Thanks for the response @kebrus

    I originally tried the UNITY_OUTPUT_DEPTH function, to no avail (again works fine in dx9, not in dx11). Then though perhaps returning a set value might help which is the result of the code in my first post, which also does nothing in dx11.

    The example depthmap shader is the first I tried, and it doesn't work properly in dx11 either.

    I only want to write to the scene depth, thus the Colormask 0 setting.

    It's just bizarre.
     
  4. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    You can always try to reconstruct what those two macros do by looking at their implementation, at least in the end you should be more aware of what it's doing and come up with a solution, you should find that information in the include files
     
  5. JakeTurner

    JakeTurner

    Unity Technologies

    Joined:
    Aug 12, 2015
    Posts:
    137
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yeah from what I can tell, this should be working. You should not need that UNITY_TRANSFER_DEPTH macro at all. Either by looking at the Unity's frame debugger, or capturing a RenderDoc frame might reveal some things.