740 likes | 905 Views
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
E N D
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 • 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
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
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1947: Cathode Ray Tube Amusement Device • First patented video game • Game: Fire a missile at a target • Hardware loop run by master clock 1950 1940 1960 1970 1980 1990 2000
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1952: Noughts and Crosses • First software-based game • Game: Tic Tac Toe • Software loop 1950 1940 1960 1970 1980 1990 2000
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
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1975: Gun Fight • First CPU-based arcade video game • Game: Two players shoot at each other • Software Sim loop • Hardware Render and Audio loops 1950 1940 1960 1970 1980 1990 2000
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1978: Gypsy Juggler • First multi-CPU game • Game: Move a gypsy and juggle eggs • Concurrent Software Sim/Audio loops • Two symmetric S2650 CPUs 1950 1940 1960 1970 1980 1990 2000
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
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1988: Genesis • First multi-CPU console • 68000 CPU for Sim/Render • Z80 CPU for Audio 1950 1940 1960 1970 1980 1990 2000
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 1994: Saturn • First symmetric Multi-CPU console • Two Hitachi RISC processors • 6 processors for video/sound/control 1950 1940 1960 1970 1980 1990 2000
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
CRT Amusement Device Gun Fight Genesis MHz Barrier Noughts and Crosses Gypsy Juggler Saturn 2003: MHz Barrier 1950 1940 1960 1970 1980 1990 2000 CPU frequency 1995-2006 MHz Year
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
Scheduling Immediate Best-Fit Aligned Architecture: Time • Scheduling • Controls when a loop iteration starts
Time 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 • “As fast as it can” • Factors + Performance - Tolerance Determinism + Simplicity Immediate Exact Faster Slower Varied
Time Architecture: Example • Early adventure games Scheduling: Immediate while (1) { GetInput(); Sim(); Render(); }
Time Architecture: 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
Time Architecture: Time Scheduling • Description • “VSynced” • Factors - Performance - Tolerance Determinism + Simplicity Aligned Exact Faster Slower Varied
Time Step None Fixed-Value Real-Time Architecture: Time • Time Step • Controls interpretation of time • “Simulated” time
Time Architecture: Time Time Step • Description • Ignores time • Factors Performance Tolerance + Determinism + Simplicity None Exact Faster Slower Varied
Time Architecture: Time Time Step • Description • Time step always constant • Factors Performance - Tolerance + Determinism - Simplicity Fixed-Value Exact Faster Slower Varied
Time Architecture: Example • Deterministic real-time games Scheduling: Time Step: Best-Fit Fixed-Value const float TIMESTEP = 1 / 60.0f; while (1) { curTime = GetTime(); if ((curTime – lastTime) >= TIMESTEP) { Sim(TIMESTEP); lastTime = curTime; } }
Time Architecture: Time Time Step • Description • Sim Time == Real Time • Factors Performance + Tolerance - Determinism - Simplicity Real-Time Exact Faster Slower Varied
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
Frequency Equal Multiple Decoupled Architecture: Coupling • Frequency • How much a loop relies on another’s frequency
Architecture: Coupling Frequency • Description • 1:1 • “Lockstep” • Factors - Video modes + Memory Performance - Tolerance - Scalability + Simplicity Equal Time (Loop 1) Time (Loop 2)
Architecture: Coupling Frequency • Description • N:1 • Multiple of frequency • Sim @ 60 Hz • Render @ 30 Hz • Factors - Video modes + Memory Performance - Tolerance - Scalability + Simplicity Multiple Time (Loop 1) Time (Loop 2)
Architecture: Coupling Frequency • Description • N:M • Two independent rates • Factors + Video modes - Memory Performance + Tolerance + Scalability - Simplicity Decoupled Time (Loop 1) Time (Loop 2)
Architecture: Example Scheduling: Time Step: Frequency: Best-Fit Fixed-Value Decoupled Scheduling: Time Step: Frequency: Aligned Fixed-Value Decoupled • Decoupled Sim and Render loops while (1) { time = GetTime() if ((time – lastTime) >= SIM_STEP) { Sim(SIM_STEP); lastTime = time; } } while (1) { Render(RENDER_STEP); WaitForVSync(); }
Data Coupling Tight Loose None Architecture: Coupling • Data Coupling • The amount and method of sharing data
Architecture: Coupling Data Coupling • Description • Data structure reliance • Factors Video modes + Memory + Performance Tolerance - Scalability - Simplicity Tight
Architecture: Coupling Data Coupling • Description • Formalized data passing • Factors Video modes - Memory - Performance Tolerance + Scalability + Simplicity Loose
Architecture: Coupling Data Coupling • Description • Fully independent • No data passing • Factors Video modes + Memory + Performance Tolerance + Scalability + Simplicity None
Architecture: Game loop complexity Concurrency Coupling Time Complexity