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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Resolved How to render Depth (Cull Front) in XR (VR) mode with Custom Pass

Discussion in 'High Definition Render Pipeline' started by ignarmezh, Feb 20, 2023.

  1. ignarmezh

    ignarmezh

    Joined:
    Mar 23, 2017
    Posts:
    49
    Hi!
    I created a simple command for rendering custom depth (with front culling, when we got back polygons) in Custom pass.
    I do it with this code:
    Code (CSharp):
    1. CoreUtils.SetRenderTarget(cmdList, Depth_Back_Buffer, ClearFlag.All);
    2. CustomPassUtils.DrawRenderers(ctx, CustomLayer, RenderQueueType.AllOpaque, CustomVFmaterial, 0, overrideRenderState: new RenderStateBlock(RenderStateMask.Depth){depthState = new DepthState(true, CompareFunction.LessEqual)});
    And very simple VF shader pass:
    Code (CSharp):
    1. Blend  Off
    2. ZWrite Off
    3. ZTest LEqual
    4. Cull Front
    5.  
    6. PackedVaryingsType Vert(AttributesMesh inputMesh)
    7.             {
    8.                 VaryingsType varyingsType;
    9.                
    10.                 //VR REQUIREMENT
    11.                 UNITY_SETUP_INSTANCE_ID(inputMesh);
    12.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(varyingsType);
    13.                
    14.                 varyingsType.vmesh = VertMesh(inputMesh);
    15.                 return PackVaryingsType(varyingsType);
    16.             }
    17.  
    18.             float4 Frag(PackedVaryingsToPS packedInput) : SV_Target
    19.             {  
    20.                 //VR REQUIREMENT
    21.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
    22.                 return 0;
    23.             }
    Everything work is okay, when I'm trying to use it in standart mode (without VR mock), I got texture (one slice ) and correct depth with Front culling (example with standart Cube in attached files "Not XR")
    But if I want to use XR mode (with MockHMD in Single-Pass Instanced rendering mode), it returned depth (in two eyes) with Back culling mode (example with standart Cube in attached files "XR", first slice - left eye, and second slice have a correct camera offset but anyway Back culling depth).
    Why is it happening and how can I solve it? (Unity 2022.2.3f1, HDRP 14.0.5, XR Plugin Management 4.2.1)
    If you needed to reproduce it, I can create a simple Unitypackage.
    Thanks in advance!
     

    Attached Files:

  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    227
    Hello,

    In your shader code, you have the ZWrite set to Off. If you try to write the depth with this setup it will not work.
    Other than that I don't see any problem or issue that could happen only in XR mode
     
  3. ignarmezh

    ignarmezh

    Joined:
    Mar 23, 2017
    Posts:
    49
    Thanks!
    ZWrite doesn't affect the result in any way.
    But when I created a test scene for you, I created a new shader and got the error (I have never seen it before):

    Code (CSharp):
    1. Shader error in 'Custom/VFshader': invalid subscript 'stereoTargetEyeIndexAsRTArrayIdx' at line 46 (on d3d11)
    2.  
    3. Compiling Subshader: 0, Pass: FrontFaceCullPass, Vertex program with STEREO_INSTANCING_ON
    4. Platform defines: SHADER_API_DESKTOP UNITY_ENABLE_DETAIL_NORMALMAP UNITY_ENABLE_REFLECTION_BUFFERS UNITY_LIGHTMAP_FULL_HDR UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BLENDING UNITY_SPECCUBE_BOX_PROJECTION UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS
    5. Disabled keywords: DOTS_INSTANCING_ON INSTANCING_ON SHADER_API_GLES30 UNITY_ASTC_NORMALMAP_ENCODING UNITY_COLORSPACE_GAMMA UNITY_FRAMEBUFFER_FETCH_AVAILABLE UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_HARDWARE_TIER1 UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_LIGHTMAP_DLDR_ENCODING UNITY_LIGHTMAP_RGBM_ENCODING UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_NO_DXT5nm UNITY_NO_FULL_STANDARD_SHADER UNITY_NO_SCREENSPACE_SHADOWS UNITY_PBS_USE_BRDF2 UNITY_PBS_USE_BRDF3 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_UNIFIED_SHADER_PRECISION_MODEL UNITY_VIRTUAL_TEXTURING
    6.  
    I tried to comment this line:
    Code (CSharp):
    1. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(varyingsType);
    And finally! Everything works now!

    Can you explain please why this happened so that in the future I will know the nuances.
    1) Why everything works with ZWrite Off parameter?
    2) Why I don't need UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO macro?
    Thanks in advance!
     
  4. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    227
    Hard to say without seeing the context of the effect, but as far as I know it shouldn't have worked.
    This is for VR support, if you don't plan on supporting VR then it's fine to remove it.