Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SkeletalModule.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
28#pragma once
29
30#include <cstdint>
31#include <cstring>
32#include <iterator>
33#include <string>
34#include <unordered_map>
35#include <vector>
36
37namespace es::skeletal {
38
40constexpr int VERTEX_FLOATS = 8;
41
43constexpr std::size_t MAX_BATCH_VERTICES = 65535;
44
52template <class T>
53class Buffer {
54public:
55 void clear() { size_ = 0; }
56
58 T* append(std::size_t count) {
59 if (size_ + count > storage_.size()) storage_.resize(size_ + count);
60 T* at = storage_.data() + size_;
61 size_ += count;
62 return at;
63 }
64
65 std::size_t size() const { return size_; }
66 const T* data() const { return storage_.data(); }
67 const T& operator[](std::size_t index) const { return storage_[index]; }
68
70 std::size_t capacity() const { return storage_.capacity(); }
71
72private:
73 std::vector<T> storage_;
74 std::size_t size_ = 0;
75};
76
85
88 virtual ~TriangleSink() = default;
89 virtual void setPremultipliedColor(bool) {}
90 virtual void emit(const float* positions, const float* uvs, int vertexCount,
91 const std::uint16_t* indices, int indexCount,
92 std::uint32_t texture, int blendMode, const float rgba[4]) = 0;
93};
94
102public:
104 struct Capacity {
105 std::size_t slots = 0;
106 std::size_t vertexFloats = 0;
107 std::size_t indices = 0;
108 };
109
113 void reset() { active_ = 0; }
114
117 MeshBatch& open(std::uint32_t texture, int blendMode, bool premultipliedColor = false) {
118 if (active_ == slots_.size()) slots_.emplace_back();
119 MeshBatch& fresh = slots_[active_++];
120 fresh.vertices.clear();
121 fresh.indices.clear();
122 fresh.texture = texture;
123 fresh.blendMode = blendMode;
124 fresh.premultipliedColor = premultipliedColor;
125 return fresh;
126 }
127
129 MeshBatch& back() { return slots_[active_ - 1]; }
130
131 std::size_t size() const { return active_; }
132 const MeshBatch& operator[](std::size_t index) const { return slots_[index]; }
133 const MeshBatch* begin() const { return slots_.data(); }
134 const MeshBatch* end() const { return slots_.data() + active_; }
135
139 Capacity total;
140 total.slots = slots_.size();
141 for (const MeshBatch& slot : slots_) {
142 total.vertexFloats += slot.vertices.capacity();
143 total.indices += slot.indices.capacity();
144 }
145 return total;
146 }
147
148private:
149 std::vector<MeshBatch> slots_;
150 std::size_t active_ = 0;
151};
152
163class BatchCollector final : public TriangleSink {
164public:
165 explicit BatchCollector(BatchList& into) : batches_(into) {}
166 void setPremultipliedColor(bool value) override { premultipliedColor_ = value; }
167
168 void emit(const float* positions, const float* uvs, int vertexCount,
169 const std::uint16_t* indices, int indexCount,
170 std::uint32_t texture, int blendMode, const float rgba[4]) override {
171 if (vertexCount <= 0 || indexCount <= 0) return;
172
173 MeshBatch& batch = batchFor(texture, blendMode, vertexCount);
174 const auto base = static_cast<std::uint16_t>(batch.vertices.size() / VERTEX_FLOATS);
175
176 float* vertex = batch.vertices.append(
177 static_cast<std::size_t>(vertexCount) * VERTEX_FLOATS);
178 for (int i = 0; i < vertexCount; ++i, vertex += VERTEX_FLOATS) {
179 vertex[0] = positions[i * 2];
180 vertex[1] = positions[i * 2 + 1];
181 vertex[2] = uvs[i * 2];
182 vertex[3] = uvs[i * 2 + 1];
183 vertex[4] = rgba[0];
184 vertex[5] = rgba[1];
185 vertex[6] = rgba[2];
186 vertex[7] = rgba[3];
187 }
188
189 std::uint16_t* index = batch.indices.append(static_cast<std::size_t>(indexCount));
190 for (int i = 0; i < indexCount; ++i) {
191 index[i] = static_cast<std::uint16_t>(base + indices[i]);
192 }
193 }
194
195private:
196 MeshBatch& batchFor(std::uint32_t texture, int blendMode, int incomingVertices) {
197 if (batches_.size() > 0) {
198 MeshBatch& open = batches_.back();
199 const bool sameState = texture == open.texture && blendMode == open.blendMode
200 && premultipliedColor_ == open.premultipliedColor;
201 const bool fits =
202 open.vertices.size() / VERTEX_FLOATS + static_cast<std::size_t>(incomingVertices)
204 if (sameState && fits) return open;
205 }
206 return batches_.open(texture, blendMode, premultipliedColor_);
207 }
208
209 BatchList& batches_;
210 bool premultipliedColor_ = false;
211};
212
220template <class T>
222public:
223 int add(T value) {
224 const int id = next_++;
225 items_.emplace(id, std::move(value));
226 return id;
227 }
228
229 T* find(int id) {
230 auto it = items_.find(id);
231 return it == items_.end() ? nullptr : &it->second;
232 }
233
234 bool erase(int id) { return items_.erase(id) > 0; }
235
238 template <class Pred>
239 void eraseIf(Pred pred) {
240 for (auto it = items_.begin(); it != items_.end();) {
241 it = pred(it->second) ? items_.erase(it) : std::next(it);
242 }
243 }
244
245 void clear() { items_.clear(); }
246
247private:
248 std::unordered_map<int, T> items_;
249 int next_ = 1;
250};
251
259public:
260 const char* publish(const char* text) {
261 buffer_ = text ? text : "";
262 return buffer_.c_str();
263 }
264
266 template <class Fn>
267 const char* publishArray(int count, Fn nameAt) {
268 buffer_ = "[";
269 for (int i = 0; i < count; ++i) {
270 if (i > 0) buffer_ += ',';
271 const char* name = nameAt(i);
272 buffer_ += '"';
273 buffer_ += name ? name : "";
274 buffer_ += '"';
275 }
276 buffer_ += ']';
277 return buffer_.c_str();
278 }
279
282 std::string& raw() { return buffer_; }
283
284private:
285 std::string buffer_;
286};
287
290 const char* animationName = nullptr;
291 const char* eventName = nullptr;
292 const char* stringValue = nullptr;
293};
294
305public:
306 static constexpr int MAX_PER_UPDATE = 64;
307
308 void clear() {
309 values_.clear();
310 strings_.clear();
311 count_ = 0;
312 }
313
314 void push(int kind, int track, float floatValue, int intValue, EventStrings strings) {
315 if (count_ >= MAX_PER_UPDATE) return;
316 pushBits(kind);
317 pushBits(track);
318 values_.push_back(floatValue);
319 pushBits(intValue);
320 strings_.push_back(strings);
321 ++count_;
322 }
323
324 int count() const { return count_; }
325 const float* values() const { return values_.data(); }
328 std::size_t byteLength() const { return values_.size() * sizeof(float); }
329 const EventStrings* strings(int index) const {
330 return index >= 0 && index < count_ ? &strings_[static_cast<std::size_t>(index)] : nullptr;
331 }
332
333private:
334 void pushBits(int value) {
335 float bits;
336 std::memcpy(&bits, &value, sizeof(bits));
337 values_.push_back(bits);
338 }
339
340 std::vector<float> values_;
341 std::vector<EventStrings> strings_;
342 int count_ = 0;
343};
344
345} // namespace es::skeletal
void setPremultipliedColor(bool value) override
void emit(const float *positions, const float *uvs, int vertexCount, const std::uint16_t *indices, int indexCount, std::uint32_t texture, int blendMode, const float rgba[4]) override
MeshBatch & open(std::uint32_t texture, int blendMode, bool premultipliedColor=false)
const MeshBatch & operator[](std::size_t index) const
const MeshBatch * begin() const
std::size_t size() const
MeshBatch & back()
The batch still open — meaningful only while size() is nonzero.
const MeshBatch * end() const
T * append(std::size_t count)
Room for count more, to write through. The next append invalidates it.
std::size_t size() const
const T * data() const
std::size_t capacity() const
What it can take before it allocates again.
const T & operator[](std::size_t index) const
static constexpr int MAX_PER_UPDATE
void push(int kind, int track, float floatValue, int intValue, EventStrings strings)
std::size_t byteLength() const
const float * values() const
const EventStrings * strings(int index) const
const char * publishArray(int count, Fn nameAt)
["a","b"] from an indexed getter — how name lists cross the boundary.
const char * publish(const char *text)
constexpr int VERTEX_FLOATS
Interleaved x,y,u,v,r,g,b,a per vertex — the layout the SDK reads back.
constexpr std::size_t MAX_BATCH_VERTICES
16-bit indices, so a batch closes before one would wrap.
What the list can take before it allocates again.
The strings one queued event borrowed from its runtime's own data.
One draw's worth of geometry.
Buffer< std::uint16_t > indices
Where a runtime pours the triangles it posed.
virtual void emit(const float *positions, const float *uvs, int vertexCount, const std::uint16_t *indices, int indexCount, std::uint32_t texture, int blendMode, const float rgba[4])=0
virtual ~TriangleSink()=default
virtual void setPremultipliedColor(bool)