On this page
Virtual & Sparse Textures
A texture’s file on disk can be much bigger than the GPU’s memory, and a scene’s textures together can be bigger than the card. Virtual textures and sparse textures are the two engine side mechanisms that make that possible, and the reason you can use 4K and 8K maps on hardware that could never hold them all.
// The point of the chapter: your code does not change.
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
The problem: the GPU memory budget¶
The GPU has a fixed pool of video memory, and every texture, mips and all, takes a place in it. A 4K texture with its mip chain is about 25 megabytes. A modern scene with a few hundred such textures would need several gigabytes, and the card would run out long before the art direction was satisfied. Something has to give, and the two things that give are space and time.
Virtual textures: address a big image, keep only what you see¶
A virtual texture is a texture whose address space is much larger than its physical memory. The full image lives on the CPU side, cut into a grid of small tiles, typically 128 by 128 texels, stored on disk. On the GPU, a much smaller physical pool holds only the tiles the scene is actually looking at this frame, at the resolutions the camera actually needs. The sampler you call in your shader addresses the virtual image, and the engine transparently translates each sample into the right physical tile.
The mental model is a page table, the same idea virtual memory uses for CPU programs. Your shader believes it is sampling one big texture. The engine decides which pages of that texture sit in the card. When the camera moves, pages in, pages out. From the shader’s perspective the texture is infinite and always complete. From the memory perspective only a working set is resident.
The practical effects a technical artist should expect. Memory use becomes flat and predictable, set by the size of the pool, not by the sum of the art. Load times shift from “upload everything” to “stream what the camera looks at”, which changes what pop in looks like: distant detail fades in as pages arrive instead of objects popping at distance. And the quality you get is adaptive, because the engine allocates its limited resident tiles to what is on screen, in full resolution, instead of shrinking everything to fit.
Sparse textures: the allocation side of the same idea¶
Sparse textures are the sibling idea on the allocation side. Instead of one dense block of memory for a big texture, the GPU reserves a scatter of small, fixed size pages, and the driver places them wherever there is room, binding them to regions of the texture as needed. The texture can be “bigger than any single free block” as long as its pages fit in the gaps. On the platforms where sparse is supported, it is the mechanism that lets very large textures exist at all, and it usually works in tandem with streaming: stream a page’s data, bind the page, done.
Where the two meet is the point: sparse is about where the memory sits, virtual is about which data sits there. A full virtual texture system uses both, a sparse allocation for the physical pool and a page table to decide content. Different engines expose different pieces of this stack, and different names for them, but the pair of ideas is the same everywhere.
What this means for you as an author¶
The good news is in the first code block of this chapter: your sample does not change. The engine presents a normal texture, and your shader samples it normally. Your responsibilities move upstream. Watch the memory budget, because virtual textures move the limit from per texture to per pool, and a scene of 8K maps still has to stream. Watch streaming distance and pool size in the render settings, because those are the knobs that control how much detail is resident and how far out the fade in begins. And test with the camera moving, because the failure mode of a badly tuned pool is not a crash, it is a scene that quietly loses its detail at speed.
What the shader does¶
The shader samples a high-resolution texture from a virtual pool. Reducing the pool size makes the distant areas fall back to lower detail while the near plane stays sharp, which is the whole selling point: resolution follows the camera.
Illustrative schematic: Near plane sharp, far plane lower detail.
Intentionally ordinary. This pass samples a texture the standard way, because that is the lesson. The virtual or sparse behavior, when the platform and the project have it on, happens below this line, and the shader that works with it is the same shader that works with a plain texture.
Shader "DecompiledArt/Textures/Basics/VirtualTextures/VirtualTextures"
{
Properties
{
_MainTex("Texture (virtual or plain, same code)", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Filtering & Mipmaps • Texture Arrays • LODs
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.