Professional Documents
Culture Documents
Designing The Framework of A Parallel Game Engine
Designing The Framework of A Parallel Game Engine
How to Get the Most Out of a Multi‐Core CPU with Your Game Engine
Table of Contents
How to Get the Most Out of a Multi-Core CPU with Your Game Engine............................................................. 1
1. Introduction............................................................................................................................................................... 4
1.1. Overview .......................................................................................................................................................... 4
1.2. Assumptions .................................................................................................................................................... 4
2. Parallel Execution State............................................................................................................................................ 5
2.1. Execution Modes ............................................................................................................................................ 5
2.1.1. Free Step Mode ..................................................................................................................................... 6
2.1.2. Lock Step Mode .................................................................................................................................... 6
2.2. Data Synchronization..................................................................................................................................... 6
3. The Engine................................................................................................................................................................. 8
3.1. Framework ....................................................................................................................................................... 9
3.1.1. Scheduler .............................................................................................................................................. 10
3.1.2. Universal Scene & Objects................................................................................................................ 10
3.2. The Managers ................................................................................................................................................ 11
3.2.1. Task Manager ...................................................................................................................................... 11
3.2.2. State Manager ...................................................................................................................................... 12
3.2.3. Service Manager .................................................................................................................................. 13
3.2.4. Environment Manager ....................................................................................................................... 13
3.2.5. Platform Manager ...............................................................................................................................13
4. Interfaces .................................................................................................................................................................. 15
4.1. Subject and Observer Interfaces ................................................................................................................ 15
4.2. Manager Interfaces ....................................................................................................................................... 15
4.3. System Interfaces .......................................................................................................................................... 15
4.4. Change Interfaces ......................................................................................................................................... 16
5. Systems ..................................................................................................................................................................... 17
5.1. Types............................................................................................................................................................... 17
5.2. System Components..................................................................................................................................... 17
5.2.1. System ................................................................................................................................................... 17
5.2.2. Scene ..................................................................................................................................................... 18
5.2.3. Object ................................................................................................................................................... 18
5.2.4. Task ....................................................................................................................................................... 18
6. Tying It All Together.............................................................................................................................................. 19
6.1. Initialization Stage......................................................................................................................................... 19
6.2. Scene Loading Stage ..................................................................................................................................... 19
6.3. Game Loop Stage ......................................................................................................................................... 20
6.3.1. Task Execution.................................................................................................................................... 20
6.3.2. Distribution.......................................................................................................................................... 21
6.3.3. Runtime Check and Exit.................................................................................................................... 21
7. Final Thoughts ........................................................................................................................................................ 22
8. About the Author ................................................................................................................................................... 23
Appendix A. Example Engine Diagram ............................................................................................................... 24
Appendix B. Engine and System Relationship Diagram.................................................................................... 25
Appendix C. The Observer Design Pattern......................................................................................................... 26
Appendix D. Tips on Implementing Tasks .......................................................................................................... 27
List of Figures.................................................................................................................................................................... 28
Bibliography....................................................................................................................................................................... 29
1. Introduction
With the advent of multiple cores within a processor the need to create a parallel game engine has
become more and more important. It is still possible to focus primarily on just the GPU and have a
single threaded game engine, but the advantage of utilizing all the processors on a system, whether
CPU or GPU, can give a much greater experience for the user. For example, by utilizing more CPU
cores a game could increase the number of rigid body physics object for greater effects on screen, or
developing smarter AI that gives it a more human like behavior.
1.1. Overview
The “Parallel Game Engine Framework” or engine is a multi-threaded game engine that is designed to
scale to as many processors as are available within a platform. It does this by executing different
functional blocks in parallel so that it can utilize all available processors. This is easier said than
done as there are many pieces to a game engine that often interact with one another and can cause
many threading errors because of that. The engine takes these scenarios into account and has
mechanisms for getting proper synchronization of data without having to be bound by
synchronization locks. The engine also has a method for executing data synchronization in parallel
in order to keep serial execution time at a minimum.
1.2. Assumptions
This paper assumes a good working knowledge of modern computer game development as well as
some experience with game engine threading or threading for performance in general.
2. Parallel Execution State
The concept of a parallel execution state in an engine is crucial to an efficient multi-threaded
runtime. In order for a game engine to truly run parallel, with as little synchronization overhead as
possible, it will need to have each system operate within its own execution state with as little
interaction as possible to anything else that is going on in the engine. Data still needs to be shared
however, but now instead of each system accessing a common data location to say, get position or
orientation data, each system has its own copy. This removes the data dependency that exists
between different parts of the engine. Notices of any changes made by a system to shared data are
sent to a state manager which then queues up all the changes, called messaging. Once the different
systems are done executing, they are notified of the state changes and update their internal data
structures, which is also part of messaging. Using this mechanism greatly reduces synchronization
overhead, allowing systems to act more independently.
Graphics
Physics Physics
AI
Other Other
State Manager
Graphics Graphics
Physics Physics
AI AI
Other Other
State Manager
Engine
Framework Managers
State
UScene Service
Environment
UObject … UObject
Platform
Interfaces
3.1. Framework
The framework is responsible for tying in all the different pieces of the engine together. Engine
initialization occurs within the framework, with the exception of the managers which are globally
instantiated. The information about the scene is also stored in the framework. For the purpose of
flexibility the scene is implemented as what is called a universal scene which contains universal objects
which are merely containers for tying together the different functional parts of a scene. More
information on this is available in section 3.1.2.
The game loop is also located within the framework and has the following flow:
UScene UObject
System System
Scene Scene
Object Object
Thread 1 Graphics AI …
Thread 3 Physics AI AI …
5.1. Types
The engine should have some predefined systems types that go along with them for standard game
components. Some examples are as follows: Geometry, Graphics, Physics (rigid body collision),
Audio, Input, AI, and Animation.
A custom type is also recommended for systems that implement functionality outside of the
common functional blocks in a game. Take note that any systems that modify the custom type’s
specific data items will need to know about the custom type’s interface as the engine does not
provide this information.
System
Scene Task
5.2.1. System
The system component, or system, is responsible for initializing system resources that will remain
more or less constant throughout the execution of the engine. An example of this is the graphics
system analyzing all the passed in resource locations to determine where they are located for quicker
loading upon use of the resource. The screen resolution would also be another item set by the
graphics system.
The system is also the main entry point for the framework and provides information about itself,
such as its type, and provides methods for creation and destruction of scenes.
5.2.2. Scene
The scene component, otherwise known as a system scene, is responsible for handling resources that
are pertinent to the existing scene. The universal scene uses this scene as an extension of its
functionality to make available the properties this system scene provides. An example of this
component is the physics scene creating a new world and setting the gravity for the world upon
scene initialization.
The scene also provides methods for creation and destruction of objects. It also owns the task
component, which is used to operate on the scene, and provide a method for retrieving it.
5.2.3. Object
The object component, alternatively a. system object, is an object within the scene and is typically
associated with what is visible to the user on screen. The universal object uses this object
component as an extension of its functionality to allow the properties this object provides to be
exposed via the universal object.
An example of how this could be used is a universal object extending geometry, graphics, and
physics to create a beam of wood on screen. The geometry would hold the position, orientation,
and scale information of the object, the graphics system would display it on screen using the given
mesh, and the physics system would apply rigid body collision to it so that it would interact with
other rigid body objects and gravity.
In certain situations a system object may be interested in the changes of a different universal object,
or one of its extensions. In this case a link can be established so that the system object can observe
the other object.
5.2.4. Task
The task component, referred to as a system task, is responsible for operating on the scene. When the
task receives a command to update, from the task manager, it will perform the system’s functionality
on the objects within the scene.
The task can also choose to subdivide its execution into subtasks and schedule the subtasks with the
task manager for even more threaded execution. Doing this allows the engine to scale more readily
to a configuration with multiple processors. This technique is known as data decomposition.
During the task’s update of the scene is when any modifications done to its objects are posted to the
state manager. Refer to section 3.2.2 for more information about the state manager.
6. Tying It All Together
This is a lot of information to absorb all at once, and the different sections aren’t really separate
from one another. The entire engine execution can be broken up into several stages as described in
the following sections.
6.3.2. Distribution
Once all tasks targeted for the current clock have completed execution, the main loop calls the state
manager to distribute the changes.
• The state manager calls each of its change controllers to distribute the changes they have queued
up. This is done by going through each subject’s changes and seeing which observer was listening
to that subject.
• The change controller then calls the observer informing it of the change (a pointer to the subject’s
interface is also passed to the observer). For free step mode, the observer gets the changed data
from the change controller, but for lock step mode the observer queries the subject for the data.
• The observers that are interested in the changes done by a system object will typically be other
system objects that are all attached to the same universal object. This makes it possible for the
change distribution to be broken up into tasks for execution in parallel. To limit synchronization,
group together in a task any universal objects’ extensions that are linked.
Engine
Framework Managers
Interfaces
AI Audio
Direct 3D 9 Direct 3D 9
Systems
Appendix B. Engine and System Relationship
Diagram
Scene
System
Task
Universal Scene
Scene
System
Framework
Task
System
Task
Manager
Task
Appendix C. The Observer Design Pattern
The observer design pattern is documented in the book “Design Patterns: Elements of Reusable
Object-Oriented Software,” written by Erich Gamma et al., and originally published by Addison-
Wesley in 1995.
The basic premise of this pattern is that any items interested in data or state changes in other items
are not burdened with having to poll the items from time to time to see if there are any changes.
The pattern defines a subject and an observer that are used for the change notification. It works by
having an observer observe a subject for any changes. The change controller acts as a mediator between
the two. The following diagram illustrates the relationship:
5 Change Controller
4 1 8
3 6
Subject Observer