1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Resources/BsEngineShaderIncludeHandler.h"
4#include "Resources/BsResources.h"
5#include "Resources/BsBuiltinResources.h"
6#include "Importer/BsImporter.h"
7#include "FileSystem/BsFileSystem.h"
8
9namespace bs
10{
11 HShaderInclude EngineShaderIncludeHandler::findInclude(const String& name) const
12 {
13 Path path = toResourcePath(name);
14
15 if (path.isEmpty())
16 return HShaderInclude();
17
18 if (name.size() >= 8)
19 {
20 if (name.substr(0, 8) == "$ENGINE$" || name.substr(0, 8) == "$EDITOR$")
21 return static_resource_cast<ShaderInclude>(Resources::instance().load(path));
22 }
23
24 for(auto& folder : mSearchPaths)
25 {
26 Path entry = folder;
27 entry.append(name);
28
29 if(FileSystem::exists(entry))
30 {
31 path = entry;
32 break;
33 }
34 }
35
36 path = Paths::findPath(path);
37 return Importer::instance().import<ShaderInclude>(path);
38 }
39
40 Path EngineShaderIncludeHandler::toResourcePath(const String& name)
41 {
42 if (name.substr(0, 8) == "$ENGINE$")
43 {
44 if (name.size() > 8)
45 {
46 Path fullPath = BuiltinResources::getShaderIncludeFolder();
47 Path includePath = name.substr(9, name.size() - 9);
48
49 fullPath.append(includePath);
50 fullPath.setFilename(includePath.getFilename() + ".asset");
51
52 return fullPath;
53 }
54 }
55#ifdef BS_IS_ASSET_TOOL
56 else if (name.substr(0, 8) == "$EDITOR$")
57 {
58 if (name.size() > 8)
59 {
60 Path fullPath = BuiltinResources::getEditorShaderIncludeFolder();
61 Path includePath = name.substr(9, name.size() - 9);
62
63 fullPath.append(includePath);
64 fullPath.setFilename(includePath.getFilename() + ".asset");
65
66 return fullPath;
67 }
68 }
69#endif
70
71 return name;
72 }
73}
74