All Projects → s1ddok → Alloy

s1ddok / Alloy

Licence: mit
Make usage of Metal API a pleasure

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Alloy

Pycaret
An open-source, low-code machine learning library in Python
Stars: ✭ 4,594 (+2480.9%)
Mutual labels:  gpu, ml
Tvm
Open deep learning compiler stack for cpu, gpu and specialized accelerators
Stars: ✭ 7,494 (+4110.11%)
Mutual labels:  metal, gpu
Ultralight
Next-generation HTML renderer for apps and games
Stars: ✭ 3,585 (+1914.04%)
Mutual labels:  metal, gpu
rindow-neuralnetworks
Neural networks library for machine learning on PHP
Stars: ✭ 37 (-79.21%)
Mutual labels:  gpu, ml
Shaderview
A library for simple shader programming
Stars: ✭ 87 (-51.12%)
Mutual labels:  metal, gpu
Atlas
An Open Source, Self-Hosted Platform For Applied Deep Learning Development
Stars: ✭ 259 (+45.51%)
Mutual labels:  gpu, ml
Toypathtracer
Toy path tracer for my own learning purposes (CPU/GPU, C++/C#, Win/Mac/Wasm, DX11/Metal, also Unity)
Stars: ✭ 753 (+323.03%)
Mutual labels:  metal, gpu
LuisaRender
High-Performance Multiple-Backend Renderer Based on LuisaCompute
Stars: ✭ 47 (-73.6%)
Mutual labels:  metal, gpu
Videngine
3D graphics for iOS using Metal and Swift
Stars: ✭ 86 (-51.69%)
Mutual labels:  metal, gpu
Metalpetal
A GPU accelerated image and video processing framework built on Metal.
Stars: ✭ 907 (+409.55%)
Mutual labels:  metal, gpu
Occa
JIT Compilation for Multiple Architectures: C++, OpenMP, CUDA, HIP, OpenCL, Metal
Stars: ✭ 230 (+29.21%)
Mutual labels:  metal, gpu
Remotery
Single C file, Realtime CPU/GPU Profiler with Remote Web Viewer
Stars: ✭ 1,908 (+971.91%)
Mutual labels:  metal, gpu
Gfx
[maintenance mode] A low-overhead Vulkan-like GPU API for Rust.
Stars: ✭ 5,045 (+2734.27%)
Mutual labels:  metal, gpu
Wheels
Performance-optimized wheels for TensorFlow (SSE, AVX, FMA, XLA, MPI)
Stars: ✭ 891 (+400.56%)
Mutual labels:  gpu, ml
Yudisplacementtransition
A GPU accelerated transition library makes use of displacement maps to create distortion effects.
Stars: ✭ 121 (-32.02%)
Mutual labels:  metal, gpu
Prophecis
Prophecis is a one-stop cloud native machine learning platform.
Stars: ✭ 156 (-12.36%)
Mutual labels:  gpu, ml
Gmonitor
gmonitor is a GPU monitor (Nvidia only at the moment)
Stars: ✭ 169 (-5.06%)
Mutual labels:  gpu
Pai
Resource scheduling and cluster management for AI
Stars: ✭ 2,223 (+1148.88%)
Mutual labels:  gpu
Creepminer
Burstcoin C++ CPU and GPU Miner
Stars: ✭ 169 (-5.06%)
Mutual labels:  gpu
Floor
A C++ Compute/Graphics Library and Toolchain enabling same-source CUDA/Host/Metal/OpenCL/Vulkan C++ programming and execution.
Stars: ✭ 166 (-6.74%)
Mutual labels:  metal

Alloy

Alloy is a tiny set of utils and extensions over Apple's Metal framework dedicated to make your Swift GPU code much cleaner and let you prototype your pipelines faster.

While this library doesn't introduce any new paradigms or concepts that significantly change the way you approach your Metal implementations, it has some optional-to-use things that you can incorporate in your apps if you find them as useful as library author did :)

  • Nano-tiny layer over vanilla Metal API
  • No external dependencies
  • Cross-platform support
  • Very Swifty

Usage examples:

Combine the power of CoreGraphics and Metal by sharing resource memory

Okay, let me see what's up

First of all, this framework provides a set of utils, that hides the majority of redudant explicity in your Metal code, while not limiting a flexibility a bit. You can easily mix Alloy and vanilla Metal code.

The only new concept that Alloy introduces is MTLContext. Internally this is meant to store objects that are usually being shared and injected across your app.

In particular, this is:

  • device: MTLDevice
  • commandQueue: MTLCommandQueue
  • standardLibrary: MTLLibrary?

Internally, it also manages a MTKTextureLoader and a cache of MTLLibraries, but this logic should be considered private. As of now, MTLContext is not threadsafe.

MTLContext usually being injected as a dependency to any object that interacts with Metal devices.

It can do a bunch of things for you, few examples:

Easily create textures from CGImage

let texture = context.texture(from: cgImage,
                              usage: [.shaderRead, .shaderWrite])

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
    }
}

