Search Unity

Graphics.DrawMeshInstanced not working on URP?

Discussion in 'Universal Render Pipeline' started by Barkley, Jan 12, 2020.

  1. Barkley

    Barkley

    Joined:
    Dec 1, 2012
    Posts:
    62
    Hi,
    I used to have a system to render grass (in a special way) where we used Graphics.DrawMeshInstanced to optimize its rendering. When I ported to URP this system is not working anymore.
    I'm calling the Graphics.DrawMeshInstanced method in the Update(). I'm not sure if this is not working because it's not supported in URP anymore or because maybe I need to do something special in the shader or anywhere for it to work (I've already marked the materials 'Support GPU Instancing' checkbox).
    I'm considering adding a render pass to do this, but I'd rather avoid it (to avoid re-doing it). Still, if it's going to be more efficient to do it like that I'd be happy to do so.
    Any information on this subject will be most welcome.
    Best regards,
    Eduardo
     
  2. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    432
    SRP doesn't support any rendering code in Updates that target the camera target because the camera target is not setup at this point.

    I'd say to inject a render pass. If you use the template we provide at Create menu this should be as simple as copying the code you have to the execute method of the renderer feature script then add the feature to the renderer.
     
  3. Barkley

    Barkley

    Joined:
    Dec 1, 2012
    Posts:
    62
    Thanks for the info! It makes a lot of sense. I've already created a few RenderPasses for other stuff so it shouldn't take me long to get that working. I just wanted to be sure that was the right way to go.

    BTW, I would suggest (and I understand this will be low on your list of priorities unless it's pretty easy which I guess it isn't) that you show some sort of warning when a Graphics.DrawXXX method is called from the wrong point. Just like you show warnings when you try to access thread unsafe functions from worker threads.

    Cheers,
    Eduardo
     
  4. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    432
    Upon further inspection, it seems my above statement is incorrect. DrawXXNow is that one that doesn't work. DrawXX should work as it defers rendering to a later step.

    Could you submit a bug about this?
     
    Barkley and GameDevCouple_I like this.
  5. Barkley

    Barkley

    Joined:
    Dec 1, 2012
    Posts:
    62
    Hi Phil,

    Thanks for the info! I can report a bug, but my project is huge (almost 10GB of data) and I'm not currently in a position to be able to start moving things to a new project to get the minimum viable project that reproduces the bug. Is there a way we could go around this? (I would be happy to send a USB drive with the project in it if necessary).

    I'm also experiencing another bug whereby every now and then graphics get corrupted and textures start to switch randomly to other textures in materials. It fixes closing the editor and restarting it, but it's pretty annoying (and it didn't happen before URP). So the same project could serve for 2 bugs :)

    Cheers,
    Eduardo
     
  6. Barkley

    Barkley

    Joined:
    Dec 1, 2012
    Posts:
    62
    Hi Phil,

    I finally found out where the problem was: the shader. My bad for not testing that first. I did implement the render pass and then had some issues with the shadow casting (I don't know how to do shadow casting for geometry rendered through a render pass).

    In order for instancing to work on URP you need to add the instancing macros to your shader AND (and that's what's not so obvious) the output stereo macros too (both in the pixel and vertex shaders).

    If you don't have that then only the first instance of the object is rendered (I believe).

    Thanks!
    Eduardo
     
unityunity