Dynamically Adjusting Texture Level of Detail

Defer generating or loading larger mipmaps until that level of detail is needed.


While the most common use for mipmaps is to improve sampling quality and performance, they have other uses, such as texture streaming. If you are loading texture data over a network or generating textures procedurally at runtime, it can be expensive to create a full set of mipmaps, especially if mipmap 0 is large. Further, if the objects being rendered are far away from the camera, the detail in mipmap 0 may never be needed. But because you aren't required to provide mipmaps when you create the texture, you can bring in additional levels of detail only when needed.

Provide Data for Smaller Mipmaps

Start by creating the textures, as described in Creating a Mipmapped Texture. Remember that Metal allocates memory for all of the mipmaps when you create the texture. Instead of loading data for all mipmaps, pick a lower mipmap, and provide data for it and any mipmaps lower in the chain. For example, if you started at mipmap level 3, as shown in the figure below, you are loading only about 2% of the total texture data required for the entire mipmap chain.

A figure showing a chain of seven mipmaps. Only mipmaps 3 through 6 have image data.

Limit Access to Higher Mipmaps

You need to keep track of the highest mipmap you've loaded and pass this information to your shader so that it samples only from mipmaps that contain data. You can do this by passing in an appropriately configured sampler, or, on some GPUs, by passing in the minimum level of detail (LOD) to your shader and using it as the minimum LOD when you sample the texture. See Control Mipmap Selection when You Sample the Texture.

Determine when Objects Get Closer to the Camera

As the scene animates, some objects may get closer to the camera. Detect when this happens by asking the shader which mipmap will be accessed or by performing a calculation based on the rendered image size, as described in Determining Which Mipmaps the GPU Will Try to Access and Using Function Specialization to Build Pipeline Variants.

Update the Mipmaps

When it seems likely that you will need more detailed textures, start preparing new mipmap data. Depending on what kind of system you are implementing, you might make a network request to your server or render a new mipmap procedurally on the device. When you have the data, copy it into the appropriate mipmaps, and update the range of mipmaps that your shaders can sample. For example, in the following diagram, two additional levels of mipmaps were loaded and copied into the texture.

A figure showing how the mipmap chain has been updated with additional texture data. Originally, mipmaps 3 through 6 had valid data. After the update, mipmaps 1 through 6 have data.

See Also

Mipmap Access

Restricting Access to Specific Mipmaps

Set the range of mipmap levels that a sampler can access.

Determining Which Mipmaps the GPU Will Try to Access

Get information at runtime about mipmaps the GPU will sample.