How do dirty flags keep transform-hierarchy updates cheap, and what goes wrong if gameplay code reads a world matrix mid-frame?
answer
- skip what did not move
- parent before child
- a moved root dirties its subtree
- cached value read before the update pass
basics
~20 sA dirty flag marks a node whose local transform changed, so only it and its descendants get new world matrices, once per frame and parent first. Reading a cached world matrix before that pass returns last frame's value.
solid answer
~50 sEach node caches its world matrix and carries a **dirty flag**. Writing a local transform sets the flag instead of recomputing anything, so a node moved three times in a frame is recomputed once, and static props cost nothing. Once per frame an update pass walks the hierarchy **parent before child** — often a single linear pass over a flat array sorted so every parent precedes its children — and recomputes a node when it is dirty or its parent was just recomputed, because a child's world matrix depends on its parent's. Moving a vehicle root still recomputes everything riding on it. The trap is the **stale read**: if AI asks for a weapon's muzzle position after the vehicle moved but before the pass, the cached matrix is last frame's. Fixes are a lazy getter that resolves dirty ancestors on demand, or defined sync points after which world matrices are valid.
code
pseudocode · 14 lines// nodes are stored so that parent[i] < i for every non-root node
for i in 0 .. nodeCount - 1:
p = parent[i]
if p != NONE and recomputed[p]:
dirty[i] = true // parent moved, so this node's world is stale
if dirty[i]:
if p == NONE:
world[i] = local[i]
else:
world[i] = world[p] * local[i]
dirty[i] = false
recomputed[i] = true
else:
recomputed[i] = falsego deeper
Recall that a dirty flag marks a changed node so only changed nodes and their children are recomputed, once per frame.
Explain the parents-first update pass, why a recomputed parent forces its children to recompute, and why a mid-frame read of a cached world matrix can return last frame's value.
Diagnose a one-frame lag in attached objects as a stale read, and choose between lazy getters with eager marking, sync points with assertions, or a forced chain update.
Decide the frame's transform sync points as an engine-wide contract, trading the unpredictable cost of lazy reads against the scheduling discipline sync points demand of every system.
## The problem dirty flags solve In a **transform hierarchy**, each node stores a **local transform** relative to its parent and a **world matrix** derived from it: with column vectors, `world(node) = world(parent) * local(node)`. Recomputing every world matrix every frame is simple but wasteful in a typical level, where most nodes — buildings, props, scenery — never move, and some nodes are moved several times in one frame by different systems (animation, physics, gameplay scripts). A **dirty flag** is a one-bit note on a node that says "my cached world matrix is out of date". It turns transform updates from "always recompute" into "recompute what changed, once". ## The mechanism step by step 1. **Write**: setting a node's local transform stores the new value and marks the node dirty. No matrix product happens yet. 2. **Repeat writes are free**: a node moved three times in a frame is still just dirty. 3. **Update pass**: once per frame, at a defined point, the engine visits nodes in an order where every parent comes before its children. 4. **Recompute**: a node is recomputed if it is dirty *or* its parent was recomputed this pass, because its world matrix is built from the parent's. 5. **Clear**: after recomputing, the node's flag is cleared and its new world matrix is cached for every reader until the next change. ## Flat arrays and propagation order The order matters for correctness, not just speed: if a child were processed before its parent, it would compose its local transform with the parent's *old* world matrix. Recursion from the root guarantees the order, but engines with many nodes often store transforms in **flat arrays sorted so each parent's index is lower than its children's**. A single forward loop then always finds the parent already up to date, touches memory sequentially and needs no recursion. The pseudocode example with this answer shows that loop, propagating "recomputed" from parent to child as it goes. Two costs remain: - The forward loop still **visits** every node to test its flag. Engines with huge static populations keep static nodes out of the array, or keep a list of dirty subtree roots and walk only those subtrees. - **Re-parenting** must keep the parents-first order, by moving the subtree in the array or by re-sorting at a sync point. ## Eager marking vs lazy resolution There are two common ways to decide who is dirty: | Strategy | Cost of a write | Cost of a read | Invariant it needs | |---|---|---|---| | Mark only the written node; propagate during the pass | constant | only valid after the pass | parents-first update order | | Mark the node and all descendants at write time | proportional to the subtree | a lazy getter can resolve on demand | "a dirty node's descendants are all dirty" | The second strategy can stop marking early when it reaches a descendant that is already dirty — that is safe only because the invariant guarantees everything beneath it is dirty too. It is the strategy a **lazy getter** needs: `getWorld(node)` recomputes only if the node itself is flagged, so the flag must already reflect any dirty ancestor. Without eager marking, a lazy getter would have to walk every ancestor on every read. ## The stale-read trap The classic bug: a vehicle is moved by gameplay early in the frame, and before the update pass an AI system reads the world matrix of the weapon its rider holds to aim at the muzzle. The cached matrix is last frame's, so shots leave from where the muzzle *was*, lagging one frame behind a fast vehicle. It is intermittent — it depends on which system runs first — which makes it hard to diagnose. Remedies: - **Lazy getters** that resolve dirty ancestors on read (with eager marking), at the cost of unpredictable work inside the read. - **Defined sync points**: world matrices are valid only after named points in the frame, and systems that need fresh values are scheduled after one; debug builds can assert when a dirty node is read. - **Explicit forced updates** for the rare system that must read mid-frame, updating just the chain from the root to the node it needs. ## What dirty flags do not save - **Moved subtrees.** Moving the vehicle root recomputes the vehicle, its seats, riders and everything they hold; dirty flags avoid work on *unchanged* nodes, not on moved ones. - **Fully dynamic scenes.** If nearly everything moves every frame, flag checks add overhead to a full recompute that happens anyway. - **Downstream work.** A recomputed world matrix often triggers more updates — bounds in spatial structures, render data — which should also be batched rather than done inside the setter.
- Why do engines store transforms in flat arrays sorted so parents come before children?Because a single forward loop then always finds each parent's world matrix already updated, with no recursion and with memory read sequentially, which suits the cache. The cost is maintaining the order: re-parenting must move the subtree in the array or trigger a re-sort at a sync point, and removals leave holes to compact.
- When does a lazy world-matrix getter need eager marking of descendants?Whenever it decides to recompute by looking only at the node's own flag. If writing a parent did not also flag its descendants, a child would look clean and return a matrix built on the parent's old transform. With eager marking the invariant 'dirty node implies dirty descendants' holds and the getter can recompute up the chain only where flags say so.
- Flagging only the moved node is enough; its children's world matrices stay valid.
- Dirty flags make moving a vehicle cheap however many objects ride on it.
- Nodes can be updated in any order as long as each is updated once per frame.
- Reading a cached world matrix is always safe because the engine updates it every frame.
- A node written several times in one frame must be recomputed after each write.