Skip to content

Instantly share code, notes, and snippets.

@kunofellasleep
Last active October 9, 2018 17:26
Show Gist options
  • Save kunofellasleep/b156204f5f8f2c59f26d74509a4f6319 to your computer and use it in GitHub Desktop.
Save kunofellasleep/b156204f5f8f2c59f26d74509a4f6319 to your computer and use it in GitHub Desktop.
public func Run(_ image:UIImage) -> UIImage{
//GPUで実行されるコマンドを格納するコンテナ
let buffer = commandQueue.makeCommandBuffer()
//コマンドを作成し、コマンドバッファに追加する(エンコード)
let encoder = buffer?.makeComputeCommandEncoder()
encoder?.setComputePipelineState(pipelineState)
//出力用テクスチャ作成
let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba8Unorm, width:1920, height: 1080, mipmapped: false)
textureDescriptor.usage = [.shaderRead, .shaderWrite]
outTexture = self.device.makeTexture(descriptor: textureDescriptor)
//コマンドバッファにデータを追加
encoder?.setTexture(outTexture, index: 0)
encoder?.setTexture(mtlTexture(from: image), index: 1)
encoder?.dispatchThreadgroups( MTLSizeMake(
Int(ceil(image.size.width / CGFloat(self.threadGroupCount.width))),
Int(ceil(image.size.height / CGFloat(self.threadGroupCount.height))),
1), threadsPerThreadgroup: threadGroupCount)
encoder?.endEncoding()
//コマンドを実行
buffer?.commit()
//完了まで待つ
buffer?.waitUntilCompleted()
//出力を返す
return self.image(from: self.outTexture)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment