Skip to content

UE5.3 Passing StructuredBuffer to Shader #14

@ChrisSchroeder01

Description

@ChrisSchroeder01

Hello,

I am currently trying to pass a Structured Buffer to the shader. The compute shader should also be able to change the values of the structs in this Structured Buffer over time (multiple executions).

I have a struct:

struct FAgent
{
	FVector2f position;
	float angle;
};

And my Execute RTComputeShader method:

static void ExecuteRTComputeShader(UTextureRenderTarget2D* RT, float deltaTime, FVector2f res, TArray<FAgent> agents)
{
	// Create a dispatch parameters struct and fill it the input array with our args
	FSlimeCSDispatchParams Params(agents.Num(), 1, 1);
	Params.Resolution = static_cast<FVector2f>(res);
	Params.RenderTarget = RT->GameThread_GetRenderTargetResource();
	Params.DeltaTime = deltaTime;
	Params.Agents = &agents;

	FSlimeCSInterface::Dispatch(Params);
}

I got the pass to shader working with the following in the DispatchRenderThread Method:

FRDGBufferRef AgentBuffer = CreateStructuredBuffer(GraphBuilder, TEXT("AgentBuffer"), sizeof(FAgent), Params.Agents->Num(), Params.Agents->GetData(), sizeof(FAgent) * Params.Agents->Num());
PassParameters->Agents = GraphBuilder.CreateUAV(AgentBuffer, PF_R32_FLOAT);

Unfortunately I could not figure out how to create an external buffer, which i assume will fix my issue that the computed data of my compute shader will not be overwritten. I tryied RegisterExternalBuffer but this Function is undefined. FRDGBuilder as a method called RegisterExternalBuffer but this method requires a TRefCountPtr parameter, but I could not figure out how to get such a value.

I am using the following Shader Parameter Definition:
SHADER_PARAMETER_RDG_BUFFER_UAV(RWStructuredBuffer<FAgent>, Agents)
because the pre-generated commented propose SHADER_PARAMETER_UAV(RWStructuredBuffer<FMyCustomStruct>, MyCustomStructs) // On the shader side: RWStructuredBuffer<FMyCustomStruct> MyCustomStructs; again I could not figure out how to get a FRHIUnorderedAccessView * from the TArray.

I came across several posts also trying to achieve what I want:
https://forums.unrealengine.com/t/loading-data-to-from-structured-buffer-compute-shaders/470083
https://forums.unrealengine.com/t/rwbuffer-and-buffer-in-compute-shaders-when-they-binding-with-the-same-resource/508164

There are a few things I noticed, it seems that Epic Games changed things from for the 5.3 Version. For example FRHICommandListImmediate::LockStructuredBuffer does not exist anymore (see: https://docs.unrealengine.com/5.3/en-US/API/Runtime/RHI/FRHICommandListImmediate/LockStructuredBuffer/)

I also looked at the Indirect Instancing examples but they looked pretty complex on the first sight. It would generally be nice to have a single Compute Shader Example with every possible Shader Parameter (the 7 commented ones) to know what to do for each parameter type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions