Rendering a Streamed Pixel World in WebGPU

How MageQuest draws a continuously streamed tile world with PixiJS and WebGPU, using per-area GPU state to keep every area's transform correct.

Rendering a Streamed Pixel World in WebGPU

Table of Contents

A World Made of Windows

MageQuest’s world is larger than any browser should try to draw at once. The client keeps a 3x3 window of map areas around the player. Each area is 64x64 tiles, each tile is 32 pixels, so a single area is 2048x2048 world pixels. A 3x3 window is nine areas, or 36864 tiles before decorations.

A five by five grid with the central three by three area window highlighted and the player in the center

Blue cells are the resident 3x3 window. The gold cell is the area containing the player.

The player can cross an area boundary without a loading screen. When they do, the resident window shifts: new areas stream in on one side, old areas are released on the other. The camera and the already-loaded areas do not jump. That constraint matters more than it sounds, because it means the renderer cannot rebuild the entire world every time the player walks a few pixels.

Two three by three area windows with an outgoing red column and an incoming green column

When the player moves west, the red column leaves the window and the green column streams in. Everything else stays put.

The client is built on PixiJS v8. On a capable browser it uses the WebGPU renderer; otherwise it falls back to WebGL. The map itself is drawn with @pixi/tilemap’s CompositeTilemap, one tilemap per area and layer. A layer can be base, bg, main, fg, top, and several decoration passes. Each one is positioned independently and keyed by ${area.id}:${layerName}.

A stack of colored map layers with a player figure at the top

Each area is a stack of layer tilemaps. Terrain sits at the bottom; foreground and the player sit above it.

Why CompositeTilemap

A naive tile renderer would create one sprite per tile. Nine areas of 64x64 tiles would mean tens of thousands of sprites, each with its own transform and draw call. CompositeTilemap batches those tiles into a single geometry buffer and draws them with one shader.

The map layers are built from integer tile ids, with flip and rotation bits packed into the high bits of the Tiled id. For each visible tile, the client computes the atlas coordinates and calls:

tilemap.tile(texture, x * tileWidth, y * tileHeight, {
  u: localId % columns * tileWidth,
  v: Math.floor(localId / columns) * tileHeight,
  tileWidth,
  tileHeight,
  rotate,
})

The tilemap owns the vertex buffer. A layer is cheap to move because its tiles are expressed relative to the tilemap container, and the container’s position is just another transform. The camera shifts the containers, not the individual tiles.

The next problem is that the tilemap shader still needs to know which transform to use for each container.

The Shared Transform Problem

The upstream WebGPU tilemap adaptor keeps a single pipe_uniforms group and a single bind group for every draw. The pipe writes the current area’s projection matrix into that buffer, queues a draw, then writes the next area’s matrix into the same buffer.

On WebGL that works because uploads happen per draw. WebGPU records commands and submits them later. By the time the GPU executes the queued draws, the uniform buffer contains the last matrix written. Every area reads the same transform, so they all collapse onto the final area’s position.

The matrix calculation is fine. The container coordinates are fine. The problem is that one mutable GPU resource is shared across commands that are executed later. With nine areas on screen, each one needs its own transform at draw time.

One Uniform Buffer per Area

The solution is to give every tilemap its own uniform group and bind group, and swap them around the base pipe’s execute call.

WorldTilemapPipe extends the upstream TilemapPipe and stores records by tilemap:

execute(instruction) {
  const { tilemap } = instruction
  let record = this.areaUniforms.get(tilemap)

  if (!record) {
    const uniforms = new UniformGroup({
      u_proj_trans: { value: new Matrix(), type: 'mat3x3<f32>' },
      u_anim_frame: { value: new Float32Array(2), type: 'vec2<f32>' },
    })

    record = { uniforms, bindGroup: new BindGroup({ loc: uniforms }) }
    record.onDestroy = () => this.releaseAreaUniforms(tilemap)
    tilemap.once('destroyed', record.onDestroy)
    this.areaUniforms.set(tilemap, record)
  }

  const uniforms = this.adaptor.pipe_uniforms
  const bindGroup = this.adaptor.bind_group

  this.adaptor.pipe_uniforms = record.uniforms
  this.adaptor.bind_group = record.bindGroup

  try {
    super.execute(instruction)
  } finally {
    this.adaptor.pipe_uniforms = uniforms
    this.adaptor.bind_group = bindGroup
  }
}

