Search Unity

Vertex Color Kills performance?

Discussion in 'Shaders' started by Eyeofgod, Feb 1, 2012.

  1. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Hi there,

    I just modified the Mobile diffuse shader included in Unity to support vertex color. The shader is pretty simple, I have just add a line:

    Code (csharp):
    1. Shader "Mobile/Diffuse Vertex Colored" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "white" {}
    4. }
    5. SubShader {
    6.     Tags { "RenderType"="Opaque" }
    7.     LOD 150
    8.  
    9. CGPROGRAM
    10. #pragma surface surf Lambert noforwardadd
    11.  
    12. sampler2D _MainTex;
    13.  
    14. struct Input {
    15.     float2 uv_MainTex;
    16.     float3 color : COLOR;
    17. };
    18.  
    19. void surf (Input IN, inout SurfaceOutput o) {
    20.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    21.     o.Albedo = c.rgb * IN.color.rgb;
    22. }
    23. ENDCG
    24. }
    25.  
    26. Fallback "Mobile/VertexLit"
    27. }
    28.  
    It runs great on iPhone 3GS and iPad 2 (60fps) with just one plane and the shader applied (the plane takes all the screen). But in iPad1 and iPhone 4, it runs at 45fps!!. The line that "breaks" everything is o.Albedo = c.rgb * IN.color.rgb . Only that. If I use vertex colors only, or the texture colors only works great, but that operation kills the performance. I can't imagine what is happening. (Using OpenGL 2.0). Do you know why?

    I will try to write a GLSL shader form scratch, but I need also Beast lightmaps and I struggling to make them working (there is another post with this subject)

    Thanks
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Try reducing the precision of the input colour, to half3 or fixed3.
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Just to clarify: if you replace "o.Albedo = c.rgb * IN.color.rgb;" by "o.Albedo = c.rgb;", the performance is OK?
     
  4. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Yes thats it. Its when I multiply both when the performance drops.

    I just tried to reduce the precision to fixed3, and it works perfectly. I though that a "simple multiplication" can't hurt performance... I have learned a good lesson here.

    Many thanks
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    There's no such thing as a simple multiplication on mobile hardware. ):
     
  6. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    So is this ultimately the answer as to why using OpenGL 2.0 with Vertex Colors with the built in shaders causes such a large performance drop? I've seen the drop on iPhone 4 as well.