Search code examples
unity-game-engineshadervirtual-realityurp

Unity URP Blit Render Feature not rendering in Single Pass Instanced VR


My blit render feature works on the PC outside VR and in multipass rendering. But when rendering the shader in single pass instanced VR the left eye is grey and the right eye is black.

singlepass instanced vr

Here is my shader that is being used in the render feature. To keep the shader simple I've removed some code from below and only showing the relevant areas:

   struct Attributes
   {
     float4 vertex : POSITION;
     float2 uv : TEXCOORD0;

     UNITY_VERTEX_INPUT_INSTANCE_ID
   };

   struct Varyings
   {
     float2 uv : TEXCOORD0;
     float4 vertex : SV_POSITION;

     UNITY_VERTEX_INPUT_INSTANCE_ID
     UNITY_VERTEX_OUTPUT_STEREO
   };

   Varyings vert(Attributes i)
   {
     UNITY_SETUP_INSTANCE_ID(i);
     
     Varyings o = (Varyings)0;
     UNITY_TRANSFER_INSTANCE_ID(i, o);
     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
     o.vertex = TransformObjectToHClip(i.vertex.xyz);
     o.uv = UnityStereoTransformScreenSpaceTex(i.uv);
     
     return o;
   }

   half4 frag(Varyings i) : SV_Target0
   {
     UNITY_SETUP_INSTANCE_ID(i);
     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

     if (SLICE_ARRAY_INDEX == 0)
     {
       return half4(1,0,0,1);
     }
     else
     {
       return half4(0,0,1,1);
     }

   }

Here is the Render Feature code:

     public class RaymarchRenderFeature : ScriptableRendererFeature
 {
   [SerializeField] private RenderPassEvent passEvent = RenderPassEvent.AfterRenderingSkybox;
 
   private RaymarchRenderPass _renderPass;
 
   public override void Create()
   {
     _renderPass = new RaymarchRenderPass(name)
     {
       renderPassEvent = passEvent
     };
   }
 
   public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
   {
     renderer.EnqueuePass(_renderPass);
   }
 }
 
 public class RaymarchRenderPass : ScriptableRenderPass
 {
   private readonly string _profilerTag;
 
   private RenderTargetIdentifier _destination;
 
   public RaymarchRenderPass(string profilerTag)
   {
     _profilerTag = profilerTag;
   }
 
   public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
   {
     RenderTextureDescriptor descriptor = cameraTextureDescriptor;
     // descriptor.enableRandomWrite = true;
     
     cmd.GetTemporaryRT(Shader.PropertyToID("_Destination"), descriptor);
     _destination = new RenderTargetIdentifier("_Destination");
   }
 
   public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
   {
     var camera = renderingData.cameraData.camera;
     var cameraColourTexture = renderingData.cameraData.renderer.cameraColorTarget;
 
 #if UNITY_EDITOR
     if (camera.cameraType is not (CameraType.SceneView or CameraType.Game))
     {
       return;
     }
 #endif
 
     if (!Raymarch.ShouldRender())
     {
       return;
     }
 
     CommandBuffer cmd = CommandBufferPool.Get(_profilerTag);
 
     Raymarch.Material.SetMatrix(Shader.PropertyToID("_CamToWorldMatrix"), camera.cameraToWorldMatrix);
     Raymarch.UploadShaderDataInvoke();
 
     cmd.Blit(cameraColourTexture, _destination, Raymarch.Material);
     cmd.Blit(_destination, cameraColourTexture);
 
     context.ExecuteCommandBuffer(cmd);
     // cmd.Clear();
     CommandBufferPool.Release(cmd);
 
     context.Submit();
   }
 
   public override void OnCameraCleanup(CommandBuffer cmd)
   {
     cmd.ReleaseTemporaryRT(Shader.PropertyToID("_Destination"));
   }
 }

Any help would be much appreciated! If you'd like to check out the rest of the code its available on GitHub: Unity-Raymarching


Solution

  • Turns out this is a known issue, check out the issue tracker page here.

    It's fixed at the time of writing but not published to the community just yet, hopefully, this will happen sometime in the near future. For the time being there is a workaround using DrawMesh instead of a Blit call. Check out this repo on how to implement it:

    https://github.com/0lento/URP_RendererFeature_SPI_Example