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.
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.
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.
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
Each change starts loads, or drops tiles that were just loaded.
Two limits
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 is in memory, so it stands in. Nothing flickers and nothing is missing.
Three of them arrive
The ready children wait. A still fills the whole place.
The last one arrives
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.
Replace
The children are the parent again, finer: terrain, imagery. The parent is hidden once they are ready.
Add
The children add what the parent does not have: buildings on a district. The parent stays drawn.
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:
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.
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.
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.
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
- nobody wants itThe oldest first: the camera left it longest ago (least recently used).
- wanted, lower errorA wanted tile whose error ranks below the one that needs the room, the lowest first, the deepest first.
- else it waitsThe new tile does not load; its parent stands in. Nothing is dropped for it.
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.
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.
Loads started
A few tiles handed out per frame, the largest error first: making a tile's content takes main-thread time.
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.
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_*).
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.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
| Part | This page | RASM |
|---|---|---|
| Tree | any tree; the application's | The 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 error | the largest over the views; 0 outside all of them | getScreenError 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, hysteresis | the application's walk | The 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-ins | nearest ready ancestor | The 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. |
| Rank | the parent's error at the tile's bounds; a parent at least its children's | The same, the shallower tile first among equals. |
| Bytes | what is really held | Textures: 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. |
| Pools | each a budget | Vertices, 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. |
| Budget | a share of what the device leaves | TileStreamerConfig::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. |
| Estimate | its last cost, the application's, the mean | The same, in that order. A tile dropped while it loaded keeps what it held as its estimate. |
| Order of drops | unwanted oldest first, then wanted of a lower rank | The 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 frame | loads started, in flight, drops | maxLoadsPerFrame (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. |
| Content | the application's | setTileContent 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&&)). |
| Ready | its data is in | The 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. |
examples/24_map_tiles, held on every frame of a 1500-frame flightRelease, headless, RTX 3060update on average, with up to 137 tiles drawn (0.6 ms at most)about 500 tiles in memory, 965 recordsWhere it lives in code
| What | Where |
|---|---|
| The API | TileStreamer, TileDesc, TileContent, TileStreamerConfig, TileStreamerStats, TileState, TileRefine in include/rasm/tileStreamer.h |
| The policy: wanted set, ranks, budget, drops, loads, stand-ins | TileResidency::update, getTileScreenError in src/scene/tileResidency.cpp |
| Measuring, polling, the budget, scene membership | TileStreamer::Impl (measure, poll, getLimits, makeView) in src/scene/tileStreamer.cpp |
| A texture's allocation size | RenderContext::getTextureMemoryBytes; rhi::Texture::memoryBytes set in VulkanDevice::createTexture |
| The C API | rasm_tile_streamer_* in include/rasm/rasm_c.h, src/c/cTiles.cpp; examples/22_c_api streams its ground as five tiles |
| Tests | tests/tileResidencyTests.cpp; the image test tile_streaming (tests/image/imageTests.cpp) |
| Example | examples/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 |
TileResidencystays 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_streamingcounts). - 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.
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
- Open Geospatial Consortium, 3D Tiles Specification 1.1: tile trees, geometric error, replace and add refinement.
- Thatcher Ulrich, Rendering Massive Terrains using Chunked Level of Detail Control, SIGGRAPH 2002 course notes.
- Patrick Cozzi and Kevin Ring, 3D Engine Design for Virtual Globes (2011): terrain and imagery tiles, out-of-core rendering.
- Cesium, Tile traversal and loading in the CesiumJS and Cesium Native sources: the cut, the load queue and the cache.