4

I am working on a project based on Unreal Engine 4 where I need to implement a customized directx11 compute shader. I am following this tutorial:Unreal HLSL Tutorial to write a customized compute shader in Unreal Engine for my project.

I used the following codes to bind the resource to the compute shader:

InputSurface.Bind(Initializer.ParameterMap, TEXT("InputSurfaceCS"));
OutputSurface.Bind(Initializer.ParameterMap, TEXT("OutputSurface"));

However, I realized that only OutputSurface is recognized by the compiler and the compiler assigned a resource to it, but the compiler didn't assign any resource to the InputSurfaceCS resource.

In the compute shader .usf file, I decalared these parameters like this:

Texture2D<float> InputSurfaceCS;
RWTexture2D<float> OutputSurface;

Does anyone run into this problem before? or know how to solve it?

Thanks, ZH

NULLPTR
  • 215
  • 1
  • 3
  • 12

1 Answers1

4

I finally solved this problem. In Unreal Engine 4, it uses the ShaderGraph to manage all shaders and uses the ParameterMap to manage all resources that declared in shaders. If a shader is needed to be used and it is compiled before, Unreal Engine 4 will just read the cached shader file and the corresponding shader parameters file. So, every time you want to bind a new shader resource (e.g. Texture2D, RWStructuredBuffer, etc.), you have to make the ParameterMap memorize your new shader resources. You can apply the following code to your own shader declaration:

virtual bool Serialize(FArchive& Ar) override
{
    bool bShaderHasOutdatedParams = FGlobalShader::Serialize(Ar);

    Ar << YourResourceName1 << YourResourceName2 << YourResourceName3 << ......;

    return bShaderHasOutdatedParams;
}
NULLPTR
  • 215
  • 1
  • 3
  • 12