Contents

makeTextureView(pixelFormat:)

Creates a new view of the texture, reinterpreting its data using a different pixel format.

Declaration

func makeTextureView(pixelFormat: MTLPixelFormat) -> (any MTLTexture)?

Parameters

  • pixelFormat:

    A new pixel format, which needs to be compatible with the original pixel format.

Return Value

A new texture object that shares the same storage allocation of the texture.

Discussion

When you create a texture normally, Metal allocates memory for the textureʼs pixel data. These storage allocations can be quite large. You can reduce memory use and avoid copying texture data by using a texture view—a texture object that shares another textureʼs storage allocation, reinterpreting the pixel data in some other format.

Not all pixel formats are compatible with one another. Reinterpretation of image data between pixel formats is supported within the following groups:

This method doesn’t change the original texture image data in any way, but it may drastically change how the data is interpreted. For example, given a texture with the MTLPixelFormat.rg16Uint pixel format that contains image data for Red 0xFFFE and Green 0x0001, this method would reinterpret that data in an MTLPixelFormat.r32Uint format as Red 0x0001FFFE.

Some format reinterpretations are supported but may not be useful. For example, this method considers the 32-bit packed color formats MTLPixelFormat.bgr10a2Unorm and MTLDataType.rg11b10Float to be compatible, but it’s unlikely that the same data can be interpreted by both formats in a meaningful way.

Some format reinterpretations require you to create the source texture with a special usage flag. Set that flag only when necessary, as it can affect performance. For more details, see pixelFormatView.

See Also

Related Documentation

Creating textures by reinterpreting existing texture data