A Core Animation layer that Metal can render into, typically to be displayed onscreen.
- iOS 8.0+
- macOS 10.11+
- Mac Catalyst 13.0+
- tvOS 9.0+
- Core Animation
CAMetal when you want to use Metal to render a layer’s contents; for example, to render into a view. Consider using
MTKView instead, because this class automatically wraps a
CAMetal object and provides a higher-level abstraction.
Adjust the layer’s properties to configure its underlying pixel format and other display behaviors.
Rendering the Layer's Contents
CAMetal creates a pool of Metal drawable objects (
CAMetal). At any given time, one of these drawable objects contains the contents of the layer. To change the layer’s contents, ask the layer for a drawable object, render into it, and then update the layer’s contents to point to the new drawable.
Call the layer’s
next method to obtain a drawable object. Get the drawable object’s texture and create a render pass that renders to that texture, as shown in the code below:
To change the layer’s contents to the new drawable, call the
present(_:) method (or one of its variants) on the command buffer containing the encoded render pass, passing in the drawable object to present.
Keeping References to Drawables
The layer reuses a drawable only if it isn’t onscreen and there are no strong references to it. Further, if a drawable isn’t available when you call
next, the system waits for one to become available. To avoid stalls in your app, request a new drawable only when you need it, and release any references to it as quickly as possible after you’re done with it.
For example, before retrieving a new drawable, you might perform other work on the CPU or submit commands to the GPU that don’t require the drawable. Then, obtain the drawable and encode a command buffer to render into it, as described above. After you commit this command buffer, release all strong references to the drawable. If you don’t release drawables correctly, the layer runs out of drawables, and future calls to
Releasing the Drawable
Don’t release the drawable explicitly; instead, embed your render loop within an autorelease pool block:
This block releases drawables promptly and avoids possible deadlock situations with multiple drawables. Release drawables as soon as possible after committing your onscreen render pass.