Issues with Custom Vertex Factory: PrimitiveIdStream, VSM support, and SV_InstanceID conflicts

Hi everyone,

I’m working on a custom Instanced Mesh Component for a grass system. I’m using a Compute Shader to calculate instance transforms and a Custom Vertex Factory to render them.

The Goal:

I initially had the grass rendering correctly, but it didn’t support Virtual Shadow Maps (VSM). I learned that to support VSM (and GPUScene), my Vertex Factory needs to support the PrimitiveIdStream (Attribute 13).

The Problem:

After modifying my Vertex Factory to include PrimitiveIdStream and updating the compilation environment, the grass instances are now rendering incorrectly (flying around / wrong transforms), as shown in the video.

I suspect the issue lies in a conflict between SV_InstanceID and UE5’s internal macros.

  1. To support GPUScene, I use:

VF_GPUSCENE_DECLARE_INPUT_BLOCK(13)

  1. This macro internally declares uint DrawInstanceId : SV_InstanceID;.

  2. However, I also need SV_InstanceID to index my local StructuredBuffer (Manual Vertex Fetch).

  3. If I try to manually declare uint InstanceId : SV_InstanceID; alongside the macro, I get a shader compiler error (overlapping semantics).

  4. If I remove my manual declaration and use the InstanceId provided by the macro, the transforms are incorrect (which leads to the artifacts in the video).

Question:

Has anyone successfully implemented a custom VF that uses both manual vertex fetch (from a local buffer) AND supports GPUScene/PrimitiveID?

How should I correctly access the SV_InstanceID (or equivalent) to index my local buffer when VF_GPUSCENE_DECLARE_INPUT_BLOCK is active? Do I need to apply a specific offset (like BatchElement.UserIndex) manually?

Any help or guidance would be greatly appreciated! I’ve been stuck on this for a few days.

Thanks!