Compile Unreal 4.17 Shader

Hi,

I am writing a Shader Plugin for my project. Currently, I am following this tutorial on Github:

And I think I place the shader files in the right place, the path looks like this:

C:\Users\Admin\Documents\AnimatedAvatar\Plugins\DXPixelShader\Shaders\Private

And here is how I compile the code in c++:

IMPLEMENT_SHADER_TYPE(, FVertexShaderExample, TEXT(“/Plugins/DXPixelShader/Private/PixelShader.usf”), TEXT(“MainVertexShader”), SF_Vertex);

However, I got errors that are listed in the following:

[2017.10.24-04.09.24:368][
0]LogShaders: Error: Can’t map virtual
shader source path
“/Plugins/DXPixelShader/Private/PixelShader.usf”.
Directory mappings are: /Engine →
…/…/…/Engine/Shaders /Project →
…/…/…/…/…/…/Users/Admin/Documents/AnimatedAvatar/Shaders
/Plugin/ComputeShader →
…/…/…/…/…/…/Users/Admin/Documents/AnimatedAvatar/Plugins/DXComputeShader/Shaders
/Plugin/PixelShader →
…/…/…/…/…/…/Users/Admin/Documents/AnimatedAvatar/Plugins/DXPixelShader/Shaders
Fatal error:
[File:D:\Build++UE4+Release-4.17+Compile\Sync\Engine\Source\Runtime\ShaderCore\Private\ShaderCore.cpp]
[Line: 476] Couldn’t find source file
of virtual shader path
‘/Plugins/DXPixelShader/Private/PixelShader.usf’

I searched on Google for hours but can not figure this out.

Thanks,
Z

Just ran into the same problem. I failed to properly name MyShader.USF (Windows was hiding the file extension).
In VS under Shader/Private it was called MyShader.USF.txt. Renameing to “MyShader.USF” fixed the problem.

4.17 Release notes say to add the shader under /Source folder (with the wrong path, by the way). Then, official tutorials after its release say put it under the plugin folder, under Shader/ and add the complete path. But the path they give is also wrong. A clarification here would be great

Any news about this @HolyKaisar ? TEXT("/Plugins/DXPixelShader/Private/PixelShader.usf") ← Shouldn’t it be /Plugins/DXPixelShader/Shaders/Private/PixelShader.usf? This path doesn’t work in my case either

What was the full path, please? Did you include Plugins/ as said (wrong) in the release notes?

I think it did not put it within plugins but one level above. Will check tomorrow if you like.

Hey not sure if you managed to figure this out, but I ran into this problem today.

In your plugins folder, you need to create a “Shaders” directory, and add your .USF/H files in there.

Once you start to load the shader files, you’ll notice in the output that the engine is virtually mapping this folder to have an alternate name, in your case it’s /Plugin/PixelShader as seen from this line of the output you pasted:

/Plugin/PixelShader -> ../../../../../../Users/Admin/Documents/AnimatedAvatar/Plugins/DXPixelShader/Shaders

Now in your shader declaration macro you want to reference that virtual directory as the base for the shader file you wish to load in your case it would look like this:

  IMPLEMENT_SHADER_TYPE(, FVertexShaderExample, TEXT("/Plugin/PixelShader/Private/PixelShader.usf"), TEXT("MainVertexShader"), SF_Vertex);

This is a bit late, but had a similar problem. Had to add a new shader source directory mapping during StartupModule().

Since we were using it in our game, we had to extend FDefaultGameModeImpl and override StartupModule, then add

AddShaderSourceDirectoryMapping(TEXT("/Project"), FPaths::Combine(*(FPaths::GameDir()), TEXT("Shaders")));

to StartupModule().

Caused by 4.21 change: Fix: Replaced automatic virtual shader directory mapping with manual approach in StartupModule(). It should reduce the amount of directory exists queries at Editor startup.

Can you explain this? What’s StartupModule? How do I override that?

see this post:

Virtual Shader Source Path - Link custom Shaders 4.21

https://forums.unrealengine.com/development-discussion/rendering/1562454-virtual-shader-source-path-link-custom-shaders-4-21