Load a compute pipeline state for a function that sits in a framework

let lib = context.shaderLibrary(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)

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)

Other things

  • Create multi-sample render target pairs
  • Create textures
  • Create depth buffers
  • Create depth/stencil states
  • etc

Other Alloy-specific types

Other types that are introduces my Alloy are

  • MTLOffscreenRenderer: this is a class that lets you create simple off-screen renderers to draw something into arbitary MTLTextures
  • ComputeCommand: this is an experimental class that does a reflection over Metal kernels and lets you assign arguments by name instead of index. This is a subject for improvements.
  • BlendingMode: this type contains the enumeration of eight Alloy's built-in blending modes. You can easily setup one of them just by calling setup(blending:) function.
    let renderPipelineDescriptor = MTLRenderPipelineDescriptor()
    renderPipelineDescriptor.colorAttachments[0].setup(blending: .alpha)
    

MTLContext minimal usage example

MTLContext is usually being injected in the class, as you usually do with MTLDevice, you should cache the context and all heavy-weighted objects so you can reuse them lates, i.e.:

import Alloy

public class BrightnessEncoder {
    public let context: MTLContext
    fileprivate let pipelineState: MTLComputePipelineState

    /**
     * This variable controls the brightness factor. Should be in range of -1.0...1.0
     */
    public var intensity: Float = 1.0

    public init(context: MTLContext) {
        self.context = context

        guard let lib = context.shaderLibrary(for: BrightnessEncoder.self),
              let state = try? lib.computePipelineState(function: "brightness")
        else { fatalError("Error during shader loading") }

        self.pipelineState = state
    }

    public func encode(input: MTLTexture,
                       in commandBuffer: MTLCommandBuffer) {
        commandBuffer.compute { encoder in
            encoder.set(textures: [input])
            encoder.set(self.intensity, at: 0)

            encoder.dispatch2d(state: self.pipelineState,
                               covering: input.size)
        }
    }

}

Note how simple it is to kick off a kernel with Alloy, no more tedious thredgroup size calculations, multiple encoder initialization with balancing .endEncoding() calls.

Then somewhere else you just do

context.scheduleAndWait { buffer in
    self.brightnessEncoder.intensity = sender.floatValue
    self.brightnessEncoder.encode(input: texture,
                                  in: buffer)

    // For Mac applications
    if case .managed = texture.storageMode {
        buffer.blit { encoder in
            encoder.synchronize(resource: texture)
        }
    }
}

With this approach you can easily stack and build your GPU pipeline layers, group blit, compute and render command encodings with Swift closures, while maintaing full flexibility of Metal API.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alloy into your Xcode project using CocoaPods, specify it in your Podfile:

# Optionally add version, i.e. '~> 0.9.0'
pod 'Alloy'

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].