├── Config └── FilterPlugin.ini ├── Source └── ShaderDirectoryMapper │ ├── ShaderDirectoryMapper.Build.cs │ └── Private │ ├── ShaderDirectoryMapper.h │ └── ShaderDirectoryMapper.cpp ├── ShaderDirectoryMapper.uplugin ├── LICENSE └── README.md /Config/FilterPlugin.ini: -------------------------------------------------------------------------------- 1 | [FilterPlugin] 2 | /README.md 3 | /LICENSE 4 | -------------------------------------------------------------------------------- /Source/ShaderDirectoryMapper/ShaderDirectoryMapper.Build.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2024-25 Amit Kumar Mehar. All Rights Reserved. 2 | 3 | using System.IO; 4 | using UnrealBuildTool; 5 | 6 | public class ShaderDirectoryMapper : ModuleRules 7 | { 8 | public ShaderDirectoryMapper(ReadOnlyTargetRules Target) : base(Target) 9 | { 10 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 11 | 12 | PrivateDependencyModuleNames.AddRange( 13 | new string[] 14 | { 15 | "Core", 16 | "Engine", 17 | "Projects", 18 | "RenderCore", 19 | "CoreUObject", 20 | "DeveloperSettings", 21 | } 22 | ); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ShaderDirectoryMapper.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion": 3, 3 | "Version": 2, 4 | "VersionName": "1.1", 5 | "FriendlyName": "ShaderDirectoryMapper", 6 | "Description": "Plugin for registering shader directories.", 7 | "Category": "Rendering", 8 | "CreatedBy": "Amit Mehar", 9 | "CreatedByURL": "https://twitter.com/amu_mhr", 10 | "DocsURL": "", 11 | "MarketplaceURL": "", 12 | "SupportURL": "https://github.com/amuTBKT/ShaderDirectoryMapper/issues", 13 | "EnabledByDefault": false, 14 | "CanContainContent": false, 15 | "IsBetaVersion": false, 16 | "Installed": false, 17 | "Modules": [ 18 | { 19 | "Name": "ShaderDirectoryMapper", 20 | "Type": "Editor", 21 | "LoadingPhase": "PostConfigInit", 22 | "PlatformAllowList": [ "Win64", "Linux", "Mac" ] 23 | } 24 | ], 25 | "Plugins": [ 26 | ] 27 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024-25 Amit Kumar Mehar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Source/ShaderDirectoryMapper/Private/ShaderDirectoryMapper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024-25 Amit Kumar Mehar. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "Engine/DeveloperSettings.h" 7 | #include "ShaderDirectoryMapper.generated.h" 8 | 9 | UCLASS(config = Engine, defaultconfig, DisplayName = "Shader Directory Mapper") 10 | class UShaderDirectoryMapperSettings : public UDeveloperSettings 11 | { 12 | GENERATED_BODY() 13 | 14 | public: 15 | virtual FName GetCategoryName() const override { return TEXT("Plugins"); } 16 | virtual void PostInitProperties() override 17 | { 18 | Super::PostInitProperties(); 19 | 20 | #if WITH_EDITOR 21 | if (IsTemplate()) 22 | { 23 | ImportConsoleVariableValues(); 24 | } 25 | #endif 26 | } 27 | 28 | #if WITH_EDITOR 29 | virtual void PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent) override 30 | { 31 | Super::PostEditChangeProperty(PropertyChangedEvent); 32 | 33 | const FProperty* MemberPropertyThatChanged = PropertyChangedEvent.MemberProperty; 34 | const FName MemberPropertyName = MemberPropertyThatChanged ? MemberPropertyThatChanged->GetFName() : NAME_None; 35 | 36 | if (PropertyChangedEvent.Property) 37 | { 38 | ExportValuesToConsoleVariables(PropertyChangedEvent.Property); 39 | } 40 | } 41 | #endif 42 | 43 | public: 44 | // If initiailized, only plugins specified in this list will be registered. 45 | UPROPERTY(config, EditAnywhere, Category = "Plugin") 46 | TSet AllowedPlugins; 47 | 48 | // Plugins to never register. 49 | UPROPERTY(config, EditAnywhere, Category = "Plugin") 50 | TSet DisallowedPlugins; 51 | }; 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShaderDirectoryMapper 2 | Unreal Engine plugin for automatically registering plugin and project shaders. 3 | 4 | # Motivation 5 | In order to use custom shaders, the engine expects them to be located in a directory virtually mapped with the shader compiler.
6 | While it's easy to add virtual shader directories, it can be a bit tedious. 7 | * Requires code compilation so doesn't work with Content only plugins. 8 | * At minimum it requires to add `Plugin.build.cs`, `PluginModule.cpp` and module dependencies.
9 | And this setup has to be repeated for every plugin that uses custom shaders! 10 | 11 | # How it Works 12 | The plugin hooks into module loading phase complete event and checks all enabled plugins and project for `/Shaders` directory. 13 | If it finds any relevant directory and it's not already registered by the plugin itself, it adds virtual mapping for it. 14 | * Plugins paths are registered with `/Plugin/{PluginName}` prefix. 15 | * Game/project path is registered with `/Game` prefix. 16 | 17 | ***Note*** The shader directory name should be "Shaders" 18 | 19 | ### Example 20 | For a plugin named "TestPlugin" and directory structure 21 | ``` 22 | TestPlugin 23 | -> Shaders 24 | -> Test.ush 25 | ``` 26 | The virtual path would be "/Plugin/TestPlugin" and header include path would be "/Plugin/TestPlugin/Test.ush" 27 | 28 | 29 | For a project named "MyProject" and directory structure 30 | ``` 31 | MyProject 32 | -> Shaders 33 | -> Test.ush 34 | ``` 35 | The virtual path would be "/Game" and header include path would be "/Game/Test.ush" 36 | 37 | # Settings 38 | The plugin settings can be configured from `ProjectSettings->Plugins->Shader Directory Mapper` page, which allows to disable certain plugins or only register specified plugins. 39 | 40 | # Contributions 41 | Special thanks to [Victor Careil](https://x.com/phyronnaz) for suggesting virtual shader path prefixes. 42 | -------------------------------------------------------------------------------- /Source/ShaderDirectoryMapper/Private/ShaderDirectoryMapper.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024-25 Amit Kumar Mehar. All Rights Reserved. 2 | 3 | #include "ShaderDirectoryMapper.h" 4 | 5 | #include "Misc/Paths.h" 6 | #include "ShaderCore.h" 7 | #include "Misc/ConfigCacheIni.h" 8 | #include "Modules/ModuleManager.h" 9 | #include "Modules/ModuleInterface.h" 10 | #include "Interfaces/IPluginManager.h" 11 | #include "Runtime/Launch/Resources/Version.h" 12 | 13 | DECLARE_LOG_CATEGORY_EXTERN(LogShaderDirectoryMapper, Log, All); 14 | DEFINE_LOG_CATEGORY(LogShaderDirectoryMapper); 15 | 16 | class FShaderDirectoryMapperModule : public IModuleInterface 17 | { 18 | // workaround for reading DeveloperSettings from config file as UObjects are not initialized during PostConfigInit 19 | struct FSettings 20 | { 21 | FSettings(const FConfigSection* Section) 22 | { 23 | if (!Section) 24 | { 25 | return; 26 | } 27 | 28 | for (FConfigSectionMap::TConstIterator It(*Section); It; ++It) 29 | { 30 | const FName KeyName = FName(It.Key().GetPlainNameString()); 31 | 32 | if (KeyName == GET_MEMBER_NAME_CHECKED(UShaderDirectoryMapperSettings, AllowedPlugins) || 33 | KeyName == GET_MEMBER_NAME_CHECKED(UShaderDirectoryMapperSettings, DisallowedPlugins)) 34 | { 35 | TSet& DstSet = (KeyName == GET_MEMBER_NAME_CHECKED(UShaderDirectoryMapperSettings, AllowedPlugins)) ? 36 | PluginsToRegister : PluginsToNeverRegister; 37 | 38 | FString ValueString = It.Value().GetValue(); 39 | if (ValueString.StartsWith("(\"") && ValueString.EndsWith("\")")) //Key=("Value") 40 | { 41 | TArray Values; 42 | ValueString.LeftChop(2).RightChop(2).ParseIntoArrayWS(Values, TEXT(","), true); 43 | for (const FString& Value : Values) 44 | { 45 | DstSet.Add(Value); 46 | } 47 | } 48 | else if (ValueString.Len() > 2) //Key=(Value) 49 | { 50 | DstSet.Add(ValueString); 51 | } 52 | } 53 | } 54 | } 55 | 56 | FORCEINLINE bool IsPluginAllowed(const FString& PluginName) const 57 | { 58 | if (!PluginsToRegister.IsEmpty()) 59 | { 60 | return PluginsToRegister.Contains(PluginName); 61 | } 62 | 63 | if (!PluginsToNeverRegister.IsEmpty()) 64 | { 65 | return !PluginsToNeverRegister.Contains(PluginName); 66 | } 67 | 68 | return true; 69 | } 70 | 71 | private: 72 | TSet PluginsToRegister; 73 | TSet PluginsToNeverRegister; 74 | }; 75 | 76 | virtual void StartupModule() override 77 | { 78 | // wait for PostConfigInit loading phase to register directories 79 | // this is required to make sure we don't add duplicates for plugins registering shaders manually (in StartupModule) 80 | IPluginManager::Get().OnLoadingPhaseComplete().AddRaw(this, &FShaderDirectoryMapperModule::OnPluginLoadingPhaseComplete); 81 | } 82 | 83 | void OnPluginLoadingPhaseComplete(ELoadingPhase::Type LoadingPhase, bool bSuccess) 84 | { 85 | if (!bSuccess) 86 | { 87 | return; 88 | } 89 | if (LoadingPhase != ELoadingPhase::PostConfigInit) 90 | { 91 | return; 92 | } 93 | IPluginManager::Get().OnLoadingPhaseComplete().RemoveAll(this); 94 | 95 | const FConfigSection* Section = GConfig->GetSection(TEXT("/Script/ShaderDirectoryMapper.ShaderDirectoryMapperSettings"), false, *GEngineIni); 96 | const FShaderDirectoryMapperModule::FSettings Settings{ Section }; 97 | 98 | TArray ExistingShaderDirectories; 99 | for (const auto& Itr : AllShaderSourceDirectoryMappings()) 100 | { 101 | // skip autogenerated directories (uproject always addds "ProjectDirectory/Intermediate/ShaderAutogen") 102 | if (!Itr.Value.Contains(TEXT("/Intermediate")) && 103 | !Itr.Value.Contains(TEXT("/ShaderAutogen"))) 104 | { 105 | ExistingShaderDirectories.Add(Itr.Value); 106 | } 107 | } 108 | 109 | // returns true if any subdirectory of "BaseDirectory" is registered (assumes plugins only register one directory) 110 | auto IsPluginDirectoryAlreadyRegistered = [&](const FString& PluginBaseDirectory) -> bool 111 | { 112 | const bool bAlreadyAdded = ExistingShaderDirectories.ContainsByPredicate( 113 | [&](const FString& Directory) 114 | { 115 | return Directory.StartsWith(PluginBaseDirectory, ESearchCase::IgnoreCase); 116 | }); 117 | return bAlreadyAdded; 118 | }; 119 | 120 | // returns true only if it encounters exact path (otherwise can return false positives for ProjDir/Plugin/Shaders) 121 | auto IsProjectDirectoryAlreadyRegistered = [&](const FString& ProjectBaseDirectory) -> bool 122 | { 123 | const bool bAlreadyAdded = ExistingShaderDirectories.ContainsByPredicate( 124 | [&](const FString& Directory) 125 | { 126 | return Directory.Equals(ProjectBaseDirectory, ESearchCase::IgnoreCase); 127 | }); 128 | return bAlreadyAdded; 129 | }; 130 | 131 | auto TryAddingShaderDirectoryMapping = [](const FString& VirtualShaderDirectory, const FString& RealShaderDirectory) 132 | { 133 | if (IPlatformFile::GetPlatformPhysical().DirectoryExists(*RealShaderDirectory)) 134 | { 135 | AddShaderSourceDirectoryMapping(VirtualShaderDirectory, RealShaderDirectory); 136 | UE_LOG(LogShaderDirectoryMapper, Log, TEXT("Adding \"%s\" shader directory as \"%s\""), *RealShaderDirectory, *VirtualShaderDirectory); 137 | 138 | #if ((ENGINE_MAJOR_VERSION * 100u + ENGINE_MINOR_VERSION) > 504) //(Version > 5.4) 139 | const FString SharedSourceDirectory = FString::Printf(TEXT("%s/Shared"), *RealShaderDirectory); 140 | if (IPlatformFile::GetPlatformPhysical().DirectoryExists(*SharedSourceDirectory)) 141 | { 142 | const FString SharedSourceVirtualDirectory = FString::Printf(TEXT("%s/Shared/"), *VirtualShaderDirectory); 143 | AddShaderSourceSharedVirtualDirectory(SharedSourceVirtualDirectory); 144 | UE_LOG(LogShaderDirectoryMapper, Log, TEXT("Adding \"%s\" shared source directory as \"%s\""), *SharedSourceDirectory, *SharedSourceVirtualDirectory); 145 | } 146 | #endif 147 | } 148 | }; 149 | 150 | // add mapping for plugins 151 | for (const TSharedRef& Plugin : IPluginManager::Get().GetEnabledPlugins()) 152 | { 153 | const FString& PluginName = Plugin->GetName(); 154 | if (!Settings.IsPluginAllowed(PluginName)) 155 | { 156 | continue; 157 | } 158 | 159 | const FString PluginBaseDirectory = Plugin->GetBaseDir(); 160 | if (!IsPluginDirectoryAlreadyRegistered(PluginBaseDirectory)) 161 | { 162 | const FString PluginShaderDirectory = FPaths::Combine(PluginBaseDirectory, TEXT("Shaders")); 163 | const FString VirtualPath = FString::Printf(TEXT("/Plugin/%s"), *PluginName); 164 | TryAddingShaderDirectoryMapping(VirtualPath, PluginShaderDirectory); 165 | } 166 | } 167 | 168 | // add mapping for uproject 169 | const FString ProjectBaseDirectory = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); 170 | if (!IsProjectDirectoryAlreadyRegistered(ProjectBaseDirectory)) 171 | { 172 | const FString ProjectShaderDirectory = FPaths::Combine(ProjectBaseDirectory, TEXT("Shaders")); 173 | const FString VirtualPath = FString(TEXT("/Game")); 174 | TryAddingShaderDirectoryMapping(VirtualPath, ProjectShaderDirectory); 175 | } 176 | } 177 | }; 178 | 179 | IMPLEMENT_MODULE(FShaderDirectoryMapperModule, ShaderDirectoryMapper) 180 | --------------------------------------------------------------------------------