Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Config/DefaultEngine.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ AppliedDefaultGraphicsPerformance=Maximum
EditorStartupMap=/Game/Maps/TestMap.TestMap
GameDefaultMap=/Game/Maps/TestMap.TestMap

[/Script/Engine.RendererSettings]
r.AntiAliasingMethod=0

2 changes: 1 addition & 1 deletion CustomComputeShader.uproject
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"FileVersion": 3,
"EngineAssociation": "4.24",
"EngineAssociation": "5.2",
"Category": "",
"Description": "",
"Modules": [
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# CustomComputeShader

A minimal Unreal Engine 4 porject for adding and using a compute shader. I published an article that covers the main steps of the process and I use this project as an example.
A minimal Unreal Engine 5 project for adding and using a compute shader. I published an article that covers the main steps of the process and I use this project as an example.

## Modules:
* **CustomComputeShader** : The primary game module
* **CustomShadersDeclarations** : The game module that contains all the code for adding and using the compute shader

## Shaders:
* **WhiteNoiseCS** : A simple compute shader that renders white noise to a texture

## UE4 Version
This project was created and tested using **UE4.24**. Id you're using **UE4.25**, you'll need to include **/Engine/Public/Platform.ush** in your shader file in order for it to compile.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FWhiteNoiseCS : public FGlobalShader
/// </summary>
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
SHADER_PARAMETER_UAV(RWTexture2D<float>, OutputTexture)
SHADER_PARAMETER(FVector2D, Dimensions)
SHADER_PARAMETER(FVector2f, Dimensions)
SHADER_PARAMETER(UINT, TimeStamp)
END_SHADER_PARAMETER_STRUCT()

Expand Down Expand Up @@ -110,8 +110,10 @@ void FWhiteNoiseCSManager::UpdateParameters(FWhiteNoiseCSParameters& params)
/// Gets a reference to the shader type from the global shaders map
/// Dispatches the shader using the parameter structure instance
/// </summary>
void FWhiteNoiseCSManager::Execute_RenderThread(FRHICommandListImmediate& RHICmdList, class FSceneRenderTargets& SceneContext)
void FWhiteNoiseCSManager::Execute_RenderThread(FRDGBuilder& GraphBuilder, const FSceneTextures& SceneTextures)
{
FRHICommandListImmediate& RHICmdList = GraphBuilder.RHICmdList;

//If there's no cached parameters to use, skip
//If no Render Target is supplied in the cachedParams, skip
if (!(bCachedParamsAreValid && cachedParams.RenderTarget))
Expand All @@ -136,13 +138,12 @@ void FWhiteNoiseCSManager::Execute_RenderThread(FRHICommandListImmediate& RHICmd
//UnbindRenderTargets(RHICmdList);

//Specify the resource transition, we're executing this in post scene rendering so we set it to Graphics to Compute
RHICmdList.TransitionResource(EResourceTransitionAccess::ERWBarrier, EResourceTransitionPipeline::EGfxToCompute, ComputeShaderOutput->GetRenderTargetItem().UAV);

RHICmdList.TransitionResource(ERHIAccess::UAVCompute, ComputeShaderOutput->GetRenderTargetItem().GetRHI());

//Fill the shader parameters structure with tha cached data supplied by the client
FWhiteNoiseCS::FParameters PassParameters;
PassParameters.OutputTexture = ComputeShaderOutput->GetRenderTargetItem().UAV;
PassParameters.Dimensions = FVector2D(cachedParams.GetRenderTargetSize().X, cachedParams.GetRenderTargetSize().Y);
PassParameters.Dimensions = FVector2f(cachedParams.GetRenderTargetSize().X, cachedParams.GetRenderTargetSize().Y);
PassParameters.TimeStamp = cachedParams.TimeStamp;

//Get a reference to our shader type from global shader map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ class CUSTOMSHADERSDECLARATIONS_API FWhiteNoiseCSManager
//Reference to a pooled render target where the shader will write its output
TRefCountPtr<IPooledRenderTarget> ComputeShaderOutput;
public:
void Execute_RenderThread(FRHICommandListImmediate& RHICmdList, class FSceneRenderTargets& SceneContext);
void Execute_RenderThread(FRDGBuilder& GraphBuilder, const FSceneTextures& SceneTextures);
};