Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

#pragma glsl compile fails

Discussion in 'Shaders' started by obviousjim, Nov 3, 2010.

  1. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
    Hello,

    I'm working with the Ocean Shader and wanted to add a step in the vertex shader to take advantage of unity3's snazzy new Vertex Texture Fetch feature to do some extra mesh distortion.

    In the OceanReflectionRefraction.shader just adding the following directives causes a strange problem:

    Code (csharp):
    1.  
    2. #pragma glsl
    3. #pragma target 3.0
    4.  
    the compilation fails. Opening up the compiled shader I found that a single variable gets redeclared a lot. in fact the same way for each main(). see below

    Code (csharp):
    1.  
    2. // snip ...
    3. attribute vec4 TANGENT;
    4. varying vec4 xlv_;
    5. varying vec4 xlv_;
    6. varying vec4 xlv_;
    7. varying vec4 xlv_;
    8. varying vec4 xlv_;
    9. varying vec4 xlv_;
    10. varying vec4 xlv_;
    11. void main() {
    12.     v2f xl_retval;
    13.     appdata_tan xlt_v;
    14.     xlt_v.vertex = vec4( gl_Vertex);
    15.     xlt_v.tangent = vec4( TANGENT);
    16.     xlt_v.normal = vec3( gl_Normal);
    17.     xlt_v.texcoord = vec4( gl_MultiTexCoord0);
    18.     xl_retval = vert( xlt_v);
    19.     gl_Position = vec4( xl_retval.pos);
    20.     xlv_ = vec4( xl_retval.projTexCoord);
    21.     xlv_ = vec4( xl_retval.bumpTexCoord, 0.0, 0.0);
    22.     xlv_ = vec4( xl_retval.viewDir, 0.0);
    23.     xlv_ = vec4( xl_retval.lightDir, 0.0);
    24.     xlv_ = vec4( xl_retval.objSpaceNormal, 0.0);
    25.     xlv_ = vec4( xl_retval.foamStrengthAndDistance, 0.0, 0.0);
    26.     xlv_ = vec4( xl_retval.redIntensity, 0.0, 0.0);
    27. }
    28. /* NOTE: GLSL optimization failed
    29. 0:70(18): error: `xlv_' redeclared
    30. 0:71(18): error: `xlv_' redeclared
    31. 0:72(18): error: `xlv_' redeclared
    32. 0:73(18): error: `xlv_' redeclared
    33. 0:74(18): error: `xlv_' redeclared
    34. 0:75(18): error: `xlv_' redeclared
    35. */
    36. //... snip
    37.  
    What could be goofy about the shader such that the GLSL compile fails in this way? I've attached it for references.

    really looking forward to vertex texture fetches. hopefully this isn't a show stopper. Thanks!
     

    Attached Files:

  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    why exactly should this compile if you define the exactly same variable 8 times.
    would need to have a totally brainburnt compiler to let this thing pass ...

    you surely messed up somewhere in there with renaming or whatever cause 8 declarations of xlv_ are useless (and overwritting 7 of them in the code is just as useless)
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    That's the thing, though: the quoted code is from the compiled shader, which is the result of the Cg -> GLSL translator choking.

    obviousjim: I've seen this problem before, and it's actually caused by the missing semantics for your struct members. If you give them semantics it should compile properly to GLSL.

    Aras is aware of this issue and will be providing a better error message at some point.
     
  4. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
    Hey Daniel,

    I see that totally makes sense. Sorry if if this is too much of a beginner question, but what are the best semantic names to define for a struct like this with so many arbitrary parameters?

    Code (csharp):
    1.  
    2. struct v2f
    3. {
    4.     float4 pos : SV_POSITION;
    5.     float4 projTexCoord : TEXCOORD0; ?
    6.     float2 bumpTexCoord : TEXCOORD1;
    7.     float3 viewDir : ??;
    8.     float3 lightDir;
    9.     float3 objSpaceNormal
    10.     float2 foamStrengthAndDistance;
    11. };
    12.  
    Thanks!
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I think people just use TEXCOORDwhatever for "extra" data. That's how Unity does it when it generates v2f structs.
     
  6. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
    Awesome, worked like a charm!

    Just curious, in order to get all the reflections / refractions working again in the shader after converting to GLSL I had to add this line to the vertex shader

    Code (csharp):
    1.  
    2.     //had to flip the normal in converting to glsl
    3.     v.normal = -v.normal;
    4.  
    Is this explainable or a known difference between Cg and GLSL