URP Shader Graphs with instanced indirect - InstanceID node seems to give 0
Asked Answered
A

4

0

Hello!
I’m trying to render meshes with shader graph shaders from a custom render pass using CommandBuffer.DrawMeshInstancedIndirect.

I’m passing my positions to the shader via a StructuredBuffer defined in a custom function node. I index the buffer with the instance id and return the position, that gets added to the object space vertex position. This works great, but it renders all of my objects on the same position (the first one in the buffer).
I checked the draw command with Renderdoc and it was indexing the buffer for every instance with 0 instead of the instance id.

I was able to make it work without shader graphs, using SV_InstanceID, but i would really love to get it working with shader graphs.

I also got multiple objects to render with DrawMeshInstanced, using shader graphs, but then i’m not able to use compute buffers to provide the count and I also have to make a long array of matrices. However the instance id worked that time.
I attached some pictures to hopefully make it more understandable. Any ideas what I’m doing wrong? (I selected instance 24 in the Renderdoc picture)

Abyssal answered 30/1, 2024 at 20:26 Comment(0)
A
0

I managed to solve it using this code:
Experiments with using DrawMeshInstancedIndirect with Shader Graph · GitHub

I was missing this:

#pragma instancing_options procedural:setup
Abyssal answered 30/1, 2024 at 20:26 Comment(0)
T
0

Hey @Abyssal , trying to achieve something similar.

How does one use #pragma instancing_options procedural:setup?

I continue to get the following error after following the comments in the link.

UNITY_INSTANCING_PROCEDURAL_FUNC must be defined.

Starting out super simple, & still struggling.
Here is what I have thus far -

#ifndef EXPANSE_INCLUDED
#define EXPANSE_INCLUDED
  
StructuredBuffer<float4> _PositionBuffer;
  
#pragma instancing_options procedural:vertInstancingSetup
  
void GetPosition_float(float instanceID, out float4 position)
{
	position = _PositionBuffer[instanceID];
}
#endif

& the barebones shader -

Any help appreciated, cheers.

Tieck answered 30/1, 2024 at 20:26 Comment(0)
E
0

This might be of further help:

Engage answered 27/1, 2024 at 12:36 Comment(0)
A
0

Just noticed the replies, sorry about that.

That #pragma line must be in a custom function node, with the type set to string.
Like this:

Then, you can define your setup function in an include file and place that in the shader graph using a custom function node.

For reference, my setup function looks like this (theinversefunction is defined in the include, it just inverts the matrix):

Just to be complete, I should also add, that the shader graph should contain the boolean keyword: “PROCEDURAL_INSTANCING_ON”, Global, Multi-Compile.

Hope this clears it up!

Abyssal answered 30/1, 2024 at 20:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.