ovi labs

Kelyje II · faster · week of

Merging triangle fans cut a quarter of the draws

A batcher only helps the geometry it can see. Letting triangle fans join the existing strip batch and bringing three more draw sites into scope cut 25.1% of the draws per frame and 12.8% of the frame time, with zero differing pixels.

Kelyje II: the Lithuanian release of Rig'n'Roll, a truck game from 2000. The browser build is its x86 code translated to WebAssembly by machine · kelyje2.lt 

After moving the hottest scene functions to the C++ rewrite, the browser build still paid for hundreds of small Direct3D 7 draw calls on every frame. Counting what the batcher rejected showed most of the leftover calls were not hard cases at all.

Counting what fell through

The native batcher in the browser build merges consecutive triangle strips that share the same render state, joining them with zero-area (degenerate) triangles so the GPU draws them in one call. Anything the batcher does not recognise falls through to JavaScript as its own draw call.

I added throwaway counters to the batcher over frames 100 to 1,000 of the 1,000-frame scripted drive. Out of 560.3 draw submissions per frame, 170.1 fell through to JavaScript. Nearly all of those came from two places: triangle fans already in scope that were rejected solely for being fans, and draws from three functions (0x005b4770, 0x005c89a0 and 0x005cfd30) that sat outside the batcher's scope (plus one legacy fan format at 0x005b5773).

Turning a fan into a strip

A triangle fan shares its first vertex across every triangle: (v0, v1, v2), then (v0, v2, v3), and so on. Expanding each triangle into an even-parity strip segment and joining it with the same degenerate stitch lets a fan ride inside a strip batch. That matches the expansion the renderer was already doing on its own.

before (separate calls)
  strip A  -> draw 1
  fan   B  -> draw 2 (fell through to JS)
  strip C  -> draw 3

after (one stitched strip)
  [strip A | deg | fan B | deg | strip C]
           -> draw 1

Two guards keep the shortcut honest. Fan merging is only allowed under Gouraud shading (any other shade mode turns it off for the rest of the run), and a fan with a non-finite vertex position stays on the JavaScript path, matching the renderer's own finite check. A single legacy fan format (0x1e2, 11.0 calls per frame) also stays on the JavaScript path.

What remained, and what it saved

With fans admitted and the three extra functions added to the submission scopes, every in-scope draw merges until the game actually changes render state: 118 texture switches (SetTexture) and 21.7 transform changes (SetTransform) per frame.

On the 1,000-frame scripted drive, total draws fell from 258,215 to 193,366 (258.2 to 193.4 per frame, 25.1% fewer). Every one of the 1,000 frames kept the same state hash and the same pixel hash as the canonical run, with zero differing pixels, alongside 1,054 submission fixture checks and 415 regression tests.

In a quiet alternating (old, new, new, old) run over frames 100 to 1,000, frame time dropped from 13.998 to 12.207 ms per frame, 1.79 ms (12.8%) faster. That is the scripted drive on one desktop browser; other scenes and browsers will vary with how often they switch textures.

I'm Ovi. I recompile old Windows games to run in a browser, build a virtual GPU for Windows XP, and write tools for my own machines, with AI since 2020. Projects and the latest log