Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RetainedText.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
7
8#include <algorithm>
9#include <cmath>
10#include <map>
11#include <vector>
12
13namespace esengine {
14
24
26inline constexpr u32 RETAINED_TEXT_BLOCK_QUADS = 64;
28
30 std::vector<f32> vertices;
31 std::vector<u16> indices;
32 std::vector<f32> blocks;
33 u32 pageId = 0;
35};
36
38 std::vector<RetainedTextBatch> batches;
39 bool sdf = false;
40 bool screen = false;
41 bool hasBox = false;
42 bool autoKind = false;
43 i32 layer = 0;
44 f32 boxWidth = 0.0f;
45 f32 boxHeight = 0.0f;
46};
47
50public:
52 void begin(u32 entity, bool sdf, bool screen, bool hasBox, i32 layer, f32 boxWidth, f32 boxHeight, bool autoKind = false) {
53 ++revision_;
54 changed_.push_back(entity);
55 RetainedText& text = entries_[entity];
57 text.sdf = sdf;
58 text.screen = screen;
59 text.hasBox = hasBox;
60 text.autoKind = autoKind;
61 text.layer = layer;
62 text.boxWidth = boxWidth;
63 text.boxHeight = boxHeight;
64 }
65
67 bool add(u32 entity, const f32* vertices, i32 vertexCount, const u16* indices, i32 indexCount, u32 pageId,
69 const auto found = entries_.find(entity);
70 if (found == entries_.end() || vertexCount <= 0 || indexCount <= 0 || vertexCount % 4 != 0 || indexCount != vertexCount / 4 * 6) return false;
71 ++revision_;
72 changed_.push_back(entity);
73 RetainedTextBatch& batch = found->second.batches.emplace_back();
74 batch.vertices.assign(vertices, vertices + static_cast<usize>(vertexCount) * TEXT_VERTEX_FLOATS);
75 batch.indices.assign(indices, indices + indexCount);
76 batch.pageId = pageId;
77 batch.mode = mode;
78 const u32 quads = static_cast<u32>(vertexCount) / 4;
79 batch.blocks.resize(static_cast<usize>((quads + RETAINED_TEXT_BLOCK_QUADS - 1) / RETAINED_TEXT_BLOCK_QUADS) * 4);
80 for (u32 start = 0; start < quads; start += RETAINED_TEXT_BLOCK_QUADS) {
81 f32 left = INFINITY, bottom = INFINITY, right = -INFINITY, top = -INFINITY;
82 const u32 end = std::min(quads, start + RETAINED_TEXT_BLOCK_QUADS) * RETAINED_TEXT_QUAD_FLOATS;
83 for (u32 i = start * RETAINED_TEXT_QUAD_FLOATS; i < end; i += TEXT_VERTEX_FLOATS) {
84 left = std::min(left, batch.vertices[i]);
85 right = std::max(right, batch.vertices[i]);
86 bottom = std::min(bottom, batch.vertices[i + 1]);
87 top = std::max(top, batch.vertices[i + 1]);
88 }
89 f32* block = batch.blocks.data() + static_cast<usize>(start / RETAINED_TEXT_BLOCK_QUADS) * 4;
90 block[0] = left;
91 block[1] = bottom;
92 block[2] = right;
93 block[3] = top;
94 }
95 return true;
96 }
97
98 void clear(u32 entity) {
99 if (!entries_.erase(entity)) return;
100 ++revision_;
101 changed_.push_back(entity);
102 }
103 void clearAll() {
104 entries_.clear();
105 ++revision_;
106 all_changed_ = true;
107 }
108
110 template<typename Visit>
111 bool takeChanged(Visit&& visit) {
112 const bool named = !all_changed_;
113 for (const u32 entity : changed_) visit(entity);
114 changed_.clear();
115 all_changed_ = false;
116 return named;
117 }
118
119 u64 revision() const { return revision_; }
120 usize size() const { return entries_.size(); }
121 std::map<u32, RetainedText>& entries() { return entries_; }
122
123private:
124 std::map<u32, RetainedText> entries_;
125 u64 revision_ = 0;
126 std::vector<u32> changed_;
127 bool all_changed_ = false;
128};
129
132inline bool retainedTextAutoIsSdf(f32 worldScaleX) {
133 constexpr double BITMAP_SCALE_EPSILON = 0.02;
134 const double scale = worldScaleX;
135 if (!std::isfinite(scale) || scale <= 0.0) return false;
136 return std::abs(scale - 1.0) > BITMAP_SCALE_EPSILON;
137}
138
139// ---- Clipping a label's quads against a view ------------------------------------------------------------------------------------------------
140// The same arithmetic, in the same precision, as sdk/src/ui/text/text-culling.ts, which the unretained path still runs: the two must agree on
141// which quads a view keeps.
142
145inline bool retainedTextClipPlanes(double* out, const f32* view, const f32* model) {
146 constexpr double ROUNDING_MARGIN = 8.0 / 8388608.0; // 8 * 2^-23
147 for (int i = 0; i < 16; ++i) {
148 if (!std::isfinite(view[i]) || !std::isfinite(model[i])) return false;
149 }
150 for (int plane = 0; plane < 6; ++plane) {
151 const int row = plane >> 1;
152 const double sign = plane % 2 == 0 ? 1.0 : -1.0;
153 for (int slot = 0; slot < 3; ++slot) {
154 const int column = slot == 2 ? 12 : slot * 4;
155 double value = 0.0, error = 0.0;
156 for (int k = 0; k < 4; ++k) {
157 const double a = view[k * 4 + 3], b = view[k * 4 + row];
158 value += (a + sign * b) * model[column + k];
159 error += (std::abs(a) + std::abs(b)) * std::abs(static_cast<double>(model[column + k]));
160 }
161 out[plane * 6 + slot] = value;
162 out[plane * 6 + slot + 3] = error * ROUNDING_MARGIN;
163 }
164 }
165 return true;
166}
167
169inline int retainedTextClassify(const double* planes, double left, double bottom, double right, double top) {
170 if (!std::isfinite(left) || !std::isfinite(bottom) || !std::isfinite(right) || !std::isfinite(top)) return 1;
171 bool inside = true;
172 const double x = std::max(std::abs(left), std::abs(right)), y = std::max(std::abs(bottom), std::abs(top));
173 for (int p = 0; p < 36; p += 6) {
174 const double a = planes[p], b = planes[p + 1], c = planes[p + 2];
175 const double margin = planes[p + 3] * x + planes[p + 4] * y + planes[p + 5];
176 const double lo = a * (a >= 0 ? left : right) + b * (b >= 0 ? bottom : top) + c;
177 const double hi = a * (a >= 0 ? right : left) + b * (b >= 0 ? top : bottom) + c;
178 if (hi < -margin) return 0;
179 if (lo < margin) inside = false;
180 }
181 return inside ? 1 : 2;
182}
183
186 std::vector<f32> vertices;
187 std::vector<u16> indices;
188 std::vector<u8> visible;
189 std::vector<u8> blockVisibility;
190};
191
194
195inline RetainedTextKept retainedTextClip(const RetainedTextBatch& batch, const double* planes, RetainedTextClip& scratch) {
196 if (!planes) return RetainedTextKept::All;
197 const u32 count = static_cast<u32>(batch.vertices.size() / RETAINED_TEXT_QUAD_FLOATS);
198 scratch.visible.assign(count, 0);
199 scratch.blockVisibility.assign(batch.blocks.size() / 4, 0);
200 u32 visibleCount = 0;
201 for (u32 start = 0; start < count; start += RETAINED_TEXT_BLOCK_QUADS) {
202 const u32 block = start / RETAINED_TEXT_BLOCK_QUADS * 4;
203 const u32 end = std::min(count, start + RETAINED_TEXT_BLOCK_QUADS);
204 const int status = retainedTextClassify(planes, batch.blocks[block], batch.blocks[block + 1], batch.blocks[block + 2], batch.blocks[block + 3]);
205 scratch.blockVisibility[start / RETAINED_TEXT_BLOCK_QUADS] = static_cast<u8>(status);
206 if (status != 2) {
207 std::fill(scratch.visible.begin() + start, scratch.visible.begin() + end, static_cast<u8>(status));
208 if (status == 1) visibleCount += end - start;
209 continue;
210 }
211 for (u32 quad = start; quad < end; ++quad) {
212 const u32 offset = quad * RETAINED_TEXT_QUAD_FLOATS;
213 const double x0 = batch.vertices[offset], y0 = batch.vertices[offset + 1];
214 const double x1 = batch.vertices[offset + 2 * TEXT_VERTEX_FLOATS], y1 = batch.vertices[offset + 2 * TEXT_VERTEX_FLOATS + 1];
215 const bool survives = retainedTextClassify(planes, std::min(x0, x1), std::min(y0, y1), std::max(x0, x1), std::max(y0, y1)) != 0;
216 scratch.visible[quad] = survives ? 1 : 0;
217 if (survives) ++visibleCount;
218 }
219 }
220 if (visibleCount == 0) return RetainedTextKept::None;
221 if (visibleCount == count) return RetainedTextKept::All;
222 scratch.vertices.resize(static_cast<usize>(visibleCount) * RETAINED_TEXT_QUAD_FLOATS);
223 scratch.indices.resize(static_cast<usize>(visibleCount) * 6);
224 u32 output = 0;
225 for (u32 start = 0; start < count; start += RETAINED_TEXT_BLOCK_QUADS) {
226 if (scratch.blockVisibility[start / RETAINED_TEXT_BLOCK_QUADS] == 0) continue;
227 const u32 end = std::min(count, start + RETAINED_TEXT_BLOCK_QUADS);
228 for (u32 quad = start; quad < end; ++quad) {
229 if (!scratch.visible[quad]) continue;
230 std::copy_n(batch.vertices.begin() + static_cast<usize>(quad) * RETAINED_TEXT_QUAD_FLOATS, RETAINED_TEXT_QUAD_FLOATS,
231 scratch.vertices.begin() + static_cast<usize>(output) * RETAINED_TEXT_QUAD_FLOATS);
232 const u16 base = static_cast<u16>(output * 4), sourceBase = static_cast<u16>(quad * 4);
233 for (u32 i = 0; i < 6; ++i) scratch.indices[static_cast<usize>(output) * 6 + i] = static_cast<u16>(batch.indices[static_cast<usize>(quad) * 6 + i] - sourceBase + base);
234 ++output;
235 }
236 }
238}
239
240} // namespace esengine
Core type definitions and utilities for ESEngine.
The retained geometry of every label, by entity. Ordered: a frame submits in entity order,...
bool takeChanged(Visit &&visit)
Hands visit every label laid out, given glyphs or let go since the last call, and forgets them; false...
u64 revision() const
Moves with every label laid out, given glyphs or let go: what a pass drawn from the store was drawn f...
bool add(u32 entity, const f32 *vertices, i32 vertexCount, const u16 *indices, i32 indexCount, u32 pageId, TextGlyphMode mode=TextGlyphMode::Color)
One batch of an entity begun with begin(). False when it was not begun or the batch is not whole quad...
std::map< u32, RetainedText > & entries()
void begin(u32 entity, bool sdf, bool screen, bool hasBox, i32 layer, f32 boxWidth, f32 boxHeight, bool autoKind=false)
Replaces any geometry the entity held, with none yet: batches follow through add().
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
constexpr u32 RETAINED_TEXT_BLOCK_QUADS
Quads in one bounds block: a view that misses the whole block does not look at its quads.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr i32 TEXT_VERTEX_FLOATS
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
RetainedTextKept
What a view keeps of a batch: nothing, all of it (read the batch itself), or a subset copied into scr...
RetainedTextKept retainedTextClip(const RetainedTextBatch &batch, const double *planes, RetainedTextClip &scratch)
constexpr u32 RETAINED_TEXT_QUAD_FLOATS
bool retainedTextAutoIsSdf(f32 worldScaleX)
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
bool retainedTextClipPlanes(double *out, const f32 *view, const f32 *model)
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
@ Color
The page's RGBA, tinted: a rasterized atlas that carries native-AA coverage in alpha,...
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
int retainedTextClassify(const double *planes, double left, double bottom, double right, double top)
0 outside, 1 inside, 2 crossing a clip plane. Non-finite bounds stay visible.
float f32
32-bit floating point
Definition Types.hpp:59
std::vector< f32 > blocks
left, bottom, right, top of each RETAINED_TEXT_BLOCK_QUADS quads.
std::vector< f32 > vertices
TEXT_VERTEX_FLOATS floats a vertex, four a quad.
TextGlyphMode mode
How the page's pixels become coverage. A label may mix them: an outline's ring, then its fill.
std::vector< u16 > indices
Six a quad, naming this batch's own vertices.
u32 pageId
The atlas page, which is the draw's texture.
Where the quads a view keeps are copied to, reused across batches and frames.
std::vector< f32 > vertices
std::vector< u8 > blockVisibility
std::vector< u16 > indices
f32 boxWidth
The box the layout used: a label whose box has since changed is stale.
bool hasBox
Laid out in a UINode box, so ordered by the UI tree rather than by layer.
std::vector< RetainedTextBatch > batches
bool screen
The domain the geometry was laid out for.
bool sdf
The pipeline the label's glyphs were chosen for, by world scale (autoKind). What each batch draws wit...
bool autoKind
The glyph pipeline was chosen from the entity's world scale, so a changed scale can change it.
i32 layer
Text.layer, for a label with no box.