Search Unity

Question "implicit truncation of vector type" - Should I care?

Discussion in 'Shader Graph' started by dgoyette, Jun 16, 2020.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I was getting some warnings in a graph: "implicit truncation of vector type"

    This seems to be because I was sending a V4 into a V3 input, such as this:

    upload_2020-6-16_1-34-57.png

    In these cases, am I really supposed to do something like the following in order to now get a warning?

    upload_2020-6-16_1-36-30.png

    Is the second approach "better" in some way? It prevents the warning, but it adds more cruft to the graph. I also don't know whether it has any performance impact one way or the other.
     
    Aldeminor, Boodums, Reahreic and 4 others like this.
  2. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    I'd like to know as well :)
     
  3. goran_okomotive

    goran_okomotive

    Joined:
    Apr 26, 2017
    Posts:
    60
    bump
     
    bantus likes this.
  4. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    Me too interesting in this.
    I see two questions here:
    1. Can it cause any trouble? For example console has more strict requirements for shaders that sometimes cause compilation errors.
    2. What is preferable way in terms of optimization?
     
  5. MagdielM

    MagdielM

    Joined:
    May 27, 2020
    Posts:
    32
    Implicit truncation warnings serve to, well, warn you that the truncation is happening so that you're not surprised when the alpha channel of your RGBA output is suddenly missing from your variable later in the graph because you passed it through a node that takes in a three-component vector.

    If I recall correctly, it's about as computationally expensive as a swizzle operation. You probably shouldn't worry about it.
     
  6. Sphax84

    Sphax84

    Joined:
    Jun 23, 2015
    Posts:
    36
    I'm interested in that as well because I get spammed in the Console by those warnings and it's pretty annoying.
    Are we supposed to get rid of those warnings by editing the Shader Graphs the way @dgoyette showed in the screenshot and is it expensive in a way or another or another method exists to remove/hide that kind of warnings?
     
  7. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    44
    Getting the same warnings here. Would be nice if we could get rid of that, as it seems to be a bug.
     
    bobbaluba likes this.
  8. vyacheslav_zemlyanik

    vyacheslav_zemlyanik

    Joined:
    Sep 21, 2020
    Posts:
    1
    I would love to know that too!
     
  9. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,306
    Maybe one day we'll find out :rolleyes:
     
  10. Double-V

    Double-V

    Joined:
    Aug 13, 2016
    Posts:
    21
    I would like to know too
     
  11. unity_yppDGGle3H5QVQ

    unity_yppDGGle3H5QVQ

    Joined:
    Feb 18, 2021
    Posts:
    1
    any news on this? I am encountering the same issue
     
  12. TheProfessor

    TheProfessor

    Joined:
    Nov 2, 2014
    Posts:
    74
    This problem affects me as well; especially since it says it's an "error" and not a "warning"; if it's an actual error what am I supposed to do? If its a warning, how do I turn it off? Having to swizzle manually just for peace of mind seems inoptimal.
     
  13. Schuell

    Schuell

    Joined:
    Oct 30, 2020
    Posts:
    1
    The warning just means that some components of the vector are being ignored as it is converted to another type with less components(for example V4 to V3). In a script, instead of writing

    float3 a = float3(1, 2, 3);
    float2 b = a;

    (which will give your a warning) you can simply write

    float3 a = float3(1, 2, 3);
    float2 b = a.xy;

    The behaviour is the same, but the warning is gone.
     
  14. ArturoSR

    ArturoSR

    Joined:
    Feb 21, 2014
    Posts:
    21
    Hello and good day.

    I have the same problem, but in my case it's relative to an empty shader (just the vertex and fragment nodes)... shader-graph-errors.jpg

    unity version: 2020.2.7f1
    shade graph: 10.3.2 (verified)

    Any help will be appreciated, thanks.
     
    tziii likes this.
  15. harsap

    harsap

    Joined:
    Apr 4, 2019
    Posts:
    1
    i have same error how to solve this ?
     
    VeganBurrito86 likes this.
  16. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    That's just a semantics issue in the Unity inspector for shaders. Instead of the header for that textbox being "Errors (#):" it should be "Output (#):"

    It's only an actual error if the message is accompanied by a red !-agon. If it's a yellow yield triangle, then it's just a warning. If you are getting an actual error because of this (the shader doesn't compile or doesn't function), you may be truncating a value type that absolutely cannot be truncated, but I haven't seen this myself.
     
  17. RoyBarina

    RoyBarina

    Joined:
    Jul 3, 2017
    Posts:
    98
    I'm getting a ginormous amount of "implicit truncation" warning messages in my console even after I did check more than a handful of times in my shaders that there isn't any implicit truncation.
    It always pointing me to a sub graph I made that a few more shader graphs uses and there is 0 truncation there.
    So I picked into the generated code and discovered the culprit! it's the "normal from texture" node! yea this one:
    upload_2022-3-2_4-16-49.png
    The generated code is:
    upload_2022-3-2_4-20-47.png
    Those 3 lines calls for 'SAMPLE_TEXTURE2D()' which should return 'float4' but then directly put in a 'float' type.
     
  18. Nalida

    Nalida

    Joined:
    Apr 23, 2021
    Posts:
    1
    I installed NET4 the problem is gone. strange :rolleyes:
     
  19. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    Still curious if we should bother dealing with this warning or ignore it.
     
  20. RoyBarina

    RoyBarina

    Joined:
    Jul 3, 2017
    Posts:
    98
    IMO
    It's probably fine to ignore. you would do a truncation anyway either manually or automatically when you need\have to.. the warning is just to let you know an automatic truncation occurred and it's not obvious if it is intended or not.
     
  21. Lizianthe

    Lizianthe

    Joined:
    May 22, 2019
    Posts:
    1
    I had warnings with the shader graphs I made in Unity URP, and I solved it by adding those shaders in Project Settings -> Graphics -> Always Included Shaders
    Change the size accordingly on the top and then you can add your shaders.
    Be aware that every time you add one, a pop up window might show up, and when you press Continue it will pop your pipeline asset out on the top, so you have to put it back. Or at least that kept happening to me. :confused:
     
  22. qweredell

    qweredell

    Joined:
    Feb 8, 2022
    Posts:
    3
    Bump

    I feel like a lot of the time this is down to the actual master stack...
     
  23. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    139
    You can safely ignore implicit truncation warnings. If you connect a Vec4 data type to a Vec3 input port (for example), you'll get that warning, but all it means is that the fourth component of the vector is being thrown away to make the conversion from Vec4 to Vec3. The only case where this would be a problem is if you actually have data in that forth component that you need to keep. That's why there's a warning. But in 99% of cases this truncation is exactly what you intended to do - so it's not a problem.
     
  24. cosmiK777

    cosmiK777

    Joined:
    Feb 15, 2022
    Posts:
    7
    I had this error today as well using Unity 2021. It doesn't seem to affect the functionality. Just thought i'd share for documentation.

    Screenshot 2023-10-02 203940.png
     
    siberiasuninc_unity likes this.
  25. richard_harrington

    richard_harrington

    Unity Technologies

    Joined:
    Sep 29, 2020
    Posts:
    22
    As BenCloward said, the warning is benign and just letting you know that it is doing some work to throw away extra vector components when plugging a vector into an input that takes a smaller vector.

    The way I generally recommend handing this for best clarity is to simply use a "Swizzle" node to "truncate" the vector yourself. This is similar to how "swizzling" works in written-shaders, so if you want to chop of the 4th component of a Vector4, you can swizzle with "xyz" or "rgb" (both are the same result).

    upload_2023-10-4_14-26-58.png

    Just for some bonus knowledge, you can also do cool things with the swizzle node, such as:
    • xxx (gives you a vector 3 where all values are the X of your input vector), same with yyy, zzz, rrr, aaa, www, etc.
    • xx (same as above, but gives you a vector2)
    • xy
    • yx
    • yxy
    • zxw
    • rab
    • bgra
    • etc.
    Swizzle nodes are a fast and relatively "cheap" way to rearrange components of a vector.
     
    Last edited: Oct 10, 2023