16#include <unordered_set>
25ScreenRect intersectRects(
const ScreenRect& a,
const ScreenRect& b) {
26 i32 x1 = std::max(a.x, b.x);
27 i32 y1 = std::max(a.y, b.y);
28 i32 x2 = std::min(a.x + a.w, b.x + b.w);
29 i32 y2 = std::min(a.y + a.h, b.y + b.h);
30 if (x2 <= x1 || y2 <= y1)
return {0, 0, 0, 0};
31 return {x1, y1, x2 - x1, y2 - y1};
34glm::vec2 worldToScreen(
const glm::vec3& world,
const glm::mat4& vp,
36 glm::vec4 clip = vp * glm::vec4(world, 1.0f);
37 f32 ndcX = clip.x / clip.w;
38 f32 ndcY = clip.y / clip.w;
39 f32 sx =
static_cast<f32>(vpX) + (ndcX * 0.5f + 0.5f) *
static_cast<f32>(vpW);
40 f32 sy =
static_cast<f32>(vpY) + (ndcY * 0.5f + 0.5f) *
static_cast<f32>(vpH);
45thread_local bool g_masksInWorld =
false;
50 glm::vec2 size{0.0f, 0.0f};
52 glm::vec2 centerOffset{0.0f, 0.0f};
57 if (
const auto* node = registry.tryGet<
ecs::UINode>(entity)) {
58 return {node->computed_size_, {0.0f, 0.0f},
true};
61 return {rect->size, {(0.5f - rect->pivot.x) * rect->size.x, (0.5f - rect->pivot.y) * rect->size.y},
true};
66ScreenRect computeMaskScreenRect(
70 const MaskBox box = maskBoxOf(registry, entity);
74 if (box.size.x <= 0.0f || box.size.y <= 0.0f)
return {0, 0, 0, 0};
80 if (transform.worldScale.x == 0 || transform.worldScale.y == 0
82 glm::vec3 axisX, axisY;
84 axisX = glm::vec3(transform.cachedMatrix_[0]);
85 axisY = glm::vec3(transform.cachedMatrix_[1]);
87 const auto turn =
flatTurnZ(transform.worldRotation);
88 axisX = glm::vec3(turn.x, turn.y, 0) * transform.worldScale.x;
89 axisY = glm::vec3(-turn.y, turn.x, 0) * transform.worldScale.y;
91 const glm::vec3 hx = axisX * box.size.x * .5f;
92 const glm::vec3 hy = axisY * box.size.y * .5f;
93 const glm::vec3 origin = g_masksInWorld ?
rebased(transform.worldPosition) : glm::vec3(transform.worldPosition);
94 const glm::vec3 center = origin + axisX * box.centerOffset.x + axisY * box.centerOffset.y;
95 const glm::vec3 corners[] = {center - hx - hy, center + hx - hy,
96 center + hx + hy, center - hx + hy};
98 f32 minX = std::numeric_limits<f32>::max();
99 f32 minY = std::numeric_limits<f32>::max();
100 f32 maxX = std::numeric_limits<f32>::lowest();
101 f32 maxY = std::numeric_limits<f32>::lowest();
102 for (
const auto& c : corners) {
103 glm::vec2 s = worldToScreen(c, vp, vpX, vpY, vpW, vpH);
104 minX = std::min(minX, s.x);
105 minY = std::min(minY, s.y);
106 maxX = std::max(maxX, s.x);
107 maxY = std::max(maxY, s.y);
111 static_cast<i32>(std::round(minX)),
112 static_cast<i32>(std::round(minY)),
113 static_cast<i32>(std::round(maxX - minX)),
114 static_cast<i32>(std::round(maxY - minY)),
119 const std::unordered_set<u32>& maskSet) {
124 if (maskSet.count(parent.id())) {
125 const auto& parentMask = registry.get<
ecs::UIMask>(parent);
134 const std::unordered_set<u32>& stencilSet) {
139 if (stencilSet.count(parent.id()))
return true;
145void applyScissorToDescendants(
147 const ScreenRect& clipRect,
const std::unordered_set<u32>& maskSet,
148 const glm::mat4& vp,
i32 vpX,
i32 vpY,
i32 vpW,
i32 vpH,
149 std::vector<std::pair<Entity, ScissorRect>>& work
152 work.emplace_back(entity, clipRect);
154 while (!work.empty()) {
155 if (++walked > registry.entityCount()) {
156 ES_LOG_WARN(
"Scissor hierarchy repeats nodes; stopping mask traversal");
160 const auto [owner, parentClip] = work.back();
163 const auto& children = registry.get<
ecs::Children>(owner).entities;
164 for (
auto it = children.rbegin(); it != children.rend(); ++it) {
166 if (!registry.valid(child))
continue;
167 ScreenRect childClip = parentClip;
168 if (maskSet.count(child.id())) {
169 const auto& childMask = registry.get<
ecs::UIMask>(child);
171 const auto childRect = computeMaskScreenRect(registry, child, vp, vpX, vpY, vpW, vpH);
172 childClip = intersectRects(parentClip, childRect);
175 frame.setEntityClipRect(child.id(), childClip.x, childClip.y, childClip.w, childClip.h);
176 work.emplace_back(child, childClip);
181static constexpr i32 MAX_STENCIL_REF = 255;
184 const MaskBox box = maskBoxOf(registry, entity);
186 if (!box.present || !transform)
return false;
187 if (box.size.x <= 0.0f || box.size.y <= 0.0f)
return true;
189 return transform->worldScale.x == 0 || transform->worldScale.y == 0
195 const std::unordered_set<u32>& stencilSet,
bool& overflowed,
196 std::vector<std::pair<Entity, i32>>& work) {
198 if (hasEmptyMaskBox(registry, entity)) {
200 frame.setEntityClipRect(entity.id(), 0, 0, 0, 0);
202 frame.setEntityStencilMask(entity.id(), refValue,
true);
204 const auto enqueueChildren = [&](
Entity owner,
i32 parentRef) {
206 const auto& children = registry.get<
ecs::Children>(owner).entities;
207 for (
auto it = children.rbegin(); it != children.rend(); ++it) {
208 if (registry.valid(*it)) work.emplace_back(*it, parentRef);
211 enqueueChildren(entity, refValue);
213 while (!work.empty()) {
214 if (++walked > registry.entityCount()) {
215 ES_LOG_WARN(
"Stencil hierarchy repeats nodes; stopping mask traversal");
219 auto [owner, currentRef] = work.back();
221 if (currentRef == 0) {
222 frame.setEntityClipRect(owner.id(), 0, 0, 0, 0);
223 }
else if (stencilSet.count(owner.id())) {
224 if (hasEmptyMaskBox(registry, owner)) {
225 frame.setEntityClipRect(owner.id(), 0, 0, 0, 0);
226 enqueueChildren(owner, 0);
229 if (currentRef == MAX_STENCIL_REF) {
231 frame.setEntityClipRect(owner.id(), 0, 0, 0, 0);
232 enqueueChildren(owner, 0);
236 frame.setEntityStencilMask(owner.id(), currentRef,
true);
238 frame.setEntityStencilTest(owner.id(), currentRef,
true);
240 enqueueChildren(owner, currentRef);
247 g_masksInWorld =
true;
248 processMasks(registry, view_projection_, vpX, vpY, vpW, vpH);
249 g_masksInWorld =
false;
258 std::vector<Entity>& scissorMasks = mask_scissor_scratch_;
259 std::vector<Entity>& stencilMasks = mask_stencil_scratch_;
260 std::unordered_set<u32>& maskSet = mask_set_scratch_;
261 std::unordered_set<u32>& stencilSet = stencil_set_scratch_;
262 scissorMasks.clear();
263 stencilMasks.clear();
267 for (
auto entity : maskView) {
269 if (!mask.enabled)
continue;
270 maskSet.insert(entity.id());
272 stencilMasks.push_back(entity);
273 stencilSet.insert(entity.id());
275 scissorMasks.push_back(entity);
279 if (scissorMasks.empty() && stencilMasks.empty())
return;
281 if (!scissorMasks.empty()) {
282 std::vector<Entity>& rootScissors = mask_root_scratch_;
283 rootScissors.clear();
284 for (
auto entity : scissorMasks) {
285 if (!hasAncestorScissorMask(registry, entity, maskSet)) {
286 rootScissors.push_back(entity);
290 for (
auto entity : rootScissors) {
291 ScreenRect rect = computeMaskScreenRect(registry, entity, projection, vpX, vpY, vpW, vpH);
292 rect = intersectRects(rect, {vpX, vpY, vpW, vpH});
293 applyScissorToDescendants(registry, *
this, entity, rect, maskSet, projection, vpX, vpY, vpW, vpH, mask_scissor_walk_scratch_);
297 if (!stencilMasks.empty()) {
298#ifdef ES_PLATFORM_WEB
299 device_.clearStencil(0);
302 std::vector<Entity>& rootStencils = mask_root_scratch_;
303 rootStencils.clear();
304 for (
auto entity : stencilMasks) {
305 if (!hasAncestorStencilMask(registry, entity, stencilSet)) {
306 rootStencils.push_back(entity);
310 bool overflowed =
false;
311 for (
auto entity : rootStencils) {
312 applyStencilHierarchy(registry, *
this, entity, 1, stencilSet, overflowed, mask_stencil_walk_scratch_);
316 ES_LOG_WARN(
"Stencil mask overflow: too many nested masks (>255)");
The rectangle a piece of world content occupies: a size, and the point of it the entity's transform s...
Hierarchy components for parent-child relationships.
Logging utilities for ESEngine.
#define ES_LOG_WARN(...)
Logs a warning message.
Frame resources and the shared scene rendering contract.
void processMasks(ecs::Registry ®istry, i32 vpX, i32 vpY, i32 vpW, i32 vpH)
void clearAllStencilMasks()
Central container for all entities and components.
T & get(Entity entity)
Gets a component from an entity (must exist).
bool hasFlatGeometryArea(Transform &transform, bool parented)
std::int32_t i32
Signed 32-bit integer.
std::size_t usize
Size type for indexing and sizes.
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
glm::vec3 rebased(const glm::vec3 &world)
A world position as the frame draws it: from the origin.
glm::vec2 flatTurnZ(const glm::quat &rotation)
The cos/sin of the turn a plane renderable draws with: its entity's rotation about Z,...
float f32
32-bit floating point
Entity identifier with packed index + generation.
The rectangle of this entity's content, in its own units.
Parent reference component.
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).