Managing Texture Memory

Take direct control of memory allocation for texture data by using sparse textures.


When you create a texture, by default, Metal allocates memory to hold the texture's pixel data. In some cases, such as when you are implementing texture streaming, you generally use only a fraction of this memory. When you use sparse textures, you take ownership of memory management for texture data and decide when to allocate and deallocate memory for a texture. In this way, sparse textures help you to use memory more efficiently.

To use sparse textures, allocate a sparse heap from which to allocate memory, and then create sparse textures from this heap. Initially, a texture has no storage. To add storage to a region inside the texture, ask the GPU to map memory from the heap for that region. A memory allocation is called a sparse tile (not to be confused with the tiles used in tile-based rendering). Sparse tiles are conceptually similar to virtual memory pages. When you don't need a region to have storage, you can unmap its sparse tile and recover that memory.

A figure showing a single sparse heap and two sparse textures. Each texture has a few regions mapped to sparse tiles on the heap.

Because sparse textures work closely with texture mipmaps, you should be familiar with mipmaps before using sparse textures. For more information, see Improving Filtering Quality and Sampling Performance.

Check for Sparse Texture Support

Not all GPUs support sparse textures. Check for support on the device object before attempting to use sparse textures:

func supportsSparseTextures() -> Bool
    return device.supportsFamily(MTLGPUFamily.familyApple6)



Creating Sparse Heaps and Sparse Textures

Allocate memory for sparse textures by creating a sparse heap.

Converting Between Pixel Regions and Sparse Tile Regions

Learn how a sparse texture's contents are organized in memory.

Assigning Memory to Sparse Textures

Use a resource state encoder to allocate and deallocate sparse tiles for a sparse texture.

Reading and Writing to Sparse Textures

Decide how to handle access to unmapped texture regions.

Estimating How Often a Texture Region Is Accessed

Use texture access patterns to determine when you need to map a texture region.

See Also

Working with Textures

Creating and Sampling Textures

Load image data into a texture and apply it to a quadrangle.

Understanding Color-Renderable Pixel Format Sizes

Know the size limits of pixel formats used by color render targets in iOS and tvOS GPUs.

Optimizing Texture Data

Optimize a texture’s data to improve GPU or CPU access.

Improving Filtering Quality and Sampling Performance

Provide multiple levels of detail for your textures by using mipmaps.

protocol MTLTexture

A resource that holds formatted image data.

class MTLTextureDescriptor

An object that you use to configure new Metal texture objects.

class MTKTextureLoader

An object that decodes common image formats into Metal textures for use in your app.

class MTLSharedTextureHandle

A texture handle that can be shared across process address space boundaries.

enum MTLPixelFormat

The data formats that describe the organization and characteristics of individual pixels in a texture.