Search Unity

Geometry shader not working on Debian10 GL3.3 SM5

Discussion in 'Shaders' started by grobonom, May 31, 2022.

  1. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    Hi all !

    I got pain with geometry shaders.
    When targetting my app to linux, they simply do nothing.

    For example this one:

    Code (CSharp):
    1. Shader "My_shaders/MyStdLitmap_transition"
    2. {
    3.     Properties {
    4.         [NoScaleOffset] _MainTex ("Diffuse Texture", 2D) = "white" {}
    5.       [NoScaleOffset] _DirtLMTex ("Dirt/AO-LM (RGB-A)", 2D) = "(RGBA: .98,.98,.98,1)" {} // bidouille: presque 1 pour eviter les effets de bord....
    6.       _NightLightColor ("Lightmap color", Color) = (1,1,1,1)
    7.         [NoScaleOffset] _BumpMap ("Normal Texture", 2D) = "bump" {}
    8.  
    9.         _TransitionDist("Transition dist", Float) = 100
    10.     }
    11.     SubShader {
    12.         Pass{
    13.             Tags { "LightMode" = "ForwardBase" }
    14.             CGPROGRAM
    15.        
    16.          #pragma require geometry
    17.  
    18.          // my variants....
    19.          #pragma multi_compile_local ___ WITH_CITY_NIGHTLIGHTS
    20.  
    21.             #pragma vertex vertex_program
    22.          #pragma geometry geometry_program
    23.          #pragma fragment fragment_program
    24.  
    25.          #pragma multi_compile_fog
    26.  
    27.          #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    28.  
    29.  
    30.          #include "Lighting.cginc"
    31.          #include "AutoLight.cginc"
    32.            
    33.             // My vars
    34.             sampler2D _MainTex;
    35.             sampler2D _BumpMap;
    36.             sampler2D _DirtLMTex;
    37.          static const half _BumpDistFade=20;
    38.          static const half _BumpLengthFade=5;
    39.          static const half _TransitionColorSpread=4;
    40.          static const half _TransitionMessFactor=10;
    41.          half _TransitionDist;
    42.        
    43. #if defined(WITH_CITY_NIGHTLIGHTS)
    44.          fixed4 _NightLightColor;
    45. #endif
    46.  
    47.  
    48.             // Base Input Structs
    49.             struct APP2V {
    50.                 float4 vertex: POSITION;
    51.                 half3 normal: NORMAL;
    52.                 half4 UV0: TEXCOORD0;
    53.                 half4 UV1: TEXCOORD1;
    54.                 half4 tangent: TANGENT;
    55.             };
    56.        
    57.             struct V2G {
    58.                 float4 pos: SV_POSITION;
    59.                 half4 UV0: TEXCOORD0;
    60.                 half4 UV1: TEXCOORD1;
    61.                 half4 posWorld: TEXCOORD2;
    62.                 half3 normalWorld: TEXCOORD3;
    63.                 half3 tangentWorld: TEXCOORD4;
    64.                 half3 binormalWorld: TEXCOORD5;
    65.             UNITY_FOG_COORDS(6) // fog in 6
    66. //#if defined (SHADOWS_SCREEN)
    67. //            SHADOW_COORDS(7) // shadows data into TEXCOORD7
    68.             unityShadowCoord4 _ShadowCoord : TEXCOORD7;
    69. //#endif  
    70.    
    71.             };
    72.  
    73.             struct G2F {
    74.                 float4 pos: SV_POSITION;
    75.                 half4 UV0: TEXCOORD0;
    76.                 half4 UV1: TEXCOORD1;
    77.                 half4 posWorld: TEXCOORD2;
    78.                 half3 normalWorld: TEXCOORD3;
    79.                 half3 tangentWorld: TEXCOORD4;
    80.                 half3 binormalWorld: TEXCOORD5;
    81.             UNITY_FOG_COORDS(6) // fog in 6
    82.             unityShadowCoord4 _ShadowCoord : TEXCOORD7;
    83.             half fade_factor: COLOR0; // transition IN/OUT fade to ambient factor
    84.             };
    85.  
    86.  
    87.  
    88.  
    89.    
    90.          inline float random11(float p)
    91.          {
    92.              p = frac(p * .1031);
    93.              p *= p + 33.33;
    94.              p *= p + p;
    95.              return((frac(p)-0.5)*2); //-1 to +1
    96.          }
    97.          inline half2 random21(float p)
    98.          {
    99.             half3 p3 = frac(float3(p,p,p) * half3(.1031, .1030, .0973));
    100.             p3 += dot(p3, p3.yzx + 33.33);
    101.             return frac((p3.xx+p3.yz)*p3.zy);
    102.          }
    103.          //========================================================================
    104.  
    105.            
    106.             // Vertex program
    107.             V2G vertex_program( APP2V v )
    108.          {
    109.                 V2G o;
    110.                
    111.                 o.normalWorld = normalize( mul( float4( v.normal, 0.0 ), unity_WorldToObject ).xyz );      
    112.                 o.tangentWorld = normalize( mul( unity_ObjectToWorld, v.tangent ).xyz );
    113.                 o.binormalWorld = normalize( cross( o.normalWorld, o.tangentWorld )* v.tangent.w);
    114.                        
    115.  
    116.                 o.posWorld = mul( unity_ObjectToWorld, v.vertex);
    117.                 o.pos = v.vertex;//UnityObjectToClipPos( v.vertex);
    118.                 o.UV0 = v.UV0;
    119.                 o.UV1 = v.UV1;
    120.  
    121. #if defined (UNITY_NO_SCREENSPACE_SHADOWS)
    122.             TRANSFER_SHADOW(o)
    123. #else
    124.             o._ShadowCoord = ComputeScreenPos(UnityObjectToClipPos( v.vertex));
    125. #endif
    126.  
    127.             UNITY_TRANSFER_FOG(o,o.pos);
    128.  
    129.                 return o;
    130.             }
    131.        
    132.        
    133.          [maxvertexcount(3)]
    134.          void geometry_program(triangle V2G IN[3], inout TriangleStream<G2F> triStream)
    135.          {
    136.             G2F o;
    137.             half4 displace_effect;
    138.  
    139.             half3 a= half3(random11(IN[0].posWorld),random11(IN[1].posWorld),random11(IN[2].posWorld));
    140.  
    141.             float3 tri_in_cam = _WorldSpaceCameraPos.xyz - (IN[0].posWorld.xyz+IN[1].posWorld.xyz+IN[2].posWorld.xyz)/3;
    142.             half tri_dist = length(tri_in_cam);
    143.            
    144.  
    145.  
    146.             // quand TransitionDist < 0 on inverse le mess far/near
    147.             // idem plus bas pour le fade au blanc....
    148.             if(_TransitionDist >= 0.0)
    149.                displace_effect = half4(a.xyz*_TransitionMessFactor*clamp(sqrt(tri_dist)-_TransitionDist,0,10),0);
    150.             else
    151.                displace_effect = half4(a.xyz*_TransitionMessFactor*clamp(-_TransitionDist-sqrt(tri_dist),0,10),0);
    152.  
    153.             for(int i = 0; i < 3; i++)
    154.             {
    155.                o.posWorld=IN[i].posWorld;
    156.                o.normalWorld=IN[i].normalWorld;
    157.                o.tangentWorld=IN[i].tangentWorld;
    158.                o.binormalWorld=IN[i].binormalWorld;
    159.  
    160.                o.pos = UnityObjectToClipPos(IN[i].pos+displace_effect);
    161.  
    162.                o._ShadowCoord = IN[i]._ShadowCoord; // transfer as is shadows coming from vertex program
    163.              
    164.                UNITY_TRANSFER_FOG(o,o.pos);
    165.              
    166.                o.UV0 = IN[i].UV0;
    167.                o.UV1 = IN[i].UV1;
    168.              
    169.                if(_TransitionDist >= 0.0)
    170.                   o.fade_factor = saturate(length(_TransitionMessFactor*sqrt(tri_dist)-_TransitionDist)/30-(_TransitionDist/_TransitionColorSpread));
    171.                else
    172.                   o.fade_factor = saturate(length(-_TransitionMessFactor*_TransitionDist-sqrt(tri_dist))/30-(_TransitionDist/_TransitionColorSpread));
    173.              
    174.                triStream.Append(o);
    175.             }
    176.  
    177.             triStream.RestartStrip();
    178.          }        
    179.        
    180.            
    181.             // Fragment program
    182.             float4 fragment_program( G2F i ): SV_Target
    183.          {
    184.             half3 pixel_in_cam = _WorldSpaceCameraPos.xyz - i.posWorld.xyz;
    185.             half pixel_dist = length(pixel_in_cam);
    186.  
    187. //                float3 viewDirection = normalize( pixel_dist );
    188.                 half3 lightDirection = normalize( _WorldSpaceLightPos0.xyz );
    189.  
    190.                 // Texture Maps
    191.             //--------------
    192.             // diffuse
    193.                 fixed4 diffuse = tex2D( _MainTex, i.UV0);
    194.  
    195.             // if diffuse texture have some alpha then clip under 0.5
    196.             clip(diffuse.a-0.5);
    197.        
    198.             // dirt-AO
    199.                 fixed4 dirt = tex2D( _DirtLMTex, i.UV1);// dirt and AO texture
    200.            
    201.             fixed3 diff = diffuse * dirt.rgb * 1.5; // *1.5 for less dark walls
    202.  
    203.             fixed3 normalDirection;
    204.            
    205.            
    206.             // fading bumps with distance ( far walls do not need bumpmaps )
    207.             //---------------------------------------------------------------
    208.             if(pixel_dist < _BumpDistFade)
    209.             {
    210.                // bump
    211.                fixed3 bump =  UnpackNormal (tex2D (_BumpMap,  i.UV0));
    212.              
    213.                // a revoir sur un vecteur plutot que sur des valeurs X et Y independantes
    214.                fixed delta=saturate((pixel_dist-(_BumpDistFade-_BumpLengthFade))/_BumpLengthFade);
    215.              
    216.                // small interpolation to make bump coming less visible...
    217.                bump = fixed3(lerp(bump.x,0,delta),lerp(bump.y,0,delta),1);
    218.  
    219.                // Normal Transpose Matrix ( i still do not get why this is here 8*(  )
    220.                half3x3 local2WorldTranspose = half3x3(
    221.                   i.tangentWorld,
    222.                   i.binormalWorld,
    223.                   i.normalWorld
    224.                );
    225.              
    226.                // Calculate Normal Direction from normal and bumpies
    227.                normalDirection = normalize( mul( bump, local2WorldTranspose ) );
    228.             }
    229.             else
    230.             {
    231.                normalDirection = i.normalWorld; // with no bumps
    232.             }
    233.              
    234.              
    235.             fixed3 outputColor;
    236.              
    237.              
    238. #if defined(WITH_CITY_NIGHTLIGHTS)
    239.             // Night lightmap and its color and its intensity
    240.             fixed lm =  dirt.a;
    241.             fixed3 emiss = lm * _NightLightColor.rgb;// * _NightLightFactor;
    242.            
    243.             // night city wall lightings with less dirt/AO....
    244.             outputColor = diff * UNITY_LIGHTMODEL_AMBIENT.rgb + diffuse*(1-(dirt.rgb*lm-(lm-1)))*emiss*2;
    245. #else
    246.             // Lighting
    247.             fixed3 diffuseReflection = _LightColor0.rgb * saturate( dot( normalDirection, lightDirection ) );
    248.            
    249.                 outputColor = diff * (diffuseReflection * SHADOW_ATTENUATION(i) + UNITY_LIGHTMODEL_AMBIENT.rgb);
    250.                
    251. #endif              
    252.  
    253.             // coming IN and OUT color setting
    254.             outputColor = lerp(outputColor,UNITY_LIGHTMODEL_AMBIENT.rgb,i.fade_factor);
    255.        
    256.            
    257.             UNITY_APPLY_FOG(i.fogCoord,outputColor);
    258.            
    259.                 return half4(outputColor, 1.0);
    260.            
    261.             // show grey normals+bumps
    262. //                return half4(saturate( dot( normalDirection, lightDirection )),saturate( dot( normalDirection, lightDirection )),saturate( dot( normalDirection, lightDirection )), 1.0);
    263.            
    264.             }
    265.            
    266.             ENDCG
    267.         }
    268.      
    269.       // shadow caster rendering pass, implemented manually
    270.       // using macros from UnityCG.cginc
    271.       Pass
    272.       {
    273.          Tags {"LightMode"="ShadowCaster"}
    274.  
    275.          CGPROGRAM
    276.          #pragma vertex vert
    277.          #pragma fragment frag
    278.          #pragma multi_compile_shadowcaster
    279.          #include "UnityCG.cginc"
    280.  
    281.          struct v2f {
    282.              V2F_SHADOW_CASTER;
    283.          };
    284.  
    285.          v2f vert(appdata_base v)
    286.          {
    287.              v2f o;
    288.              TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    289.              return o;
    290.          }
    291.  
    292.          float4 frag(v2f i) : SV_Target
    293.          {
    294.              SHADOW_CASTER_FRAGMENT(i)
    295.          }
    296.          ENDCG
    297.       }
    298.    }
    299. }
    Draws nothing in linux.
    However, it works like a charm in ( windows ) editor and windows target.

    Could anyone please give it a try on a simple project targetted to linux and ran on a debian10 ?
    My OpenGL is 3.3 and SM5...

    Thanks a bunch !

    Happy unitying ! :)
     
  2. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    bumpies ? :)
     
  3. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    noone targets to linux ? :(
     
  4. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    please @bgolus , if you are still here, can you maybe help me on this case ? :)
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    No idea.

    You might try running the Linux version of the editor and see if that shader gives you any useful errors. But my guess is you're running into a driver issue, and not something on Unity's side.
     
  6. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    okay. i think this is the best option :)
    Moreover as my linux runs on VMWARE^^

    Strange thing though is that the bug also appears on a non virtual linux.
    I just don't know enough about this problem for having a clue on its causes. Thanks @bgolus !
    Sorry i bothered you on this but i thought you could find one of my misuses of shaders ;)

    Have nice day and happy unitying ! :)