Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
DrawParams.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
7
8#include "./DrawParams.hpp"
9
10#include <cctype>
11
12namespace esengine {
13
14const DrawParamSlot* DrawParamsLayout::find(const std::string& name) const {
15 for (const auto& s : slots) {
16 if (s.name == name) return &s;
17 }
18 return nullptr;
19}
20
21void drawParamSizeAlign(DrawParamType t, u32& size, u32& align) {
22 switch (t) {
24 case DrawParamType::Int: size = 4; align = 4; break;
25 case DrawParamType::Vec2: size = 8; align = 8; break;
26 case DrawParamType::Vec3: size = 12; align = 16; break;
27 case DrawParamType::Vec4: size = 16; align = 16; break;
28 // std140 matrices are column arrays with vec4 stride.
29 case DrawParamType::Mat3: size = 48; align = 16; break;
30 case DrawParamType::Mat4: size = 64; align = 16; break;
31 }
32}
33
34namespace {
35
36struct LooseDecl {
37 std::string name;
38 std::string member;
40 usize begin = 0;
41 usize end = 0;
42};
43
44bool typeFromToken(const std::string& tok, DrawParamType& out) {
45 if (tok == "float") { out = DrawParamType::Float; return true; }
46 if (tok == "int") { out = DrawParamType::Int; return true; }
47 if (tok == "vec2") { out = DrawParamType::Vec2; return true; }
48 if (tok == "vec3") { out = DrawParamType::Vec3; return true; }
49 if (tok == "vec4") { out = DrawParamType::Vec4; return true; }
50 if (tok == "mat3") { out = DrawParamType::Mat3; return true; }
51 if (tok == "mat4") { out = DrawParamType::Mat4; return true; }
52 return false;
53}
54
55bool isIdentChar(char c) {
56 return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
57}
58
60std::vector<std::string> tokenize(const std::string& line) {
61 std::vector<std::string> tokens;
62 std::string cur;
63 for (char c : line) {
64 if (std::isspace(static_cast<unsigned char>(c))) {
65 if (!cur.empty()) { tokens.push_back(cur); cur.clear(); }
66 } else if (c == ';') {
67 if (!cur.empty()) { tokens.push_back(cur); cur.clear(); }
68 tokens.push_back(";");
69 } else {
70 cur += c;
71 }
72 }
73 if (!cur.empty()) tokens.push_back(cur);
74 return tokens;
75}
76
84std::vector<LooseDecl> collectLooseDecls(const std::string& src) {
85 std::vector<LooseDecl> out;
86 bool inBlockComment = false;
87
88 usize lineStart = 0;
89 while (lineStart < src.size()) {
90 usize lineEnd = src.find('\n', lineStart);
91 if (lineEnd == std::string::npos) lineEnd = src.size();
92 std::string line = src.substr(lineStart, lineEnd - lineStart);
93
94 // Resolve comment state: drop /*...*/ segments (line-local or spanning).
95 std::string code;
96 code.reserve(line.size());
97 for (usize i = 0; i < line.size();) {
98 if (inBlockComment) {
99 usize close = line.find("*/", i);
100 if (close == std::string::npos) { i = line.size(); break; }
101 inBlockComment = false;
102 i = close + 2;
103 } else if (line.compare(i, 2, "/*") == 0) {
104 inBlockComment = true;
105 i += 2;
106 } else if (line.compare(i, 2, "//") == 0) {
107 break;
108 } else {
109 code += line[i++];
110 }
111 }
112
113 const auto tokens = tokenize(code);
114 // uniform [precision] type name ;
115 if (tokens.size() >= 4 && tokens[0] == "uniform" && tokens.back() == ";") {
116 usize t = 1;
117 std::string precision;
118 if (tokens[t] == "highp" || tokens[t] == "mediump" || tokens[t] == "lowp") {
119 precision = tokens[t];
120 ++t;
121 }
122 DrawParamType type;
123 // Exactly: type name ; — anything longer (initializer, extra
124 // declarator) or shorter is not a liftable declaration.
125 if (tokens.size() == t + 3 && typeFromToken(tokens[t], type)) {
126 const std::string& name = tokens[t + 1];
127 bool identOk = !name.empty() && !std::isdigit(static_cast<unsigned char>(name[0]));
128 for (char c : name) identOk = identOk && isIdentChar(c);
129 if (identOk) {
130 LooseDecl d;
131 d.name = name;
132 d.type = type;
133 d.member = (precision.empty() ? "" : precision + " ") + tokens[t] + " " + name + ";";
134 d.begin = lineStart;
135 d.end = lineEnd;
136 out.push_back(std::move(d));
137 }
138 }
139 }
140
141 lineStart = lineEnd + 1;
142 }
143 return out;
144}
145
147std::string applyRewrite(const std::string& src, const std::vector<LooseDecl>& decls,
148 const std::string& blockText) {
149 std::string out = src;
150 // Back to front so byte ranges stay valid.
151 for (usize i = decls.size(); i-- > 0;) {
152 const auto& d = decls[i];
153 out.replace(d.begin, d.end - d.begin, i == 0 ? blockText : "");
154 }
155 return out;
156}
157
158} // namespace
159
160DrawParamsRewrite rewriteLooseUniforms(const std::string& vertexSrc, const std::string& fragmentSrc) {
161 DrawParamsRewrite result{vertexSrc, fragmentSrc, {}};
162
163 // Uniform blocks need GLSL ES 3.0 — a 1.00 source (no #version 300
164 // directive; attribute/varying style) keeps its loose uniforms, which GL
165 // handles as before.
166 if (vertexSrc.find("#version 300") == std::string::npos ||
167 fragmentSrc.find("#version 300") == std::string::npos) {
168 return result;
169 }
170
171 // A source that already mentions the block name authored its own layout —
172 // do not stack a second definition on top.
173 if (vertexSrc.find(DRAW_PARAMS_BLOCK) != std::string::npos ||
174 fragmentSrc.find(DRAW_PARAMS_BLOCK) != std::string::npos) {
175 return result;
176 }
177
178 const auto vsDecls = collectLooseDecls(vertexSrc);
179 const auto fsDecls = collectLooseDecls(fragmentSrc);
180 if (vsDecls.empty() && fsDecls.empty()) return result;
181
182 // Union of both stages in first-appearance order; both stages must agree on
183 // a shared name's type (else the program is broken regardless — leave the
184 // sources for the linker to report).
185 std::vector<const LooseDecl*> unionDecls;
186 for (const auto* decls : {&vsDecls, &fsDecls}) {
187 for (const auto& d : *decls) {
188 const LooseDecl* seen = nullptr;
189 for (const auto* u : unionDecls) {
190 if (u->name == d.name) { seen = u; break; }
191 }
192 if (seen == nullptr) {
193 unionDecls.push_back(&d);
194 } else if (seen->type != d.type) {
195 return result;
196 }
197 }
198 }
199
200 auto alignUp = [](u32 v, u32 a) -> u32 { return (v + a - 1) & ~(a - 1); };
201 u32 offset = 0;
202 std::string blockText = std::string("layout(std140) uniform ") + DRAW_PARAMS_BLOCK + " {\n";
203 for (const auto* d : unionDecls) {
204 u32 size = 0, align = 0;
205 drawParamSizeAlign(d->type, size, align);
206 offset = alignUp(offset, align);
207 result.layout.slots.push_back({d->name, d->type, offset});
208 offset += size;
209 blockText += " " + d->member + "\n";
210 }
211 blockText += "};";
212 result.layout.blockSize = alignUp(offset, 16);
213
214 if (!vsDecls.empty()) result.vertexSrc = applyRewrite(vertexSrc, vsDecls, blockText);
215 if (!fsDecls.empty()) result.fragmentSrc = applyRewrite(fragmentSrc, fsDecls, blockText);
216 return result;
217}
218
219} // namespace esengine
Per-draw shader parameter block — the UBO home for what used to be loose uniforms.
DrawParamType
GLSL type of one lifted uniform (the subset the rewriter supports).
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
DrawParamsRewrite rewriteLooseUniforms(const std::string &vertexSrc, const std::string &fragmentSrc)
Lifts loose non-sampler uniform declarations out of both stages into one shared std140 DrawParams blo...
constexpr const char * DRAW_PARAMS_BLOCK
GLSL block name; must match the rewriter's generated block + Shader::compile lookup.
void drawParamSizeAlign(DrawParamType t, u32 &size, u32 &align)
std140 size/base-alignment of a supported param type.
One lifted uniform's slot in a shader's std140 DrawParams block.
const DrawParamSlot * find(const std::string &name) const
u32 blockSize
0 ⇒ the shader has no DrawParams block.
std::vector< DrawParamSlot > slots
Result of rewriteLooseUniforms; layout.empty() ⇒ sources unchanged.