Pass TArray of custom struct to Compute Shader

Hello,

I have a struct in both my .cpp file and .usf file called Agent:

struct Agent
{
	FVector2D position;
	float angle;
};

How can I pass a TArray of this struct to my compute shader, possibly using some type of the SHADER_PARAMETER macro?
Any help is welcome!

Did you ever find the answer to this ?

You can use a Shared header file to allow both your .cpp and your .usf files to know about this struct.
See “NaniteVisualization.h” and “NaniteDefinitions.h” for example.

Don’t forget you can include header files in your .usf files.

After that to pass TArray to your shader file you need a buffer. In your FGlobalShader implem :

BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
		SHADER_PARAMETER_RDG_BUFFER_UAV(RWStructuredBuffer<FAgent>, Agent)
END_SHADER_PARAMETER_STRUCT()

You then have to follow the requirements to dispatch the shader with a UAV parameter.

In your .usf file, use a buffer :

RWStructuredBuffer<FAgent> Agents;

[numthreads(1, 1, 1)]
void MainCS(uint3 ID : SV_DispatchThreadID)
{
    float2 position = Agents[<any_index>].position;
    float angle = Agents[<any_index>].angle;
}