Problem with DrawMeshInstance with HDRP or LWRP

Hy,
Im trying to use DrawMeshInstancedIndirect with the new render pipeline, a got a nice result for my grass sistem using dinamic batching.
Video:
https://www.reddit.com/r/Unity3D/comments/b1ysgp/thanks_this_is_the_result_of_all_of_that_feedback/

,
But is not so well performant, the CPU time is very high for this “little” amount of grass. So some friend suggested to use DrawMeshInstancedIndirect, but the exemple code in the forums are just for Legacy.
So i thought you guys could help me a little cos im really lost.

i tried to adapt a simple shader from shadergraph. I exported the master node to code, and it returned tha actual shader that unity uses, is a hude shader of 1800 lines.
Then i tried to adapt it doing some of the things there are on the docs : docs.unity3d.com/560/Documentation/ScriptReference/Graphics.DrawMeshInstancedIndirect.html,

This very very simple shadergraph structure:


Generates this *.shader file: Unlit Shader - Shadergraph Code - Pastebin.com. 1.8k lines.

So to implement something from the docs that i’ve mentioned before, i need to focus on
UNITY_PROCEDURAL_INSTANCING_ENABLED
and
#pragma multi_compile_instancing
#pragma instancing_options procedural:setup

The script works fine, only the positions are messed, something dont let the meshes go to the places where they are supposed to be, as you can see here:
This happens with the normal shaders of HDRP, and the customized one that i modified (added some things from the exemples docs of DrawMeshInstancedIndirect.)

I really liked how my scene looks, im impressed that i alone made everything (just the character from an asset). But im diving in the “performace wise” things, and for grass this really seems the way. Any other suggestion for good performace on thousand of grasses?

I could reduce the Drawcalls a lot by dynamic batching, but the CPU time was the same, and then someone suggested DrawMeshInstancedIndirect.

Hi.

Instanced indirect setup function has to be done a bit differently on HD SRP since it renders in camera space to remove floating origin issues.

Have a look at these nodes I made for the shader graph. These are for Vegetation Studios indirect implementation but you can change to yours.

https://github.com/AwesomeTechnologies/VegetationStudioPro-ShaderGraphNodes

Lennart

3 Likes

Wow, gonna try it out in some minutes! Gonna give some feedback soon !

the important part is the 2 defines.

#define unity_ObjectToWorld unity_ObjectToWorld
#define unity_WorldToObject unity_WorldToObject

that gets around a problem with setting the matrix. This is needed on HD srp only

2 Likes

Well, just plugging the node dows not made the trick, instead, it turned my shader to pink.


I made a video. We can see that it is rendering, but is not showing nothing anywhere.

Even if i only plug the custom node, the shader stays pink and the same behaviour accours.

And where im suposed to put this code? everywhere where i put it, it trows an warning and nothing happens. =C

Even with a clean shader still not showing nothing but the CPU time goes crazy

This has to go in your setup function where you set up the matrixes for rendering.

The function you call with
#pragma instancing_options procedural:setup

But I think you missed a step here.

In order for instanced indirect to work you need to set up a compute buffer on the CPU containing all the info you need to create the Object2World and World2Object matrixes in the setup function.

This data you can access as a structured buffer there.

Then you call the DrawMeshInstanceIndirect with a material that has this compute buffer assigned.

1 Like

I updated the shader:


But the same behavior of a default shader happens, it renders all the meshes in the same place.

Im using the code from the Docs:

How does you c# code look? setting up the compute buffer and doing the render call?

also could you post your setup function in the shader?

Sure, the shader :


Sorry for bad formatting, im just a noob in shaders yet

The code:

And why the node is not working? any hint? Im suposed to do this manual editing if the node works?

first the 2 define lines has to be inside the setup function. at the top.

I did not check the math but you seem to assign the buffer and render as you shoud.

Lennart

Like this, right?

yes.

Same =C
I even tried to copy the shader code from your script, but the same happend as well.

The shader code from my script will only work with the setup from Vegetation Studio Pro. It provides the pre created matrixes and control data in the compute buffer

Dont seems to work anyway, editing your files, editing the shaders, etc =C, but i will keep trying

Yeahhh babyy!!! I editted your solution and managed to work! Just need now to ajust the positions and Voilá!


Many many thanks! Works wonderfully!

4 Likes

Could you elaborate on how you fixed this?

1 Like