A collection of Metal shader functions.


protocol MTLLibrary


A MTLLibrary object contains Metal shading language source code that is compiled during the app build process or at runtime from a text string.

Don’t implement this protocol yourself; instead, use the library creation methods provided by the MTLDevice protocol. To create a MTLLibrary from a pre-compiled Metal library binary, call one of these MTLDevice methods:

To create a MTLLibrary by compiling source code at runtime, call one of these MTLDevice methods:


Querying Library Contents

var functionNames: [String]

The names of all public functions in the library.


Finding Shader Functions

func makeFunction(name: String) -> MTLFunction?

Returns an object representing a shader function in the library.


func makeFunction(name: String, constantValues: MTLFunctionConstantValues) -> MTLFunction

Synchronously generates a specialized shader function.


Identifying the Library

var device: MTLDevice

The device object that created the library.


var label: String?

A string that identifies the library.


Handling Errors

struct MTLLibraryError

Errors returned by Metal for library errors.

enum MTLLibraryError.Code

Error codes returned by Metal for library errors.

let MTLLibraryErrorDomain: String

The error domain for Metal libraries.


Inherits From

See Also


Understanding the Metal Shading Language Filename Extension

Use the .metal filename extension to gain access to Metal's build, profile, and debug tools.

Building a Library with Metal's Command-Line Tools

Use command-line tools to run the Metal compiler toolchain.

class MTLCompileOptions

Compilation settings for a Metal shader library.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software