• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simplifies Metal setup. Can be used for graphics shader, realtime video processing and image processing, like computation of two image difference.

GPUOperator

Swift Version Carthage 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

Graphics Shader

Implementation

Thundershower Fourier Series
thundershower Fourier
Reference Concept

Realtime Video Processing

Implementation

Edge Detection Color Conversion
camera monochrome
Prewitt Monochrome

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

github "horita-yuya/GPUOperator"

License

GPUOperator is available under the MIT license.