Last active
October 9, 2018 17:26
-
-
Save kunofellasleep/b156204f5f8f2c59f26d74509a4f6319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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