Hi all,
I am trying to practice metal coding in video stream in iOS 10.2 and my xCode version is 8.2.1. Unfortunately, I am suffering the error message as "Execution of the command buffer was aborted due to an error during execution. Caused GPU Hang Error (IOAF code 3)". I already set the camera capture session as "AVCaptureSessionPreset640x480". I would like to know if there is anyone who could find the workaround solution?
my sample code is as below.
required init(frame frameRect: CGRect) {
super.init(frame: frameRect, device: MTLCreateSystemDefaultDevice())
self.device = device
defaultLibrary = device!.newDefaultLibrary()
commandQueue = device!.makeCommandQueue()
let kernalFunction = defaultLibrary.makeFunction(name: "ObjectFilter")
do{
pipelineState = try device!.makeComputePipelineState(function: kernalFunction!)
}
catch{
fatalError("Unable to create pipeline state")
}
threadsPerGroup = MTLSizeMake(8,8,1)
}
override func draw(_ rect: CGRect) {
guard let drawable = currentDrawable else
{
return
}
let commandBuffer = commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
commandEncoder.setComputePipelineState(pipelineState)
commandEncoder.setTexture(self.cameraTexture, at: 0)
if(self.cameraTexture != nil){
numThreadgroups = MTLSizeMake((self.cameraTexture?.width)!/threadsPerGroup.width, (self.cameraTexture?.height)!/threadsPerGroup.height, 1)
}else{
numThreadgroups = MTLSizeMake(2048 / threadsPerGroup.width, 1536 / threadsPerGroup.height, 1)
}
commandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup)
commandEncoder.endEncoding()
let destinationTexture = drawable.texture
if self.cameraTexture != nil{
sobelFilter.encode(to: commandBuffer, sourceTexture: self.cameraTexture!, destinationTexture: destinationTexture)
}
commandBuffer.present(drawable)
commandBuffer.commit();
}
kernel void ObjectFilter(texture2d<float, access::read> inputTexture [[texture(0)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]]){
float4 inputColor = inputTexture.read(gid);
float4 luminanceWeights = float4(0.299, 0.587, 0.114, 0);
float luminance = dot(inputColor, luminanceWeights);
float4 gray = float4(luminance, luminance, luminance, 1.0);
outTexture.write( float4(gray.rgb, 1.0), gid);
}