Search Unity

variable 'o' used without having been completely initialized Compiling Vertex program

Discussion in 'Shaders' started by ricecrispie60, Mar 4, 2015.

  1. ricecrispie60

    ricecrispie60

    Joined:
    May 6, 2014
    Posts:
    20
    So this was working fine until i 'upgraded' to unity 5.0. I think i'm pasting the relevent area of the shader...

    Code (CSharp):
    1.         struct v2f {
    2.            
    3.             float4 pos : SV_POSITION;
    4.             float4 uv : TEXCOORD0;
    5.             float4 uv2  : TEXCOORD4;    
    6.             float4 uv3  : TEXCOORD5;
    7.         };
    8.  
    9.         v2f vert (appdata_full v)
    10.         {
    11.            
    12.             v2f o;
    13.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    14.            
    15.             _SpecularRotation *= 3.14159 / 180;
    16.            
    17.             specularMatrix = float2x2(cos(_SpecularRotation),-sin(_SpecularRotation), sin(_SpecularRotation), cos(_SpecularRotation));
    18.             specularMatrix *=0.5;
    19.             specularMatrix +=0.5;
    20.             specularMatrix = specularMatrix * 2-1;
    21.             o.uv3.xy = TRANSFORM_TEX(v.texcoord.xy,_sphereBaseTex );
    22.             o.uv3.xy -=.5;
    23.             o.uv3.xy = mul ( o.uv3.xy, specularMatrix );
    24.             o.uv3.xy +=.5;
    25.  
    26.             _RotationDegrees *= 3.14159 / 180;
    27.             float s = sin ( _RotationDegrees);
    28.             float c = cos ( _RotationDegrees);
    29.          
    30.             float2x2 rotationMatrix = float2x2( c, -s, s, c);
    31.            
    32.             rotationMatrix *=0.5;
    33.             rotationMatrix +=0.5;
    34.             rotationMatrix = rotationMatrix * 2-1;
    35.            
    36.             v.texcoord.xy -= 0.5;
    37.             v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix );
    38.             v.texcoord.xy += 0.5;
    39.                        
    40.  
    41.             o.uv.xy = TRANSFORM_TEX(v.texcoord.xy,_surfaceTex) + frac(float2(_surfaceScrollX, 0) * _Time);
    42.             o.uv.zw = TRANSFORM_TEX(v.texcoord.xy,_cloudTex) + frac(float2(_cloudScrollX, 0) * _Time);
    43.            
    44.            
    45.             o.uv2.xy = TRANSFORM_TEX(v.texcoord.xy,_maskTex);
    46.             o.uv2.xy = TRANSFORM_TEX(v.texcoord.xy,_staticLightTex);
    47.  
    48.             return o;
    49.         }
    Any help would be super!
     
    Atabek likes this.
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I think your last line

    o.uv2.xy= TRANSFORM_TEX(v.texcoord.xy,_staticLightTex);

    should be

    o.uv2.zw= TRANSFORM_TEX(v.texcoord.xy,_staticLightTex);
     
    Atabek likes this.
  3. ricecrispie60

    ricecrispie60

    Joined:
    May 6, 2014
    Posts:
    20
    Thanks for responding Farfarer, no dice - full error message:
    which is the 'return o;' line ...can i provide more info?
     
  4. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Now you have to properly initialize every variable of a struct you use, so in your case you never output a value to uv2.zw and uv3.zw

    farfarer already gave you the answer to uv2.zw, now you need to do something similar to uv3.zw

    if you don't plan to initialize it here but still want the zw component of uv3 you can simply do something like o.uv3.zw = 0;
     
    gigazelle likes this.
  5. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Initialize your output using this provided macro:

    Code (CSharp):
    1. UNITY_INITIALIZE_OUTPUT(v2f, o);
     
  6. ricecrispie60

    ricecrispie60

    Joined:
    May 6, 2014
    Posts:
    20
    That fixed everything thanks so much grizzly! How on earth do you know these things :)

    kerbus: Do you have a link to that memo on the initialization rules?

    Again i want to thank you guys for chipping in, i find the shader documentation an impenetrable maze most of the time so most of my small knowlege is based of reading threads here, Farfarer in partiulcar has basicaly written most of my shader stuff with the many contributions made in the forums.

    If you guys are interested heres the flatplanet shader no longer displaying purple circles of fail:-

     
    Armegalo likes this.
  7. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Nice work, ricecrispie!

    It's due to the fussy HLSL compiler. UT5 now compiles to HLSL for both dx9 & 11 and hence you were seeing the error.

    From the UT5 release notes...:)

    "Direct3D 9 shader compiler was switched from Cg 2.2 to HLSL, the same compiler that's used for D3D11. This fixes a bunch of issues where Cg was generating invalid D3D9 shader assembly code; and generally produces slightly more efficient shaders. However, HLSL is slightly more picky about syntax; in general you have to do same shader fixes for D3D9 as for D3D11 now (e.g. UNITY_INITIALIZE_OUTPUT for “out” structures)."

    http://unity3d.com/unity/whats-new/unity-5.0
     
    Last edited: Mar 6, 2015
  8. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    I actually found it out by trial and error when is was trying to upgrade some shaders, i only found out about the macro later, I actually prefer not using the macro because now the shader basically tells me when I have unnecessary fields, there's no point in using a float 4 to store a float 1
     
  9. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Thank you for that! Unity's own "Effects/Explosions/Particles/Alpha Blended Light Soft" was throwing that same error in 5.3.5p2. Inserting UNITY_INITIALIZE_OUTPUT(v2f, o); right after v2f o; (line 160) solved the problem.
     
  10. pophead2

    pophead2

    Joined:
    Jun 20, 2016
    Posts:
    1
    Thanks guys! This error occurred "randomly" after installing 2017.1. Fixed it with your solution. TY and have a good weekend.