GPU data

Tile streaming under a memory budget

A map of a country, or an open-world level, does not fit in GPU memory. So it is cut into tiles, and only the tiles the camera needs are in memory. This page explains how an engine decides which tiles to load, which to keep and which to drop while the camera moves, with a fixed memory budget and no long frames: a tree of tiles, the screen-space error, the cut, a parent that stands in for its children, and the residency cache. The last part shows how RASM does it. Loading one file without freezing the frame is in Streaming assets in the background.

In short

  • The world is a tree of tiles: the root covers everything coarsely, each child covers a quarter of its parent with more detail.
  • Each tile has a geometric error: how far it is from the full detail, in world units. Seen from the camera, it is a number of pixels: the screen-space error.
  • Walk the tree from the root and refine a tile while its error is over a limit τ. The tiles where the walk stops are the cut: what should be drawn.
  • Refine above τ, merge back only below about 0.8 τ (hysteresis), or a tile at the edge flickers.
  • Until all its children are in memory, a tile's parent stands in for them. So the parents load first, and stay longest.
  • A byte budget counts what the tiles really hold. To make room, drop tiles nobody wants (the oldest first), then wanted tiles of a lower error. A tile that does not fit waits.
  • Bound the work per frame: a few loads started, a few in flight, a few drops. Never wait for the GPU.

1The problem

A map of 32 × 32 km with 256 m tiles at its finest level has 16 384 tiles there, and 21 845 with the coarser levels above them. At 200 KiB each (a mesh and an image) that is over 4 GiB. The GPU has room for a few hundred.

Every tile at full detail 21 845 tiles: over 4 GiB Only what the camera needs 13 tiles: small near the camera GPU memory the tiles' budget The budget is a number of bytes the application sets, or a share of what the device says it can spare. the camera
Far from the camera a tile covers few pixels, so a coarse tile looks the same as fine ones. Only near the camera do the fine tiles matter. The question is which tiles to hold, as the camera moves, without going over the budget and without a long frame.

2A tree of tiles

The usual tree is a quadtree: level 0 is one tile for the whole world, and each tile has four children, each a quarter of it. A tile's mesh has the same number of vertices at every level, and its image the same number of pixels. So a child shows its quarter with twice the detail.

0 size S, error e 1111 size S/2, error e/2 size S/4, error e/4 4 children 16 children The root covers everything coarsely; each level halves the tiles and their error.
The geometric error of a tile: how far its surface and its colors may be from the full detail, in world units. With a fixed grid per tile it halves at each level. A leaf (the finest level) has an error of 0.

Quadtree

A 2D map, terrain, imagery: four children per tile. Web map tiles (zoom / x / y) are one.

Octree

A city in 3D, a point cloud: eight children per tile.

Any tree

3D Tiles and similar formats: each tile names its children, its bounds and its error.

What the streamer needs from a tile is the same in all three: its parent, a bounding sphere, and its geometric error. What a tile is (a file name, a mesh made from a height map, buildings from a vector tile) belongs to the application.

3The screen-space error and the cut

A tile's geometric error, seen from the camera, is a number of pixels. It is the same rule as picking a mesh's level of detail (LOD selection): the error times the pixels per world unit, which shrink with the depth.

pixels = e × P11 × H / 2 / (w − r)

e
the tile's geometric error, in world units
P11 × H / 2
pixels per world unit at depth 1: the projection's P11 and the view's height in pixels
w − r
the view depth of the bounding sphere's nearest point (an orthographic view has no division)

The walk: start at the root; if its error on screen is over a limit τ, go into its children, else stop. The tiles where the walk stops are the cut. A tile outside every view has an error of 0, so the walk stops there at once: far and unseen places stay coarse.

Drag the camera over the map and change its height and the pixel limit. Near the camera the walk goes deep; far away one coarse tile covers a large area. Raise τ: fewer, coarser tiles.
Several views

With two views (the camera and a map in a corner), a tile's error is the largest over the views that see it. One cut then serves both: the map view does not need the fine tiles, but they are not wrong there, only finer.

4Hysteresis: refine above, merge below

A tile whose error is close to τ crosses it back and forth as the camera moves a little. With one limit it refines, merges, refines: its children load, go, load again.

One limit

τ refinemergerefinemerge the children load and go, frame after frame

Each change starts loads, or drops tiles that were just loaded.

Two limits

