После нескольких дней напряженной работы мне удалось создать MPSImageGUidedFilter. Ниже приведен код, над которым нужно работать:
import UIKit
import MetalPerformanceShaders import MetalKit
class ViewController: UIViewController {
public var texIn: MTLTexture!
public var coefficient: MTLTexture!
public var context: CIContext!
let device = MTLCreateSystemDefaultDevice()!
var queue: MTLCommandQueue!
var Metalview: MTKView { return view as! MTKView }
override func viewDidLoad() {
super.viewDidLoad()
Metalview.drawableSize.height = 412
Metalview.drawableSize.width = 326
Metalview.framebufferOnly = false
Metalview.device = device
Metalview.delegate = self
queue = device.makeCommandQueue()
let textureLoader = MTKTextureLoader(device: device)
let urlCoeff = Bundle.main.url(forResource: "mask", withExtension: "png")
do {
coefficient = try textureLoader.newTexture(URL: urlCoeff!, options: [:])
} catch {
fatalError("coefficient file not uploaded")
}
let url = Bundle.main.url(forResource: "guide", withExtension: "png")
do {
texIn = try textureLoader.newTexture(URL: url!, options: [:])
} catch {
fatalError("resource file not uploaded")
}
}
}
extension ViewController: MTKViewDelegate {
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
func draw(in view: MTKView) {
guard let commandBuffer = queue.makeCommandBuffer(),
let drawable = view.currentDrawable else {
return
}
let shader = MPSImageGuidedFilter(device: device, kernelDiameter: 5)
shader.epsilon = 0.001
// let shader = MPSImageGaussianBlur (устройство: устройство, сигма: 3)
// let shader = MPSImageSobel (устройство: устройство) // let shader = MPSImageAreaMax (устройство: устройство, kernelWidth: 3, kernelHeight: 3) // let shader = MPSImageAreaMin (устройство: устройство, kernelWidth: 5, kernelHeight: 5) // let shader = MPSImageMedian (устройство: устройство, kernelDiameter: 3) // let shader = MPSImageBox (устройство: устройство, kernelWidth: 9, kernelHeight: 9) // let shader = MPSImageTent (устройство: устройство, kernelWidth: 9, kernelHeight: 9)
let textOut = drawable.texture
shader.encodeReconstruction(to: commandBuffer, guidanceTexture: texIn, coefficientsTexture: coefficient, destinationTexture: textOut)
// шейдер .encode (commandBuffer: commandBuffer, sourceTexture: texIn, destinationTexture: textOut) commandBuffer.present (drawable) commandBuffer.commit ()
}
}