The base execute still does the matrix math: it composes the global projection matrix, the world transform, and the tilemap’s own transform into u_proj_trans. The difference is that it now writes into a resource owned by this tilemap.

The teardown matters as much as the setup. When an area unloads, the tilemap emits destroyed; the pipe removes its listener, destroys the bind group, clears the uniform buffer, and drops the record. Without that, streaming a long session would leak one buffer per area per layer.

Why Draw Bundles Had to Go

Pixi can record repeated draw calls into render bundles. A bundle captures the commands and resources used at record time, which is normally a large win for a tilemap that redraws every frame.

For a streamed world it is the wrong tool. A tilemap’s world transform changes as the camera moves, and its uniforms change whenever the area’s position changes. A cached bundle would replay the old transform. The custom pipe disables bundling per tilemap:

addRenderable(tilemap, instructionSet) {
  tilemap.canBundle = false
  super.addRenderable(tilemap, instructionSet)
}

This is a deliberate trade: a little more CPU work per frame in exchange for correct per-area transforms. For nine visible areas the cost is acceptable. The alternative is a cache invalidation problem that is much harder to reason about than a direct draw.

What This Buys Us

The result is that every visible area can move independently while sharing one renderer and one shader.

  • Correct area transforms. Nine areas can be queued in one frame and each draw reads the matrix for its own tilemap. Streaming a new area does not change the transform of an area that was already on screen.
  • Stable camera framing. The camera can move, an area can shift, and only that area’s uniform buffer changes. Loaded neighbors keep their own state instead of inheriting the last area’s position.
  • One ownership model. Each area’s tilemaps own their uniforms and bind groups. When the area unloads, those resources are released with it.
  • A WebGL fallback that still works. The custom pipe is installed only for the WebGPU renderer. WebGL keeps the stock pipe because its per-draw uploads already handle the transform correctly.
  • A predictable cost. Nine areas mean at most nine (times layers) small uniform buffers, not a global pool or a cache invalidation scheme. The CPU records a direct draw for each tilemap instead of replaying a stale bundle.

The renderer is deliberately not trying to be clever. It does not batch areas into one transform or cache a queued frame. It gives each area the state it needs and lets the GPU execute the queue correctly.

Implementation Notes

A few details make the design work in practice.

GPU commands are deferred. A resource that looks correct when you write it may be different when the GPU reads it. If two queued draws share a mutable buffer, assume the last write wins.

Disable caching when correctness depends on mutation. Render bundles are excellent for static content. They are dangerous for content whose transform changes every frame, so the pipe marks each streamed tilemap as non-bundleable.

Give streamed resources an owner and a lifetime. Each area’s tilemaps, uniforms, bind groups, and textures are released together. The pipe hooks the tilemap’s destroyed event so an area unload cannot leave a GPU buffer behind.

Replace extensions explicitly. Pixi keeps the first extension registered under a name. The WebGPU loader removes the stock tilemap pipe and installs the custom one before the renderer is constructed. Tests verify that the final renderer configuration contains exactly one tilemap pipe, and that it is the custom implementation.

Test the queue, not just one draw. The test suite reproduces the shared-buffer problem with the stock pipe, then queues nine area draws through the custom pipe and checks every retained matrix independently.

What Comes Next

The renderer works under both backends, but there is still room to improve.

The biggest remaining win is a pooled uniform-buffer ring. Every tilemap needs its own transform, but the buffers could be recycled between submissions instead of allocating one per tilemap. The current design is deliberately simple: one buffer per tilemap, no cache invalidation.

The second is culling. The client already keeps only a 3x3 window, but within a window it still uploads whole areas. A tighter visible-rectangle pass could reduce vertex work further without changing the streaming model.

The third is upstreaming the approach. The shared uniform buffer is a real limitation in @pixi/tilemap’s WebGPU adaptor. A per-draw uniform offset, or a small pool of buffers recycled per queue slot, would solve it for every project using the library.

For now, the architecture is doing what it needs to do: stream a large pixel world in small windows, keep the camera stable, and let each area own the GPU state it needs. The draw queue is the important boundary: a command is not finished when it is recorded, so every recorded draw must carry the state it needs.

MageQuest is playable at magequest.online.

Related Articles

Latest Articles

Go to top of page