sculpto
Static Public Member Functions | List of all members
scl::assets_manager::shader_preprocessor Class Reference

#include <shaders_preprocessor.h>

Static Public Member Functions

static void SeparateShaders (const std::string &ShaderDebugName, const std::string &ShaderText, std::vector< shader_props > &Out)
 
static void ProcessIncludes (const std::string &ShaderDebugName, const std::string &ShaderFolderPath, std::string &ShaderText)
 

Detailed Description

Static shader preprocessor class.

Definition at line 17 of file shaders_preprocessor.h.

Member Function Documentation

◆ ProcessIncludes()

void scl::assets_manager::shader_preprocessor::ProcessIncludes ( const std::string &  ShaderDebugName,
const std::string &  ShaderFolderPath,
std::string &  ShaderText 
)
static
  • Process include directives in shader text.
Parameters
ShaderDebugName- debug name to show if error occures.
ShaderFolderPath- path to folder where shalder is located.
ShaderText[in,out]- text of shader to process.
Returns
None.

Definition at line 106 of file shaders_preprocessor.cpp.

109{
110 size_t offset;
111 while ((offset = ShaderText.find(shader_preprocessor::LexemInclude, 0)) != std::string::npos)
112 {
113 ShaderText.erase(offset, strlen(shader_preprocessor::LexemInclude));
114
115 int spaces_count = 0;
116 std::string include_path = ReadWord(ShaderText.begin() + offset, ShaderText.end(), &spaces_count);
117 ShaderText.erase(offset, spaces_count + include_path.size());
118
119 std::string file_name = std::string(include_path.begin() + 1, include_path.end() - 1);
120 std::filesystem::path file_path = ShaderFolderPath / std::filesystem::path(file_name);
121 ShaderText.insert(offset, LoadFile(file_path));
122 }
123}
std::string LoadFile(const std::filesystem::path &FilePath)
Definition: files_load.cpp:12

◆ SeparateShaders()

void scl::assets_manager::shader_preprocessor::SeparateShaders ( const std::string &  ShaderDebugName,
const std::string &  ShaderText,
std::vector< shader_props > &  Out 
)
static
  • Separate text to diffrent shader blocks function.
Parameters
ShaderDebugName- debug name to show if error occures.
ShaderText- inptu text to separate to blocks.
Out- output array of shaders text.

Definition at line 48 of file shaders_preprocessor.cpp.

51{
52 size_t offset = ShaderText.find(shader_preprocessor::LexemBlockStart, 0);
53 if (offset == std::string::npos)
54 {
55 SCL_CORE_ERROR("Error while preprocessing shader \"{}\".\nNo shader blcoks was found in text.", ShaderDebugName);
56 return;
57 }
58 std::string global_block(ShaderText.begin(), ShaderText.begin() + offset);
59
60 while (offset != std::string::npos)
61 {
62 size_t block_start_offset = ShaderText.find(' ', offset);
63 if (block_start_offset == std::string::npos)
64 SCL_CORE_ERROR("Error while preprocessing shader \"{}\".\nNo block text found, block skiped.", ShaderDebugName);
65
66 size_t block_end_offset = ShaderText.find(shader_preprocessor::LexemBlockEnd, offset);
67 if (block_end_offset == std::string::npos)
68 SCL_CORE_ERROR("Error while preprocessing shader \"{}\".\nNo block end lexem found. Block skiped.", ShaderDebugName);
69
70 if (block_end_offset != std::string::npos && block_start_offset != std::string::npos)
71 {
72 int spaces_count;
73 std::string block_text(ShaderText.begin() + block_start_offset, ShaderText.begin() + block_end_offset);
74 std::string block_shader_type_string = ReadWord(block_text.begin(), block_text.end(), &spaces_count);
75
76 shader_type block_shader_type;
77 bool block_shader_type_ok = true;
78 std::transform(block_shader_type_string.begin(), block_shader_type_string.end(), block_shader_type_string.begin(),
79 [](unsigned char c) { return std::tolower(c); });
80
81 switch (StringId(block_shader_type_string.c_str()))
82 {
83 case "compute"_id: block_shader_type = shader_type::COMPUTE; break;
84 case "vert"_id:
85 case "vertex"_id: block_shader_type = shader_type::VERTEX; break;
86 case "geom"_id:
87 case "geometry"_id: block_shader_type = shader_type::GEOMETRY; break;
88 case "pixel"_id:
89 case "frag"_id:
90 case "fragment"_id: block_shader_type = shader_type::PIXEL; break;
91 default:
92 SCL_CORE_ERROR("Error while preprocessing shader \"{}\".\nShader type \"{}\" is unknown. Block skipped.", ShaderDebugName, block_shader_type_string);
93 block_shader_type_ok = false;
94 }
95
96 if (block_shader_type_ok)
97 Out.push_back({
98 block_shader_type,
99 global_block + std::string(block_text.begin() + spaces_count + block_shader_type_string.size(), block_text.end())
100 });
101 }
102 offset = ShaderText.find(shader_preprocessor::LexemBlockStart, block_end_offset);
103 }
104}
#define SCL_CORE_ERROR(...)
Definition: log.h:44
shader_type
Definition: shader.h:17
constexpr string_id StringId(const char *Str)
Definition: string_id.h:87

The documentation for this class was generated from the following files: