Games: Playing with Threads

Games: Playing with Threads
Ben Nicholson
Shared Technology Group Lead
Frontier Developments
[email protected]
www.frontier.co.uk
Games: Playing With Threads
What is This Talk?
It Is:
A game developer’s view on the history, current state, and future direction
of parallel processing in high-end video games.
It Isn’t:
An introduction to parallel processing and the various primitives involved.
Games: Playing With Threads
What will I Talk About?
• History of Parallel Processing in Games
• Current State of Play
• What Might a Future Game Engine Look Like?
Games: Playing With Threads
What will I Talk About?
• History of Parallel Processing in Games
• Current State of Play
• What Might a Future Game Engine Look Like?
Games: Playing With Threads
Typical 90’s (and Earlier) Game Loop
void Run()
{
Time previousTime = GetCurrentTime();
while(IsHavingFun())
{
Time const currentTime = GetCurrentTime();
Time const timeStep = currentTime - previousTime;
for each (object in gameObjects)
{
object->Update(timeStep);
}
for each (object in gameObjects)
{
object->Render()
}
previousTime = CurrentTime;
}
}
}
Images: Nintendo Entertainment System/Famicom photo by Evan-Amos, Sega Master System 2 photo by Darz-Mol (GNU Free Document Licence) ,
Super Nintendo Entertainment System/Super Famicom photo by Evan-Amos (Creative Commons Attribution Share Alike 3.0 Unported), Sega Mega Drive/Sega Genesis photo by Evan-Amos, Super Mario World © Nintendo
Games: Playing With Threads
Hardware Acceleration: First Steps Towards Parallel Processing
Example usage of GPU:
Images: StarFox © Nintendo, Virtua Racing © Sega
Games: Playing With Threads
Switch To Optical Storage Media
Nintendo GameCube, Sony PlayStation (1), Sega Saturn
Cartridge storage replaced by optical media
Pro:
• Cheaper for large volume of data storage
Con:
• Much slower access rate
Games: Playing With Threads
Storage Properties: Cartridge (on chip) vs. Optical Media
N64 (Cartridge)
• 4 – 64MB (depending on game)
• ‘Real time’ and predictable data access rates.
• Bandwidth limited by CPU & Bus
(Bus speed (250MB/s)).
PlayStation 1 (2 x speed CD Drive)
• 650MB storage per disk (double layered)
• 0.31MB/s transfer rate (maximum)
• ~35 minutes to read entire disk
PlayStation 3 (single speed Blu-Ray drive)
• 50GB storage per disk (double layered)
• 9MB/s transfer rate
• ~95 minutes to read entire disk
Images: Driver 2 © Infogrames/Reflections Ubisoft, Metal Gear Solid 4: Guns of the Patriots © Konami
Games: Playing With Threads
The Solution: Asynchronous Resource Loading
Usually looks something like this:
Request a resource (eg. model, animation, sound etc.)
resHandle = RequestResource<ResourceType>(resourcePath);
and then periodic calls to:
resHandle.GetIsLoaded();
or a single call to finish loading (if there’s no other work to do):
resHandle.WaitUntilLoaded();
Now we can access the data...
ResourceType* resData = resHandle.GetData();
Image: Grand Theft Auto III © Rockstar Games
Games: Playing With Threads
Aside: Vector Units (A different kind of parallelism)
Speeds up vector maths operations by using single instructions operating
on all components in parallel
Can be used to calculate multiple floating point values in parallel (using the
same algorithm)
Games: Playing With Threads
Aside: CPU Memory Cache
• Small block(s) of very fast on chip memory
• Caches recently accessed pages of memory.
• Cache based optimisations heavily used in games
• Doesn’t play nicely with multithreading
Games: Playing With Threads
What will I Talk About?
• History of Parallel Processing in Games
• Current State of Play
• What Might a Future Game Engine Look Like?
Games: Playing With Threads
Current Generation: Consoles With Multi-Core CPUs Arrive
• PlayStation 3, Xbox 360 (and later WiiU)
• PlayStation 3
• 3Ghz ‘CELL Broadband Engine’ CPU
• Power Processing Element (PPE)
• 8 x Synergistic Processing Elements (SPEs)
• 5 or 6 really
• 256KB local storage
• Main memory access via manual DMA transfers only
Images: Sony PlayStation 3 image by Evan-Amos, Flower © That Game Company/Sony Computer Entertainment, Uncharted 3 © Naughty Dog/Sony Computer Entertainment, Heavy Rain © Quantic Dream/Sony Computer Entertainment,
Little Big Planet 2 © Media Molecule/Sony Computer Entertainment
Games: Playing With Threads
Current Generation: Consoles With Multi-Core CPUs Arrive
• Xbox 360
• 3Ghz Xenon CPU
• 3 core PowerPC processor (very similar to 3xPS3 PPU cores)
Images: Microsoft XBox 360 image by Evan-Amos, Halo 4 © 343 Industries/Microsoft, Kinectimals © Frontier Developments/Microsoft,
Viva Piñata © Rare/Microsoft, Forza Motorsport 4 © Turn 10 Studios/Microsoft
Games: Playing With Threads
We Now Have Multiple Cores
But…
• SPUs are best suited to small and streaming tasks.
So
• PS3 really only has one main CPU hardware thread (and 6 SPUs)
• 360 has three main CPU hardware threads (and no SPUs)
We only have a small number of cores
How to use the cores?
What about cross platform titles?
Games: Playing With Threads
Solution: Separate Render Thread
Games: Playing With Threads
Naturally Parallelisable Tasks
• Physics
• Simulation Islands
• Animation
• Split up ‘calculate animation state’ from ‘decompress/calculate
animation’ and leave gap before it is required.
Games: Playing With Threads
Naturally Parallelisable Tasks
Object Update Interface:
class GameObject
{
...
virtual
virtual
virtual
virtual
virtual
...
};
void
void
void
void
void
AdvancePreAnimation(float timeStep);
AdvancePostAnimation(float timeStep);
AdvancePrePhysics(float timeStep);
AdvanceDuringPhysics(float timeStep);
AdvancePostPhysics(float timeStep);
(or pass ‘update type’ but still messy)
Games: Playing With Threads
Games: Playing With Threads
Games: Playing With Threads
What will I Talk About?
• History of Parallel Processing in Games
• Current State of Play
• What Might a Future Game Engine Look Like?
Games: Playing With Threads
What’s Next for Gaming Hardware?
Sony PlayStation 4 Recently announced
• 8 Core 64-bit x86 CPU
• 8GB GDDR5 RAM (176GB/sec)
• Blu-Ray Drive
A new approach to multi-threading in games is required to handle this kind
of architecture effectively…
Games: Playing With Threads
Requirements of a Highly Multi-Threaded Game Engine
•
•
•
•
Deterministic
Reasonable processing overhead compared to the serial version
Scales well as number of cores increases
Assumed to run on a regular shared memory architecture with some coprocessors for specific/small tasks
What Do We Need To Lose?
• Having a global ‘current game state’.
Games: Playing With Threads
What Existing Ideas/Technologies Can We Use?
Job Systems
Games: Playing With Threads
What Existing Ideas/Technologies Can We Use?
Job Systems (Example Usage)
Games: Playing With Threads
What Existing Ideas/technologies Can We Use?
Functional (Style) Programming
"No matter what language you work in, programming in a functional style
provides benefits. You should do it whenever it is convenient, and you
should think hard about the decision when it isn’t convenient.”
John Carmack, ID Games
Pure Functions:
• Only use the parameters which are passed into it as input
• Return only values which are calculated purely from the input values
• Have no logical side effects
Games: Playing With Threads
What Existing Ideas/technologies Can We Use?
Copy-On-Write
• Smart pointers which require locks to get to data.
Mechanism for allowing large modifiable data sets to be passed around
more efficiently.
• Pass handles to data
• Data must be locked for read or write access
• When locked for write access:
• Written to directly if not-shared
• Copied if shared
Games: Playing With Threads
What Existing Ideas/technologies Can We Use?
Component Systems
• Most modern game engines use game objects comprised of
Components
• Components are basic functionality building blocks
Eg.
• DogGameObject
• SkeletalMeshInstance
• PhysicsAssetInstance
• DogMovementController
Games: Playing With Threads
What Existing Ideas/technologies Can We Use?
Independent Game and Render Frame Rates
Games: Playing With Threads
A Potential Solution
-
Store all game state in a ‘Game Frame’ structure
Generate each frame as a stand alone state from the previous frame
Games: Playing With Threads
A Potential Solution
-
Objects & Components can only access previous frame’s data, or any
objects which they explicitly depend on.
-
Maintain a queue of ‘available updates’ (JobSystem)
-
Prioritise Objects/Components which have dependants
Lots of Objects/Components which can be updated in parallel
Games: Playing With Threads
A Potential Solution
Pros:
- Using Copy-On-Write allows each frame to represent the complete
game state for that frame with a reasonable overhead
- All independent objects can be updated in parallel
- Scales well as long as number of independent objects increases in line
with number of cores
Cons:
- Requires well disciplined coding
- Potentially Difficult to inspect current state in debugger
Cons could be fixed with a new language and support tools?
Games: Playing With Threads
Summary
-
Multithreading complex code is hard
Multithreading complex code which is scalable to many cores is harder
It is an unsolved problem
An exciting time in game development
Thank You!
Ben Nicholson
Shared Technology Group Lead
Frontier Developments
[email protected]
www.frontier.co.uk
Introduction To Frontier Developments
Ben Nicholson
Shared Technology Group Lead
Frontier Developments
[email protected]
www.frontier.co.uk
Who are We?
•Founded in 1994 by David Braben
•Offices in Cambridge, UK and Halifax, Canada
•Large Independent games developer
•Develop games using our own technology (Cobra Engine)
•Diverse Range of Projects...
What Have We Done?
What Have We Done?
What Are We Doing Now?
Coaster Crazy
•Released 14 November
•iOS
•Freemium
•Roller Coasters
•Crazy
What Are We Doing Now?
•Kickstarter
•High End PCs
•MMO
What Are We Doing Now?
Also, projects which haven’t been
announced yet...
?
How Do We Do It?
In House (Cobra) Engine
• Platform abstraction (C++, Objective C, etc.)
• Component system (C++)
• Gameplay scripting (Lua)
• In-house Physics SDK (C++)
• Highly-optimised custom maths library (C++ and assembly/intrinsics)
• Content tools (C# & C++)
• Cross platform build system (JamPlus + Python)
• Automated testing system (BuildBot + Python)
• Code Documentation (Doxygen + BuildBot)
• Middleware (Scaleform, Wwise)
What Do We Need?
Designers, Artists, Programmers, Testers, Producers, Managers
Programming disciplines:
Systems, Platform, Engine, Tools, Physics, Rendering, Audio, Networking,
Gameplay, AI, GUI, R&D, Technical Artists
Art disciplines:
Concept, Environment, Character, Animators, Effect, GUI, Technical Artists
Design disciplines:
Concept, Level, Gameplay, Mechanics, GUI, endless...
YOU?
Thank You!
Ben Nicholson
Shared Technology Group Lead
Frontier Developments
[email protected]
www.frontier.co.uk