-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathShaderCodeBuilder.cpp
More file actions
133 lines (108 loc) · 4.11 KB
/
ShaderCodeBuilder.cpp
File metadata and controls
133 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "ShaderCodeBuilder.hpp"
#include "FileUtility.hpp"
#include "spdlog/spdlog.h"
#include "tracy/Tracy.hpp"
#include <unordered_set>
#include <regex>
namespace {
static const std::filesystem::path kShadersPath{"./shaders"};
// https://stackoverflow.com/questions/26492513/write-c-regular-expression-to-match-a-include-preprocessing-directive
// https://stackoverflow.com/questions/42139302/using-regex-to-filter-for-preprocessor-directives
[[nodiscard]] auto findIncludes(const std::string &src) {
// Source: #include "filename"
// [1] "
// [2] filename
// [3] "
// TODO: Ignore directives inside a comment
static const std::regex kIncludePattern(
R"(#\s*include\s*([<"])([^>"]+)([>"]))");
return std::sregex_iterator{src.cbegin(), src.cend(), kIncludePattern};
}
[[nodiscard]] std::tuple<std::string, bool> statInclude(const std::smatch &m) {
const auto isBetween = [&](auto &&a, auto &&b) {
return m[1].compare(a) == 0 && m[3].compare(b) == 0;
};
if (isBetween("\"", "\""))
return {m[2].str(), true};
else if (isBetween("<", ">"))
return {m[2].str(), false};
return {"", false};
}
void resolveInclusions(std::string &src,
std::unordered_set<std::size_t> &alreadyIncluded,
int32_t level, const std::filesystem::path ¤tDir) {
ptrdiff_t offset{0};
// Copy to avoid dereference of invalidated string iterator
std::string temp{src};
for (auto match = findIncludes(temp); match != std::sregex_iterator{};
++match) {
const auto lineLength = match->begin()->length();
auto [filename, isRelative] = statInclude(*match);
if (filename.empty()) {
SPDLOG_WARN("Ill-formed directive: {}", match->begin()->str());
src.erase(match->position() + offset, lineLength);
offset -= lineLength;
continue;
}
const auto next =
((isRelative ? currentDir : kShadersPath) / filename).lexically_normal();
if (!alreadyIncluded.insert(std::filesystem::hash_value(next)).second) {
SPDLOG_TRACE("Already included: {}, skipping", filename);
src.erase(match->position() + offset, lineLength);
offset -= lineLength;
continue;
}
auto codeChunk = readText(next);
if (!codeChunk.empty()) {
resolveInclusions(codeChunk, alreadyIncluded, level + 1,
next.parent_path());
} else {
codeChunk = std::format(R"(#error "{}" not found!)", filename);
}
src.replace(match->position() + offset, lineLength, codeChunk);
offset += codeChunk.length() - lineLength;
}
}
} // namespace
//
// ShaderCodeBuilder class:
//
ShaderCodeBuilder &
ShaderCodeBuilder::setDefines(const std::vector<std::string> &defines) {
m_defines = defines;
return *this;
}
ShaderCodeBuilder &ShaderCodeBuilder::addDefine(const std::string &s) {
m_defines.emplace_back(s);
return *this;
}
ShaderCodeBuilder &ShaderCodeBuilder::replace(const std::string &phrase,
const std::string_view s) {
m_patches.insert_or_assign(phrase, s);
return *this;
}
std::string ShaderCodeBuilder::build(const std::filesystem::path &p) {
const auto filePath = kShadersPath / p;
auto sourceCode = readText(filePath);
if (sourceCode.empty()) throw std::runtime_error{"Empty string"};
ZoneScoped;
std::unordered_set<std::size_t> alreadyIncluded;
resolveInclusions(sourceCode, alreadyIncluded, 0, filePath.parent_path());
for (const auto &[phrase, patch] : m_patches) {
const auto pos = sourceCode.find(phrase);
if (pos != std::string::npos)
sourceCode.replace(pos, phrase.length(), patch);
}
auto versionDirectivePos = sourceCode.find("#version");
assert(versionDirectivePos != std::string::npos);
if (!m_defines.empty()) {
std::ostringstream oss;
std::transform(m_defines.cbegin(), m_defines.cend(),
std::ostream_iterator<std::string>{oss, "\n"},
[](const auto &s) { return std::format("#define {}", s); });
const auto endOfVersionLine =
sourceCode.find_first_of("\n", versionDirectivePos) + 1;
sourceCode.insert(endOfVersionLine, oss.str());
}
return sourceCode;
}