Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Terrain shader makes spikes on gpu on android

Discussion in 'Shaders' started by mrtkhosravi, Aug 3, 2015.

  1. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    I have created a simple scene with one terrain, one camera and one directional light. The target platform is android.
    There is nothing special about them. I disabled Terrain's cast shadow and changed its material to legacy diffuse.
    And then created the corresponding shader to customize and boost performance.
    Code (CSharp):
    1. Shader "Nature/Terrain/Diffuse" {
    2.     Properties {
    3.     [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" { }
    4.     [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" { }
    5.     [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" { }
    6.     [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" { }
    7.     [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" { }
    8.     [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" { }
    9.     [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
    10. }
    11. SubShader {
    12.         Tags {
    13.             "SplatCount" = "4"
    14.         "Queue" = "Geometry-99"
    15.         "RenderType" = "Opaque"
    16.     }
    17.     LOD 300
    18.  
    19. CGPROGRAM
    20.  
    21. #pragma surface surf Lambert nolightmap noforwardadd nofog
    22.  
    23. struct Input {
    24.             half2 uv_Control : TEXCOORD0;
    25.             float2 uv_Splat0 : TEXCOORD1;
    26.             float2 uv_Splat1 : TEXCOORD2;
    27.             float2 uv_Splat2 : TEXCOORD3;
    28.             float2 uv_Splat3 : TEXCOORD4;
    29. };
    30.  
    31. sampler2D _Control;
    32. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    33.  
    34. void surf (Input IN, inout SurfaceOutput o) {
    35.     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    36.     fixed3 col;
    37.     col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    38.     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    39.     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    40.     col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    41.     o.Albedo = col;
    42. }
    43. ENDCG
    44. }
    45.  
    As you can see it's basically the terrain first pass shader with minimal changes. Now when I build and run on android device THIS happens:
    2.jpg There are huge spikes coming from Graphics.PresentAndSync and I wonder why there are gpu spikes? It doesn't make any sense.
    Also the rendering path is forward. The vsync option is disbaled. Fog is off. The mobile device is somewhat high-end with 4cores and 2GB ram and PowerVR SGX544 gpu.
    So anyone can explain that?

    EDIT:
    After further investigation it turned out that it is caused by a gpu event. Here is the profiler at a certain period:
    11.jpg
    and at the same time PowerVR gpu profiling shows:
    12.jpg
    Consider the resemblance between the occurrence of unity profiler spikes and gray vertical lines in gpu profiling. For every gray line there is a spike in unity profiler. At higher zoom level you can see:
    13.jpg
    On the top row is renderer tasks and tasks with same color belong to the same frame. You can see that between every three frame there is a huge gap that takes like 10 to 25ms. This amount differs for each gray line. So every few frames there is a huge gap between two frames. So what is the nature of this gap? According to the gpu profiler document:
    Power-off periods are represented by vertical grey blocks in the graph view. These events occur when the hardware has gone to sleep or powering down to save power due to a lack of work.
    Now here is the question: Why it powers down the gpu to save power when the gpu is under such a heavy load???

     
    Last edited: Aug 8, 2015
  2. Moostapha1

    Moostapha1

    Joined:
    Sep 23, 2014
    Posts:
    5
    I have same problem. No objects in screen but Graphics.PresentAndSync uses 80% CPU