SpiecsEngine
 
Loading...
Searching...
No Matches
ShaderCompiler.cpp
Go to the documentation of this file.
1/**
2* @file ShaderCompiler.cpp.
3* @brief The ShaderCompiler Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9#include "ShaderHelper.h"
10
11#include <glslc/src/file_includer.h>
12#include <libshaderc_util/include/libshaderc_util/file_finder.h>
13
14namespace Spices {
15
16 void ShaderCompiler::CompileToSPV(const std::string& data, const ShaderStage& stage, const std::string& name, std::vector<uint8_t>& spirv)
17 {
19
20 /**
21 * @brief Instance Compiler.
22 */
23 shaderc::Compiler compiler;
24 shaderc::CompileOptions options;
25
26 /**
27 * @brief Shader Header Search folder and processer.
28 */
29 shaderc_util::FileFinder fileFinder;
30 fileFinder.search_path().push_back(SPICES_ENGINE_ASSETS_PATH + "Shaders/src");
31
32#ifdef SPICES_DEBUG
33
34 options.SetGenerateDebugInfo(); // -g
35
36#endif
37
38 /**
39 * @brief Compile Settings.
40 */
41 options.SetOptimizationLevel(shaderc_optimization_level_performance); // -o
42 options.SetTargetSpirv(shaderc_spirv_version_1_6); // --target-env=spv1.6
43 options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_3); // --target-env=vulkan1.3
44 options.SetIncluder(std::make_unique<glslc::FileIncluder>(&fileFinder));
45
46 /**
47 * @brief Runtime Compile.
48 */
49 shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(data, ShaderHelper::ToShaderCKind(stage), name.c_str(), options);
50 if (module.GetCompilationStatus() != shaderc_compilation_status_success)
51 {
52 std::stringstream ss;
53 ss << "Error compiling module: " << module.GetErrorMessage();
54
55 SPICES_CORE_ERROR(ss.str())
56 }
57
58 /**
59 * @brief Get Compiled spirv code.
60 */
61 std::vector<uint32_t> code32 = { module.cbegin(), module.cend() };
62 spirv.resize(code32.size() * 4);
63 memcpy(spirv.data(), code32.data(), spirv.size());
64 }
65}
#define SPICES_PROFILE_ZONE
Wrapper of shaderc compiler.
ShaderStage
enum of shader stage.