Techniqueadvanced
Virtual texturing
Also called sparse virtual texturing, megatexture.
Page fixed-size tiles of a huge texture into a physical cache on demand, using a feedback pass and an indirection table, like virtual memory.
Virtual texturing treats a texture that is far too large for video memory as a virtual address space. Each level of its mip pyramid is cut into pages of a fixed size, such as texels. Only the pages that the current view needs live in a physical cache texture, and an indirection table maps each virtual page to its slot in the cache. A feedback pass finds the needed pages, and missing pages load in the background, as in demand paging of virtual memory. A plain mipmap keeps every level of the texture in memory. A texture clipmap keeps a fixed-size square of each level around one center point, which suits a terrain seen from one place. Virtual texturing keeps any set of pages, chosen by what the pixels actually sample, so it works for a whole world with unique texture on every surface.
In plain words
Think of a huge map book with thousands of pages, and a small desk that holds only a few of them. You only need the pages for the part of the world you look at now, and a less detailed overview page for things far away.
Virtual texturing does this with a giant texture. It cuts the picture, and each smaller copy of it, into equal square tiles. A small table says, for each tile, where on the desk it lies, if it is there at all. Each frame, the renderer asks which tiles the screen used. Tiles that are missing load from disk, and tiles that nobody used for a while leave the desk. While a tile loads, the blurrier copy of that area stands in for it.
When it applies
- The texture data of a scene is much larger than video memory: unique texture over a whole world, large terrain imagery, many detailed decals baked into one texture.
- The view at any moment sees a small part of the data at a fine level. The working set of pages fits in the cache.
- The data can stream from disk or from a fast decompressor fast enough to follow the camera, and a short blur while a page loads is acceptable.
- The shader can afford one dependent texture read for the indirection, and the engine can afford a feedback pass and a readback.
- It does not pay for small scenes whose textures fit in memory. Plain mipmapped textures are simpler and filter better then.
The template
The inputs are a virtual texture of texels with its mip levels, a page size with a border of texels, and a physical cache of slots.
- Offline, cut every mip level into pages of texels. Copy a border of texels from the neighbors around each page, so that bilinear filtering inside a slot gives the same result as in the full texture. Compress the pages, for example with block compression, and store them on disk.
- Feedback. Render the scene at a low resolution, and write for each pixel the page it needs: the page coordinates and the mip level, computed from the texture coordinate and its derivatives.
- Read the feedback buffer back to the CPU, or reduce it on the GPU, and collect the set of distinct page requests, for example with a hash table.
- For each requested page that is not in the cache, queue a load. The coarsest levels are always resident, so every request has a resident ancestor in the quadtree of pages.
- When a load ends, place the page in a free slot. If no slot is free, evict a page that was not requested in this frame, chosen by least recently used order.
- Update the indirection table. For each virtual page at each level, store the slot and the level of the finest resident page that covers it, which is the page itself or an ancestor.
- In the shading pass, read the indirection entry for the texture coordinate, compute the address of the texel in the cache from the slot and the offset inside the page, and sample the cache.
Step 4 makes every lookup valid: the indirection entry always points to a resident page that covers the texel, at the level requested or a coarser one. This is a proved property of the fallback to the ancestor. The address of step 7 is exact arithmetic, a law of the layout. The eviction order is a heuristic. The best order needs the future requests, as in Belady's algorithm, and LRU only imitates it. The page size, the border, the feedback resolution and the number of loads per frame are tuning.
Classic examples
Sizes. Take texels on a side, pages of texels, and 4 bytes per texel. Level 0 has pages, and all levels together have pages. Level 0 alone holds 64 GiB, or 8 GiB with 4 bits per texel compression. A cache of texels holds 64 MiB and pages. A screen of pixels at about one texel per pixel needs full pages. That is a lower bound, and partly covered pages, two mip levels at once and the borders raise the real count. The cache size is then set by measurement.
An address trace. Without borders, a texel at on level 0 is in page , at the offset inside the page. If that page is in slot , the cache texel is . If it is not loaded, but its parent on level 1 is in slot , the same point is texel on level 1, in page at the offset , and the cache texel is .
Unique texturing of a game world. id Tech 5 textured whole levels with one very large virtual texture, so that artists could paint every surface without repeated tiles. Pages streamed from disk and were transcoded to a GPU format at load time.
Terrain. Satellite imagery and baked terrain materials cover many kilometers at a fine resolution. Virtual texturing streams the pages near the camera, which a texture clipmap also does for one center, often together with geometry clipmaps for the heights.
Variations
- Clipmaps. The texture clipmap of Tanner, Migdal and Jones (1998) keeps a fixed region of each level around a center and updates it as the center moves. It needs no feedback and no indirection, but it serves only one area of interest.
- Procedural virtual texturing. Pages are rendered on the GPU when they are requested, by blending materials and decals, in place of loading them from disk.
- Hardware sparse textures. Modern graphics APIs let the application map and unmap pages of a texture in memory and ask whether a texel is resident. The hardware does the indirection, and the engine still chooses the pages.
- Feedback without a pass. Write the page requests from the main shading pass, for a small random subset of the pixels, in place of a separate low-resolution pass.
- Several virtual textures. Albedo, normals and other material channels share one indirection table, with one cache texture per channel.
Pitfalls
- Filtering across page borders. Bilinear and anisotropic filtering read neighbors of the texel, and in the cache those neighbors belong to another page. Borders of texels fix this for footprints up to texels. Wider anisotropic footprints still bleed.
- Derivatives and mip selection. The cache address jumps at page edges, so derivatives of the cache address are wrong there. Compute the level and the derivatives from the virtual coordinate, and sample with explicit gradients.
- Trilinear filtering. Blending two levels needs both pages. Without both, use one level, or do two lookups.
- Latency and blur. A page needs some frames to load. After a fast turn of the camera, surfaces are blurry for a moment and then sharpen. Load coarse pages first, and predict the camera motion.
- Thrashing. If the working set of pages is larger than the cache, pages evict each other every frame. Lower the resolution of the requests by one level when the cache overflows.
- Missed pages. A low-resolution feedback pass can miss small or thin objects, which then stay blurry. Jitter the feedback pixels over the frames.
- Indirection precision. The table needs enough bits for the slot, the level and the fraction of the coordinate. For texels, 32-bit floats for the texture coordinate are close to their limit.
Exercises
- Show that the levels to of a texture with pages of texels hold pages, and compare this with the pages of level 0.
- Write the address computation of step 7 with a border of texels, so that the payload of a page is texels.
- Show that after step 6 every indirection entry points to a resident page that covers the entry.
- A camera turns by 90 degrees in half a second, and the disk delivers 200 pages per second. How long does it take to replace a working set of 400 pages, and what does the player see?
- Compare the memory of a virtual texture cache of 1024 pages with the memory of a texture clipmap of the same texture with a clip size of 2048.
References
- Tanner, Migdal and Jones, The clipmap, a virtual mipmap, SIGGRAPH 1998, the whole paper (a fixed-size region of each mip level around a center, updated as the center moves)
- Sean Barrett, Sparse virtual textures, GDC 2008, pages of the mip pyramid, a page table texture and a feedback pass (general locator)
- J. M. P. van Waveren, id Tech 5 challenges, SIGGRAPH 2009 course, texture virtualization in a shipped engine, with feedback, streaming and transcoding of pages (general locator)
- Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Chapter 19, Acceleration Algorithms, the material on texture streaming and virtual texturing (general locator)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag YZPSJD Edit this page History Report an error Markdown