Protocol

MTLBuffer

A memory allocation for storing unformatted data that is accessible to the GPU.

Overview

The MTLBuffer protocol defines the interface for objects that represent an allocation of unformatted, device-accessible memory that can contain any type of data. Your app does not define classes that implement this protocol.

Do not create a MTLBuffer object by using standard allocation or initialization techniques. Instead, use the following MTLDevice methods to create MTLBuffer objects:

On systems with dedicated memory, when buffers are created with a managed resource storage mode (MTLStorageMode.managed), Metal allocates a copy in CPU memory and a second copy in GPU memory. After your application writes are complete, you must call didModifyRange(_:) to inform Metal that the updated contents in the CPU memory copy must be uploaded to the GPU memory copy. No separate allocation is made on systems with integrated memory.

Topics

Creating a Texture That Shares Buffer Data

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

Creates a linear texture that shares its storage with this buffer.

Required.

Locating the Buffer’s Storage

func contents() -> UnsafeMutableRawPointer

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

Required.

Invalidating the Buffer’s Storage

func didModifyRange(Range<Int>)

Informs the device that the CPU has modified a buffer’s range.

Debugging

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.

Required.

Properties

var length: Int

The logical size of the buffer, in bytes.

Required.

Relationships

Inherits From

See Also

Buffers

Basic Buffers

Demonstrates how to manage hundreds of vertices with a vertex buffer.

class MTLPipelineBufferDescriptor

An object that contains the mutability options for buffers used in a render or compute pass.