MetalTools provides a convenient, Swifty way of working with Metal. This library is heavily used in computer vision startups ZERO10 and Prisma.
Please see the package's documentation for the detailed usage instructions.
MTLContext
is a central component of the MetalTools framework, designed to streamline Metal-based operations. It encapsulates an MTLDevice
and an MTLCommandQueue
, providing a unified interface for common Metal tasks.
import MetalTools
do {
let context = try MTLContext()
// Use the context for further operations
} catch {
print("Failed to create MTLContext: \(error)")
}
See how you can group encodings with Swift closures.
self.context.scheduleAndWait { buffer in
buffer.compute { encoder in
// compute command encoding logic
}
buffer.blit { encoder in
// blit command encoding logic
}
}
encoder.setTextures(source, destination)
encoder.setValue(affineTransform, at: 0)
let texture = try context.texture(
from: cgImage,
usage: [.shaderRead, .shaderWrite]
)
let encoder = JSONEncoder()
let data = try encoder.encode(texture.codable())
let decoder = JSONDecoder()
let decodableTexture = try decoder.decode(MTLTextureCodableBox.self, from: data)
let decodedTexture = try decodableTexture.texture(device: self.context.device)
let library = context.library(for: Foo.self)
let computePipelineState = try lib.computePipelineState(function: "brightness")
let buffer = context.buffer(
for: InstanceUniforms.self,
count: 99,
options: .storageModeShared
)
let renderPipelineDescriptor = MTLRenderPipelineDescriptor()
renderPipelineDescriptor.colorAttachments[0].setup(blending: .alpha)
- Ready-to-use compute kernels
- Simple geometry renderers
- Create multi-sample render target pairs
- Create depth buffers
- Create depth / stencil states
- and more!
MetalTools is licensed under MIT license.