---
id: "virtual-texturing"
title: "Sparse virtual texturing"
aliases: [virtual textures, megatexture, SVT]
domain: "performance"
stage: "performance"
realtime: "realtime"
platforms: [desktop-gpu, unity, unreal, webgpu]
year_introduced: 2008
inputs: [texture, uv-atlas]
outputs: [texture]
style: "both"
dynamic: "both"
cost: "medium"
authority: "Waveren"
url: "https://texelatlas.vercel.app/t/virtual-texturing/"
sources_verified_at: "2026-11-29"
---
# Sparse virtual texturing (Waveren)

> Treat a huge texture address space as virtual memory: only the tiles the camera actually samples are streamed into a small physical cache, found through a page table or indirection texture.

## Reach for it when

- textures too big to fit in gpu memory
- unique texture detail across a huge terrain
- too many texture bindings for large worlds
- texture streaming causes visible pop-in
- need per-pixel unique painted surface at world scale

## How it works

Surfaces sample from a single enormous virtual texture, split into fixed-size pages (e.g. 128x128 texels) per mip level. A small physical texture or atlas holds the resident pages, and an indirection (page table) texture maps virtual page coordinates and mip to a location in that cache.

To know which pages are needed, a feedback pass renders the scene at low resolution writing each pixel's virtual page and mip into a target. The CPU or a compute pass reads it back, requests missing pages, loads and decodes them (often transcoding to a block compressed format), and updates the page table with an LRU eviction policy.

The shader samples the page table, computes the physical UV including border or padding handling, and falls back to a coarser resident mip while a requested page is still loading.

## When to use it

