Contents

getBytes(_:bytesPerRow:from:mipmapLevel:)

Copies pixel data from the first slice of the texture to a buffer in system memory.

Declaration

func getBytes(_ pixelBytes: UnsafeMutableRawPointer, bytesPerRow: Int, from region: MTLRegion, mipmapLevel level: Int)

Parameters

  • pixelBytes:

    A pointer to a destination buffer in system memory.

  • bytesPerRow:

    The number of bytes (stride) between two adjacent rows of pixel data in the destination buffer. For Type1d and Type1darray, use 0. For raw and packed pixel types, the stride is the number of pixels in one row. For compressed pixel formats, the stride is the number of bytes from the beginning of one row of blocks to the beginning of the next.

    Your data type determines how you should compute bytesPerRow:

    • For raw or packed pixel data, use a multiple of the pixel size less than Max * pixel size.

    • For compressed pixel data, use a multiple of the compression block size. When working with PowerVR Texture Compression (PVRTC), use 0.

    Nonzero values smaller than the texture width or any values not a multiple of the pixel or block size cause an error.

  • region:

    The location of a block of pixels in the texture slice. For textures compressed as PVRTC, use the entire texture for the region.

  • level:

    A zero-indexed value that selects the texture’s mipmap level as the method’s data source. Use 0 for textures that don’t have mipmaps.

Discussion

This method runs on the CPU and immediately copies the pixel data from the texture to system memory, but it doesn’t synchronize with any GPU texture memory operations. Ensure all operations that write or render to the texture complete before reading the texture’s contents using one of the following methods:

For multisample textures, the method consecutively positions each sample within a pixel in memory and treats the pixels as part of one row.

See Also

Copying data from a texture image