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

Problems using Graphics.DrawMeshInstancedIndirect on Android

Discussion in 'General Graphics' started by Aaron-Meyers, Feb 27, 2018.

  1. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    I'm having a real struggle trying to port a system that uses Graphics.DrawMeshInstancedIndirect over to work on the Android platform. After searching the web for a bit, I've come away with the impression that everyone else out there is just instancing the day away with no problems at all, so I'm not sure what is causing the problems in my setup.

    I'm using the latest Android API. I have the Unity Editor running in forced ES 3.1 AEP emulation mode. I have "Require ES3.1+AEP" checked in Player Settings, so I think I've got all the crucial settings in place.

    The system and shader that goes with it is rather complex, so I decided to start with something much simpler. I tried using the example code from the documentation page here: https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html

    I was getting strange an unexpected results so I further simplified the shader in the example and added the #pragma multi_compile_instancing directive, exposing the instancing checkbox on the material inspector, which solved some problems.

    I removed all the lighting because I don't need it and I was also seeing some weirdness with the color. The one problem that seems to persist for me now is that no matter what mesh I use, it becomes a cube. To be more specific, from what I can tell, every component of every vertex seems to be forced to either 1, 0 or -1, which looks cube-ish with random triangle edges within it.

    As you can see in the shader code, I'm doing only the simplest transformations (the problem persists without even those) and using UnityObjectToClipPos() at the end of the shader:

    Code (csharp):
    1. Shader "Instanced/InstancedShaderEvenSimpler" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.  
    7.         Pass {
    8.  
    9.             Tags {"LightMode"="ForwardBase"}
    10.  
    11.             CGPROGRAM
    12.  
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #pragma target 4.5
    16.             #pragma multi_compile_instancing
    17.             #pragma instancing_options procedural:setup
    18.  
    19.             #include "UnityCG.cginc"
    20.             #include "UnityInstancing.cginc"
    21.  
    22.             sampler2D _MainTex;
    23.             fixed4 _Color;
    24.  
    25.             void setup() { }
    26.  
    27.             struct v2f
    28.             {
    29.                 float4 pos : SV_POSITION;
    30.                 fixed4 color : COLOR;
    31.             };
    32.             v2f vert (appdata_full v )
    33.             {
    34.                 v2f o;
    35.                 o.color = _Color;
    36.                 float3 vertPos = v.vertex.xyz * .25;
    37.                 #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    38.                     UNITY_SETUP_INSTANCE_ID(v);
    39.                     vertPos.z += (unity_InstanceID - 10.0 ) * .35;
    40.                     o.color = fixed4( abs(v.normal), 1.0 );
    41.                 #endif
    42.  
    43.                 o.pos = UnityObjectToClipPos(float4(vertPos, 1.0f));
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 return i.color;
    50.             }
    51.  
    52.             ENDCG
    53.         }
    54.     }
    55. }
    Here is what this looks like running in the editor:

    editor screenshot.PNG

    Here is what it looks like running on a Pixel XL:

    Screenshot_20180227-114512.png
    Would love any insight into what could be causing this.

    Alternatively, if anyone has an example project using Graphics.DrawMeshInstancedIndirect that will ~just work~ on an Android device, I would love to try it out.

    Thanks!

    -A
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Hi,

    This sounds like it may be a bug - it looks like the instancing is only partly working; the instance ID looks to be correct, but the mesh geometry is corrupted.

    Could you report a bug so that the Android team can investigate it? (https://unity3d.com/unity/qa/bug-reporting)

    Thanks
     
    Last edited: Mar 2, 2018
  3. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    richardkettlewell likes this.
  4. eddietree

    eddietree

    Joined:
    Sep 8, 2014
    Posts:
    38
    Hello @richardkettlewell, we are encountering this exact same as well where DrawMeshInstancedIndirect seems to be broken on all Android devices that we are testing with. Built using Unity 2017.4.0f1 and Unity 2018.1.0b12 beta.

    I did a full writeup (including super simpel repro project for Unity to download), which you can find here:
    https://docs.google.com/document/d/1vC7BayasyGKzSqMJnSM1g3mzR5PzZqXw4KQ_bfijUso/edit?usp=sharing

    Is this still on your radar? Any thoughts on when we can expect a potential fix? I've reported a bug as well.

    Thanks for your time!
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    We already have it logged, from the user above your post: https://issuetracker.unity3d.com/is...y-on-devices-containing-adreno-or-nvidia-gpus (assuming your bug report is the same, it will be closed as a duplicate).

    The current thinking from the mobile team appears to be that there is a driver bug causing it not to function correctly on some GPUs. It appears to work with Mali GPUs, but not NVidia or Adreno.

    In this case, we probably need to report it to those driver teams. (if we haven't already).
    A fix is probably not coming soon, sorry :(
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Heads-up: a fix is coming :)
     
    vutruc80 and richardkettlewell like this.
  7. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    725
    I am still on 2017.4. Has this been fixed in a current release?
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    2017.4 should get it soon.
    The lowest version where it's fixed at the moment is 2018.1.9f1.
     
    joshuacwilde likes this.