Latest 0.1.0
Homepage https://github.com/horita-yuya/GPUOperator
License MIT
Platforms ios 12.0
Authors

Swift
Version
License
Platform

Metal is used for rendering graphics and performing parallel computations like image processing. But,

there are many boiler plates to use Metal normally.

  • Creating Metal device
  • Creating command queue
  • Creating library, taking caring for which bundle includes it.
  • PipelineState, CommandBuffer, Threadgroup ……… :scream:

This library simplifies these steps.

Examples

Shader: Thundershower

Implementation

thundershower

Realtime Video Processing: Edge Detection

Implementation

camera

  • Using Prewitt filter
  • Same as this.

Image Processing: Two Images Difference

Implementation

Before After Difference

This infers

  • Bold font was changed to regular font.
  • The last processors. word was removed.
  • The border disappeared.

Getting Started

1. Define kernel function and kernel class

#include <metal_stdlib>
using namespace metal;

kernel void monochrome(texture2d<half, access::read_write> dest [[ texture(0) ]],
                       texture2d<half, access::read_write> source [[ texture(1) ]],
                       uint2 gid [[ thread_position_in_grid ]]) {
    half3 color = source.read(gid).rgb;
    half max_color = fmax3(color.r, color.g, color.b);

    dest.write(half4(half3(max_color), 1), gid);
}
import GPUOperator

final class Monochrome: Kernel {
    static let functionName: String = "monochrome"
}

2. Setup GPUOperator instance

let gpuOperator = try? GPUOperator()
try? gpuOperator?.install(kernel: Monochrome())

3. Configure Rendering Process

let renderingView = RenderingView(frame: .zero)
renderingView.gpuOperator = gpuOperator
renderingView.run()

The run() function just starts displaylink process inside RenderingView.
If you want to use MTLView (Metal Standard class), following example would be helpful.

import MetalKit
extension ViewController: MTKViewDelegate {
    func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}

    func draw(in view: MTKView) {
        autoreleasepool {
            guard let drawable = view.currentDrawable else { return }
            gpuOperator?.commit(drawable: drawable)
        }
    }
}

Requirements

  • Swift 5.0
  • iOS 12.0+
  • Any simulators are unsupported.

Installation

Cocoapods

pod 'GPUOperator'

Carthage

Coming soon.

License

GPUOperator is available under the MIT license.

Latest podspec

{
    "name": "GPUOperator",
    "version": "0.1.0",
    "summary": "Simple wrapper for using Metal easily.",
    "description": "Simple wrapper for using Metal easily.nCan run graphics shader and parallel computations without boiler plates.",
    "homepage": "https://github.com/horita-yuya/GPUOperator",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "horita-yuya": "[email protected]"
    },
    "source": {
        "git": "https://github.com/horita-yuya/GPUOperator.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "12.0"
    },
    "source_files": "Sources/**/*.{swift,metal}",
    "swift_version": "5.0"
}

Pin It on Pinterest

Share This