15 for (
const auto& s :
slots) {
16 if (s.name == name)
return &s;
44bool typeFromToken(
const std::string& tok,
DrawParamType& out) {
55bool isIdentChar(
char c) {
56 return std::isalnum(
static_cast<unsigned char>(c)) || c ==
'_';
60std::vector<std::string> tokenize(
const std::string& line) {
61 std::vector<std::string> tokens;
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(
";");
73 if (!cur.empty()) tokens.push_back(cur);
84std::vector<LooseDecl> collectLooseDecls(
const std::string& src) {
85 std::vector<LooseDecl> out;
86 bool inBlockComment =
false;
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);
96 code.reserve(line.size());
97 for (
usize i = 0; i < line.size();) {
99 usize close = line.find(
"*/", i);
100 if (close == std::string::npos) { i = line.size();
break; }
101 inBlockComment =
false;
103 }
else if (line.compare(i, 2,
"/*") == 0) {
104 inBlockComment =
true;
106 }
else if (line.compare(i, 2,
"//") == 0) {
113 const auto tokens = tokenize(code);
115 if (tokens.size() >= 4 && tokens[0] ==
"uniform" && tokens.back() ==
";") {
117 std::string precision;
118 if (tokens[t] ==
"highp" || tokens[t] ==
"mediump" || tokens[t] ==
"lowp") {
119 precision = tokens[t];
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);
133 d.member = (precision.empty() ?
"" : precision +
" ") + tokens[t] +
" " + name +
";";
136 out.push_back(std::move(d));
141 lineStart = lineEnd + 1;
147std::string applyRewrite(
const std::string& src,
const std::vector<LooseDecl>& decls,
148 const std::string& blockText) {
149 std::string out = src;
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 :
"");
166 if (vertexSrc.find(
"#version 300") == std::string::npos ||
167 fragmentSrc.find(
"#version 300") == std::string::npos) {
178 const auto vsDecls = collectLooseDecls(vertexSrc);
179 const auto fsDecls = collectLooseDecls(fragmentSrc);
180 if (vsDecls.empty() && fsDecls.empty())
return result;
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; }
192 if (seen ==
nullptr) {
193 unionDecls.push_back(&d);
194 }
else if (seen->type != d.type) {
200 auto alignUp = [](
u32 v,
u32 a) ->
u32 {
return (v + a - 1) & ~(a - 1); };
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;
206 offset = alignUp(offset, align);
207 result.
layout.
slots.push_back({d->name, d->type, offset});
209 blockText +=
" " + d->member +
"\n";
214 if (!vsDecls.empty()) result.
vertexSrc = applyRewrite(vertexSrc, vsDecls, blockText);
215 if (!fsDecls.empty()) result.
fragmentSrc = applyRewrite(fragmentSrc, fsDecls, blockText);
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.
std::uint32_t u32
Unsigned 32-bit integer.
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.