Depth textures are flipped when in VR mode


We are using URP in unity 2021.3.12f1 in flat mode our shaders work fine but in VR mode our custom "ghost" shader is misbehaving. We have narrowed it down using the frame debugger to the left eye using the right eye's depth texture and the right eye is using the left eye's depth texture. meaning that our "ghosts" are being occluded by / showing thoguh objects that are not present on each eye. (pictured in the attachment)

Is this a known issue or am I doing something oviously wrong?


Did you try 2021.3.29 to check if it was a bug?
If that doesn't work maybe share the shader if no one knows

wow apparently I didnt press send on my response Doh!

Yeah we've tested it in 2021.3.29 and its still broken there.

I've attached the Shader in question :

9199700--1283006--AlbertGhostMemoryCmdShader.shader (2.25 KB)

I'm not great with text based shaders. You could file a bug report, and/or wait for someone else to respond.
Another solution might be using stencil rendering

If the buffers are consistently swapped, then you can use the "shader_feature" keyword to create a workaround. Add this to the shader to create a varint:

#pragma shader_feature _SWAP_DEPTH_BUFFERS

Then if your main shader body you can do

#if (defined(_SWAP_DEPTH_BUFFERS)
                float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ_SWAP_DEPTH_BUFFERS(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
                float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));

Then you can search through the built-in shaders to find the definition of SAMPLE_DEPTH_TEXTURE_PROJ to create your own which just swaps the eye buffers before sampling it:

#define SAMPLE_DEPTH_TEXTURE_PROJ_SWAP_DEPTH_BUFFERS(sampler, uv) UNITY_SAMPLE_TEX2DARRAY(sampler, float3((uv).x/(uv).w, (uv).y/(uv).w, (float)(unity_StereoEyeIndex == 1 ? 0 : 1))).r

For now you can activate/deactivate the keyword to turn it on and off, and hopefully remove it entirely once the bug is fixed. Beyond that a simple repro project in a bug report would help get it fixed sooner rather than later.


Unfortunatly this doesn't work as we are using URP. I'm hoping I can copy the URP package and edit wherever it does the depth texture stuff in the way you have suggested :-)

Ah, I see. Yes, I responded based on the shader you posted which is written using the built-in includes. Using that shader with URP may explain the buffer mismatch. The docs have an example of getting the depth for URP. You will want to include:

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"

And then use SampleSceneDepth() to get the depth, and there is a good chance that will fix your left/right swap problem and not require that workaround at all.

If you are still having issues with the buffers being swapped, you will have to rewrite the workaround to modify the "UnityStereoTransformScreenSpaceTex" function. Do the same thing to swap the buffers in the "TransformStereoScreenSpaceTex" function where it calculates the offset.

float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];


float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex == 0 ? 1 : 0];

You shouldn't need to make your own copy of the URP. Just redefine the function with an appended name in your own shader or include and add a shader_feature like before to toggle using it or the base URP version.

1 Like