39 void update(
World& world)
override { compose(world.registry); }
100 u32 composedEpoch_ = 0;
101 u64 composedRegistry_ = 0;
103 void markComposed(
Registry& registry) {
108 std::vector<Entity> dirty_to_clear_;
112 bool tracking_ =
false;
113 std::vector<Entity>* changedOut_ =
nullptr;
114 std::vector<Entity> changed_;
117 bool overflowed_ =
false;
120 const glm::mat4* matrix;
124 std::vector<Slot> slots_;
125 bool have_dirty_ =
false;
126 bool have_static_ =
false;
127 bool have_children_ =
false;
128 bool have_collapsed_ =
false;
130 void setFlatCollapsed(Registry& registry, Entity entity,
bool collapsed) {
131 if (!collapsed && !have_collapsed_)
return;
132 const bool present = have_collapsed_ && registry.has<FlatGeometryCollapsed>(entity);
133 if (collapsed && !present) {
134 registry.emplace<FlatGeometryCollapsed>(entity);
135 have_collapsed_ =
true;
136 }
else if (!collapsed && present) {
137 registry.remove<FlatGeometryCollapsed>(entity);
148 void compose(Registry& registry) {
150 if (tracking_ && !overflowed_) changedOut_ = &changed_;
151 updateDirtyTransforms(registry);
152 changedOut_ =
nullptr;
154 if (changed_.size() > registry.countOf<Transform>()) {
159 markComposed(registry);
164 u64 seenRegistry_ = 0;
166 u32 seenTransformVersion_ = 0;
167 u32 seenUnstampedEpoch_ = 0;
168 std::vector<Entity> retargeted_;
169 std::vector<std::pair<u32, u32>> changedSlots_;
177 void updateDirtyTransforms(Registry& registry) {
178 dirty_to_clear_.clear();
179 have_dirty_ = !registry.entitiesWith<TransformDirty>().empty();
180 have_static_ = !registry.entitiesWith<TransformStatic>().empty();
181 auto& rel = registry.childOf();
182 have_children_ = rel.linkCount() != 0;
183 have_collapsed_ = !registry.entitiesWith<FlatGeometryCollapsed>().empty();
186 const bool retargetsListed = rel.takeRetargeted(retargeted_);
187 const bool full = seenRegistry_ != registry.identity() || seenTick_ == 0 || !retargetsListed
188 || seenTransformVersion_ != registry.poolVersion<
Transform>()
190 if (full) composeAll(registry);
191 else composeChanged(registry);
193 for (Entity e : dirty_to_clear_) {
194 if (registry.has<TransformDirty>(e)) {
195 registry.remove<TransformDirty>(e);
199 seenRegistry_ = registry.identity();
200 seenTick_ = registry.beginChangeRead();
201 if (seenTick_ == 0) seenTick_ = registry.beginChangeRead();
202 seenTransformVersion_ = registry.poolVersion<
Transform>();
206 bool inTree(
const ExclusiveRelation& rel, Entity entity)
const {
207 return have_children_ && (rel.hasTarget(entity) || rel.hasSources(entity));
211 void composeAll(Registry& registry) {
212 const auto& rel = registry.childOf();
217 registry.eachLive<
Transform>([®istry, &rel,
this](Entity entity,
Transform& transform) {
218 if (!inTree(rel, entity)) copyRoot(registry, entity, transform);
221 registry.eachLive<
Transform>([®istry, &rel,
this](Entity entity,
Transform& transform) {
222 if (inTree(rel, entity))
return;
223 const bool isStatic = have_static_ && registry.has<TransformStatic>(entity);
224 const bool isDirty = have_dirty_ && registry.has<TransformDirty>(entity);
225 if (isStatic && !isDirty)
return;
226 updateRootTransform(registry, entity, transform, isDirty);
229 if (!have_children_)
return;
230 for (
const auto& tree : rel.cascade()) {
231 if (!tree.alive)
continue;
232 slots_.resize(tree.entries.size());
233 for (
u32 i = 0, n =
static_cast<u32>(tree.entries.size()); i < n; ++i) composeEntry(registry, tree.entries, i);
238 void composeChanged(Registry& registry) {
239 const auto& rel = registry.childOf();
240 const auto& trees = have_children_ ? rel.cascade() : emptyCascade();
241 changedSlots_.clear();
244 const auto changed = [&](Entity entity,
bool tagged,
Transform* transform) {
245 if (!tagged && have_static_ && registry.has<TransformStatic>(entity))
return;
246 if (inTree(rel, entity)) {
247 const auto at = rel.cascadeAt(entity);
248 changedSlots_.emplace_back(at.tree, at.slot);
252 if (tagged) dirty_to_clear_.push_back(entity);
255 updateRootTransform(registry, entity, *transform, tagged);
259 registry.eachChangedSince<
Transform>(seenTick_, [&](Entity entity,
Transform& transform) {
260 if (!inTree(rel, entity)) { copyRoot(registry, entity, transform);
return; }
261 const auto at = rel.cascadeAt(entity);
262 changedSlots_.emplace_back(at.tree, at.slot);
265 registry.eachChangedSince<
Transform>(seenTick_, [&](Entity entity,
Transform& transform) {
266 changed(entity, have_dirty_ && registry.has<TransformDirty>(entity), &transform);
271 for (
const Entity entity : retargeted_) {
272 if (registry.valid(entity)) changed(entity,
true, registry.tryGet<Transform>(entity));
275 for (
const Entity entity : registry.entitiesWith<TransformDirty>()) {
277 if (!registry.changedSince<Transform>(entity, seenTick_)) changed(entity,
true, registry.tryGet<Transform>(entity));
280 if (changedSlots_.empty())
return;
283 std::sort(changedSlots_.begin(), changedSlots_.end());
285 for (
const auto [t, start] : changedSlots_) {
286 const auto& order = trees[t].entries;
287 if (t != tree) { tree = t; covered = 0; slots_.resize(order.size()); }
288 if (start < covered)
continue;
289 const u32 parent = order[start].parent;
291 for (
u32 i = start, end = order[start].end; i < end; ++i) composeEntry(registry, order, i);
292 covered = order[start].end;
296 static const std::vector<ExclusiveRelation::CascadeTree>& emptyCascade() {
297 static const std::vector<ExclusiveRelation::CascadeTree> none;
307 Slot contextOf(Registry& registry,
const std::vector<ExclusiveRelation::CascadeEntry>& order,
u32 slot) {
308 static const glm::mat4 identity{1.0f};
311 Slot context{&identity,
true,
false};
312 for (
usize k = chain_.size(); k-- > 0;) {
313 const Entity entity = order[chain_[k]].entity;
314 auto* transform = registry.tryGet<
Transform>(entity);
315 const bool localDirty = have_dirty_ && registry.has<TransformDirty>(entity);
316 const bool collapsedTag = have_collapsed_ && registry.has<FlatGeometryCollapsed>(entity);
317 if (k + 1 == chain_.size()) {
319 if (!transform) context = {&identity,
true,
false};
320 else if (have_static_ && !localDirty && registry.has<TransformStatic>(entity)) context = {&transform->cachedMatrix_,
false, collapsedTag};
323 transform->cachedMatrix_ =
math::compose(transform->position, transform->rotation, transform->scale);
324 context = {&transform->cachedMatrix_,
true, transform->scale.x == 0 || transform->scale.y == 0};
328 const bool dirty = context.dirty || localDirty;
329 const bool keepStatic = transform && have_static_ && !dirty && registry.has<TransformStatic>(entity);
331 context = {transform ? &transform->cachedMatrix_ : context.matrix, keepStatic ? false : dirty, collapsedTag};
335 std::vector<u32> chain_;
341 bool plainRoots()
const {
return !have_static_ && !have_dirty_ && !changedOut_; }
344 void copyRoot(Registry& registry, Entity entity, Transform& transform) {
345 transform.worldPosition = transform.position;
346 transform.worldRotation = transform.rotation;
347 transform.worldScale = transform.scale;
348 transform.decomposed_ =
true;
349 const bool collapsed = transform.scale.x == 0 || transform.scale.y == 0;
350 if (collapsed || have_collapsed_) setFlatCollapsed(registry, entity, collapsed);
354 void updateRootTransform(Registry& registry, Entity entity, Transform& transform,
bool localDirty) {
357 if (!transform.decomposed_
358 || transform.worldPosition != transform.position
359 || transform.worldRotation != transform.rotation
360 || transform.worldScale != transform.scale) {
361 changedOut_->push_back(entity);
364 transform.worldPosition = transform.position;
365 transform.worldRotation = transform.rotation;
366 transform.worldScale = transform.scale;
367 transform.decomposed_ =
true;
368 setFlatCollapsed(registry, entity, transform.scale.x == 0 || transform.scale.y == 0);
369 if (localDirty) dirty_to_clear_.push_back(entity);
373 void composeEntry(Registry& registry,
const std::vector<ExclusiveRelation::CascadeEntry>& order,
u32 i) {
374 static const glm::mat4 identity{1.0f};
375 const Entity entity = order[i].entity;
376 auto* transform = registry.tryGet<
Transform>(entity);
377 const bool localDirty = have_dirty_ && registry.has<TransformDirty>(entity);
383 if (localDirty) dirty_to_clear_.push_back(entity);
384 setFlatCollapsed(registry, entity,
false);
385 slots_[i] = {&identity,
true,
false};
386 }
else if (have_static_ && !localDirty && registry.has<TransformStatic>(entity)) {
388 slots_[i] = {&transform->cachedMatrix_,
false, registry.has<FlatGeometryCollapsed>(entity)};
390 updateRootTransform(registry, entity, *transform, localDirty);
392 transform->cachedMatrix_ =
math::compose(transform->worldPosition, transform->worldRotation, transform->worldScale);
393 slots_[i] = {&transform->cachedMatrix_,
true, transform->scale.x == 0 || transform->scale.y == 0};
398 const Slot parent = slots_[order[i].parent];
399 const bool dirty = parent.dirty || localDirty;
400 const bool keepStatic = transform && have_static_ && !dirty && registry.has<TransformStatic>(entity);
401 bool flatCollapsed = parent.flatCollapsed;
402 if (transform && !keepStatic) {
403 flatCollapsed = transform->scale.x == 0 || transform->scale.y == 0
404 || (parent.flatCollapsed && transform->rotation.x == 0 && transform->rotation.y == 0);
405 const auto matrix = *parent.matrix *
math::compose(transform->position, transform->rotation, transform->scale);
406 if (changedOut_) ++visited_;
409 if (matrix != transform->cachedMatrix_) {
410 if (changedOut_) changedOut_->push_back(entity);
411 transform->cachedMatrix_ = matrix;
412 transform->decomposed_ =
false;
416 }
else if (keepStatic) {
417 flatCollapsed = registry.has<FlatGeometryCollapsed>(entity);
419 setFlatCollapsed(registry, entity, flatCollapsed);
420 if (localDirty) dirty_to_clear_.push_back(entity);
423 slots_[i] = {transform ? &transform->cachedMatrix_ : parent.matrix, keepStatic ? false : dirty, flatCollapsed};