τ: refine 0.8 τ: merge refine, once the cut stays put while the error wobbles

A tile refined last frame stays refined until its error falls under the lower limit.

The walk keeps one flag per tile: was it refined last frame. It is the application's walk, so the flag is the application's too.

5A parent stands in

The cut says what should be drawn. Some of it is not in memory yet. Drawing a hole there looks broken. Drawing the children that are ready next to their parent overlaps it. The answer: while any child is missing, draw the parent in place of all of them.

The cut wants four children

A drawn in their place A1 to A4: asked for The camera came closer. The walk goes into A, but its children are still on their way.

A is in memory, so it stands in. Nothing flickers and nothing is missing.

Three of them arrive

A still drawn ready, not drawn · A4 on its way Drawing A1 to A3 with A would overlap; drawing them alone leaves a hole at A4.

The ready children wait. A still fills the whole place.

The last one arrives

A hidden, kept in memory all four drawn The swap happens in one frame: the children show and A hides, never both, never neither.

A stays in memory: when the camera backs off, it is drawn again at once.

The rule, for any tree: for each wanted tile that is not ready, draw its nearest ready ancestor, and hide everything under that ancestor. For this to work, the parents must be in memory before their children. So the parents of the cut are wanted too, and rank at least as high as their children.

children instead of the parent

Replace

The children are the parent again, finer: terrain, imagery. The parent is hidden once they are ready.

children on top of the parent

Add

The children add what the parent does not have: buildings on a district. The parent stays drawn.

nothing here yet a hole

A hole

Only when no ancestor is ready either: at the very start, before the root is in.

6The residency budget

The tiles in memory are a cache: the wanted ones, and some the camera left, kept in case it comes back. A budget limits it. Count what is really there, not what was guessed:

mip 01 pad the allocation's size

Textures: the allocation

The memory allocator's size of each image: every mip, and the alignment the driver adds. Width × height × 4 is only an estimate.

its ranges of the shared pools

Meshes: their share of the pools

In a GPU-driven renderer a mesh is ranges of big shared buffers. Its bytes are its vertices, indices and other streams there.

slots rows rows

Every fixed pool

Texture slots, material and instance rows, the vertex and index pools: each is a second budget. A tile that does not fit one must wait like one over the bytes.

others80% of the rest

A budget from the device

The driver says how much memory the program may use now (Vulkan: VK_EXT_memory_budget). Take a share of what the rest leaves.

Before a tile is loaded its cost is a guess: its own last measured cost if it was in memory before, else what the application says, else the mean of the tiles measured so far. Once its content exists, the real number replaces the guess.

The order tiles go in

  1. nobody wants itThe oldest first: the camera left it longest ago (least recently used).
  2. wanted, lower errorA wanted tile whose error ranks below the one that needs the room, the lowest first, the deepest first.
  3. else it waitsThe new tile does not load; its parent stands in. Nothing is dropped for it.
A tile is ranked by its parent's error

A tile is wanted because its parent was too coarse. So rank it by its parent's error, seen at the tile's own bounds. Ranked by its own error, every leaf (error 0) would come last. And a parent ranks at least as high as its wanted children: they need it to stand in.

The camera flies a circle. Lower the budget until the wanted tiles do not fit: the far ones wait, their parents stand in, and the memory stays under the budget. Raise it: the tiles the camera left stay as a cache. With 1 load per frame, children arrive late and the parents stand in longer.

7Bounded work per frame

A frame that loads or drops a hundred tiles is a long frame. Each step gets a limit, and what is over waits for the next frame.

4 this framenext

Loads started

A few tiles handed out per frame, the largest error first: making a tile's content takes main-thread time.

unwanted: cancel

Loads in flight

So the loader's queue stays short and new priorities count soon. A load nobody wants any more gives its place up.

later destroy now, free later

Drops

A few destroyed per frame. The memory comes back once the GPU is done with it (deferred destruction), with no wait.

8Pitfalls

