@@ -9,14 +9,6 @@
9import Metal
9import Metal
10import UIKit
10import UIKit
13− var texture: MTLTexture
15− init(texture: MTLTexture) {
16− self.texture = texture
20class MetalMetaballRenderer: MetaballRenderer {
12class MetalMetaballRenderer: MetaballRenderer {
22 typealias TargetView = UIImageView
14 typealias TargetView = UIImageView
@@ -30,7 +22,7 @@ class MetalMetaballRenderer: MetaballRenderer {
30 var previousSize = CGSize.zero
22 var previousSize = CGSize.zero
32 let context = MTLContext()
24 let context = MTLContext()
33− var renderingTexture: TextureRef!
25+ var renderingTexture: MTLTexture!
34 var imageBuffer: UnsafeMutablePointer<Void>!
26 var imageBuffer: UnsafeMutablePointer<Void>!
36 let dataSource: MetaballDataSource
28 let dataSource: MetaballDataSource
@@ -50,31 +42,17 @@ class MetalMetaballRenderer: MetaballRenderer {
50 updateFrame()
42 updateFrame()
61 dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.rawValue), 0)) { () -> Void in
45 dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.rawValue), 0)) { () -> Void in
63 // Render graphics to metal texture
47 // Render graphics to metal texture
64 self.renderToRenderingTexture()
48 self.renderToRenderingTexture()
65− self.metalIsBusy = false
67 // Transform metal texture into image
50 // Transform metal texture into image
68− let uiimage = self.image(texture: self.renderingTexture.texture)
51+ let uiimage = self.createImageFromTexture()
70 dispatch_async(dispatch_get_main_queue(), { () -> Void in
53 dispatch_async(dispatch_get_main_queue(), { () -> Void in
71 // Send image to view
54 // Send image to view
72 self.targetView.image = uiimage
55 self.targetView.image = uiimage
74− // If the image is dirty, update it
76− self.updateTargetView()
@@ -87,7 +65,7 @@ class MetalMetaballRenderer: MetaballRenderer {
87 let textureDescriptor =
65 let textureDescriptor =
88 MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
66 MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
90− renderingTexture = TextureRef(texture: context.device.newTextureWithDescriptor(textureDescriptor))
68+ renderingTexture = context.device.newTextureWithDescriptor(textureDescriptor)
93 let kernelFunction: MTLFunction?
71 let kernelFunction: MTLFunction?
@@ -107,7 +85,7 @@ class MetalMetaballRenderer: MetaballRenderer {
108 let commandEncoder = commandBuffer.computeCommandEncoder()
86 let commandEncoder = commandBuffer.computeCommandEncoder()
109 commandEncoder.setComputePipelineState(pipeline)
87 commandEncoder.setComputePipelineState(pipeline)
110− commandEncoder.setTexture(renderingTexture.texture, atIndex: 0)
88+ commandEncoder.setTexture(renderingTexture, atIndex: 0)
111 let metaballInfoBuffer = metaballBuffer()
89 let metaballInfoBuffer = metaballBuffer()
112 commandEncoder.setBuffer(metaballInfoBuffer, offset: 0, atIndex: 0)
90 commandEncoder.setBuffer(metaballInfoBuffer, offset: 0, atIndex: 0)
113 commandEncoder.dispatchThreadgroups(threadGroups,
91 commandEncoder.dispatchThreadgroups(threadGroups,
@@ -122,7 +100,9 @@ class MetalMetaballRenderer: MetaballRenderer {
125− func image(texture texture: MTLTexture) -> UIImage {
103+ func createImageFromTexture() -> UIImage {
104+ let texture = renderingTexture
126 // Get image info
106 // Get image info
127 let imageSize = CGSizeMake(CGFloat(texture.width),
107 let imageSize = CGSizeMake(CGFloat(texture.width),
128 CGFloat(texture.height))
108 CGFloat(texture.height))