MTLBuffer
A resource that stores data in a format defined by your app.
Declaration
protocol MTLBuffer : MTLResourceMentioned in
- Improving CPU performance by using argument buffers
- Estimating how often a texture region is accessed
- Setting resource storage modes
- Converting a GPU’s counter data into a readable format
- Synchronizing a managed resource in macOS
- Improving ray-tracing data access using per-primitive data
- Indexing argument buffers
- Optimizing texture data
- Simplifying GPU resource management with residency sets
- Specifying drawing and dispatch arguments indirectly
Overview
An MTLBuffer instance 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 instances:
makeBuffer(length:options:) creates a
MTLBufferinstance with a new storage allocation.makeBuffer(bytes:length:options:) creates a
MTLBufferinstance by copying data from an existing storage allocation into a new allocation.makeBuffer(bytesNoCopy:length:options:deallocator:) creates a
MTLBufferinstance that reuses an existing storage allocation and does not allocate any new storage.
The Metal framework doesn’t know anything about the contents of an 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 need to call didModifyRange: to tell Metal to copy any changes to the GPU.