Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GLSL Optimization failed: xlv_ redeclared

Discussion in 'Shaders' started by zarro45, Jan 17, 2013.

  1. zarro45

    zarro45

    Joined:
    Jan 17, 2013
    Posts:
    4
    Hello everyone,

    I wrote a vertex and fragment shader that works just fine on Direct3D, but when it compiles on a Mac gives strange vertex errors. I tried forcing it to compile to glsl, to see if that would fix the problem, and I get the following problem in the compiled shader:

    Code (csharp):
    1.  
    2. varying vec4 xlv_;
    3. varying vec2 xlv_;
    4. varying float xlv_;
    5. varying vec4 xlv_;
    6. varying float xlv_;
    7. void main() {
    8.     v2f xl_retval;
    9.     appdata_img xlt_v;
    10.     xlt_v.vertex = vec4( gl_Vertex);
    11.     xlt_v.texcoord = vec2( gl_MultiTexCoord0);
    12.     xl_retval = vert( xlt_v);
    13.     gl_Position = vec4( xl_retval.pos);
    14.     xlv_ = vec4( xl_retval.pos2);
    15.     xlv_ = vec2( xl_retval.uv1);
    16.     xlv_ = float( xl_retval.svc);
    17.     xlv_ = vec4( xl_retval.vr);
    18.     xlv_ = float( xl_retval.draw);
    19. }
    20. /* NOTE: GLSL optimization failed
    21. 0:0(0): error: `xlv_' redeclared
    22. 0:0(0): error: `xlv_' redeclared
    23. 0:0(0): error: `xlv_' redeclared
    24. 0:0(0): error: `xlv_' redeclared
    25. 0:450(7): error: type mismatch
    26. 0:451(7): error: type mismatch
    27. 0:453(7): error: type mismatch
    28. */
    What is the solution to this error? Is there a way to force the compiler to not redeclare a variable? Is there a misconception I have about writing shaders that is causing this problem? I declared this struct for the vertex shader, this is what it's having trouble with I think?

    Code (csharp):
    1. struct v2f
    2.     {
    3.         float4 pos : POSITION; //internal, used for display
    4.         float4 pos2; //Position in world, relative to player position in world
    5.         float2 uv1; //Used to specify what part of the texture to grab in the fragment shader(not relativity specific, general shader variable)
    6.         float svc; //sqrt( 1 - (v-c)^2), calculated in vertex shader to save operations in fragment. It's a term used often in lorenz and doppler shift calculations, so we need to keep it cached to save computing
    7.         float4 vr; //Relative velocity of object vpc - viw
    8.         float draw; //Draw the vertex?  Used to not draw objects that are calculated to be seen before they were created. Object's start time is used to determine this. If something comes out of a building, it should not draw behind the building.
    9.     };
    Any help would be appreciated!
    Thanks
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Well... you're defining the variable _xlv 5 times in the first 5 lines of the code you pasted.

    Edit; Misread, nevermind.
     
    Last edited: Jan 17, 2013
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Since it works fine in D3D, I assume Zarro wrote the shader in Cg. The pasted GLSL is produced by Unity.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    My bad, I misread it as attempting to write a GLSL shader copy/pasted from a D3D one (it's been a long day).

    Sounds like one for Aras and his GLSL optimiser.
     
  5. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Original code?
     
  6. Mistale

    Mistale

    Joined:
    Apr 18, 2012
    Posts:
    173
    zarro45: I had the same problem today and it seems to be caused by the struct, just as you say.
    In my case it was enough to add a type after each variable in the struct, such as TEXCOORD for uv:s and so on.
    The typename is probably used as a suffix, and without one there'll be multiple variables in the GLSL code with the same name.
     
  7. zarro45

    zarro45

    Joined:
    Jan 17, 2013
    Posts:
    4
    Do you mean like the first line in my struct, pos: POSITION? I'm new to shaders, basically porting over code written by another member of the team. Thank you for your reply!
     
  8. Mistale

    Mistale

    Joined:
    Apr 18, 2012
    Posts:
    173
    That's exactly what I mean. But I'm also new to shaders, so I don't know which types you should use for your custom values.
    It's strange that we (atleast me) don't get any errors in Unity during the compilation of the shader...

    I hope someone more into shaders will come along and give some pointers.
     
  9. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    For anything other than normals, tangents, position and colour, you'll need to use TEXCOORD0 through to TEXCOORD7.

    While it sounds like they're for UVs, each one will take anything up to a float4.
     
  10. zarro45

    zarro45

    Joined:
    Jan 17, 2013
    Posts:
    4
    That fixed the problem immediately. Thanks!
     
  11. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It seems like there are a few silent or confusing problems that arise from not declaring semantics everywhere possible. Perhaps someone who has encountered these issues would like to file a comprehensive bug report?
     
  12. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    Thank you for that Mistale, I struggled on this thing all day.