computer-graphics-tools/metal-tools
A Swift framework that simplifies working with Apple's Metal API.
Description
MetalTools provides a convenient, Swifty way of working with Metal. This library is heavily used in computer vision startups ZERO10 and Prisma.
Usage
Please see the package's documentation for the detailed usage instructions.
MTLContext
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)")
}Dispatch command buffers in both sync/async manner
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
}
}Set resources to Command Encoder
encoder.setTextures(source, destination)
encoder.setValue(affineTransform, at: 0)Easily create textures from CGImage
let texture = try context.texture(
from: cgImage,
usage: [.shaderRead, .shaderWrite]
)Serialize and deserialize MTLTexture
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)Load a compute pipeline state for a function that sits in a framework
let library = context.library(for: Foo.self)
let computePipelineState = try lib.computePipelineState(function: "brightness")Allocate buffer by value type
let buffer = context.buffer(
for: InstanceUniforms.self,
count: 99,
options: .storageModeShared
)Setup blending mode in render passes
let renderPipelineDescriptor = MTLRenderPipelineDescriptor()
renderPipelineDescriptor.colorAttachments[0].setup(blending: .alpha)Other things
- Ready-to-use compute kernels
- Simple geometry renderers
- Create multi-sample render target pairs
- Create depth buffers
- Create depth / stencil states
- and more!
License
MetalTools is licensed under MIT license.
Package Metadata
Repository: computer-graphics-tools/metal-tools
Homepage: https://swiftpackageindex.com/computer-graphics-tools/metal-tools
Stars: 80
Forks: 6
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
Topics: computer-graphics, metal, swift
README: README.md