GPUOperator 0.1.0

GPUOperator 0.1.0

Maintained by h-yuya.



  • By
  • horita-yuya

GPUOperator

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 ......... 😱

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.

License

GPUOperator is available under the MIT license.