{
 "id": "virtual-texturing",
 "title": "Sparse virtual texturing",
 "aliases": [
  "virtual textures",
  "megatexture",
  "SVT"
 ],
 "domain": "performance",
 "subdomain": "texture streaming",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "desktop-gpu",
  "unity",
  "unreal",
  "webgpu"
 ],
 "year_introduced": 2008,
 "inputs": [
  "texture",
  "uv-atlas"
 ],
 "outputs": [
  "texture"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "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.",
 "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.\n\nTo 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.\n\nThe 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": "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` or `basis-universal-ktx2` for page payloads; `virtual-shadow-maps` applies the same paging idea to shadow depth.",
 "cost": {
  "class": "medium",
  "notes": "Extra indirection fetch per sample plus a feedback pass and CPU/IO streaming work; qualitative."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://mrelusive.com/publications/papers/Software-Virtual-Textures.pdf",
   "type": "web",
   "title": "Software Virtual Textures",
   "authors": [
    "J.M.P. van Waveren"
   ],
   "year": null,
   "venue": "mrelusive.com",
   "url": "https://mrelusive.com/publications/papers/Software-Virtual-Textures.pdf",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": null,
    "page_title": null,
    "host": "mrelusive.com",
    "title_confirmed_by_page": false,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://mrelusive.com/publications/papers/Software-Virtual-Textures.pdf → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://mrelusive.com/publications/papers/Software-Virtual-Textures.pdf → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "waverensoftware"
  },
  {
   "role": "tutorial",
   "key": "url:https://silverspaceship.com/src/svt",
   "type": "tutorial",
   "title": "Sparse Virtual Textures",
   "authors": [
    "Sean Barrett"
   ],
   "year": null,
   "venue": "silverspaceship.com",
   "url": "https://silverspaceship.com/src/svt",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261018140107/https://silverspaceship.com/src/svt/",
     "timestamp": "20261018140107"
    },
    "page_title": "Sparse Virtual Textures",
    "host": "silverspaceship.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://silverspaceship.com/src/svt → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://silverspaceship.com/src/svt → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://silverspaceship.com/src/svt → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "barrettsparse"
  }
 ],
 "presets": [
  {
   "id": "virtual-texturing-live",
   "title": "Sparse Virtual Texturing — mip feedback & resident page-table cache (left) vs streamed megatexture (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_PAGE_DENS": 6,
    "P_MIP_BIAS": 0,
    "P_CACHE_HUD": 1,
    "P_SPLIT": 0.5
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_PAGE_DENS = 6.0; // P Virtual Pages [3.0, 12.0, 1.0]\nconst float P_MIP_BIAS = 0.0; // P Mip LOD Bias [-1.0, 1.5, 0.1]\nconst float P_CACHE_HUD = 1.0; // P Page Table HUD [0.0, 1.0, 0.05]\nconst float P_SPLIT = 0.50; // P Feedback / Stream [0.0, 1.0, 0.02]\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(127.1, 311.7));\n  p += dot(p, p + 34.5);\n  return fract(p.x * p.y);\n}\n\n// High-detail procedural megatexture (unique non-repeating terrain + architectural decals)\nvec3 megatexture(vec2 uv, float lod) {\n  float scale = 1.0 / pow(2.0, clamp(lod, 0.0, 4.0));\n  vec2 q = uv * 8.0;\n  float n1 = sin(q.x * 1.4 + sin(q.y * 1.1)) * 0.5 + 0.5;\n  float n2 = sin(q.x * 5.2 * scale + q.y * 4.8 * scale) * 0.5 + 0.5;\n  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)));\n  vec3 stone = mix(vec3(0.32, 0.28, 0.24), vec3(0.68, 0.62, 0.52), n1);\n  vec3 moss = vec3(0.18, 0.44, 0.24);\n  vec3 base = mix(stone, moss, smoothstep(0.55, 0.85, sin(uv.x * 3.1 - uv.y * 2.3) * 0.5 + 0.5));\n  return base * (0.72 + 0.28 * masonry) * (0.85 + 0.15 * n2);\n}\n\nvec3 mipColor(float m) {\n  if (m < 0.5) return vec3(0.95, 0.24, 0.22); // Mip 0 highest detail\n  if (m < 1.5) return vec3(0.98, 0.68, 0.18); // Mip 1\n  if (m < 2.5) return vec3(0.18, 0.88, 0.56); // Mip 2\n  if (m < 3.5) return vec3(0.20, 0.64, 0.98); // Mip 3\n  return vec3(0.62, 0.34, 0.92);              // Mip 4+ coarse fallback\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  vec2 uv01 = fragCoord / max(iResolution.xy, vec2(1.0));\n  float screenX = uv01.x;\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isStream = screenX >= split;\n  float pageDens = clamp(floor(P_PAGE_DENS + 0.5), 3.0, 14.0);\n  float mipBias = clamp(P_MIP_BIAS, -1.0, 1.8);\n  float hudVis = clamp(P_CACHE_HUD, 0.0, 1.0);\n\n  vec3 ro = vec3(0.25 * sin(iTime * 0.4), 1.15, 2.2 - iTime * 0.55);\n  vec3 rd = normalize(vec3(uv - vec2(0.0, 0.14), -1.35));\n\n  vec3 sky = mix(vec3(0.08, 0.12, 0.20), vec3(0.20, 0.30, 0.48), smoothstep(-0.1, 0.5, uv.y));\n  vec3 col = sky;\n\n  // Perspective textured ground plane + side walls forming an infinite virtual-textured canyon\n  if (rd.y < -0.01) {\n    float t = -ro.y / rd.y;\n    if (t > 0.0 && t < 38.0) {\n      vec3 p = ro + rd * t;\n      vec2 vUV = p.xz * 0.25;\n\n      // Analytical screen-space mip level from ray distance & grazing angle (feedback pass)\n      float footprint = t / max(abs(rd.y) * 2.2, 0.12);\n      float mipFloat = clamp(log2(max(footprint * 0.45, 0.25)) + mipBias, 0.0, 4.2);\n      float mipIdx = floor(mipFloat);\n\n      // Virtual page coordinates at this mip level\n      float pagesAtMip = max(pageDens / pow(2.0, mipIdx * 0.5), 1.5);\n      vec2 pageCoord = floor(vUV * pagesAtMip);\n      vec2 pageFrac = fract(vUV * pagesAtMip);\n\n      // Simulate sparse physical page cache residency (a few newly visible tiles streaming in!)\n      float pageHash = hash21(pageCoord + mipIdx * 13.7);\n      float resident = step(0.14, fract(pageHash + floor(iTime * 0.8) * 0.07));\n      float effectiveMip = (resident > 0.5) ? mipIdx : min(mipIdx + 2.0, 4.0);\n\n      vec3 texCol = megatexture(vUV, effectiveMip);\n      float diff = 0.72 + 0.28 * sin(p.x * 0.8) * cos(p.z * 0.8);\n\n      // Page border grid\n      vec2 pf = abs(pageFrac - 0.5);\n      float pEdge = smoothstep(0.45, 0.495, max(pf.x, pf.y));\n\n      if (!isStream) {\n        // Left: GPU feedback buffer showing requested (pageX, pageY, mip) + sparse cache miss indicators\n        vec3 fbCol = mipColor(mipIdx);\n        col = mix(texCol * 0.35, fbCol * 0.92, 0.72);\n        // Highlight streaming-miss tiles flashing white-cyan while falling back to coarser mip\n        if (resident < 0.5) {\n          col = mix(col, vec3(0.95, 0.98, 1.0), 0.55);\n        }\n        col = mix(col, vec3(0.04, 0.06, 0.10), pEdge * 0.82);\n      } else {\n        // Right: final megatexture sampled via indirection page table -> physical cache atlas\n        col = texCol * diff;\n        if (resident < 0.5) {\n          // Subtle coarser fallback blur tint on newly streamed tile\n          col = mix(col, megatexture(vUV, 3.5) * diff, 0.65);\n        }\n        col = mix(col, vec3(0.15, 0.25, 0.38), pEdge * 0.16);\n      }\n\n      float fog = 1.0 - exp(-t * t * 0.0028);\n      col = mix(col, sky, clamp(fog, 0.0, 0.92));\n    }\n  }\n\n  // Bottom-left physical page-cache atlas inset showing packed resident 8x8 tiles\n  vec2 hudMin = vec2(0.03, 0.05);\n  vec2 hudSize = vec2(0.24, 0.30);\n  vec2 hUV = (uv01 - hudMin) / hudSize;\n  if (hudVis > 0.05 && hUV.x > 0.0 && hUV.x < 1.0 && hUV.y > 0.0 && hUV.y < 1.0) {\n    vec2 tileId = floor(hUV * 6.0);\n    vec2 tileF = fract(hUV * 6.0);\n    float hVal = hash21(tileId + floor(iTime * 0.7) * 0.11);\n    float slotMip = floor(hVal * 4.5);\n    float isUsed = step(0.18, hash21(tileId * 3.7 + 2.1));\n    vec3 slotCol = (isUsed > 0.5) ? mix(mipColor(slotMip), megatexture(tileF + tileId * 0.3, slotMip), 0.45) : vec3(0.06, 0.08, 0.12);\n    float tBorder = smoothstep(0.44, 0.49, max(abs(tileF.x - 0.5), abs(tileF.y - 0.5)));\n    slotCol = mix(slotCol, vec3(0.03, 0.04, 0.07), tBorder);\n    float outerB = min(min(hUV.x, 1.0 - hUV.x), min(hUV.y, 1.0 - hUV.y));\n    slotCol = mix(slotCol, vec3(0.75, 0.88, 1.0), smoothstep(0.03, 0.008, outerB));\n    col = mix(col, slotCol, 0.92 * hudVis);\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "virtual-texturing",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:37.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6244,
      "std": 0.0901,
      "min": 0.2234,
      "max": 0.9014
     },
     "image": "/specimens/virtual-texturing-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "basis-universal-ktx2",
  "block-texture-compression",
  "virtual-shadow-maps",
  "nanite-virtualized-geometry",
  "neural-texture-compression"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "basis-universal-ktx2",
   "why": "Pages are stored in a supercompressed form and transcoded to the GPU format as they are streamed into the cache."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/performance/virtual-texturing.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Waveren",
 "url": "https://texelatlas.vercel.app/t/virtual-texturing/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 3.73,
  "tier": "light",
  "p50_ms": 0.1157,
  "p95_ms": 0.1179,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "virtual-texturing-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 3.09,
   "tier": "light",
   "p50_ms": 0.0491,
   "p95_ms": 0.0493,
   "speedup_x": 2.36,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 2.22,
   "tier": "light",
   "p50_ms": 0.0638,
   "p95_ms": 0.0643,
   "speedup_x": 1.81,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}