git.iko.soy/metal-metaballs
on master help sign in
Added context swap (like opengl's buffer swap, but still synchronous).
commit04bc8cdf5bdf9f32e90f618d51a3c5f0a5dcadaf authorvinivendra <vinivendra@gmail.com> dateWed 10 Feb 2016, 21:17 (11 years ago) parent354b432
1 file changed+12−5
PixelImage/MetalMetaballRenderer.swift+12−5
PixelImage/MetalMetaballRenderer.swift+12−5
@@ -18,7 +18,8 @@ class MetalMetaballRenderer {
1818 let targetView: TargetView
1919
2020 let context = MTLContext()
21− var computeContext: MTLComputeContext
21+ var activeComputeContext: MTLComputeContext
22+ var idleComputeContext: MTLComputeContext
2223
2324 let dataSource: MetaballDataSource
2425
@@ -50,8 +51,11 @@ class MetalMetaballRenderer {
5051 let height = Int(frame.height)
5152 let textureDescriptor =
5253 MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
53− let texture = context.device.newTextureWithDescriptor(textureDescriptor)
54− computeContext = MTLComputeContext(size: targetView.size, texture: texture)
54+
55+ let texture1 = context.device.newTextureWithDescriptor(textureDescriptor)
56+ activeComputeContext = MTLComputeContext(size: targetView.size, texture: texture1)
57+ let texture2 = context.device.newTextureWithDescriptor(textureDescriptor)
58+ idleComputeContext = MTLComputeContext(size: targetView.size, texture: texture2)
5559 }
5660
5761 func updateTargetView() {
@@ -62,11 +66,14 @@ class MetalMetaballRenderer {
6266
6367 dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.rawValue), 0)) { () -> Void in
6468
69+ let computeContext = self.activeComputeContext
70+ swap(&self.activeComputeContext, &self.idleComputeContext)
71+
6572 // Render graphics to metal texture
66− self.renderToContext(self.computeContext)
73+ self.renderToContext(computeContext)
6774
6875 // Transform metal texture into image
69− let uiimage = self.createImageFromContext(self.computeContext)
76+ let uiimage = self.createImageFromContext(computeContext)
7077
7178 dispatch_async(dispatch_get_main_queue(), { () -> Void in
7279 // Send image to view