@@ -39,8 +39,7 @@ class MetalMetaballRenderer {
3939
4040 let width = Int(frame.width)
4141 let height = Int(frame.height)
42− let textureDescriptor =
43− MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
42+ let textureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
4443
4544 let texture1 = context.device.newTextureWithDescriptor(textureDescriptor)
4645 activeComputeContext = MTLComputeContext(size: targetView.size, texture: texture1)
@@ -55,7 +54,8 @@ class MetalMetaballRenderer {
5554
5655 state = .Ending
5756
58− dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.rawValue), 0)) { () -> Void in
57+ let userInteractiveQueue = dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.rawValue), 0)
58+ dispatch_async(userInteractiveQueue) { () -> Void in
5959
6060 let computeContext = self.activeComputeContext
6161 swap(&self.activeComputeContext, &self.idleComputeContext)
@@ -74,10 +74,10 @@ class MetalMetaballRenderer {
7474
7575 dispatch_semaphore_signal(self.semaphore)
7676
77− dispatch_async(dispatch_get_main_queue(), { () -> Void in
77+ dispatch_async(dispatch_get_main_queue()) { () -> Void in
7878 // Send image to view
7979 self.targetView.image = uiimage
80− })
80+ }
8181 }
8282 }
8383
@@ -94,8 +94,7 @@ class MetalMetaballRenderer {
9494
9595 // Configure info for computing
9696 let threadGroupCounts = MTLSizeMake(8, 8, 1)
97− let threadGroups = MTLSizeMake(width / threadGroupCounts.width,
98− height / threadGroupCounts.height, 1)
97+ let threadGroups = MTLSizeMake(width / threadGroupCounts.width, height / threadGroupCounts.height, 1)
9998
10099 // Send commands to metal and render
101100 let commandBuffer = context.commandQueue.commandBuffer()
@@ -113,7 +112,6 @@ class MetalMetaballRenderer {
113112
114113 commandBuffer.commit()
115114 commandBuffer.waitUntilCompleted()
116−
117115 } catch _ {
118116 print("Error!")
119117 }
@@ -123,8 +121,7 @@ class MetalMetaballRenderer {
123121 let texture = computeContext.texture
124122
125123 // Get image info
126− let imageSize = CGSizeMake(CGFloat(texture.width),
127− CGFloat(texture.height))
124+ let imageSize = CGSizeMake(CGFloat(texture.width), CGFloat(texture.height))
128125 let width = Int(imageSize.width)
129126 let height = Int(imageSize.height)
130127 let imageByteCount = width * height * 4
@@ -137,27 +134,20 @@ class MetalMetaballRenderer {
137134 }
138135
139136 // Transfer texture info into image buffer
140− texture.getBytes(computeContext.imageBuffer, bytesPerRow: bytesPerRow,
141− fromRegion: region, mipmapLevel: 0)
137+ texture.getBytes(computeContext.imageBuffer, bytesPerRow: bytesPerRow, fromRegion: region, mipmapLevel: 0)
142138
143139 // Get info for UIImage
144− let provider = CGDataProviderCreateWithData(nil, computeContext.imageBuffer,
145− imageByteCount, nil)
140+ let provider = CGDataProviderCreateWithData(nil, computeContext.imageBuffer, imageByteCount, nil)
146141 let bitsPerComponent = 8
147142 let bitsperPixel = 32
148143 let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
149− let bitmapInfo = CGBitmapInfo(rawValue:
150− CGImageAlphaInfo.PremultipliedLast.rawValue |
151− CGBitmapInfo.ByteOrder32Big.rawValue)
144+ let bitmapInfoRaw = CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue
145+ let bitmapInfo = CGBitmapInfo(rawValue: bitmapInfoRaw)
152146 let renderingIntent = CGColorRenderingIntent.RenderingIntentDefault
153147
154148 // Create UIImage from image buffer
155− let imageRef = CGImageCreate(width, height,
156− bitsPerComponent, bitsperPixel, bytesPerRow,
157− colorSpaceRef, bitmapInfo, provider,
158− nil, false, renderingIntent)
159− let image = UIImage(CGImage: imageRef!, scale: 0.0,
160− orientation: UIImageOrientation.Up)
149+ let imageRef = CGImageCreate(width, height, bitsPerComponent, bitsperPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, nil, false, renderingIntent)
150+ let image = UIImage(CGImage: imageRef!, scale: 0.0, orientation: .Up)
161151
162152 return image
163153 }
@@ -165,7 +155,9 @@ class MetalMetaballRenderer {
165155 func metaballEdgesBuffer() -> MTLBuffer {
166156 let floats = dataSource.metaballGraph.adjacencyMatrix.buffer
167157
168− let buffer = context.device.newBufferWithBytes(floats, length: floats.count * sizeof(Float), options: .StorageModeShared)
158+ let bufferLength = floats.count * sizeof(Float)
159+ let device = context.device
160+ let buffer = device.newBufferWithBytes(floats, length: bufferLength, options: .StorageModeShared)
169161
170162 return buffer
171163 }
@@ -175,7 +167,11 @@ class MetalMetaballRenderer {
175167 // Exclude metaballs far from the view's bounds
176168 let border: CGFloat = 100
177169 let metaballs = self.dataSource.metaballs
178− var floats: [Float] = metaballs.reduce([Float](count: 5, repeatedValue: 0)) { (var array, metaball) -> [Float] in
170+
171+ var floats = [Float](count: 5, repeatedValue: 0)
172+ floats = metaballs.reduce(floats) {
173+ (var array, metaball) -> [Float] in
174+
179175 let x = CGFloat(metaball.midX)
180176 let y = CGFloat(metaball.midY)
181177 if -border < x ≤ targetView.width + border &&
@@ -196,8 +192,10 @@ class MetalMetaballRenderer {
196192 floats[0] = Float(floats.count) / 5
197193
198194 // Create buffer
199− let buffer = context.device.newBufferWithBytes(floats, length: floats.count * sizeof(Float), options: .StorageModeShared)
200−
195+ let bufferLength = floats.count * sizeof(Float)
196+ let device = context.device
197+ let buffer = device.newBufferWithBytes(floats, length: bufferLength, options: .StorageModeShared)
198+
201199 return buffer
202200 }
203201}