Search Unity

Number of UV channels depending on platform?

Discussion in 'Shaders' started by Nifflas, Nov 15, 2018.

  1. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    Is there a limit to the number of UV channels I can use that differs per platform, and if so, how do I find this information? I've been googling without much success.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Do you mean UVs on the mesh appdata, or TEXCOORD semantics?
     
  3. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    I didn't know it'd differ. I rarely actually use them for textures. I primarily use up to all four components on the first four UV channels for vertex offsetting & color effects for unlit shaders. If the answer is different depending on context, I'd like to know it for both.

    (Admittedly I don't know how the TEXCOORD semantics work, I use Amplify Shader Editor)
     
    Last edited: Nov 16, 2018
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    To be honest, I'm not entirely sure what the limit is, or the platform limitations. For sure every platform has these:

    Position: Vector4
    Stored and passed to the GPU as a four component 32 bit float vector. Potentially quantized down to 10 bits per channel for compression on disk / faster uploads to the GPU.

    Color: Color32
    Stored and passed to the GPU as a Color32 value. Always only 8 bits (256 steps) of precision per component, the shader will see this as a quantized 0.0 to 1.0 float value per component.

    Normal: Vector3
    Stored and passed to the GPU as a three component 32 bit float vector. Potentially quantized down to 6 bits per channel for compression on disk / faster uploads to the GPU. Will get modified by skinned meshes!

    Tangent: Vector4
    Stored and passed to the GPU as a four component 32 bit float vector. Potentially quantized down to 6 bits per channel for compression on disk / faster uploads to the GPU. Will get modified by skinned meshes!

    UVs 0 - 3 : Vector4
    Stored and passed to the GPU as a four component 32 bit float vector. Potentially quantized down to 8 bits per channel for compression on disk / faster uploads to the GPU.

    Those are the ones I know for sure can be accessed on all platforms. It used to be that Unity had shader code specific to the Xbox that exposed up to UV 5 to the shader, but that's been gone since somewhere mid Unity 5 era. I know of no reasons from a platform or API standpoint that would prevent you from using all 8 UVs that Unity exposes to script (0 - 7). Most OpenGL 2.0 hardware had support for at least 16 vertex attributes, and D3D11 hardware supports up to 32 per vertex attributes. The first 4 position, color, normal and tangent, plus 8 UVs fits into that with room to spare. There might be some mobile hardware out there floating around with support for only 8 vertex attributes, but if you're not targeting OpenGL ES 2.0 then you should be okay.
     
  5. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    This is super helpful! Luckily, I only intend to use the first four UV channels (though all four components of them), so I think I'll be safe! I'm a little bit worried about the "potentially quantized down to 8 bits" though (an accuracy of 1/256 is definitely all I need, but how does that map to the UV values I use that always go from 0-1? Though I personally don't, values outside that range can definitely be put into the mesh data). I guess I'll just have to test though!
     
    Last edited: Nov 19, 2018
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The amount of compression depends on a few settings in Unity. The per-channel compression amounts are enumerated within the documentation for each of the mesh compression levels listed here:
    https://docs.unity3d.com/ScriptReference/ModelImporterMeshCompression.html

    But you have to actually set the compression level per mesh for those to take effect. The default for imported meshes is "Off".
    https://docs.unity3d.com/Manual/FBXImporter-Model.html

    And you have to manually tell Unity to compress meshes that are generated from script with an editor only function call.
    https://docs.unity3d.com/ScriptReference/MeshUtility.SetMeshCompression.html

    And you can select which channels to even allow compression to be applied to in the player settings via the Vertex Compression drop down.
    https://docs.unity3d.com/Manual/class-PlayerSettingsStandalone.html


    So, short version, your meshes aren't using compression right now.
     
    SenseEater and SugoiDev like this.
  7. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    Ah, fantastic! I was worried it was automatically applied depending on build platform. By the looks of it, there won't be any major roadblocks then!

    Thank you so much for the help!