[MetaballsKit] Better error handling

This commit is contained in:
Eryn Wells 2017-08-05 09:29:50 -07:00
parent 0c175acc79
commit d08f4a7e23

View file

@ -10,7 +10,7 @@ import Foundation
import MetalKit import MetalKit
public enum MetaballsError: Error { public enum MetaballsError: Error {
case metalError case metalError(String)
} }
public struct Ball { public struct Ball {
@ -194,27 +194,17 @@ public class Field {
return return
} }
self.device = device self.device = device
do { sampleComputeState = try computePipelineStateForSamplingKernel(withDevice: device)
sampleComputeState = try computePipelineStateForSamplingKernel(withDevice: device)
} catch let e {
throw e
}
} }
public func computePipelineStateForSamplingKernel(withDevice device: MTLDevice) throws -> MTLComputePipelineState? { public func computePipelineStateForSamplingKernel(withDevice device: MTLDevice) throws -> MTLComputePipelineState? {
do { let bundle = Bundle(for: type(of: self))
let bundle = Bundle(for: type(of: self)) let library = try device.makeDefaultLibrary(bundle: bundle)
let library = try device.makeDefaultLibrary(bundle: bundle) guard let samplingKernel = library.makeFunction(name: "sampleFieldKernel") else {
guard let samplingKernel = library.makeFunction(name: "sampleFieldKernel") else { throw MetaballsError.metalError("Unable to create sampling kernel function")
return nil
}
let state = try device.makeComputePipelineState(function: samplingKernel)
return state
}
catch let e {
print("\(e)")
throw e
} }
let state = try device.makeComputePipelineState(function: samplingKernel)
return state
} }
public func computeEncoderForSamplingKernel(withDevice device: MTLDevice, commandBuffer buffer: MTLCommandBuffer) throws -> MTLComputeCommandEncoder { public func computeEncoderForSamplingKernel(withDevice device: MTLDevice, commandBuffer buffer: MTLCommandBuffer) throws -> MTLComputeCommandEncoder {
@ -223,7 +213,7 @@ public class Field {
let sampleTexture = makeSampleTextureIfNeeded(withDevice: device), let sampleTexture = makeSampleTextureIfNeeded(withDevice: device),
let state = sampleComputeState let state = sampleComputeState
else { else {
throw MetaballsError.metalError throw MetaballsError.metalError("Missing Metal buffers or compute state")
} }
let encoder = buffer.makeComputeCommandEncoder() let encoder = buffer.makeComputeCommandEncoder()