77
88## Motivation
99
10- "Flag" and "bit" are synonymous in programming -- they both mean a single micron
11- of data that can be in one of two states. We call those "true" and "false", or
12- sometimes "set" and "cleared". I'll use all of these interchangeably. "Dirty
13- bit" is an equally <span name =" specific " >common</span > name for this pattern,
14- but I figured I'd stick with the name that didn't seem as prurient.
15-
16- <aside name =" specific " >
17-
18- Wikipedia's editors don't have my level of self-control and went with [ dirty
19- bit] ( http://en.wikipedia.org/wiki/Dirty_bit ) .
20-
21- </aside >
22-
23- ### Locating a ship at sea
24-
2510Many games have something called a * scene graph* . This is a big data structure
2611that contains all of the objects in the world. The rendering engine uses this to
2712determine where to draw stuff on the screen.
@@ -49,13 +34,13 @@ transform to it, and then renders it there in the world. If we had a scene
4934
5035However, most scene graphs are <span name =" hierarchical " >* hierarchical* </span >.
5136An object in the graph may have a parent object that it is anchored to. In that
52- case, its transform is relative to the * parent's* position and isn't its
37+ case, its transform is relative to the * parent's* position and isn't an
5338absolute position in the world.
5439
5540For example, imagine our game world has a pirate ship at sea. Atop the ship's
5641mast is a crow's nest. Hunched in that crow's nest is a pirate. Clutching the
57- pirate's shoulder is a parrot. The ship's local transform will position it in
58- the sea. The crow's nest's transform positions it on the ship, and so on.
42+ pirate's shoulder is a parrot. The ship's local transform positions the ship in
43+ the sea. The crow's nest's transform positions the nest on the ship, and so on.
5944
6045<span name =" pirate " ></span >
6146<img src =" images/dirty-flag-pirate.png " alt =" A pirate ship containing a crow's nest with a pirate in it with a parrot on his shoulder. " />
@@ -146,7 +131,7 @@ world transform *four* times when we only need the result of the final one.
146131We only moved four objects, but we did * ten* world transform calculations.
147132That's six pointless calculations that get thrown out before they are ever used
148133by the renderer. We calculated the parrot's world transform * four* times, but it
149- only got rendered once.
134+ is only rendered once.
150135
151136The problem is that a world transform may depend on several local transforms.
152137Since we recalculate immediately each time * one* of the transforms changes, we end up
@@ -168,12 +153,26 @@ engineering a little slippage.
168153
169154</aside >
170155
171- To do this, we add a flag to each object in the graph. When the local transform
156+ To do this, we add a * flag* to each object in the graph. "Flag" and "bit" are
157+ synonymous in programming -- they both mean a single micron of data that can be
158+ in one of two states. We call those "true" and "false", or sometimes "set" and
159+ "cleared". I'll use all of these interchangeably.
160+
161+ When the local transform
172162changes, we set it. When we need the object's world transform, we check the
173163flag. If it's set, we calculate the world transform and then clear the flag. The
174164flag represents, "Is the world transform out of date?" For reasons that aren't
175165entirely clear, the traditional name for this "out-of-date-ness" is "dirty".
176- Hence: * a dirty flag* .
166+ Hence: * a dirty flag* . "Dirty bit" is an equally
167+ <span name =" specific " >common</span > name for this pattern, but I figured I'd
168+ stick with the name that didn't seem as prurient.
169+
170+ <aside name =" specific " >
171+
172+ Wikipedia's editors don't have my level of self-control and went with [ dirty
173+ bit] ( http://en.wikipedia.org/wiki/Dirty_bit ) .
174+
175+ </aside >
177176
178177If we apply this pattern and then move all of the objects in our previous
179178example, the game ends up doing:
@@ -238,7 +237,7 @@ There are a couple of other requirements too:
238237 that's often a better choice than using this pattern and calculating the
239238 derived data from scratch when needed.
240239
241- All of this makes it sound like dirty flags are never appropriate, but you'll
240+ This makes it sound like dirty flags are rarely appropriate, but you'll
242241find a place here or there where they help. <span name =" hacks " >Searching</span >
243242your average game codebase for the word "dirty" will often turn up uses of this
244243pattern.
@@ -481,7 +480,7 @@ up like this.
481480This is similar to the original naïve implementation. The key changes are that
482481we check to see if the node is dirty before calculating the world transform and
483482we store the result in a field instead of a local variable. When the node is
484- clean, we skip ` combine() ` completely and use the old but still correct ` world_ `
483+ clean, we skip ` combine() ` completely and use the old- but- still- correct ` world_ `
485484value.
486485
487486The <span name =" clever " >clever</span > bit is that ` dirty ` parameter. That will
@@ -552,7 +551,7 @@ This pattern is fairly specific, so there are only a couple of knobs to twiddle:
552551
553552 <aside name =" hysteresis " >
554553
555- The term in human-computer interaction for in intentional delay between
554+ The term in human-computer interaction for an intentional delay between
556555 when a program receives user input and when it responds is [ * hysteresis* ] ( http://en.wikipedia.org/wiki/Hysteresis ) .
557556
558557 </aside >
0 commit comments