Tiles flicker between two levels at one place
One limit for refine and merge. Merge only under a lower limit (hysteresis).
Holes appear when the camera dives in
The children were wanted, the parents were not. Keep the parents of the cut wanted, and rank them at least as high as their children.
The leaves load last, or never
Tiles ranked by their own error: a leaf's is 0. Rank by the parent's error.
Memory goes over the budget for a moment
A tile's real size was only known once its file was read. Count what its content holds as soon as it exists (a texture is allocated before its pixels come), and drop at once to get under again.
One huge tile empties the cache
It is larger than the whole budget, so dropping everything else still does not make room. Drop it first; it then waits with its measured size and never pushes others out.
A load nobody wants holds its place for seconds
Loads keep running after the camera turned away. Cancel a load that is no longer wanted when a wanted one needs its place.
A shared mesh is destroyed with a tile
Two tiles listed the same mesh (or a loader returned the same handle for the same file). A tile owns only what is its own; shared content stays outside the tiles.
The pools fill although the bytes fit
Texture slots or table rows ran out first. Every fixed pool is a budget too.

9In RASM

TileStreamer (include/rasm/tileStreamer.h, in the rasm::renderer library) keeps one scene's tiles under a byte budget. The application owns its tree and what a tile is; the streamer owns the budget, the residency, the eviction order, the limits per frame and what is drawn. The two talk through handles and arrays, with no callbacks, so the C API has the same calls (rasm_tile_streamer_*).

Exports While an export of the scene runs, TileStreamer::setViews adds its view (the whole image's size, errors in its pixel-scaled pixels) to the views the screen error takes, so the application's cut loads the detail the export needs. The export waits until the streamer has nothing loading or changing, and from its first tile to its last TileStreamer::update only polls the loads: nothing drawn is shown, hidden or dropped (isExportFrozen), so every tile shows the same tiles. examples/24_map_tiles --export-check checks it.
the application TileStreamer the renderer's objects
application setViews(views) walk its tree:getScreenError(tile) update(cut) getTilesToLoadsetTileContent TileStreamer::update poll loads: ready, failed, size so far limits: budget, free pool rows drop, hand out (by error) drawn set: stand-ins hide, destroy, show renderer isLoaded, meshstate, entity rows getPoolStats,queryMemoryStats scene membership,destroy* handed out
Call order in a frame: setViews, the walk, update, then getTilesToLoad and setTileContent (now or in a later frame), then Renderer::render. The policy itself is plain data with no renderer, in TileResidency, so it is unit-tested.
// From examples/24_map_tiles/main.cpp, shortened: the frame of a map engine.
streamer.setViews(views.data(), static_cast<uint32_t>(views.size()));
world.cut.clear();
visit(world, streamer, 0, 0, 0, rasm::TileHandle{}); // refines over 24 px, merges under 0.8 × 24 px
streamer.update(world.cut.data(), static_cast<uint32_t>(world.cut.size()));
uint32_t count = 0;
const rasm::TileHandle *toLoad = streamer.getTilesToLoad(count);
// ... ask the server for each; when one arrives:
streamer.setTileContent(node.tile, {.entities = entities, .entityCount = 1, .materials = materials, .materialCount = 1, .textures = textures, .textureCount = 2});
engine.render(views.data(), static_cast<uint32_t>(views.size()));

Where RASM follows this page, and where it differs

PartThis pageRASM
Treeany tree; the application'sThe application makes tile records with createTile (TileDesc: parent, a sphere center in doubles and radius, geometricError, refine, estimatedBytes) as its walk reaches new places, and destroyTile when it forgets them (its children become roots). At most TileStreamerConfig::maxTiles.
Screen errorthe largest over the views; 0 outside all of themgetScreenError with the views of setViews (views of other scenes are skipped): |P11| × the view's height in pixels / 2, over the depth of the sphere's near side, the four side planes of each view's frustum. The same rule as selectLod (LOD selection).
The cut, hysteresisthe application's walkThe application's. examples/24_map_tiles refines over 24 px and merges under 0.8 × 24 px, and hangs each glTF landmark under an empty ADD tile, so the terrain around it does not wait for the model. A point cloud's octree is all ADD tiles (Streaming point clouds).
Stand-insnearest ready ancestorThe same, with TileRefine::REPLACE (the default) and TileRefine::ADD per tile. The cut's ancestors are wanted too. One drawn set per scene: every view of the scene draws it.
Rankthe parent's error at the tile's bounds; a parent at least its children'sThe same, the shallower tile first among equals.
Byteswhat is really heldTextures: the allocation's size (RenderContext::getTextureMemoryBytes, VMA's). Meshes: their vertices, colors, skins, morph deltas, indices and meshlets in the geometry pools, and a point chunk's points in the point pool (8 B a unit; its indices are the range every chunk shares and do not count); a glTF mesh's own materials and textures count with it. Measured once the tile is ready, and while it loads.
Poolseach a budgetVertices, indices, sub-meshes, meshlets, instance rows, materials, bindless slots (RendererConfig::maxBindlessTextures) and the point pool's units (RendererConfig::geometryPoolPoints), each limited to its capacity minus what is not a tile's content (getPoolStats; slots that wait for the GPU, getFreeBindlessTextureSlots, count as taken). The reservations of tiles still loading are not in the pools yet, so they do not count as other users.
Budgeta share of what the device leavesTileStreamerConfig::memoryBudget in bytes; 0: deviceShare (0.8) of the device-local budget left after everything that is not a tile (queryMemoryStats, VK_EXT_memory_budget), read every 30 updates.
Estimateits last cost, the application's, the meanThe same, in that order. A tile dropped while it loaded keeps what it held as its estimate.
Order of dropsunwanted oldest first, then wanted of a lower rankThe same. First of all, a tile that alone is over a limit. A tile that cannot fit even so waits, and the next ones still try (at most 16 misses an update).
Limits per frameloads started, in flight, dropsmaxLoadsPerFrame (4), maxLoadsInFlight (16), maxDropsPerFrame (32). An unwanted load in flight is cancelled for a wanted one: its content is destroyed, which drops an AssetLoader load at once.
Contentthe application'ssetTileContent with TileContent (entities, meshes, materials, textures), only for a tile it handed out and still waits for: the streamer owns them from then on and destroys them, entities first, when it drops the tile, or at once when the tile was cancelled meanwhile. The entities must not be in a scene: the streamer adds them while the tile is drawn. An empty content is fine: a tile that only groups its children. An application that makes its tiles' meshes builds them on its own threads (buildMesh, buildVectorMesh), so the frame only uploads them (createMesh(MeshBuild&&)).
Readyits data is inThe renderer's own state: every mesh READY with its import's textures filled (isLoaded), every texture out of the loading set, every entity with its instance rows and its textures. A mesh that FAILED, or an entity with no instance rows 4 updates after its mesh is in (a full instance table), fails the tile (TileState::FAILED), handed out again after retryFailedAfter updates.
48 MiBthe budget of examples/24_map_tiles, held on every frame of a 1500-frame flightRelease, headless, RTX 3060
0.03 msupdate on average, with up to 137 tiles drawn (0.6 ms at most)about 500 tiles in memory, 965 records
2.6 msthe longest frame after the first 30, with 4 tiles uploaded per framethe tiles' data made on 3 threads

