Undeclared identifier Texture2DSample in custom Global Shader for Plugin.

So far I’ve succeeded in creating a UE Plugin with a custom Global Shader class that can be used via Blueprint to set a Render Texture Target to a custom color.

I’m now passing a Texture2D reference to my shader and attempting to just simply sample the texture in the shader.

void MainVS(
in float4 InPosition : ATTRIBUTE0,
in float2 InUV : ATTRIBUTE1,
out float2 OutUV : TEXCOORD0,
out float4 Output : SV_POSITION
Output = InPosition;
OutUV = InUV;

Texture2D MyTex;
SamplerState MySampler;
float4 MainPS(  
in float2 OutUV : TEXCOORD0,
out float4 OutColor : SV_Target0
 OutColor = Texture2DSample(MyTex, MySampler, OutUV);

However, this fails with

X3004 - Undeclared Identifier "Texture2DSample"

By which I figure it is missing the definition that is inside “Common.ush” in the Engine shaders folder.

So I update the shader with:


But now I get a huge amount of errors about inability to load from /Engine/Generated/… USH files. I can’t copy and select the text (that would be too easy), so I have to resort a screenshot.

I’m clearly going about this the wrong way, but I’m unable to find any examples online and the UE documentation has been very out of date and finding the information to get this far has been immensely frustrating.

In my plugin module, I set up the virtual directories for my shader manually in the constructor:

void FCloudModule::StartupModule()
    FString PluginDir = FPaths::Combine(FPaths::ProjectPluginsDir(), TEXT("Cloud"));
    FString PluginShaderDir = FPaths::Combine(PluginDir, TEXT("Shaders"));
    AddShaderSourceDirectoryMapping(TEXT("/Plugin/Cloud"), PluginShaderDir);

As usual… I figure it out 5 mins after posting…

Turns out I need to include Platform.ush from the Public folder.


(and for bonus points, the observant among you will also realise MainPS should be void, not float4)