Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Phani2100 · Feb 26, 2020 at 05:11 AM · vrstrange iocgeometry shader

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by chulini · Apr 08, 2021 at 09:51 AM

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
         }
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 8bitgoose · Nov 14, 2021 at 04:53 AM 0
Share

For those who are using URP, works great, you just have to replace

UNITY_INITIALIZE_OUTPUT(g2f, o)

with

ZERO_INITIALIZE(g2f, o)

They just redid how to zero out a struct.

avatar image
0

Answer by Markus_T · Jan 18 at 07:45 PM

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
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Geometry Shader doesn't work well on Oculus Quest 0 Answers

External Videosource via Blackmagic 0 Answers

Applying Image Post-Processing Effects to Right Eye 1 Answer

Cardboard: How to get Head / Camera to inherit rotation from parent object? 1 Answer

Unable to find libaudiopluginvrunity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges