Search Unity

Geometry Shader Input: GL_TRIANGLES_ADJACENCY

Discussion in 'Shaders' started by Polytopes, Apr 18, 2014.

  1. Polytopes

    Polytopes

    Joined:
    Apr 18, 2014
    Posts:
    4
    Hello,

    I wondering if we can use other geometry input than point and triangle.
    In my case, I try to implementate a technic that I read in order to extrude vertices depending on the silhouette. It has to use GL_TRIANGLES_ADJACENCY, in order to determine the normal of adjacent faces. I tried with several different semantics but it has been a failed each time.
    If anyone could give me a clue, I thank you in advance.

    I searched a lot on internet, but it seems that there are not a lot of informations about geometry shaders used on unity.

    Here is my last try (Shader warning in 'Custom/Billboard': Program 'VS_Main', unrecognized identifier 'triangles_adjacency' (compiling for d3d11) at line 35):
    Code (csharp):
    1.  
    2. // Geometry Shader -----------------------------------------------------
    3. [maxvertexcount(4)]
    4. void GS_Main(triangles_adjacency GS_INPUT p[6], inout TriangleStream<FS_INPUT> triStream)
    5. {
    6. ...
    7. }
     
  2. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Geometry shaders in Unity are currently only HLSL style Geometry shaders, as far as I'm aware, so their input might be different than what you expect.
    You can choose from the following primitive types:
    • point
    • line
    • triangle
    • lineadj
    • triangleadj
    DataType is a regular input struct that you might also see for fragment and vertex shaders.
     
  3. Polytopes

    Polytopes

    Joined:
    Apr 18, 2014
    Posts:
    4
    Hello RC-1290

    Thank you for your fast answer, it's very interesting, because since I started to program shaders on unity, I've always believed that the #CG shaders in unity was based more on CgFX than HLSL, although in all cases, it's still very different from the two.
    I'll try what you suggested tonight and I'll let you updated about the result :)

    Thank you.
     
  4. Polytopes

    Polytopes

    Joined:
    Apr 18, 2014
    Posts:
    4
    Hello,

    I investigate tonight about what RC-1290 wrote, but I'm deceipt because after debbugging, the triangleadj seems to feed data only in the vertices 0, 1 and 2. The other vertices are just 0; therefore, it doesn't send adjacency information.
    Here is my shader code to verify what I say, and I attached a screen shot of the result (vertices 0, 2 and 4 should be the main triangle of the triangleadj):

    Code (csharp):
    1. Shader "Custom/ShaderTest02"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             Tags { "RenderType"="Opaque" }
    13.             LOD 200
    14.        
    15.             CGPROGRAM
    16.                 #pragma target 5.0
    17.                 #pragma vertex VS_Main
    18.                 #pragma fragment FS_Main
    19.                 #pragma geometry GS_Main
    20.                 #include "UnityCG.cginc"
    21.  
    22.                 // **************************************************************
    23.                 // Data structures                                              *
    24.                 // **************************************************************
    25.                 struct appdata
    26.                 {
    27.                     float4 vertex : POSITION;
    28.                     float2 texcoord1 : TEXCOORD0;
    29.                     float3 normal : NORMAL;
    30.                 };
    31.  
    32.                 struct GS_INPUT
    33.                 {
    34.                     float4  pos : POSITION;
    35.                     float3  normal : NORMAL;
    36.                     float2 texcoord1 : TEXCOORD0;
    37.                 };
    38.  
    39.                 struct FS_INPUT
    40.                 {
    41.                     float4 pos : POSITION;
    42.                     float2 uv1 : TEXCOORD0;
    43.                 };
    44.  
    45.  
    46.                 // **************************************************************
    47.                 // Vars
    48.                 // **************************************************************
    49.                 sampler2D _MainTex;
    50.                
    51.                 // **************************************************************
    52.                 // Shader Programs
    53.                 // **************************************************************
    54.  
    55.                 // Vertex Shader ------------------------------------------------
    56.                 GS_INPUT VS_Main(appdata_base v)
    57.                 {
    58.                     GS_INPUT output = (GS_INPUT)0;
    59.  
    60.                     output.pos =  mul(_Object2World, v.vertex);
    61.                     output.normal = v.normal;
    62.                     output.texcoord1 = v.texcoord;
    63.  
    64.                     return output;
    65.                 }
    66.                
    67.                 // Geometry Shader -----------------------------------------------------
    68.                 [maxvertexcount(3)]
    69.                 void GS_Main(triangleadj GS_INPUT p[6], inout TriangleStream<FS_INPUT> triStream)
    70.                 {
    71.                     FS_INPUT pIn;
    72.                    
    73.                     float4x4 vp = mul(UNITY_MATRIX_MVP, _World2Object);
    74.                     pIn.pos = mul(vp, p[0].pos);
    75.                     pIn.uv1 = p[0].texcoord1;
    76.                     triStream.Append(pIn);
    77.                    
    78.                     pIn.pos = mul(vp, p[2].pos);
    79.                     pIn.uv1 = p[1].texcoord1;
    80.                     triStream.Append(pIn);
    81.                    
    82.                     pIn.pos = mul(vp, p[4].pos);
    83.                     pIn.uv1 = p[2].texcoord1;
    84.                     triStream.Append(pIn);
    85.                     triStream.RestartStrip();
    86.                 }
    87.  
    88.  
    89.  
    90.                 // Fragment Shader -----------------------------------------------
    91.                 float4 FS_Main(FS_INPUT i) : COLOR
    92.                 {
    93.                     float4 c = tex2D (_MainTex, i.uv1);
    94.                     return c;
    95.                 }
    96.  
    97.             ENDCG
    98.         }
    99.     }
    100. }
    101.  
    $BugGeomShader.jpg
     
  5. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
  6. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Well... I mean, it's not supported by default. I guess you could always go deeper.
     
  7. Polytopes

    Polytopes

    Joined:
    Apr 18, 2014
    Posts:
    4
    Thanks RC-1290. I'll investigate for a way to build myself the data, it's a bit sad because it'll take a bit of process while it shouldn't because it could be taken in charge by the pipeline. However, it shouldn't be so difficult to build my own data by using a script C#.
    I guess that it'll be one of improvements for the next versions, but in the mean time, the Geometry Shaders aren't yet assumed as a default feature on all graphic cards and all different supports as cell phones, therefore, I guess that it's not a priority.

    Thanks for your time :)
     
  8. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Yeah, I believe they're still looking for a way to make these 'new' features work on both DirectX and OpenGL using the same shader, because most solutions really only handle DX9-level stuff. Aras recently posted another blog post about this.