Skip to content

computer-graphics-tools/metal-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetalTools

Platform Compatibility Swift Version

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

License

MetalTools is licensed under MIT license.