Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
WgslBindings.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17#include "../../core/Types.hpp"
18
19#include <cstdlib>
20#include <cstring>
21
22namespace esengine {
23
29inline constexpr u32 textureBindingForUnit(u32 unit) { return unit < 8 ? unit : (unit < 16 ? unit + 8 : 32 + 2 * (unit - 16)); }
30
32inline constexpr u32 samplerBindingForUnit(u32 unit) { return unit < 8 ? unit + 8 : (unit < 16 ? unit + 16 : 33 + 2 * (unit - 16)); }
33
35inline constexpr u32 WGSL_MAX_TEXTURE_UNITS = 17;
36
44inline u64 scanWGSLBindingMask(const char* source, u32 group) {
45 if (!source) return 0;
46 u64 mask = 0;
47 for (const char* p = source; (p = std::strstr(p, "@group(")) != nullptr;) {
48 p += 7;
49 char* end = nullptr;
50 const unsigned long g = std::strtoul(p, &end, 10);
51 if (end == p) continue;
52 p = end;
53 if (g != group) continue;
54 const char* b = std::strstr(p, "@binding(");
55 if (!b) break;
56 b += 9;
57 const unsigned long idx = std::strtoul(b, &end, 10);
58 if (end == b) continue;
59 p = end;
60 if (idx < 64) mask |= (u64{1} << idx);
61 }
62 return mask;
63}
64
78inline void scanWGSLReachedUnits(const char* source, u32& textures, u32& samplers) {
79 textures = 0;
80 samplers = 0;
81 if (!source) return;
82 const auto isIdent = [](char c) {
83 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
84 };
85 const auto isSpace = [](char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; };
86 // Whether the name starting at @p p (ending before @p end) is one the source declares for itself.
87 const auto ownName = [&](const char* p, const char* end) -> bool {
88 const char* b = p;
89 while (b != source && isSpace(b[-1])) --b;
90 const char* after = end;
91 while (isSpace(*after)) ++after;
92 if (b != source && (b[-1] == '(' || b[-1] == ',' || b[-1] == '{') && *after == ':') return true;
93 const char* w = b;
94 while (w != source && isIdent(w[-1])) --w;
95 const usize len = static_cast<usize>(b - w);
96 if (len == 3 && std::strncmp(w, "let", 3) == 0) return true;
97 if (len == 5 && std::strncmp(w, "const", 5) == 0) return true;
98 if (len != 3 || std::strncmp(w, "var", 3) != 0) return false;
99 while (w != source && isSpace(w[-1])) --w;
100 return w == source || w[-1] != ')';
101 };
102 u32 ownTextures = 0;
103 u32 ownSamplers = 0;
104 for (const char* p = source; *p; ++p) {
105 if (*p != 't' && *p != 's') continue;
106 if (p != source && isIdent(p[-1])) continue;
107 char* end = nullptr;
108 const unsigned long unit = std::strtoul(p + 1, &end, 10);
109 if (end == p + 1 || isIdent(*end)) continue;
110 if (unit < WGSL_MAX_TEXTURE_UNITS) {
111 (*p == 't' ? textures : samplers) |= (1u << unit);
112 if (ownName(p, end)) (*p == 't' ? ownTextures : ownSamplers) |= (1u << unit);
113 }
114 p = end - 1;
115 }
116 textures &= ~ownTextures;
117 samplers &= ~ownSamplers;
118}
119
120} // namespace esengine
Core type definitions and utilities for ESEngine.
u64 scanWGSLBindingMask(const char *source, u32 group)
Bit mask of the @group(N) @binding(i) indices a WGSL source declares.
constexpr u32 samplerBindingForUnit(u32 unit)
Group-1 binding of a texture unit's sampler (see textureBindingForUnit).
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr u32 textureBindingForUnit(u32 unit)
Group-1 binding of a texture unit. Units 0-7 are the batch stream and take bindings 0-7; a material's...
void scanWGSLReachedUnits(const char *source, u32 &textures, u32 &samplers)
The tN / sN names a WGSL source REACHES, by unit.
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
constexpr u32 WGSL_MAX_TEXTURE_UNITS
Units a tN / sN name can carry — the batch stream and a material's own.