Choose it for terrain, large worlds or unique-texel assets (decals, painted surfaces, baked lightmaps) that exceed GPU memory or would need hundreds of texture bindings. Prefer ordinary mipmapped textures with streaming of whole mip chains for modest scenes. Combine with [Block texture compression (BC7 / ASTC)](https://texelatlas.vercel.app/t/block-texture-compression.md) or [Basis Universal / KTX2 supercompression](https://texelatlas.vercel.app/t/basis-universal-ktx2.md) for page payloads; [Virtual Shadow Maps](https://texelatlas.vercel.app/t/virtual-shadow-maps.md) applies the same paging idea to shadow depth.

## Cost

medium — Extra indirection fetch per sample plus a feedback pass and CPU/IO streaming work; qualitative.
Measured GPU Weight: 3.73× baseline (light) — 0.1157 ms median / 0.1179 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 3.09× baseline (light) — 0.0491 ms median (2.36× faster on L4).
Ampere A100 scaling: 2.22× baseline (light) — 0.0638 ms median (1.81× faster on A100).

## Failure modes

- Texture pop-in or blurry surfaces while pages stream in, especially on fast camera cuts
- Anisotropic and trilinear filtering across page borders need padding or manual filtering, otherwise visible seams
- Feedback buffer latency of a few frames lets fast motion outrun the cache
- Cache thrash when the working set exceeds the physical pool causes constant reloads
- Page-table lookup breaks hardware derivative-based mip selection if gradients are not passed carefully

## Engine support

- metal: builtin — `MTLSparseTextureMappingMode via MTLResourceStateCommandEncoder.updateTextureMapping; sparse heaps` (evidence: https://developer.apple.com/tutorials/data/documentation/metal/mtlsparsetexturemappingmode.json, checked 2026-12-01) — Sparse residency is an API primitive; page table, feedback pass and streaming are yours.
- unity: builtin — `HDRP Asset > Streaming Virtual Texturing; Shader Graph Sample Virtual Texture node` (evidence: https://github.com/Unity-Technologies/Graphics/blob/557f6ac22b948284f57ffae6a789751fd1f63620/Packages/com.unity.render-pipelines.high-definition/Documentation~/HDRP-Asset.md, checked 2026-12-01) — HDRP.
- unreal: builtin — `Project Settings > Engine > Rendering > Virtual Textures; Streaming (SVT) and Runtime (RVT) virtual textures` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/virtual-texturing-in-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: texture, uv-atlas
- Outputs: texture
- Platforms: desktop-gpu, unity, unreal, webgpu

## Presets

### Sparse Virtual Texturing — mip feedback & resident page-table cache (left) vs streamed megatexture (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_PAGE_DENS": 6,
  "P_MIP_BIAS": 0,
  "P_CACHE_HUD": 1,
  "P_SPLIT": 0.5
}
```

```glsl
const float P_PAGE_DENS = 6.0; // P Virtual Pages [3.0, 12.0, 1.0]
const float P_MIP_BIAS = 0.0; // P Mip LOD Bias [-1.0, 1.5, 0.1]
const float P_CACHE_HUD = 1.0; // P Page Table HUD [0.0, 1.0, 0.05]
const float P_SPLIT = 0.50; // P Feedback / Stream [0.0, 1.0, 0.02]

float hash21(vec2 p) {
  p = fract(p * vec2(127.1, 311.7));
  p += dot(p, p + 34.5);
  return fract(p.x * p.y);
}

// High-detail procedural megatexture (unique non-repeating terrain + architectural decals)
vec3 megatexture(vec2 uv, float lod) {
  float scale = 1.0 / pow(2.0, clamp(lod, 0.0, 4.0));
  vec2 q = uv * 8.0;
  float n1 = sin(q.x * 1.4 + sin(q.y * 1.1)) * 0.5 + 0.5;
  float n2 = sin(q.x * 5.2 * scale + q.y * 4.8 * scale) * 0.5 + 0.5;
  float masonry = smoothstep(0.06, 0.12, min(abs(fract(q.x * 2.0 * scale) - 0.5), abs(fract(q.y * 2.0 * scale) - 0.5)));
  vec3 stone = mix(vec3(0.32, 0.28, 0.24), vec3(0.68, 0.62, 0.52), n1);
  vec3 moss = vec3(0.18, 0.44, 0.24);
  vec3 base = mix(stone, moss, smoothstep(0.55, 0.85, sin(uv.x * 3.1 - uv.y * 2.3) * 0.5 + 0.5));
  return base * (0.72 + 0.28 * masonry) * (0.85 + 0.15 * n2);
}

vec3 mipColor(float m) {
  if (m < 0.5) return vec3(0.95, 0.24, 0.22); // Mip 0 highest detail
  if (m < 1.5) return vec3(0.98, 0.68, 0.18); // Mip 1
  if (m < 2.5) return vec3(0.18, 0.88, 0.56); // Mip 2
  if (m < 3.5) return vec3(0.20, 0.64, 0.98); // Mip 3
  return vec3(0.62, 0.34, 0.92);              // Mip 4+ coarse fallback
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  vec2 uv01 = fragCoord / max(iResolution.xy, vec2(1.0));
  float screenX = uv01.x;
  float split = clamp(P_SPLIT, 0.0, 1.0);
  bool isStream = screenX >= split;
  float pageDens = clamp(floor(P_PAGE_DENS + 0.5), 3.0, 14.0);
  float mipBias = clamp(P_MIP_BIAS, -1.0, 1.8);
  float hudVis = clamp(P_CACHE_HUD, 0.0, 1.0);

  vec3 ro = vec3(0.25 * sin(iTime * 0.4), 1.15, 2.2 - iTime * 0.55);
  vec3 rd = normalize(vec3(uv - vec2(0.0, 0.14), -1.35));

  vec3 sky = mix(vec3(0.08, 0.12, 0.20), vec3(0.20, 0.30, 0.48), smoothstep(-0.1, 0.5, uv.y));
  vec3 col = sky;

  // Perspective textured ground plane + side walls forming an infinite virtual-textured canyon
  if (rd.y < -0.01) {
    float t = -ro.y / rd.y;
    if (t > 0.0 && t < 38.0) {
      vec3 p = ro + rd * t;
      vec2 vUV = p.xz * 0.25;

      // Analytical screen-space mip level from ray distance & grazing angle (feedback pass)
      float footprint = t / max(abs(rd.y) * 2.2, 0.12);
      float mipFloat = clamp(log2(max(footprint * 0.45, 0.25)) + mipBias, 0.0, 4.2);
      float mipIdx = floor(mipFloat);

      // Virtual page coordinates at this mip level
      float pagesAtMip = max(pageDens / pow(2.0, mipIdx * 0.5), 1.5);
      vec2 pageCoord = floor(vUV * pagesAtMip);
      vec2 pageFrac = fract(vUV * pagesAtMip);

      // Simulate sparse physical page cache residency (a few newly visible tiles streaming in!)
      float pageHash = hash21(pageCoord + mipIdx * 13.7);
      float resident = step(0.14, fract(pageHash + floor(iTime * 0.8) * 0.07));
      float effectiveMip = (resident > 0.5) ? mipIdx : min(mipIdx + 2.0, 4.0);

      vec3 texCol = megatexture(vUV, effectiveMip);
      float diff = 0.72 + 0.28 * sin(p.x * 0.8) * cos(p.z * 0.8);

      // Page border grid
      vec2 pf = abs(pageFrac - 0.5);
      float pEdge = smoothstep(0.45, 0.495, max(pf.x, pf.y));

      if (!isStream) {
        // Left: GPU feedback buffer showing requested (pageX, pageY, mip) + sparse cache miss indicators
        vec3 fbCol = mipColor(mipIdx);
        col = mix(texCol * 0.35, fbCol * 0.92, 0.72);
        // Highlight streaming-miss tiles flashing white-cyan while falling back to coarser mip
        if (resident < 0.5) {
          col = mix(col, vec3(0.95, 0.98, 1.0), 0.55);
        }
        col = mix(col, vec3(0.04, 0.06, 0.10), pEdge * 0.82);
      } else {
        // Right: final megatexture sampled via indirection page table -> physical cache atlas
        col = texCol * diff;
        if (resident < 0.5) {
          // Subtle coarser fallback blur tint on newly streamed tile
          col = mix(col, megatexture(vUV, 3.5) * diff, 0.65);
        }
        col = mix(col, vec3(0.15, 0.25, 0.38), pEdge * 0.16);
      }

      float fog = 1.0 - exp(-t * t * 0.0028);
      col = mix(col, sky, clamp(fog, 0.0, 0.92));
    }
  }

  // Bottom-left physical page-cache atlas inset showing packed resident 8x8 tiles
  vec2 hudMin = vec2(0.03, 0.05);
  vec2 hudSize = vec2(0.24, 0.30);
  vec2 hUV = (uv01 - hudMin) / hudSize;
  if (hudVis > 0.05 && hUV.x > 0.0 && hUV.x < 1.0 && hUV.y > 0.0 && hUV.y < 1.0) {
    vec2 tileId = floor(hUV * 6.0);
    vec2 tileF = fract(hUV * 6.0);
    float hVal = hash21(tileId + floor(iTime * 0.7) * 0.11);
    float slotMip = floor(hVal * 4.5);
    float isUsed = step(0.18, hash21(tileId * 3.7 + 2.1));
    vec3 slotCol = (isUsed > 0.5) ? mix(mipColor(slotMip), megatexture(tileF + tileId * 0.3, slotMip), 0.45) : vec3(0.06, 0.08, 0.12);
    float tBorder = smoothstep(0.44, 0.49, max(abs(tileF.x - 0.5), abs(tileF.y - 0.5)));
    slotCol = mix(slotCol, vec3(0.03, 0.04, 0.07), tBorder);
    float outerB = min(min(hUV.x, 1.0 - hUV.x), min(hUV.y, 1.0 - hUV.y));
    slotCol = mix(slotCol, vec3(0.75, 0.88, 1.0), smoothstep(0.03, 0.008, outerB));
    col = mix(col, slotCol, 0.92 * hudVis);
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: MIT — written for this index.

## Combines with

- [Basis Universal / KTX2 supercompression](https://texelatlas.vercel.app/t/basis-universal-ktx2.md): Pages are stored in a supercompressed form and transcoded to the GPU format as they are streamed into the cache.

## Combination ideas

- [Block-compressed tile streaming inside a hardware-filtered virtual texture cache](https://texelatlas.vercel.app/ideas/block-compressed-virtual-texture-cache/)

## Sources

1. [primary] J.M.P. van Waveren Software Virtual Textures. mrelusive.com. https://mrelusive.com/publications/papers/Software-Virtual-Textures.pdf (verified via http on 2026-11-29)
2. [tutorial] Sean Barrett Sparse Virtual Textures. silverspaceship.com. https://silverspaceship.com/src/svt (verified via http on 2026-11-29)

## Cite this entry

Texel. "Sparse virtual texturing." https://texelatlas.vercel.app/t/virtual-texturing/ (accessed 2026-12-01).
