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

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:

CoreUtils.SetRenderTarget(cmdList, Depth_Back_Buffer, ClearFlag.All);
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:

Blend  Off
ZWrite Off
ZTest LEqual
Cull Front

PackedVaryingsType Vert(AttributesMesh inputMesh)
            {
                VaryingsType varyingsType;
               
                //VR REQUIREMENT
                UNITY_SETUP_INSTANCE_ID(inputMesh);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(varyingsType);
               
                varyingsType.vmesh = VertMesh(inputMesh);
                return PackVaryingsType(varyingsType);
            }

            float4 Frag(PackedVaryingsToPS packedInput) : SV_Target
            {   
                //VR REQUIREMENT
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
                return 0;
            }

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!


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

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):

Shader error in 'Custom/VFshader': invalid subscript 'stereoTargetEyeIndexAsRTArrayIdx' at line 46 (on d3d11)

Compiling Subshader: 0, Pass: FrontFaceCullPass, Vertex program with STEREO_INSTANCING_ON
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
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

I tried to comment this line:

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!

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.