MetalTools 提供了一种方便、Swift 的方式来使用 Metal。该库被广泛应用于计算机视觉初创公司 ZERO10 和 Prisma。
请参考软件包的文档以获取详细的使用说明。
MTLContext
是 MetalTools 框架的核心组件,旨在简化基于 Metal 的操作。它封装了 MTLDevice
和 MTLCommandQueue
,为常见的 Metal 任务提供了一个统一的接口。
import MetalTools
do {
let context = try MTLContext()
// Use the context for further operations
} catch {
print("Failed to create MTLContext: \(error)")
}
了解如何使用 Swift 闭包对编码进行分组。
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)
MetalTools 使用 MIT 许可证。