Skip to content
LLuka Piplica
game-engineeringretro-computinggraphics-programmingcarmackdoomsystems-programming

Trick the Eye, Save the Clock: How DOOM Simulated 3D Space on 33 MHz

A low-level teardown of Binary Space Partitioning (BSP) trees, front-to-back occlusion, fixed-point math, and 1D column projection in John Carmack's 1993 rendering engine.

L

Luka Piplica

10 min read
First-person perspective of DOOM gameplay demonstrating real-time pseudo-3D column rendering and status bar HUD.

When id Software released DOOM in December 1993, the minimum system requirement listed an Intel 386 processor. In practice, the engine was built around a specific hardware envelope: an Intel 486SX running at 33 MHz without a hardware floating-point unit (FPU), driving a 320x200 8-bit framebuffer at 35 logic ticks per second. Without hardware 3D pipelines or GPU acceleration, geometry transformation, visibility queries, and pixel drawing were executed entirely as general-purpose 32-bit integer instructions.

To fit within this budget, John Carmack discarded the standard 3D pipeline. Restricting geometry to vertical walls and horizontal planes transformed 3D projection into a combination of 1D column scaling and precomputed 2D spatial lookups.

1. The 1993 Hardware Bottleneck

Conventional 3D graphics pipelines transform vertices using 4x4 matrices, depth-sort geometry, and interpolate perspective-correct textures across arbitrary polygons—operations that rely on fast floating-point multiply-accumulate instructions.

ConstraintTypical 1993 TargetImpact on Render Architecture
CPUIntel 386 DX or 486SX at 25–33 MHzFloating-point ops required software emulation interrupts
Target Budget35 logic ticks/second (half-refresh of 70 Hz CRT)~28.5 ms total per frame for rendering, AI, movement, and audio
Display ModeVGA Mode Y (unchained 320x200, 8-bit indexed)Provides 256 KB VRAM for hardware page flipping
Bus BandwidthISA-bus access speedsDirect VRAM writes were slow; overdraw degraded memory throughput

Instead of evaluating general matrix transformations at runtime, Carmack moved spatial calculations into a pre-compilation step, reducing runtime work to integer table fetches and array scans.

2. From Grids to Arbitrary Geometry

id’s previous title, Wolfenstein 3D (1992), simplified visibility by mapping all walls to a rigid 2D grid. The renderer cast a ray per screen column, stepping through cells until it hit a solid block, then drew a vertical texture slice scaled by distance.

DOOM retained column-based rendering but replaced the uniform grid with sectors: arbitrary polygonal regions bounded by 2D line segments called linedefs. Each sector defines its own floor height, ceiling height, flat textures, and light level. Sectors can be non-convex, disconnected, or adjacent to sectors with varying heights, enabling steps, ledges, and open pits.

This architecture is classified as 2.5D: floors and ceilings are strictly horizontal planes, walls are vertical extrusions, and two sectors cannot overlap at different heights within the same 2D horizontal coordinates.

Wolfenstein 3D grid raycasting versus DOOM angled non-grid level geometry comparison diagram Figure 1: Wolfenstein 3D mapped geometry to fixed grid cells. DOOM’s sector model supports arbitrary wall angles and variable sector heights at the cost of a complex visibility problem.

Without a uniform grid, simple raycasting became inefficient. Checking every linedef against 320 screen columns per frame exceeded the 33 MHz instruction budget, requiring visibility sorting to happen offline.

3. Binary Space Partitioning (BSP) Trees

DOOM resolves visibility sorting by constructing a Binary Space Partitioning (BSP) tree during map compilation using a tool called a node builder.

Pre-computation: Slicing the Map into Convex Subsectors

The node builder recursively splits the 2D map along partition lines, sorting wall segments into “front” and “back” child sets relative to each split. Slicing continues until the leaves form subsectors: strictly convex polygons containing wall segments that cannot occlude one another from any camera position inside that subsector.

Determining which side of a partition line a point sits on uses the implicit 2D line equation, implemented via a cross-product between the line vector and the camera offset:

cross=(x−x1)⋅dy−(y−y1)⋅dx\text{cross} = (x - x_1) \cdot dy - (y - y_1) \cdot dx

Each internal node stores its partition line and child bounding boxes, allowing the runtime engine to evaluate spatial position using integer cross-products.

DOOM BSP tree binary space partitioning 2D level map diagram id Software Figure 2: A 2D map partitioned into subsectors. The resulting binary tree allows front-to-back traversal relative to camera coordinates.

Runtime Traversal: Front-to-Back Order

During rendering, the engine walks the BSP tree starting at the root node. At each internal node, it evaluates the sign of cross to determine which child node contains the camera. The renderer recurses into the near child node first before checking the far child node.

Because partition lines divide space relative to the viewer, walking the tree near-child-first outputs subsectors in strict front-to-back order without sorting geometry at runtime.

4. Front-to-Back Occlusion & Overdraw Elimination

Front-to-back traversal allows the renderer to track screen coverage and clip occluded geometry before drawing pixels.

For each of the 320 screen columns, the engine maintains two boundary arrays: ceilingclip and floorclip (defined in r_segs.c). As wall segments draw, rasterization is restricted to the unwritten vertical span between ceilingclip[x] and floorclip[x]. Once a segment is rendered, the bounds shift inward. When ceilingclip[x] >= floorclip[x], column x is fully covered.

Horizontal clipping across column ranges is tracked using an array of solid intervals (solidsegs). Before drawing a wall segment or traversing deeper into a distant BSP branch, the engine checks its screen-space bounding box against solidsegs. If the range is covered, the subtree is skipped.

