Title: Sim, Render, Repeat
1Sim, Render, Repeat
- An Analysis of Game Loop Architectures
- Daniel Martin and Michael Balfour
March 23, 2006
2Who?
3Introduction 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 ?
4Introduction Motivation
- Hidden complexities
- Little coverage
- Highest-level game architecture
5Introduction Agenda
- Definitions
- Timeline
- Architecture decisions
- Case study
- Questions
- Slides will be made available on the GDC website
6What?
- Game loop architectures defined
7Definition Background
- Source Code
- Digger
- Duke3D
- Freespace2
- Harry Potter
- Madden
- Books
- 3D Game Engine Architecture, First Edition
- Core Techniques and Algorithms in Game
Programming - Game Coding Complete, Second Edition
- NASCAR
- Popcap Engine
- Quake2
- Unreal Tournament v432
- Wumpus
8Definition Pseudocode pattern
GameLoop() Startup() while (!done)
GetInput() Sim()
Render() Shutdown()
9Definition 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
10Definition Formal
- Video Game
- A collection of one or more game loops processing
inputs and outputs for entertainment purposes.
11Definition Formal
- Game Loop Architecture
- An architecture consisting of one or more game
loops and the infrastructure to manage their
execution and dependencies.
12When?
131947 Cathode Ray Tube Amusement Device
1950
1960
1940
1970
1980
1990
2000
- First patented video game
- Game Fire a missile at a target
- Hardware loop run by master clock
141952 Noughts and Crosses
1950
1960
1970
1980
1990
2000
1940
- First software-based game
- Game Tic Tac Toe
- Software loop
151940-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
161975 Gun Fight
1950
1960
1940
1970
1980
1990
2000
- First CPU-based arcade video game
- Game Two players shoot at each other
- Software Sim loop
- Hardware Render and Audio loops
171978 Gypsy Juggler
1950
1960
1940
1970
1980
1990
2000
- First multi-CPU game
- Game Move a gypsy and juggle eggs
- Concurrent Software Sim/Audio loops
- Two symmetric S2650 CPUs
181970-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
191988 Genesis
1950
1960
1940
1970
1980
2000
1990
- First multi-CPU console
- 68000 CPU for Sim/Render
- Z80 CPU for Audio
201994 Saturn
1950
1960
1940
1970
1980
1990
2000
- First symmetric Multi-CPU console
- Two Hitachi RISC processors
- 6 processors for video/sound/control
211980-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
222003 MHz Barrier
1950
1960
1940
1970
1980
2000
1990
CPU frequency 1995-2006
MHz
Year
232000-2006 Analysis
- Paradigm Shift
- The Free Lunch is Over
- Parallel by necessity
- Legacy code wont run magically faster
- More performance requires mastering concurrency
- Single CPU no longer the norm
24How?
- Game Loop Architecture Decisions
25Architecture Game loop complexity
Concurrency
Coupling
Time
Complexity
26Architecture Game loop complexity
Concurrency
Coupling
Time
Complexity
27Architecture 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
28Architecture Time
- Challenge
- How to maintain a loop frequency with variable
execution time - Factors
- Performance
- Tolerance
- Architecture Decisions
- Scheduling
- Time Step
29Architecture Time
- Scheduling
- Controls when a loop iteration starts
30Architecture 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
31Architecture Time
Scheduling
- Description
- As fast as it can
- Factors
- Performance
- - Tolerance
- Determinism
- Simplicity
Immediate
Exact
Faster
Slower
Varied
32Architecture Example
Scheduling Immediate
while (1) GetInput() Sim()
Render()
33Architecture Time
Scheduling
- Description
- Averaging
- Tries to
- Maintain frequency
- Start at exact times
- Catch up
- Factors
- Performance
- Tolerance
- Determinism
- - Simplicity
Best-Fit
Exact
Faster
Slower
Varied
34Architecture Time
Scheduling
- Description
- VSynced
- Factors
- - Performance
- - Tolerance
- Determinism
- Simplicity
Aligned
Exact
Faster
Slower
Varied
35Architecture Time
- Time Step
- Controls interpretation of time
- Simulated time
36Architecture Time
Time Step
- Description
- Ignores time
- Factors
- Performance
- Tolerance
- Determinism
- Simplicity
None
Exact
Faster
Slower
Varied
37Architecture Time
Time Step
- Description
- Time step always constant
- Factors
- Performance
- - Tolerance
- Determinism
- - Simplicity
Fixed-Value
Exact
Faster
Slower
Varied
38Architecture Example
Scheduling Time Step
Best-Fit Fixed-Value
const float TIMESTEP 1 / 60.0f while (1)
curTime GetTime() if ((curTime lastTime)
gt TIMESTEP)
Sim(TIMESTEP) lastTime curTime
- Deterministic real-time games
39Architecture Example
Scheduling Time Step
Immediate Fixed-Value
const float TIMESTEP 1 / 60.0f while (1)
Sim(TIMESTEP)
- Early PC games, assumed CPU bottleneck
40Architecture Time
Time Step
- Description
- Sim Time Real Time
- Factors
- Performance
- Tolerance
- - Determinism
- - Simplicity
Real-Time
Exact
Faster
Slower
Varied
41Architecture Example
Scheduling Time Step
Aligned Real-Time
while (1) WaitForVSync() curTime
GetTime() step curTime lastTime
Clamp(step, 0, MAX_STEP) Render(step)
lastTime curTime
- Interpolating Render loop, aligned to VSync
42Architecture Game Loop Complexity
Concurrency
Coupling
Time
Complexity
43Architecture Coupling
- Problem
- Supporting systems at different frequencies
- Solution
- Multiple game loops
- Consequence
- Loop Coupling
- Dependencies between every pair of loops
44Architecture 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
45Architecture Coupling
- Frequency
- How much a loop relies on anothers frequency
46Architecture Coupling
Frequency
- Description
- 11
- Lockstep
- Factors
- - Video modes
- Memory
- Performance
- - Tolerance
- - Scalability
- Simplicity
Equal
Time (Loop 1)
Time (Loop 2)
47Architecture Coupling
Frequency
- Description
- N1
- Multiple of frequency
- Sim _at_ 60 Hz
- Render _at_ 30 Hz
- Factors
- - Video modes
- Memory
- Performance
- - Tolerance
- - Scalability
- Simplicity
Multiple
Time (Loop 1)
Time (Loop 2)
48Architecture Coupling
Frequency
- Description
- NM
- Two independent rates
- Factors
- Video modes
- - Memory
- Performance
- Tolerance
- Scalability
- - Simplicity
Decoupled
Time (Loop 1)
Time (Loop 2)
49Architecture Example
Scheduling Time Step Frequency
Aligned Fixed-Value Decoupled
Best-Fit Fixed-Value Decoupled
Scheduling Time Step Frequency
while (1) time GetTime() if ((time
lastTime) gt SIM_STEP)
Sim(SIM_STEP) lastTime time
while (1) Render(RENDER_STEP)
WaitForVSync()
- Decoupled Sim and Render loops
50Architecture Coupling
- Data Coupling
- The amount and method of sharing data
51Architecture Coupling
Data Coupling
- Description
- Data structure reliance
- Factors
- Video modes
- Memory
- Performance
- Tolerance
- - Scalability
- - Simplicity
Tight
52Architecture Coupling
Data Coupling
- Description
- Formalized data passing
- Factors
- Video modes
- - Memory
- - Performance
- Tolerance
- Scalability
- Simplicity
Loose
53Architecture Example
Scheduling Time Step
Frequency Data Coupling
Aligned N/A
Decoupled Loose
Scheduling Time Step
Best-Fit N/A
- Sim at 60 Hz, Render at 50 Hz
54Architecture Coupling
Data Coupling
- Description
- Fully independent
- No data passing
- Factors
- Video modes
- Memory
- Performance
- Tolerance
- Scalability
- Simplicity
None
55Architecture Game loop complexity
Concurrency
Coupling
Time
Complexity
56Architecture Concurrency
- Problem
- Hardware makers need cutting edge performance
- Solution
- Hardware contains multiple CPUs
- Consequence
- Concurrency
- Mapping between game loops and CPUs
57Architecture Concurrency
- Challenge
- How to manage simultaneous execution
- Factors
- Performance
- Simplicity
- Scalability
- Architecture Decisions
- Low-Level Concurrency
- High-Level Concurrency
58Architecture Concurrency
- Low-Level
- Parallelism within a game loop
Low-Level
None
Instruction
Functions
59Architecture Concurrency
- Low-Level
- Covered extensively
- Easiest transition to NextGen
- Start small and grow
- OpenMP
- Bottom up approach
- Can be postponed
60Architecture Concurrency
- High-Level
- Parallelism across a pair of game loops
High-Level
Sequential
Interleaved
Parallel
61Architecture Concurrency
High-Level
- Description
- No overlap of loop execution
- Factors
- - Performance
- - Scalability
- Simplicity
Sequential
Exact
Varied
62Architecture Concurrency
High-Level
- Description
- Loops iterate in parallel
- Sequential at instruction level
- Concurrency on 1 CPU
- Factors
- - Performance
- Scalability
- - Simplicity
Interleaved
Exact
Varied
63Architecture Concurrency
High-Level
- Description
- Instructions execute at same time
- Factors
- Performance
- Scalability
- - Simplicity
Parallel
Exact
Varied
64Why?
65Case Study Madden Xbox vs Madden X360
VS
66Case Study Madden Xbox vs Madden X360
Sim Render Audio
Scheduling
Immediate
Best-Fit
Aligned
Sim
Render Audio
67Case Study Madden Xbox vs Madden X360
Audio
Sim Render
Sim
Render Audio
68Case Study Madden Xbox vs Madden X360
Sim-Audio Render-Audio
Sim-Render
Frequency Coupling
Equal
Multiple
Decoupled
Sim-Render Sim-Audio Render-Audio
69Case Study Madden Xbox vs Madden X360
Sim-Render
Render-Audio
Sim-Audio
Render-Audio
Sim-Render Sim-Audio
70Case Study Madden Xbox vs Madden X360
Sim Render Audio
Low-Level Concurrency
None
Instruction
Functions
Sim Render Audio
71Case Study Madden Xbox vs Madden X360
Sim Render
Audio
High-Level Concurrency
Sequential
Interleaved
Parallel
Sim Render Audio
72!
73Conclusion
- Hidden complexities
- Make key architecture decisions early
- Our decisions
- Use multiple game loops
- Decouple data
74???
Slides, speaker notes and references
available http//www.gdconf.com
75Where?
76References Literature
- Game Loops in Literature
- 3D Game Engine Architecture, First Edition,
David Eberly - http//www.elsevier.com/wps/find/bookdescription.c
ws_home/704123/descriptiondescription - 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/a4
458b374e5b16f16e725fdbeed4.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/starcha
rts/starcharts.pdf
77References Video Game History
- Patent 2455992 Cathode Ray Tube Amusement
Device, Thomas Goldsmith Jr and Estle Ray Mann - http//www.pong-story.com/2455992.pdf
- PONG-Story, David Winter
- http//www.pong-story.com/intro.htm
- The Edsac Simulator, Martin Campbell-Kelly
- http//www.dcs.warwick.ac.uk/edsac/
- The First Video Game, Brookhaven National
Laboratory - http//www.bnl.gov/bnlweb/history/higinbotham.asp
- Gun Fight, Midway Mfg. Co.
- http//www.arcadedocs.com/vidmanuals/G/gunfight_2.
pdf - Atari Jaguar, Wikipedia
- http//en.wikipedia.org/wiki/Atari_JaguarTechnica
l_specifications - SEGA SATURN F.A.Q., John Hokanson Jr.
- http//www.classicgaming.com/saturn/content/vault/
faqs/saturn/system/saturnfaqb.txt
78References Concurrency
- The Free Lunch is Over A Fundamental Turn
Toward Concurrency in Software, Herb Sutter - http//www.gotw.ca/publications/concurrency-ddj.ht
m - Software and the Concurrency Revolution, Herb
Sutter and James Larus - http//www.acmqueue.org/modules.php?nameContentp
ashowpagepid332page1 - Debugging Concurrency, Philippe Paquet
- http//www.gamasutra.com/features/20050606/paquet_
01.shtml - Real-World Case Studies Threading Games for
High Performance on Intel Processors, Sara
Sarmiento - http//cache-www.intel.com/cd/00/00/20/40/204080_2
04080.pdf - Trials and Tribulations of Debugging
Concurrency, Kang Su Gatlin - http//acmqueue.com/modules.php?nameContentpash
owpagepid226 - Software Challenges in Nanoscale Technologies,
James Larus - http//research.microsoft.com/larus/Talks/CRA20A
rchitecture20Challenge.ppt - Threading 3D Game Engine Basics, Henry Gabb and
Adam Lake - http//www.gamasutra.com/features/20051117/gabb_01
.shtml - Compiler Technology for Scalable Architectures,
Alexandre E. Eichenberger, Kevin OBrien, and
Kathryn OBrien - http//domino.research.ibm.com/comm/research_proje
cts.nsf/pages/cellcompiler.index.html - The Next Mainstream Programming Language A Game
Developers Perspective, Tim Sweeney - http//www.cs.princeton.edu/dpw/popl/06/Tim-POPL.
ppt
79References Public Source Code
- Digger, Windmill Software
- http//www.digger.org/download.html
- Duke Nukem 3D Source Code, 3D Realms
- http//www.3drealms.com/downloads.html
- Freespace2 Source Code, Volition Software
- http//www.gamespot.com/pc/sim/freespace2/download
.html?sid2862847 - PopCap Engine, PopCap Games
- http//developer.popcap.com/
- Quake II Source Code, Id Software
- http//www.idsoftware.com/business/techdownloads/
- Unreal Tournament v432 Source Code, Epic Games
- http//www.fileshack.com/file.x?fid308
- Wumpus 1, Gregory Yob
- http//www.atariarchives.org/morebasicgames/showpa
ge.php?page178