./jerome{m}jack in

~/Notes/BSP Occlusion and Span Buffers.md · note · budding · 4 min · updated

BSP Occlusion and Span Buffers

// status: budding

Modern engines throw millions of triangles at a hardware Z-buffer and let early-Z or Hi-Z sort out what stays hidden. In 1998–2000, fill-rate was scarce, transform-and-lighting ran on a single Pentium II core, and Deus Ex needed to cull whole rooms, hallways, mirrors, and sleeping AI pawns before a single triangle ever reached Direct3D.

Unreal Engine 1's answer — URender::OccludeBsp inside Render.dll (0x10b17ae0) — is one of the cleanest visibility algorithms ever shipped: a front-to-back BSP walk paired with a per-zone 1D horizontal span buffer, portal merging, recursive child frames for mirrors and skyboxes, and a visibility stamp that literally tells gameplay scripts which actors exist.

bsp · liveDrag the camera or rotate its heading, step through OccludeBsp front-to-back, toggle portals and mirrors, and watch opaque walls carve intervals out of the 1D span buffer while zone-stamped guards wake up.open ↗

Front-to-back guarantees first hit wins

Every level in Unreal Engine 1 is compiled into a Binary Space Partitioning (BSP) tree of splitting planes. Given a camera position C, every internal node splits the world into a front half-space (where C \cdot N - W \ge 0) and a back half-space.

Walk the near child first, then process the polygons lying on the node's own plane, then walk the far child. Because the tree is strictly ordered by plane splits, any opaque wall you visit earlier is guaranteed to lie in front of every opaque wall you visit later. You never need a Z-buffer during visibility determination — every screen interval covered by a nearer wall is permanently occluded.

text
OccludeBsp(node):
  1. Test node bounding box against the active zones' span buffers.
     If no free pixels remain inside the box -> prune the whole subtree!
  2. Classify camera against node plane -> pick (near_child, far_child).
  3. Recurse into near_child.
  4. Clip & rasterize coplanar polygons on this node:
     - Opaque wall:   subtract its spans from the zone's free span list.
     - Portal:        copy surviving spans into the far zone's active span list.
     - Mirror / Sky:  spawn a reflected or skybox child FSceneNode frame.
  5. If all active zones have empty span lists -> stop the walk early!
  6. Recurse into far_child.

The 1D span buffer

Instead of storing a full-resolution 2D coverage bitmap, Render.dll tracks visibility per scanline as a sorted linked list of half-open free intervals [x_1, x_2) — the pixels on that row that have not yet been blocked by an opaque wall.

At the start of a frame, only the camera's starting zone is active, and its span buffer starts completely open: [0, W) on every scanline. When an opaque polygon projects to [a, b) on a row, subtracting [a, b) from the free list splits, trims, or deletes any overlapping free intervals. Three integer operations cover everything:

  • BoxIsVisible: projects a subtree's bounding box to screen [X1..X2, Y1..Y2] and checks whether any free span intersects it. If not, the entire branch — rooms, lights, actors, everything behind that wall — is skipped in a few dozen cycles.
  • CopyFromRaster(Update): clips a polygon's raster spans against the zone's free list, returning only the visible fragments and (for opaque surfaces) subtracting them from the free list at the same time.
  • MergeWith: merges two sorted span lists, coalescing adjacent intervals [a, b) and [b, c) into [a, c).

In our Deus Ex decompilation, the integer span-buffer routines (0x10b21730..0x10b22459) matched the original DLL's 8 MB heap byte-for-byte across 18,000 randomized calls.

Portals, zones, and mirrors

Doorways, windows, and hallways between rooms are tagged as zone portals (PF_Portal). A portal polygon does not subtract from the current zone's opaque coverage; instead, OccludeBsp clips the portal against the current zone's free spans and merges whatever pokes through into the far zone's span buffer.

If a closed pillar or corridor blocks the doorway, zero spans survive the clip, so the far zone never even becomes active — every room behind it stays completely dark to the renderer. If a sliver of the doorway is visible, only that narrow slice of screen columns becomes free in the far zone, automatically clipping everything seen through the door without any extra frustum math.

Mirrors (PF_Mirrored) and skyboxes (PF_FakeBackdrop) push the exact same idea one level deeper: whenever a mirror polygon survives span clipping, CreateChildFrame reflects the camera's FCoords across the mirror plane, sets the mirror plane as a custom NearClip plane, hands the child frame only the mirror's surviving spans as its starting free buffer, and runs OccludeBsp recursively (up to depth 3).

Visibility drives gameplay (LastRenderTime)

Here is the twist that caught us during decompilation: in Unreal Engine 1, OccludeBsp is not just a graphics optimization — gameplay depends on it.

Every dynamic actor (NPCs, cameras, security bots, pickups) gets a screen-space bounding proxy (FDynamicSprite) that filters down the BSP tree alongside the walls. Whenever any sliver of an actor's proxy survives against its zone's free spans, the renderer prepends it to Frame->Sprite and stamps Actor.LastRenderTime.

UnrealScript AI state machines in Deus Ex read LastRenderTime and PlayerCanSeeMe() to decide whether a guard has been spotted, whether an ambient animation should tick, or whether an object is safe to cull. Get the span buffer wrong by a single column across a crack in a doorway, and a guard in the next room wakes up on a different tick.


Part of the Deus Ex in the Browser notes · see Analytic Volumetric Fog · 80-bit Ghosts · Spatial acceleration structures · back to Notes.