Screen column occlusion clipping visualization top bottom clip window rendering Figure 3: Per-column clip arrays narrow as wall segments draw front-to-back. When a column span closes, distant geometry behind it is skipped.

5. Affine Texture Mapping via Vertical Column Projection

Because walls are strictly vertical, perpendicular depth ZZ remains uniform across any single screen column. Perspective correction reduces from a per-pixel operation to a single division per column.

The vertical scale factor SS mapping world coordinates to screen space is:

S=dZS = \frac{d}{Z}

where dd is the projection plane distance and ZZ is the perpendicular distance along the column ray. Once SS is evaluated, texture mapping along the column reduces to linear interpolation:

DOOM vertical wall column texture mapping perspective projection diagram Figure 4: Vertical wall alignment means perspective scale S=d/ZS = d/Z is evaluated once per screen column. Column fills execute as an affine scaling loop.

Perspective division runs 320 times per frame instead of 64,000 times (320×200320 \times 200). Floor and ceiling surfaces (visplanes) use an equivalent horizontal approach, drawing spans across screen rows where depth ZZ remains constant.

6. Fixed-Point Arithmetic & Trigonometric Lookup Tables

Coordinates, scale factors, and ray directions use 16.16 fixed-point representations: 32-bit signed integers where the upper 16 bits store the integer component and the lower 16 bits store the fractional part, scaled by FRACUNIT (216=655362^{16} = 65536). Integer conversion uses bit shifts (X≪16X \ll 16).

valuereal≈rawint32216\text{value}_{\text{real}} \approx \frac{\text{raw}_{\text{int32}}}{2^{16}}

Addition and subtraction execute as standard integer operations. Multiplication uses the x86 imul instruction, producing a 64-bit result across EDX:EAX that is shifted right by 16 bits (shrd) inside FixedMul to prevent register overflow:


Angles use Binary Angle Measurement (BAM) stored as 32-bit unsigned integers (angle_t), where 360∘360^\circ maps to 2322^{32}. Trigonometric functions index precomputed lookup tables (finesine[] with 8192 entries) by shifting the BAM angle right by 19 bits (angle >> 19).

16.16 fixed point number format bit layout integer fraction diagram Figure 5: Bit layout of 16.16 fixed-point numbers. BAM angles map onto 32-bit integer values and index lookup tables via angle >> 19.

This code illustrates BSP tree traversal, side classification, and bounding box checks based on r_bsp.c:

r_bsp.c
1// Core BSP traversal logic based on r_bsp.c (id Software, 1993).
2
3#define NF_SUBSECTOR 0x8000 // Bit 15 marks a leaf (subsector) index
4
5void R_RenderBSPNode(int bspnum)
6{
7 node_t *bsp;
8 int side;
9
10 // Leaf reached: node index references a subsector.
11 if (bspnum & NF_SUBSECTOR)
12 {
13 if (bspnum == -1)
14 R_Subsector(0);
15 else
16 R_Subsector(bspnum & (~NF_SUBSECTOR));
17 return;
18 }
19
20 bsp = &nodes[bspnum];
21
22 // Determine camera side relative to partition line
23 side = R_PointOnSide(viewx, viewy, bsp);
24
25 // Process near subtree first for front-to-back draw order
26 R_RenderBSPNode(bsp->children[side]);
27
28 // Test far subtree bounding box against accumulated solid ranges
29 if (R_CheckBBox(bsp->bbox[side ^ 1]))
30 R_RenderBSPNode(bsp->children[side ^ 1]);
31}

7. System Architecture Impact

DOOM’s rendering pipeline demonstrates how algorithm constraints can adapt to CPU bottlenecks. By reducing 3D visibility to 2D spatial trees, mapping 2D textures via 1D vertical scaling, and using 16.16 fixed-point math, the engine delivered real-time pseudo-3D graphics on standard 386 and 486 hardware.

These optimizations laid the groundwork for future engine architectures. Spatial partitioning structures like BSP trees and PVS (Potentially Visible Sets) became core components of 1990s 3D engines, while early span clipping established concepts that evolved into modern hardware occlusion queries and early-z rejection in modern GPUs.


References & Further Reading


Technical Glossary

TermDefinition
BSP Tree (Binary Space Partitioning)A precomputed binary tree that recursively divides a 2D map along partition lines, enabling front-to-back traversal without runtime sorting.
SubsectorA strictly convex leaf region of the BSP tree, guaranteed to contain no internal wall segments that occlude each other.
OverdrawWriting to a screen pixel that is later overwritten by nearer geometry in the same frame.
Affine Texture MappingTexture mapping that performs perspective correction once per column or row rather than per pixel.
Fixed-Point Arithmetic (16.16)A 32-bit integer representation with 16 bits for the integer part and 16 bits for the fractional part, avoiding the need for an FPU.
FRACUNITThe scale constant (216=655362^{16} = 65536) used to convert between standard integers and DOOM’s 16.16 fixed-point representation.
BAM (Binary Angle Measurement)An angle representation using angle_t (32-bit unsigned integer) where 360∘360^\circ maps onto 2322^{32} wraparound, indexing LUTs via angle >> 19.
LUT (Lookup Table)A precomputed array of values (sine, cosine, tangent) used to replace runtime function calls with a table fetch.
Mode Y (Planar VGA)An unchained variant of VGA Mode 13h (320x200, 256 colors) exposing 256 KB VRAM for hardware triple-buffering page flipping and fast block writes.
2.5DA rendering model where geometry is extruded vertically from a 2D floor plan, disallowing true stacked 3D volumes.
Back to Blog
Share:

Follow along

Stay in the loop — new articles, thoughts, and updates.