Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
GlslLoopFloor.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include <algorithm>
6#include <cctype>
7#include <string>
8#include <string_view>
9
10namespace esengine::gfx {
11
12inline constexpr std::string_view kLoopFloorUniform = "es_loopFloor";
13
14namespace detail {
15
16inline bool identChar(char c) {
17 return std::isalnum(static_cast<unsigned char>(c)) != 0 || c == '_';
18}
19
20inline std::size_t skipBlank(const std::string& s, std::size_t at) {
21 while (at < s.size() && (s[at] == ' ' || s[at] == '\t' || s[at] == '\r' || s[at] == '\n')) ++at;
22 return at;
23}
24
25inline bool takeWord(const std::string& s, std::size_t& at, std::string_view word) {
26 if (s.compare(at, word.size(), word) != 0) return false;
27 const std::size_t end = at + word.size();
28 if (end < s.size() && identChar(s[end])) return false;
29 at = end;
30 return true;
31}
32
33inline std::size_t takeIdent(const std::string& s, std::size_t at) {
34 if (at >= s.size() || !(std::isalpha(static_cast<unsigned char>(s[at])) != 0 || s[at] == '_')) return std::string::npos;
35 while (at < s.size() && identChar(s[at])) ++at;
36 return at;
37}
38
39inline std::size_t floorInsertionPoint(const std::string& s) {
40 std::size_t pos = 0, line = 0;
41 int depth = 0;
42 bool sawVersion = false;
43 while (line < s.size()) {
44 std::size_t end = s.find('\n', line);
45 if (end == std::string::npos) end = s.size();
46 const std::size_t first = skipBlank(s, line);
47 if (first >= end) {
48 line = end + 1;
49 continue;
50 }
51 if (s[first] != '#') break;
52 std::size_t word = skipBlank(s, first + 1);
53 if (takeWord(s, word, "version")) sawVersion = true;
54 else if (takeWord(s, word, "if") || takeWord(s, word, "ifdef") || takeWord(s, word, "ifndef")) ++depth;
55 else if (takeWord(s, word, "endif")) --depth;
56 if (depth == 0) pos = std::min(end + 1, s.size());
57 line = end + 1;
58 }
59 return sawVersion ? pos : 0;
60}
61
62} // namespace detail
63
64inline std::string floorConstantLoops(const std::string& src) {
65 std::string out;
66 std::size_t copied = 0, at = 0, loops = 0;
67 while ((at = src.find("for", at)) != std::string::npos) {
68 const std::size_t start = at;
69 at += 3;
70 std::size_t p = start;
71 if (!detail::takeWord(src, p, "for")) continue;
72 p = detail::skipBlank(src, p);
73 if (p >= src.size() || src[p] != '(') continue;
74 p = detail::skipBlank(src, p + 1);
75 for (std::string_view q : {"highp", "mediump", "lowp"}) {
76 std::size_t probe = p;
77 if (detail::takeWord(src, probe, q)) { p = detail::skipBlank(src, probe); break; }
78 }
79 if (!detail::takeWord(src, p, "int")) continue;
80 p = detail::skipBlank(src, p);
81 const std::size_t identEnd = detail::takeIdent(src, p);
82 if (identEnd == std::string::npos) continue;
83 p = detail::skipBlank(src, identEnd);
84 if (p >= src.size() || src[p] != '=') continue;
85 p = detail::skipBlank(src, p + 1);
86 const std::size_t valueStart = p;
87 if (p < src.size() && src[p] == '-') p = detail::skipBlank(src, p + 1);
88 while (p < src.size() && std::isdigit(static_cast<unsigned char>(src[p])) != 0) ++p;
89 if (p < src.size() && detail::identChar(src[p])) continue;
90 const std::size_t valueEnd = p;
91 p = detail::skipBlank(src, p);
92 if (p >= src.size() || src[p] != ';') continue;
93 out.append(src, copied, valueStart - copied);
94 out += '(';
95 out.append(src, valueStart, valueEnd - valueStart);
96 out += " + ";
97 out += kLoopFloorUniform;
98 out += ')';
99 copied = valueEnd;
100 at = valueEnd;
101 ++loops;
102 }
103 if (loops == 0) return src;
104 out.append(src, copied, std::string::npos);
105 const std::size_t insert = detail::floorInsertionPoint(out);
106 std::string declaration = "uniform highp int ";
107 declaration += kLoopFloorUniform;
108 declaration += ";\n";
109 out.insert(insert, declaration);
110 return out;
111}
112
113} // namespace esengine::gfx
std::size_t takeIdent(const std::string &s, std::size_t at)
bool takeWord(const std::string &s, std::size_t &at, std::string_view word)
std::size_t floorInsertionPoint(const std::string &s)
std::size_t skipBlank(const std::string &s, std::size_t at)
std::string floorConstantLoops(const std::string &src)
constexpr std::string_view kLoopFloorUniform