A lightweight Swift Package for loading and executing Metal compute functions with a fluent Swift API.
MetalFunction handles Metal library lookup, compute pipeline creation, argument binding, thread dispatch, command-buffer completion, and pipeline caching.
Create a MetalFunction, bind its arguments in kernel parameter order, and execute it:
let values = ...
try await MetalFunction(name: "add_constant", bundle: .module)
.buffer(values)
.bytes(5 as Float)
.execute(width: values.count)The buffer, bytes, and execute calls correspond to the Metal function's argument order and execution grid.
MetalFunction supports compile-time Metal function constants by specialize the function when creating it:
try await MetalFunction(name: "add_const", bundle: .module, constants: [.bool(true)])
.buffer(values)
.execute(width: values.count)Supported constant types include Bool, UInt32, Int32, UInt8, and Float.
MetalFunction(name:bundle:constants:)loads and specializes a Metal function.buffer(_:)binds anMTLBufferor mutable buffer pointer.buffer(copying:)creates a Metal buffer by copying Swift values.bytes(_:)binds a value or array as constant bytes.execute(width:height:depth:)dispatches the compute grid and awaits completion.MetalFunction.isSupportedchecks whether the current device is supported.
A MetalFunction instance represents a single command encoding. Create a new instance for each execution. Compiled functions and pipeline states are cached by bundle, function name, and specialization constants.
MetalFunction uses Swift Package Manager. Add a dependency in Package.swift:
dependencies: [
.package(url: "https://github.com/Vaida12345/MetalFunction.git", from: "1.1.0")
]and then adding the appropriate module to your target dependencies.
You can add this framework as a dependency to your Xcode project by clicking File -> Swift Packages -> Add Package Dependency. The package is located at:
https://github.com/Vaida12345/MetalFunction.git
This package uses DocC for documentation. View on Github Pages.