Hot Topics
Gaming

Doom in an SQL database: How SQLDoom achieves 35 FPS

Lukas Vogel has achieved a technical feat with the SQLDoom project, running Doom entirely within an SQL database. By utilizing approximately 1,300 lines of SQL queries and 89 common table expressions, the system generates full-color 640x480 bitmap frames at rates between 35 and 60 FPS. The project leverages CedarDB to manage game geometry and state, representing a massive leap from previous ASCII-based database experiments. This article explores the technical architecture, the transition from WAD files to relational tables, and the inherent advantages of using a database for multiplayer synchronization.

Doom in an SQL database: How SQLDoom achieves 35 FPS

How does the SQLDoom architecture function?

The SQLDoom project operates by offloading the heavy lifting of game logic and frame generation to a relational database engine. While it may seem counterintuitive to use a database for real-time rendering, Vogel utilizes a small Python client to manage essential external tasks such as user input, output, and game timing. The core of the engine resides within CedarDB tables, which track every aspect of the game's geometry and current state.

The rendering process is driven by a complex web of SQL queries. Specifically, about 1,300 lines of code, organized into 89 common table expressions (CTEs), work in tandem to process the data. These queries transform raw table entries into 35 bitmap frames per second. Unlike traditional game engines that rely on highly optimized C++ loops, SQLDoom relies on the relational engine's ability to process sets of data to determine what should appear on the screen.

The role of the Python client

It is a common misconception that the database handles every single aspect of the experience. The Python client acts as the vital bridge between the user and the data. It captures keyboard and mouse inputs, sends them to the database to update the game state, and then reads the resulting bitmap data to display the frame on the monitor. Without this lightweight layer, the database would have no way to interact with the hardware or the human player.

How were Doom's WAD files converted to SQL?

Converting the classic Doom WAD files into a relational format proved to be a relatively straightforward task for Vogel due to the inherent structure of the original game data. The Doom engine originally organized levels into specific components such as vertices, lines, and sectors. Because these components are already modular and hierarchical, they map naturally to the rows and columns of a relational database.

A significant technical challenge in early 3D games was the Binary Space Partitioning (BSP) tree, which Doom used to manage visibility. Vogel successfully translated this concept into SQL by implementing a pre-computed 'sort_key' for objects. This key is calculated for each position during the initial load time. Once this key is stored in the tables, the engine can use a standard 'ORDER BY' statement to determine which walls are visible to the player and which should be culled. This method allows the database to ignore irrelevant geometry, which is essential for maintaining playable frame rates.

What are the rendering limitations and workarounds?

Despite the success of the project, the transition from SQL tables to first-person frames presented specific algorithmic hurdles, particularly regarding floors and ceilings. In the original Doom engine, floors and ceilings are rendered using a column-by-column approach involving 'visplanes' and specific state mutations. This method is highly efficient for hardware but does not translate easily to the set-based logic of an SQL database.

To solve this, Vogel implemented what he describes as a "pretty hacky" replacement. Instead of the original engine's elegant method, SQLDoom iterates over an ordered list of panels to simulate the rendering of these surfaces. While this is less efficient than the original code, it allows the project to achieve full-color 640x480 resolution, a significant upgrade over Vogel's previous attempt, 'DoomQL,' which was limited to grayscale ASCII graphics and 90-degree-angled maps similar to Wolfenstein 3D.

Performance benchmarks on modern hardware

The performance of SQLDoom is highly dependent on the underlying hardware's ability to process complex queries rapidly. According to the project documentation, running the engine on a Ryzen 7-powered laptop allows for a smooth 60 FPS in many scenarios. However, during complex scenes with high object density or intricate geometry, the frame rate can dip to approximately 35 FPS. This demonstrates that while the database approach is viable, it remains much more computationally expensive than a native engine.

Why is a database beneficial for multiplayer gaming?

Beyond the technical novelty, using a database for a game engine offers legitimate advantages for multiplayer environments. Vogel notes that the database provides a steady "reference snapshot" of the entire game state. Because relational databases are designed to handle concurrency and data integrity, they can solve several common issues found in networked gaming.

In a traditional multiplayer setup, desynchronization is a frequent problem. However, with SQLDoom, the database ensures that there are no partially applied updates or physics bugs. If a player fires a rocket, the database handles the transaction such that there is no disagreement between clients over whether the projectile actually hit a target. The inherent ACID (Atomicity, Consistency, Isolation, Durability) properties of a database provide a single source of truth that is much harder to achieve with standard packet-based state synchronization.

How can users experience SQLDoom?

Accessing the project is relatively simple for those with the technical inclination. Users can run the engine locally by downloading the code from GitHub, obtaining a copy of CedarDB, and providing a standard Doom WAD file. For those who prefer not to undergo a manual setup, a free online hosted demo match is available for testing. However, users should be aware that the hosted version may suffer from lower performance compared to a local installation due to network latency and server constraints.

Key takeaways

  • SQLDoom uses 1,300 lines of SQL and 89 common table expressions to render gameplay.
  • The engine achieves 640x480 color resolution, a major improvement over previous ASCII versions.
  • Performance on a Ryzen 7 laptop typically ranges between 35 and 60 FPS.
  • The database structure provides a reliable "reference snapshot" for multiplayer synchronization.
  • WAD files are converted to SQL by mapping vertices and sectors to relational tables.

FAQ: SQLDoom and Database Gaming

Can I play Doom in a database on any computer?

You can play it on most modern computers by using the GitHub source code, a CedarDB instance, and a Doom WAD file. However, performance will vary based on your CPU's ability to process the complex SQL queries required for real-time rendering.

How is this different from the previous DoomQL project?

DoomQL was an earlier attempt by Lukas Vogel that resulted in grayscale ASCII graphics and simpler, 90-degree-angled maps. SQLDoom is a much more advanced iteration that supports full-color 640x480 bitmap frames and more accurate geometry rendering.

Is the database actually better for multiplayer?

Yes, in certain ways. The database acts as a single source of truth, preventing issues like partially applied updates or physics disagreements. This ensures that all players see the same game state, which is a core strength of relational database concurrency.

Does the engine use the original Doom rendering logic?

Not entirely. While it uses the original data structure, some parts like floor and ceiling rendering have been replaced with "hacky" panel-based iterations because the original "visplanes" method does not translate well to SQL logic.

What hardware is needed for a good experience?

A modern processor, such as a Ryzen 7, is recommended. While the engine can run on various setups, higher-end hardware helps maintain a stable frame rate, especially during complex scenes where the FPS might drop to 35.

Conclusion

The SQLDoom project serves as a fascinating intersection of database management and game engine architecture. By successfully translating the modular structure of Doom's WAD files into relational tables, Lukas Vogel has proven that even the most rigid data structures can be repurposed for real-time interactive media. While the method is computationally intensive and requires creative workarounds for certain rendering tasks, the benefits for multiplayer state consistency are significant. Ultimately, SQLDoom is more than a technical curiosity; it is a demonstration of the versatile power of SQL querying.