Search Unity

Unity CommandBuffer.DrawMeshInstanced Does Not Work With URP

Discussion in 'Universal Render Pipeline' started by ryanzec, Dec 1, 2020.

  1. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    I have the following code trying to test out rendering mesh manually with Unity 2020.1 using URP (I commented out mesh generation since I know that works):
    Code (CSharp):
    1. public class DrawMeshTestMB : MonoBehaviour {
    2.   [SerializeField]
    3.   private int _width;
    4.  
    5.   [SerializeField]
    6.   private int _height;
    7.   private Material _material;
    8.   private Mesh _mesh;
    9.   private Vector3[] _vertices;
    10.   private Vector2[] _uvs;
    11.   private int[] _triangles;
    12.   private List<Vector3> _positions = new List<Vector3>();
    13.   private Matrix4x4[] _matrices;
    14.  
    15.   private List<int> _t;
    16.  
    17.   // attach OnEndCameraRendering event
    18.  
    19.   private void Awake() {
    20.    // generate mesh / material data
    21.  
    22.    for (var x = 0; x < _width; x++) {
    23.      for (var y = 0; y < _height; y++) {
    24.        var matrixIndex = x + (y * x);
    25.        var position = transform.position + new Vector3(0.45f * x, 0.7f * y);
    26.        var rotation = Quaternion.Euler(Vector3.zero);
    27.        var scale = Vector3.one;
    28.      
    29.        _positions.Add(position);
    30.  
    31.        _matrices[matrixIndex] = Matrix4x4.TRS(position, rotation, scale);
    32.      }
    33.    }
    34.   }
    35.   private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera) {
    36.    GameLogger.LogDebug("OnEndCameraRendering");
    37.  
    38.    CommandBuffer myCommandBuffer = new CommandBuffer();
    39.  
    40.    // this works
    41.    for (var i = 0; i < _matrices.Length; i++) {
    42.      myCommandBuffer.DrawMesh(_mesh, _matrices[i], _material, 0);
    43.    }
    44.  
    45.    // this does not
    46.    myCommandBuffer.DrawMeshInstanced(_mesh, 0, _material, -1, _matrices);
    47.  
    48.    Graphics.ExecuteCommandBuffer(myCommandBuffer);
    49.   }
    50. }
    The issue I am running into in that while myCommandBuffer.DrawMesh() works, myCommandBuffer.DrawMeshInstanced() does not and I can't find any reason why it would not. The material is GPU instancing enabled and there are no error in the console. I even look into the frame debugger and the call to render does not even show up. It is using the same data so it is not like I can creating the data differently.

    Does anyone know what is wrong with my code here? Is there some weird setting that might be wrong or something not obvious?
     
  2. xgonzal2

    xgonzal2

    Joined:
    Jul 3, 2012
    Posts:
    62
    DrawMeshInstanced works fine for me in URP and 2020.1 as I've been using it for a lot of things. I haven't tried doing it with the endCameraRendering event so don't know if there might be something bad there. You didn't show your shader but I assume you also have the pragma for GPU instacing?
     
  3. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    So it turn of that is has to do with the default lit shared for 2d (I am using the 2d renderer). If I use the unlit shared, that seems to work which is a bit disappointing as I really want to be able to use the 2d lighting system and trying to learn shared stuff is a bit more than I want to bite off right now.

    I am going to do some profiling with just using DrawMesh() and see if that giving me the performance I am going to want since I can use the default lit shared with that called.
     
  4. xgonzal2

    xgonzal2

    Joined:
    Jul 3, 2012
    Posts:
    62
    Oh that's unfortunate. I can't say I've tried using the 2D system but taking a quick glance at the sprite lit default shader for 2D and I don't see any pragmas to enable GPU instancing.