A resource that stores data in a format defined by your app.


protocol MTLBuffer


A MTLBuffer object can be used only with the MTLDevice that created it. Don’t implement this protocol yourself; instead, use the following MTLDevice methods to create MTLBuffer objects:

The Metal framework doesn’t know anything about the contents of a MTLBuffer, just its size. You define the format of the data in the buffer and ensure that your app and your shaders know how to read and write the data. For example, you might create a struct in your shader that defines the data you want to store in the buffer and its memory layout.

If you create a buffer with a managed resource storage mode (MTLStorageMode.managed), you must call didModifyRange(_:) to tell Metal to copy any changes to the GPU.


Creating a Texture That Shares Buffer Data

func makeTexture(descriptor: MTLTextureDescriptor, offset: Int, bytesPerRow: Int) -> MTLTexture?

Creates a texture that shares its storage with the buffer.


Reading the Buffer's Data on the CPU

func contents() -> UnsafeMutableRawPointer

Gets the system address of the buffer’s storage allocation.


Synchronizing Data to the GPU for Managed Buffers

func didModifyRange(Range<Int>)

Informs the GPU that the CPU has modified a section of the buffer.

Debugging Buffers

func addDebugMarker(String, range: Range<Int>)

Adds a debug marker string to a specific buffer range.

func removeAllDebugMarkers()

Removes all debug marker strings from the buffer.


Reading Buffer Length

var length: Int

The logical size of the buffer, in bytes.


Creating Views of Buffers on Other GPUs

func makeRemoteBufferView(MTLDevice) -> MTLBuffer?

Creates a remote view of the buffer for another GPU in the same peer group.


var remoteStorageBuffer: MTLBuffer?

The buffer on another GPU that the buffer was created from, if any.



Inherits From