Pink and blue rectangle with bounding box
Unreal Blueprint graph calling a compute shader

Material graph to render target

Material graph to render target via compute in UE5

This is similar to the Draw Material to Render Target node provided by Unreal but executed through compute.

Get the code

$ shadeup-unreal
 > [COMPUTE] Compute Shader
 > Material Evaluation Render Target
OR

Usage

  1. Generate or download the template using the instructions above
  2. Rebuild your project
  3. Create or navigate to an existing Blueprint
  4. Right-click and add the node called “Execute Material RTCompute Shader”

Notes

Helpful reading


Sample snippet

// rtmat.usf

#include "/Engine/Generated/Material.ush"
#include "/Engine/Public/Platform.ush"

RWTexture2D<float3> RenderTarget;

[numthreads(THREADS_X, THREADS_Y, THREADS_Z)]
void RTMatExample(
	uint3 DispatchThreadId : SV_DispatchThreadID,
	uint GroupIndex : SV_GroupIndex )
{
	float4 SvPosition = float4(DispatchThreadId.xy, 0, 0);

	FMaterialPixelParameters MaterialParameters = MakeInitializedMaterialPixelParameters();

	// There are various inputs we can provide to the material graph via FMaterialPixelParameters
	// See: https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Shaders/Private/MaterialTemplate.ush#L262

	MaterialParameters.VertexColor = half4(1, 1, 0, 1);

	FPixelMaterialInputs PixelMaterialInputs = (FPixelMaterialInputs)0;

	// This is the call to the material graph
	CalcMaterialParameters(MaterialParameters, PixelMaterialInputs, SvPosition, true);

	// PixelMaterialInputs is a struct that contains the outputs of the material graph
	// Use the provided helper methods ( GetMaterialXYZ(...) ) to access the outputs
	// See: https://github.com/chendi-YU/UnrealEngine/blob/master/Engine/Shaders/MaterialTemplate.usf#L1298

	RenderTarget[DispatchThreadId.xy] = GetMaterialBaseColor(PixelMaterialInputs);
}