Where it lives in code

WhatWhere
The APITileStreamer, TileDesc, TileContent, TileStreamerConfig, TileStreamerStats, TileState, TileRefine in include/rasm/tileStreamer.h
The policy: wanted set, ranks, budget, drops, loads, stand-insTileResidency::update, getTileScreenError in src/scene/tileResidency.cpp
Measuring, polling, the budget, scene membershipTileStreamer::Impl (measure, poll, getLimits, makeView) in src/scene/tileStreamer.cpp
A texture's allocation sizeRenderContext::getTextureMemoryBytes; rhi::Texture::memoryBytes set in VulkanDevice::createTexture
The C APIrasm_tile_streamer_* in include/rasm/rasm_c.h, src/c/cTiles.cpp; examples/22_c_api streams its ground as five tiles
Teststests/tileResidencyTests.cpp; the image test tile_streaming (tests/image/imageTests.cpp)
Exampleexamples/24_map_tiles: a 32 km quadtree of terrain tiles (height maps lifted on the GPU) with roads and lakes draped on them (vector features), and glTF landmarks; examples/25_vector_map: a city's vector tiles under empty ADD tiles, two views
Keep these true
  • TileResidency stays plain data: no renderer, so every rule of the policy has a unit test.
  • A frame where no tile changes allocates nothing: every list keeps its capacity (the image test tile_streaming counts).
  • Hide before destroy, and show in the same update that hides the stand-in, so a tile and its stand-in never draw together and never both go missing.
  • A tile owns only what is listed in its content. Never list content another tile or the application still uses.
Not done yet

One drawn set per scene: two views of a scene cannot draw different cuts; two scenes and two streamers can (D43). No cross-fade when a parent and its children swap. No partial residency of one texture (only its high mips).

Further reading