Copying data into or out of mipmaps
Specify which mipmaps that the data transfer affects.
Overview
When you copy data between resources, and the source or destination is a texture, specify which mipmaps that the data transfer affects.
Copy data from system memory to a mipmap
When you copy data from system memory into a texture, using the replace(region:mipmapLevel:withBytes:bytesPerRow:) or similar method, state which mipmap is the destination of that copy.
Call this routine once for each mipmap you want to fill, changing the region to match the size of the mipmap level you’re writing to.
Copy mipmap data between Metal resources
If you already have data in Metal resources, use an MTLBlitCommandEncoder to copy data to and from different mipmaps in a texture.
To copy all matching data between two textures, encode a command using the copy(from:to:): method. The two textures need to have the same pixel format and type. Metal copies all matching mipmap sizes to the destination texture.
To copy a selection of mipmaps from one texture to another, use the copy(from:sourceSlice:sourceLevel:to:destinationSlice:destinationLevel:sliceCount:levelCount:) method. Specify the first source mipmap level and first destination mipmap level, both of which need to have the same dimensions. Also specify the number of mipmap levels you want to copy.
For example, the following code assumes that the destination texture is twice as large in both dimensions as the source texture. Mipmap 1 in the destination matches the size of the source mipmap 0, so the code passes 0 as the source level and 1 as the destination level. It also passes 5 as the level count to copy 5 mipmaps.
If you need to copy data between buffers and textures, encode a separate blit command for each mipmap level to copy. See MTLBlitCommandEncoder for other methods that copy data to and from textures.