SpiecsEngine
 
Loading...
Searching...
No Matches
ShaderLoader.cpp
Go to the documentation of this file.
1/**
2* @file ShaderLoader.cpp.
3* @brief The ShaderLoader Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
8#include "ShaderLoader.h"
9#include "Systems/ResourceSystem.h"
10#include "Core/Library/FileLibrary.h"
11#include "Resources/Shader/Shader.h"
12#include "Resources/Shader/ShaderCompiler.h"
13#include "Render/Vulkan/VulkanRenderBackend.h"
14#include "Render/Vulkan/VulkanShaderModule.h"
15#include "shaderc/shaderc.hpp"
16
17namespace Spices {
18
19 /**
20 * @brief Const variable: Original Shader File Path.
21 */
22 const std::string defaultShaderPath = "Shaders/src/";
23
24 bool ShaderLoader::Load(const std::string& fileName, ShaderStage stage, Shader* outShader)
25 {
27
28 bool isFind = false;
29 std::string filePath;
30 for (auto& it : ResourceSystem::GetSearchFolder())
31 {
32 filePath = it + defaultShaderPath + "Shader." + fileName + "." + ShaderHelper::ToString(stage);
33 if (FileLibrary::FileLibrary_Exists(filePath.c_str()))
34 {
35 isFind = true;
36 break;
37 }
38 }
39 if (!isFind)
40 {
41 std::stringstream ss;
42 ss << "Shader: " << fileName << "file is not found";
43
44 SPICES_CORE_WARN(ss.str());
45
46 return false;
47 }
48
49 /**
50 * @brief Read file as bytes.
51 */
52 std::ifstream stream(filePath);
53 std::stringstream strStream;
54 strStream << stream.rdbuf();
55
56 /**
57 * @brief Compile to spv.
58 */
59 std::vector<uint8_t> spirv;
60 ShaderCompiler::CompileToSPV(strStream.str(), stage, fileName, spirv);
61
62 /**
63 * @brief Create shader module.
64 */
65 outShader->m_ShaderModule = std::make_shared<VulkanShaderModule>(VulkanRenderBackend::GetState(), fileName, stage, spirv, filePath);
66
67 return true;
68 }
69}
#define SPICES_PROFILE_ZONE
static bool Load(const std::string &fileName, ShaderStage stage, Shader *outShader)
Public called API, it is entrance.
ShaderLoader Class. This class only defines static function for load data from shader file.
Shader Class.
Definition Shader.h:19
ShaderStage
enum of shader stage.
const std::string defaultShaderPath
Const variable: Original Shader File Path.