Geometry shader in VR stereo rendering mode "Single pass instanced"

I am working on a VR project on HTC Vive. I am trying to create a line out of a point in Geometry shader. It works in Stereo rendering mode “Single pass” but does not work in “Single pass instanced” in XR Settings. Below is the simple shader content.

Shader “Unlit/StereoGeomTest”
{
Properties
{
_MainTex(“Texture”, 2D) = “white” {}
}
SubShader
{
Tags { “RenderType” = “Opaque” }
LOD 100

	Pass
	{
		CGPROGRAM
		#pragma vertex vert
		#pragma geometry geom
		#pragma fragment frag

		#include "UnityCG.cginc"

		struct appdata
		{
			float4 vertex : POSITION;
			UNITY_VERTEX_INPUT_INSTANCE_ID //Insert
		};

		struct v2g
		{
			float4 vertex : SV_POSITION;
			UNITY_VERTEX_OUTPUT_STEREO
		};

		struct g2f
		{
			float4 vertex : SV_POSITION;
			UNITY_VERTEX_OUTPUT_STEREO
		};

		sampler2D _MainTex;
		float4 _MainTex_ST;

		v2g vert(appdata v)
		{
			v2g o;
			UNITY_SETUP_INSTANCE_ID(v);
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

			o.vertex = v.vertex;
			return o;
		}

		[maxvertexcount(2)]
		void geom(point v2g i[1], inout LineStream<g2f> lineStream)
		{
			g2f p0, p1;

			float4 newVertex0 = i[0].vertex + float4(-10, 0, 0, 0);
			p0.vertex = UnityObjectToClipPos(newVertex0);
			UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i, p0);
			lineStream.Append(p0);

			float4 newVertex1 = i[0].vertex + float4(10, 0, 0, 0);
			p1.vertex = UnityObjectToClipPos(newVertex1);
			UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i, p1);
			lineStream.Append(p1);
		}

		fixed4 frag(g2f i) : SV_Target
		{
			fixed4 col = fixed4(1,1,1,1);
			return col;
		}
	ENDCG
        }
}

}

In general, I am not able to generate a new vertex and calculate clip space position in geometry shader in “Single pass instanced” stereo rendering mode. Same works fine in “Single pass” stereo rendering mode. Any idea, what is missing?,I am working on a VR project on HTC Vive with Stereo rendering mode set to “Single pass instanced” in XR settings.

The macro setup for stereo rendering of geometry shaders is as follows:

// Unity Stereo rendered Geometry shader outline
	CGPROGRAM
	#pragma vertex vert
	#pragma geometry geom
	#pragma fragment frag
	#pragma target 4.0

	struct a2v
	{
		...
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	struct v2g
	{
		...
		UNITY_VERTEX_OUTPUT_STEREO_EYE_INDEX
	};

	struct g2f
	{
		...
		UNITY_VERTEX_OUTPUT_STEREO
	};

	v2g vert(a2v v)
	{
		v2g o;
		UNITY_SETUP_INSTANCE_ID(v);
		UNITY_INITIALIZE_OUTPUT_STEREO_EYE_INDEX(o);
		...
		return o;
	}

	[maxvertexcount(...)]
	void geom(triangle v2g input[3], inout TriangleStream<g2f> triStream)
	{
		g2f vert;

		UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input[0]);
		UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(vert);
		...
		triStream.Append(vert);
		...
		triStream.Append(vert);
	}
	
	fixed4 frag(g2f i) : SV_Target
	{
		UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);  // required if doing stereo work in frag
		...
		return ...
	}
	ENDCG

Hey @Phani2100 I had a pretty similar problem. Took me a while to figure out how to do this but googling around, this forum post helped me to handle instanced geometry shaders but doesn’t deal with stereo instancing.

As there is no official documentation for stereo instancing in geometry shaders but there is for fragment shaders I ended up trying out unity macros in orders that makes sense until it worked. ¯\_(ツ)_/¯

I changed a little bit your shader in order to make it work. If I apply this shader to a quad I see two parallel horizontal white lines. Which are rendered properly in VR:

Shader "Unlit/StereoGeomTest"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

   Pass
	 {
		CGPROGRAM
		#pragma vertex vert
		#pragma geometry geom
		#pragma fragment frag
		#include "UnityCG.cginc"
		struct appdata
		{
			float4 vertex : POSITION;
			UNITY_VERTEX_INPUT_INSTANCE_ID
		};
		struct v2g
		{
			float4 vertex : SV_POSITION;
			UNITY_VERTEX_INPUT_INSTANCE_ID
		};
		struct g2f
		{
			float4 vertex : SV_POSITION;
			UNITY_VERTEX_INPUT_INSTANCE_ID
			UNITY_VERTEX_OUTPUT_STEREO
		};
		sampler2D _MainTex;
		float4 _MainTex_ST;
		v2g vert(appdata v)
		{
			v2g o;
			// set all values in the v2g o to 0.0
			UNITY_INITIALIZE_OUTPUT(v2g, o);
			// setup the instanced id to be accessed
			UNITY_SETUP_INSTANCE_ID(v);
			// copy instance id in the appdata v to the v2g o
			UNITY_TRANSFER_INSTANCE_ID(v, o);
			o.vertex = v.vertex;
			return o;
		}
		
		[maxvertexcount(2)]
		void geom(point v2g i[1], inout LineStream<g2f> lineStream)
		{
			g2f o;
			// set all values in the g2f o to 0.0
			UNITY_INITIALIZE_OUTPUT(g2f, o);
			// setup the instanced id to be accessed
			UNITY_SETUP_INSTANCE_ID(i[0]);
			// copy instance id in the v2f i[0] to the g2f o
			UNITY_TRANSFER_INSTANCE_ID(i[0], o);
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
			
			float4 newVertex0 = i[0].vertex + float4(-1, 0, 0, 0);
			o.vertex = UnityObjectToClipPos(newVertex0);
			lineStream.Append(o);
			float4 newVertex1 = i[0].vertex + float4(1, 0, 0, 0);
			o.vertex = UnityObjectToClipPos(newVertex1);
			lineStream.Append(o);
		}
		fixed4 frag(g2f i) : SV_Target
		{
			fixed4 col = fixed4(1,1,1,1);
			return col;
		}
	ENDCG
		}
	}
}