6. History of DOOM
Series of FPS games developed by id
Software
— Started in 1992 by John Carmack
developing the Doom Engine
— Rise of the term "Game Engine"
— Unparalleled 3D graphics features at
the time
— Stereo sound
DOOM Technical Review 6
61. Code review
DOOM 3 (2004) source code
— Released in 2011
— Contanins idTech 4
— Does not contain the game data
— C with Classes
— No Exceptions
— No Refrences, uses pointers
— Minimal usage of templates
DOOM Technical Review 61
62. Code review
I spent a bit of time going through the Doom3
source code. It’s probably the cleanest and
nicest looking code I’ve ever seen.
It’s magic. When we explore the Doom3 source
code, we can’t say: WOW it’s beautiful!
DOOM Technical Review 62
64. Code review
— Doom3.exe startup
— Loads the game DLL in its process
memory space via LoadLibrary
— Get the address of GetGameAPI in the
game dll using win32's GetProcAddress
— Call GetGameAPI
gameExport_t * GetGameAPI_t( gameImport_t *import );
DOOM Technical Review 64
65. Code review
— Doom3.exe view on game DLL
typedef struct {
int version; // API version
idGame * game; // interface to run the game
idGameEdit * gameEdit; // interface for in-game editing
} gameExport_t;
DOOM Technical Review 65
66. Code review
— Game DLL view on Doom3.exe
typedef struct {
int version; // API version
idSys * sys; // non-portable system services
idCommon * common; // common
idCmdSystem * cmdSystem // console command system
idCVarSystem * cvarSystem; // console variable system
idFileSystem * fileSystem; // file system
idNetworkSystem * networkSystem; // network system
idRenderSystem * renderSystem; // render system
idSoundSystem * soundSystem; // sound system
idRenderModelManager * renderModelManager; // render model manager
idUserInterfaceManager * uiManager; // user interface manager
idDeclManager * declManager; // declaration manager
idAASFileManager * AASFileManager; // AAS file manager
idCollisionModelManager * collisionModelManager; // collision model manager
} gameImport_t;
DOOM Technical Review 66
67. Code review
— First usage of C++ in idTech
— OO and v-funcs are used, but optimised
using tricks
idCommonLocal commonLocal; // Implementation
idCommon * common = &commonLocal; // Interface
— Templates are only used in low level
utility classes (mainly idLib)
DOOM Technical Review 67
68. Code review
— Main loop (1/2)
idCommonLocal commonLocal; // OS Specialized object
idCommon * common = &commonLocal; // Interface pointer (since Init is OS dependent it is an abstract method
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
Sys_SetPhysicalWorkMemory( 192 << 20, 1024 << 20 ); //Min = 201,326,592 Max = 1,073,741,824
Sys_CreateConsole();
// Since the engine is multi-threaded mutexes are initialized here: One mutex per "critical" (concurrent execution) section of code.
for (int i = 0; i < MAX_CRITICAL_SECTIONS; i++ ) {
InitializeCriticalSection( &win32.criticalSections[i] );
}
common->Init( 0, NULL, lpCmdLine ); // Assess how much VRAM is available (not done via OpenGL but OS call)
Sys_StartAsyncThread(){ // The next look runs is a separate thread.
while ( 1 ){
usleep( 16666 ); // Run at 60Hz
common->Async(); // Do the job
Sys_TriggerEvent( TRIGGER_EVENT_ONE ); // Unlock other thread waiting for inputs
pthread_testcancel(); // Check if we have been cancelled by the main thread (on shutdown).
}
}
Sys_ShowConsole
...
DOOM Technical Review 68
69. Code review
— Main loop (2/2)
...
while( 1 ){
Win_Frame(); // Show or hide the console
common->Frame(){
session->Frame() // Game logic
{
for (int i = 0 ; i < gameTicsToRun ; i++ )
RunGameTic(){
game->RunFrame( &cmd ); // From this point execution jumps in the GameX86.dll address space.
for( ent = activeEntities.Next(); ent != NULL; ent = ent->activeNode.Next() )
ent->GetPhysics()->UpdateTime( time ); // let entities think
}
}
session->UpdateScreen( false ); // normal, in-sequence screen update
{
renderSystem->BeginFrame
idGame::Draw // Renderer front-end. Doesn't actually communicate with the GPU !!
renderSystem->EndFrame
R_IssueRenderCommands // Renderer back-end. Issue GPU optimized commands to the GPU.
}
}
}
}
DOOM Technical Review 69
71. Code review
— Renderer
1. Frontend generates intermediate
world representation and light-entity
interaction table
2. Backend issues GL draw calls
3. GPU executes the commands recieved
from the driver
DOOM Technical Review 71
72. Code review
Some interesting choices (1 / 5)
— idClass as a common class
— Provides :
— Instance creation
— Type info management
— event management
DOOM Technical Review 72
74. Code review
Some interesting choices (3 / 5)
— Main code is highly decoupled from the
GUI code
— Again not-so-common practice
— Means that only GUI programmers
have to care about MFC
DOOM Technical Review 74
75. Code review
Some interesting choices (4 / 5)
— Very good utility library idlib
— Provides utilities to treat strings,
containers, and memory
— Crucial to develop high quality C++
code efficiently
DOOM Technical Review 75
76. Code review
Some interesting choices (5 / 5)
— KISS philosophy
— A good example is id's compiler
implementation
— Just works
DOOM Technical Review 76
78. Parallelism
— idTech 4
— Decoupled renderer (Frontend and
Backend)
— Async handling of frame-rate
independent subsystems (e.g., input
and Sound)
DOOM Technical Review 78
79. Parallelism
— idTech 5: Job system
— Simplicity key to scalability
— Job has well defined input and output
— Independent stateless, no stalls,
always completes
— Jobs added to job lists, multiple fully
independent job lists
— Not GP-GPU ready at the time
DOOM Technical Review 79
85. Megatextures
— Key to achieve their unique art style in
vast environments
DOOM Technical Review 85
86. Megatextures
— Key to achieve their unique art style in
vast environments
DOOM Technical Review 86
87. Megatextures
— Purpose : Use textures much bigger than
V-RAM
— Key idea : use indirection in texture
access
— Similar to virtual memory addressing
— Solution : Dynamically load and keep
parts of the texture that are currently in-
use
DOOM Technical Review 87
96. Megatextures
— Implementation
— Uses a custom texture compressor
— Runtime transcoder generates blocks
of 40kB DXT texture data from
typically 2-6kB compressed data
— Runs in parallel to texture updates on
platforms that can directly write
texture memory
DOOM Technical Review 96
98. id So!ware programming principles
Warnings!
1. These guidelines are written by John
Romero...
Romero wants an empire, I just want to
create good programs.
— John Carmack
2. They worked in the 90s, some will sound
insane in today's standards
DOOM Technical Review 98
99. id So!ware programming principles
1 / 9
— No prototypes – always maintain
constantly shippable code.
— Polish as you go.
DOOM Technical Review 99
100. id So!ware programming principles
2 / 9
— It’s incredibly important that your game
can always be run by your team.
— Bulletproof your engine by providing
defaults upon load failure.
DOOM Technical Review 100
101. id So!ware programming principles
3 / 9
— Keep your code absolutely simple.
— Keep looking at your functions and figure
out how you can simplify further.
DOOM Technical Review 101
102. id So!ware programming principles
4 / 9
— Great tools help make great games.
— Spend as much time on tools as
possible.
DOOM Technical Review 102
103. id So!ware programming principles
5 / 9
— Use a superior development system than
your target to develop your game.
DOOM Technical Review 103
104. id So!ware programming principles
6 / 9
— Write your code for this game only, not
for a future game.
— You’re going to be writing new code later
because you’ll be smarter.
DOOM Technical Review 104
105. id So!ware programming principles
7 / 9
— Encapsulate functionality to ensure
design consistency.
— This minimizes mistakes and saves
design time.
DOOM Technical Review 105
106. id So!ware programming principles
8 / 9
— Try to code transparently.
— Tell your lead and peers exactly how you
are going to solve your current task.
DOOM Technical Review 106
107. id So!ware programming principles
9 / 9
— Programming is a creative art form based
in logic.
— Every programmer is different.
DOOM Technical Review 107
108. Further readings
1. DOOM source code releases
2. Masters of DOOM
3. Game Engine Architecture
DOOM Technical Review 108