MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / Import

Method Import

Source/Engine/ContentImporters/ImportShader.cpp:14–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include "Engine/Content/Assets/Shader.h"
13
14CreateAssetResult ImportShader::Import(CreateAssetContext& context)
15{
16 // Base
17 IMPORT_SETUP(Shader, 20);
18 const int32 SourceCodeChunk = 15;
19 context.SkipMetadata = true;
20
21 // Read text (handles any Unicode convert into ANSI)
22 StringAnsi sourceCodeText;
23 if (File::ReadAllText(context.InputPath, sourceCodeText))
24 return CreateAssetResult::InvalidPath;
25
26 // Load source code
27 if (context.AllocateChunk(SourceCodeChunk))
28 return CreateAssetResult::CannotAllocateChunk;
29 const auto sourceCodeSize = sourceCodeText.Length();
30 if (sourceCodeSize < 10)
31 {
32 LOG(Warning, "Empty shader source file.");
33 return CreateAssetResult::Error;
34 }
35
36 // Ensure the source code has an empty line at the end (expected by glslang)
37 auto sourceCodeChunkSize = sourceCodeSize + 1;
38 if (sourceCodeText[sourceCodeSize - 1] != '\n')
39 sourceCodeChunkSize++;
40
41 const auto& sourceCodeChunk = context.Data.Header.Chunks[SourceCodeChunk];
42 sourceCodeChunk->Data.Allocate(sourceCodeChunkSize);
43 const auto sourceCode = sourceCodeChunk->Get();
44 Platform::MemoryCopy(sourceCode, sourceCodeText.Get(), sourceCodeSize);
45 sourceCode[sourceCodeChunkSize - 2] = '\n';
46
47 // Encrypt source code
48 Encryption::EncryptBytes(sourceCode, sourceCodeChunkSize - 1);
49 sourceCode[sourceCodeChunkSize - 1] = 0;
50
51 // Set Custom Data with Header
52 ShaderStorage::Header20 shaderHeader;
53 Platform::MemoryClear(&shaderHeader, sizeof(shaderHeader));
54 context.Data.CustomData.Copy(&shaderHeader);
55
56#if COMPILE_WITH_SHADER_CACHE_MANAGER
57 // Invalidate shader cache
58 ShaderCacheManager::RemoveCache(context.Data.Header.ID);
59#endif
60
61 return CreateAssetResult::Ok;
62}
63
64#endif

Callers

nothing calls this directly

Calls 6

MemoryClearFunction · 0.85
AllocateChunkMethod · 0.45
LengthMethod · 0.45
AllocateMethod · 0.45
GetMethod · 0.45
CopyMethod · 0.45

Tested by

no test coverage detected