Document 7660154
Download
Report
Transcript Document 7660154
Sim, Render, Repeat
An Analysis of Game Loop Architectures
Daniel Martin and Michael Balfour
March 23, 2006
“Who?”
Introduction
Introduction: The Speakers
Technical directors at EA-Tiburon
MS degrees
7+ years in the game industry
Exposed to many games and architecture
Also programmed on Apple ][’s and mainframes
Introduction: Motivation
Hidden complexities
Little coverage
Highest-level game architecture
Introduction: Agenda
Definitions
Timeline
Architecture decisions
Case study
Questions
Slides will be made available on the GDC website
“What?”
Game loop architectures defined
Definition: Background
Source Code
Digger
Duke3D
Freespace2
Harry Potter
Madden
NASCAR
Popcap Engine
Quake2
Unreal Tournament v432
Wumpus
Books
3D Game Engine Architecture, First Edition
Core Techniques and Algorithms in Game Programming
Game Coding Complete, Second Edition
Definition: Pseudocode pattern
GameLoop()
{
Startup();
while (!done)
{
GetInput();
Sim();
Render();
}
Shutdown();
}
Definition: Formal
Game Loop
A thread of execution consisting of
a startup phase,
a looping phase that processes inputs/outputs,
a shutdown phase.
We use “Game Loop” and “Loop” interchangeably
“When?”
Game Loop Timeline
1947: Cathode Ray Tube Amusement Device
CRT Amusement Device
Noughts and Crosses
Gun Fight
Genesis
Gypsy Juggler
First patented video game
Game: Fire a missile at a target
Hardware loop run by master clock
Saturn
MHz Barrier
1952: Noughts and Crosses
CRT Amusement Device
Noughts and Crosses
Gun Fight
Gypsy Juggler
First software-based game
Game: Tic Tac Toe
Software loop
Genesis
Saturn
MHz Barrier
1940-1970: Analysis
Game loops have existed for 50+ years
We still use the same game loop patterns today
Seems to be the most intuitive architecture for games
Hardware and software loops
Independently developed
Similar results
1975: Gun Fight
CRT Amusement Device
Noughts and Crosses
Gun Fight
Genesis
Gypsy Juggler
Saturn
First CPU-based arcade video game
Game: Two players shoot at each other
Software Sim loop
Hardware Render and Audio loops
MHz Barrier
1978: Gypsy Juggler
CRT Amusement Device
Gun Fight
Noughts and Crosses
Genesis
Gypsy Juggler
Saturn
First multi-CPU game
Game: Move a gypsy and juggle eggs
Concurrent Software Sim/Audio loops
Two symmetric S2650 CPUs
MHz Barrier
1970-1980: Analysis
Multi-CPU arcade games have existed for 28+ years
Multi-CPU common in arcade games
Typically 2-3 CPUs
Common use of specialized processors
Moved hardware Render/Audio Loops to software
1988: Genesis
CRT Amusement Device
Gun Fight
Noughts and Crosses
Genesis
Gypsy Juggler
First multi-CPU console
68000 CPU for Sim/Render
Z80 CPU for Audio
Saturn
MHz Barrier
1994: Saturn
CRT Amusement Device
Noughts and Crosses
Gun Fight
Genesis
Gypsy Juggler
Saturn
First symmetric Multi-CPU console
Two Hitachi RISC processors
6 processors for video/sound/control
MHz Barrier
1980-2000: Analysis
Multi-CPU Consoles have existed for 18+ years
Many consoles with asymmetric CPUs
Few consoles with symmetric CPUs
“One very fast central processor would be preferable… I think
only one out of 100 programmers is good enough to get that
kind of speed out of the Saturn.”
– Yu Suzuki
2003: MHz Barrier
CRT Amusement Device
Gun Fight
Noughts and Crosses
4000
Genesis
Gypsy Juggler
MHz Barrier
Saturn
CPU frequency 1995-2006
3500
3000
MHz
2500
Intel
Amd
2000
1500
1000
500
0
95
96
97
98
99
00
01
Year
02
03
04
05
06
2000-2006: Analysis
Paradigm Shift
“The Free Lunch is Over”
Parallel by necessity
Legacy code won’t run magically faster
More performance requires mastering concurrency
Single CPU no longer the norm
“How?”
Game Loop Architecture Decisions
Architecture: Game loop complexity
Concurrency
Coupling
Time
Complexity
Architecture: Game loop complexity
Concurrency
Coupling
Time
Complexity
Architecture: Time
Problem
Running smoothly in real-time
Solution
Frequency-Driven Game Loop
Divide time into discrete iterations
Attempt to run “fast enough” and “smooth enough”
Consequence
Each Loop iteration is a time slice
Architecture: Time
Challenge
How to maintain a loop frequency with variable execution
time
Factors
Performance
Tolerance
Architecture Decisions
Scheduling
Time Step
Determinism
Simplicity
Architecture: Time
Scheduling
Controls when a loop iteration starts
Scheduling
Immediate
Best-Fit
Aligned
Architecture: Legend
Time Axis
Real time
Ticks at desired frequency
Box
Loop iteration
Length is processing time
Arrows
Time step
Length is simulated time
Data
Data used by a loop
Time
Architecture: Time
Scheduling
Description
Immediate
“As fast as it can”
Factors
Exact
+ Performance
Faster
- Tolerance
Determinism
Slower
+ Simplicity
Varied
Time
Architecture: Example
Scheduling: Immediate
Time
Early adventure games
while (1)
{
GetInput();
Sim();
Render();
}
Architecture: Time
Scheduling
Best-Fit
Exact
Description
“Averaging”
Tries to
Maintain frequency
Start at exact times
Catch up
Faster
Slower
Factors
Varied
+ Performance
+ Tolerance
Time
Determinism
- Simplicity
Architecture: Time
Scheduling
Description
Aligned
“VSynced”
Factors
Exact
- Performance
Faster
- Tolerance
Slower
Determinism
+ Simplicity
Varied
Time
Architecture: Time
Time Step
Controls interpretation of time
“Simulated” time
Time Step
None
Fixed-Value
Real-Time
Architecture: Time
Time Step
Description
None
Ignores time
Factors
Exact
Performance
Faster
Tolerance
+ Determinism
Slower
+ Simplicity
Varied
Time
Architecture: Time
Time Step
Fixed-Value
Description
Time step always constant
Factors
Exact
Performance
Faster
- Tolerance
Slower
+ Determinism
- Simplicity
Varied
Time
Architecture: Example
Scheduling: Best-Fit
Time Step: Fixed-Value
Time
const float TIMESTEP = 1 / 60.0f;
while (1)
{
curTime = GetTime();
if ((curTime – lastTime) >=
TIMESTEP)
{
Sim(TIMESTEP);
lastTime = curTime;
}
}
Deterministic real-time games
Architecture: Time
Time Step
Real-Time
Description
Sim Time == Real Time
Factors
Exact
Performance
Faster
+ Tolerance
Slower
- Determinism
- Simplicity
Varied
Time
Architecture: Game Loop Complexity
Concurrency
Coupling
Time
Complexity
Architecture: Coupling
Problem
Supporting systems at different frequencies
Solution
Multiple game loops
Consequence
Loop Coupling
Dependencies between every pair of loops
Architecture: Coupling
Challenge
How to split code and data into multiple loops
Factors
Performance
Tolerance
Simplicity
Architecture Decisions
Frequency Coupling
Data Coupling
Video Modes
Memory
Scalability
Architecture: Coupling
Frequency
How much a loop relies on another’s frequency
Frequency
Equal
Multiple
Decoupled
Architecture: Coupling
Frequency
Equal
Time (Loop 1)
Description
1:1
“Lockstep”
Factors
- Video modes
+ Memory
Performance
- Tolerance
- Scalability
Time (Loop 2)
+ Simplicity
Architecture: Coupling
Frequency
Multiple
Time (Loop 1)
Description
N:1
Multiple of frequency
Sim @ 60 Hz
Render @ 30 Hz
Factors
- Video modes
+ Memory
Performance
Time (Loop 2)
- Tolerance
- Scalability
+ Simplicity
Architecture: Coupling
Frequency
Decoupled
Time (Loop 1)
Description
N:M
Two independent rates
Factors
+ Video modes
- Memory
Performance
+ Tolerance
Time (Loop 2)
+ Scalability
- Simplicity
Architecture: Example
Scheduling: Best-Fit
Time Step: Fixed-Value
Frequency: Decoupled
while (1)
{
time = GetTime()
if ((time – lastTime) >=
SIM_STEP)
{
Sim(SIM_STEP);
lastTime = time;
}
}
Scheduling: Aligned
Time Step: Fixed-Value
Frequency: Decoupled
while (1)
{
Render(RENDER_STEP);
WaitForVSync();
}
Decoupled Sim and Render loops
Architecture: Coupling
Data Coupling
The amount and method of sharing data
Data Coupling
Tight
Loose
None
Architecture: Coupling
Data Coupling
Tight
Description
Data structure reliance
Factors
Video modes
+ Memory
+ Performance
Tolerance
- Scalability
- Simplicity
Architecture: Coupling
Data Coupling
Loose
Description
Formalized data passing
Factors
Video modes
- Memory
- Performance
Tolerance
+ Scalability
+ Simplicity
Architecture: Coupling
Description
Data Coupling
None
Fully independent
No data passing
Factors
Video modes
+ Memory
+ Performance
Tolerance
+ Scalability
+ Simplicity
Architecture: Game loop complexity
Concurrency
Coupling
Time
Complexity
Architecture: Concurrency
Problem
Hardware makers need cutting edge performance
Solution
Hardware contains multiple CPUs
Consequence
Concurrency
Mapping between game loops and CPUs
Architecture: Concurrency
Challenge
How to manage simultaneous execution
Factors
Performance
Simplicity
Scalability
Architecture Decisions
Low-Level Concurrency
High-Level Concurrency
Architecture: Concurrency
Low-Level
Parallelism within a game loop
Low-Level
None
Instruction
Functions
Architecture: Concurrency
Low-Level
Covered extensively
Easiest transition to NextGen
Start small and grow
OpenMP
“Bottom up” approach
Can be postponed
Architecture: Concurrency
High-Level
Parallelism across a pair of game loops
High-Level
Sequential
Interleaved
Parallel
Architecture: Concurrency
High-Level
Sequential
Exact
Description
No overlap of loop
execution
Factors
- Performance
Varied
- Scalability
+ Simplicity
Time
Architecture: Concurrency
High-Level
Interleaved
Exact
Varied
Description
Loops iterate in parallel
Sequential at instruction
level
Concurrency on 1 CPU
Factors
- Performance
Time
+ Scalability
- Simplicity
Architecture: Concurrency
High-Level
Description
Parallel
Exact
Instructions execute at
same time
Factors
+ Performance
Varied
+ Scalability
- Simplicity
Time
“Why?”
Case Study
Case Study: Madden Xbox vs Madden X360
VS
Case Study: Madden Xbox vs Madden X360
Sim
Render
Audio
Scheduling
Immediate
Best-Fit
Sim
Aligned
Render
Audio
Case Study: Madden Xbox vs Madden X360
Sim
Render
Audio
Time Step
None
Fixed-Value
Sim
Real-Time
Render
Audio
Case Study: Madden Xbox vs Madden X360
Sim-Audio
Render-Audio
Sim-Render
Frequency Coupling
Equal
Multiple
Decoupled
Sim-Render
Sim-Audio
Render-Audio
Case Study: Madden Xbox vs Madden X360
Sim-Render
Sim-Audio
Render-Audio
Data Coupling
Tight
Loose
Sim-Render
Sim-Audio
None
Render-Audio
Case Study: Madden Xbox vs Madden X360
Sim
Render
Audio
Low-Level Concurrency
None
Instruction
Sim
Render
Audio
Functions
Case Study: Madden Xbox vs Madden X360
Sim
Render
Audio
High-Level Concurrency
Sequential
Interleaved
Parallel
Sim
Render
Audio
“!”
Conclusion
Conclusion
Hidden complexities
Make key architecture decisions early
Our decisions
Use multiple game loops
Decouple data
“???”
Questions?
Slides, speaker notes and references available
http://www.gdconf.com
“Where?”
References
References: Literature
Game Loops in Literature
“3D Game Engine Architecture, First Edition”, David Eberly
http://www.elsevier.com/wps/find/bookdescription.cws_home/704123/description#descripti
on
“Core Techniques and Algorithms in Game Programming”, Daniel Sanchez-Crespo
http://www.novarama.com/users/dani/docs/book/index-en.shtml
“Game Coding Complete, Second Edition”, Mike McShaffry
http://www.mcshaffry.com/GameCode/
Task Manager Papers
“Game Engineering for a Multiprocessor Architecture”, Abdennour El Rhalibi, Dave
England, and Steven Costa
http://www.gamesconference.org/digra2005/papers/a4458b374e5b16f16e725fdbeed4.doc
“Compositional Constraints Generation for Concurrent Real-Time Loops with
Interdependent Iterations”, I. Assayad and S. Yovine
http://www-verimag.imag.fr/~yovine/articles/i2cs05.pdf
“Hierarchical Finite State Machines with Multiple Concurrency Models”, Alain Girault,
Bilung Lee, and Edward A. Lee
http://ptolemy.eecs.berkeley.edu/papers/99/starcharts/starcharts.pdf
References: Video Game History
“Patent #2455992: Cathode Ray Tube Amusement Device”, Thomas Goldsmith Jr and Estle
Ray Mann
“PONG-Story”, David Winter
http://www.arcadedocs.com/vidmanuals/G/gunfight_2.pdf
“Atari Jaguar”, Wikipedia
http://www.bnl.gov/bnlweb/history/higinbotham.asp
“Gun Fight”, Midway Mfg. Co.
http://www.dcs.warwick.ac.uk/~edsac/
“The First Video Game”, Brookhaven National Laboratory
http://www.pong-story.com/intro.htm
“The Edsac Simulator”, Martin Campbell-Kelly
http://www.pong-story.com/2455992.pdf
http://en.wikipedia.org/wiki/Atari_Jaguar#Technical_specifications
“SEGA SATURN F.A.Q.”, John Hokanson Jr.
http://www.classicgaming.com/saturn/content/vault/faqs/saturn/system/saturnfaqb.txt
References: Concurrency
“The Free Lunch is Over: A Fundamental Turn Toward Concurrency in Software”, Herb Sutter
“Software and the Concurrency Revolution”, Herb Sutter and James Larus
http://www.gamasutra.com/features/20051117/gabb_01.shtml
“Compiler Technology for Scalable Architectures”, Alexandre E. Eichenberger, Kevin O’Brien, and Kathryn
O’Brien
http://research.microsoft.com/~larus/Talks/CRA%20Architecture%20Challenge.ppt
“Threading 3D Game Engine Basics”, Henry Gabb and Adam Lake
http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=226
“Software Challenges in Nanoscale Technologies”, James Larus
http://cache-www.intel.com/cd/00/00/20/40/204080_204080.pdf
“Trials and Tribulations of Debugging Concurrency”, Kang Su Gatlin
http://www.gamasutra.com/features/20050606/paquet_01.shtml
“Real-World Case Studies: Threading Games for High Performance on Intel Processors”, Sara Sarmiento
http://www.acmqueue.org/modules.php?name=Content&pa=showpage&pid=332&page=1
“Debugging Concurrency”, Philippe Paquet
http://www.gotw.ca/publications/concurrency-ddj.htm
http://domino.research.ibm.com/comm/research_projects.nsf/pages/cellcompiler.index.html
“The Next Mainstream Programming Language: A Game Developer’s Perspective”, Tim Sweeney
http://www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.ppt
References: Public Source Code
“Digger”, Windmill Software
“Duke Nukem 3D Source Code”, 3D Realms
http://www.idsoftware.com/business/techdownloads/
“Unreal Tournament v432 Source Code”, Epic Games
http://developer.popcap.com/
“Quake II Source Code”, Id Software
http://www.gamespot.com/pc/sim/freespace2/download.html?sid=2862847
“PopCap Engine”, PopCap Games
http://www.3drealms.com/downloads.html
“Freespace2 Source Code”, Volition Software
http://www.digger.org/download.html
http://www.fileshack.com/file.x?fid=308
“Wumpus 1”, Gregory Yob
http://www.atariarchives.org/morebasicgames/showpage.php?page=178