SakeTami
Puppygames
Puppygames

patreon


Multithreaded Resource Creation

So I've now started to be able to work on Battledroid again! Still only part-time but much more time than before. Fruits are already being borne of this labour! Multi-threaded fruits, at that, the most complicated and difficult to comprehend of all fruits.

One thing that all games do when the start up is load a bunch of resources in. Most games take a fair amount of time doing this, because they have a lot of resources to load. Most games try and do some sort of animation or progress indication while it's happening too, to varying degrees of success. But even AAA games are guilty generally of their loading animations freezing for a second, and the vast majority of games do the whole thing using just one single thread, which in this day and age of 4, 6 or even 8 core CPUs that are now the norm in desktop machines, is a tragic waste of potential resources.

And of course slightly annoying for your players to wait on.

What we want is a resource loading system that is entirely smooth so we can do a lovely slick animated splash screen / progress indicator, and one that uses every available CPU cycle so it can fully take advantage of these powerful multicore processors.

This is a story in two parts.

Super Attack Programmer Dan's Threading Library

Dan has made a rather simple library for speedily executing a big pile of tasks, each of which can depend on an arbitrary number of other tasks in the tree. In addition, some tasks need to be executed in the thread that owns the current OpenGL context.

Once the tree of dependencies is constructed, it finds all the tasks with no prerequisite tasks, and then feeds them to a thread pool with a number of threads that equals the number of processors available. Every time a task is completed, it unlocks other tasks, which are then fed into a queue to await being slurped out by the hungry thread pool.

It's a very elegant and simple and above all efficient system with several uses.

The threading library is already used by the Voxoid rendering engine to super-efficiently multithread the entire rendering of the engine from start to finish.

Multithreaded Resource Creator

This explores the graph of game resources - all kept in a single giant map of names to resources - and builds up a big dependency tree.

 Each resource is dependent on a post-create task. This in turn is dependent on a main thread task. And that in turn is dependent on a pre-create task.  

The pre-create task is then dependent on all other resources that it references being created, with one or two caveats (we're not allowed circular dependencies - the tree of dependency must by an acyclic graph).

And thus, we have a super-fast, super-efficient resource creation tree.

The threading library kicks off the background threads, and then we run any main thread tasks for at most 15 milliseconds (about a frame), before going off to render the next frame of the splash screen animation.

The first few times we run the resource creation we record how long it takes overall (with a default estimation that I work out during development). After a while we start using a rolling average, and use this duration to interpolate the splash screen animation such that it fades out perfectly to black just as the very last resource is created.

Slick!

Next post... temporal anti-aliasing a go-go. TAA is sick, and mind bogglingly effective.

Comments

Interesting write up!

Elmar van Rijnswou

We have our own very lightweight code - about 10 classes, JDK8 level. There's very little overhead at all. (The only dev tool we use is Eclipse)

Puppygames

What threading/concurrency library are you using? Dev tools?

Person


More Creators