munificent / game-programming-patterns

Source repo for the book
http://gameprogrammingpatterns.com/
Other
4.03k stars 495 forks source link

DirtyFlag optimization by avoiding remaking decision #379

Open nategoose opened 8 months ago

nategoose commented 8 months ago

Once you know that a sub-tree is dirty, you know that all of its children are dirty and can avoid making that decision again. Splitting render into Clean (so far) and (known to be) Dirty functions lets most of the nodes only have to either make the decision about clean/dirty (on the clean so far path) or combine the transforms (on the dirty path). The only node that has to do both is the 1st node reached that is dirty. Note that if renderMesh can be moved to the end of the calls (after rendering children) then tail call optimization is also a possibility. Tail call optimization is not reasonably doable when the functions end in a for loop. But it's also possible that renderMesh might be more expensive if done after children.

diff --git a/code/cpp/dirty-flag.h b/code/cpp/dirty-flag.h index 31df713..cf674df 100644 --- a/code/cpp/dirty-flag.h +++ b/code/cpp/dirty-flag.h @@ -147,6 +147,8 @@ namespace DirtyFlag        static const int MAXCHILDREN = 16;        GraphNode* children[MAXCHILDREN];        int numChildren;