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 "BsSLImporter.h"
4#include "FileSystem/BsDataStream.h"
5#include "FileSystem/BsFileSystem.h"
6#include "BsSLFXCompiler.h"
7#include "Importer/BsShaderImportOptions.h"
8
9namespace bs
10{
11 bool SLImporter::isExtensionSupported(const String& ext) const
12 {
13 String lowerCaseExt = ext;
14 StringUtil::toLowerCase(lowerCaseExt);
15
16 return lowerCaseExt == u8"bsl";
17 }
18
19 bool SLImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
20 {
21 return true; // Plain-text so I don't even check for magic number
22 }
23
24 SPtr<Resource> SLImporter::import(const Path& filePath, SPtr<const ImportOptions> importOptions)
25 {
26 String source;
27 {
28 Lock fileLock = FileScheduler::getLock(filePath);
29
30 SPtr<DataStream> stream = FileSystem::openFile(filePath);
31 source = stream->getAsString();
32 }
33
34 SPtr<const ShaderImportOptions> io = std::static_pointer_cast<const ShaderImportOptions>(importOptions);
35 String shaderName = filePath.getFilename(false);
36 BSLFXCompileResult result = BSLFXCompiler::compile(shaderName, source, io->getDefines(), io->languages);
37
38 if (result.shader != nullptr)
39 result.shader->setName(shaderName);
40
41 if(!result.errorMessage.empty())
42 {
43 String file;
44 if (result.errorFile.empty())
45 file = filePath.toString();
46 else
47 file = result.errorFile;
48
49 LOGERR("Compilation error when importing shader \"" + file + "\":\n" + result.errorMessage + ". Location: " +
50 toString(result.errorLine) + " (" + toString(result.errorColumn) + ")");
51 }
52
53 return result.shader;
54 }
55
56 SPtr<ImportOptions> SLImporter::createImportOptions() const
57 {
58 return bs_shared_ptr_new<ShaderImportOptions>();
59 }
60}