git.iko.soy/metal-metaballs
on master help sign in
Commented the metal renderer.
f7ae50cvinivendra
PixelImage.xcodeproj/project.xcworkspace/xcuserdata/vini.xcuserdatad/UserInterfaceState.xcuserstate

Binary file not shown.

PixelImage/MetalMetaballRenderer.swift+14−9
@@ -66,7 +66,7 @@ class MetalMetaballRenderer: MetaballRenderer {
66 // Create up-to-date metaball buffer
66 // Create up-to-date metaball buffer
67 let metaballInfoTexture = metaballTexture(metaballs)
67 let metaballInfoTexture = metaballTexture(metaballs)
68
68
69− //
69+ // Send commands to metal and render
70 let commandBuffer = context.commandQueue.commandBuffer()
70 let commandBuffer = context.commandQueue.commandBuffer()
71
71
72 let commandEncoder = commandBuffer.computeCommandEncoder()
72 let commandEncoder = commandBuffer.computeCommandEncoder()
@@ -84,28 +84,32 @@ class MetalMetaballRenderer: MetaballRenderer {
84 print("Error!")
84 print("Error!")
85 }
85 }
86
86
87+ // Transform new metal texture into image
87 targetView.image = image(texture: renderingTexture)
88 targetView.image = image(texture: renderingTexture)
88 }
89 }
89
90
90 func image(texture texture: MTLTexture) -> UIImage {
91 func image(texture texture: MTLTexture) -> UIImage {
92+ // Get image info
91 let imageSize = CGSizeMake(CGFloat(texture.width),
93 let imageSize = CGSizeMake(CGFloat(texture.width),
92 CGFloat(texture.height))
94 CGFloat(texture.height))
93 let width = Int(imageSize.width)
95 let width = Int(imageSize.width)
94 let height = Int(imageSize.height)
96 let height = Int(imageSize.height)
95 let imageByteCount = width * height * 4
97 let imageByteCount = width * height * 4
98+ let bytesPerRow = width * 4
99+ let region = MTLRegionMake2D(0, 0, width, height)
96
100
101+ // Allocate the buffer if needed
97 if imageBuffer == nil {
102 if imageBuffer == nil {
98 imageBuffer = malloc(imageByteCount)
103 imageBuffer = malloc(imageByteCount)
99 }
104 }
100
105
101− let bytesPerRow = width * 4
106+ // Transfer texture info into image buffer
102− let region = MTLRegionMake2D(0, 0, width, height)
103 texture.getBytes(imageBuffer, bytesPerRow: bytesPerRow,
107 texture.getBytes(imageBuffer, bytesPerRow: bytesPerRow,
104 fromRegion: region, mipmapLevel: 0)
108 fromRegion: region, mipmapLevel: 0)
105
109
110+ // Get info for UIImage
106 let provider = CGDataProviderCreateWithData(nil, imageBuffer,
111 let provider = CGDataProviderCreateWithData(nil, imageBuffer,
107 imageByteCount, nil)
112 imageByteCount, nil)
108−
109 let bitsPerComponent = 8
113 let bitsPerComponent = 8
110 let bitsperPixel = 32
114 let bitsperPixel = 32
111 let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
115 let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
@@ -113,6 +117,8 @@ class MetalMetaballRenderer: MetaballRenderer {
113 CGImageAlphaInfo.PremultipliedLast.rawValue |
117 CGImageAlphaInfo.PremultipliedLast.rawValue |
114 CGBitmapInfo.ByteOrder32Big.rawValue)
118 CGBitmapInfo.ByteOrder32Big.rawValue)
115 let renderingIntent = CGColorRenderingIntent.RenderingIntentDefault
119 let renderingIntent = CGColorRenderingIntent.RenderingIntentDefault
120+
121+ // Create UIImage from image buffer
116 let imageRef = CGImageCreate(width, height,
122 let imageRef = CGImageCreate(width, height,
117 bitsPerComponent, bitsperPixel, bytesPerRow,
123 bitsPerComponent, bitsperPixel, bytesPerRow,
118 colorSpaceRef, bitmapInfo, provider,
124 colorSpaceRef, bitmapInfo, provider,
@@ -124,21 +130,22 @@ class MetalMetaballRenderer: MetaballRenderer {
124 }
130 }
125
131
126 func metaballTexture(metaballs: [Metaball]) -> MTLTexture {
132 func metaballTexture(metaballs: [Metaball]) -> MTLTexture {
133+ // Create Float array for buffer
127 let floats: [Float] = metaballs.reduce([Float(metaballs.count)]) { (var array, metaball) -> [Float] in
134 let floats: [Float] = metaballs.reduce([Float(metaballs.count)]) { (var array, metaball) -> [Float] in
128 array.append(Float(metaball.midX))
135 array.append(Float(metaball.midX))
129 array.append(Float(metaball.midY))
136 array.append(Float(metaball.midY))
130 return array
137 return array
131 }
138 }
132− let size = floats.count
133
139
140+ // Create texture
141+ let size = floats.count
134 let textureDescriptor =
142 let textureDescriptor =
135 MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(
143 MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(
136 .R32Float, width: size, height: 1, mipmapped: false)
144 .R32Float, width: size, height: 1, mipmapped: false)
137−
138 let texture = context.device.newTextureWithDescriptor(textureDescriptor)
145 let texture = context.device.newTextureWithDescriptor(textureDescriptor)
139
146
147+ // Copy array info into texture
140 let region = MTLRegionMake2D(0, 0, size, 1)
148 let region = MTLRegionMake2D(0, 0, size, 1)
141−
142 texture.replaceRegion(region, mipmapLevel: 0, withBytes: floats,
149 texture.replaceRegion(region, mipmapLevel: 0, withBytes: floats,
143 bytesPerRow: sizeof(Float) * size)
150 bytesPerRow: sizeof(Float) * size)
144
151
@@ -155,6 +162,4 @@ class MetalMetaballRenderer: MetaballRenderer {
155 deinit {
162 deinit {
156 free(imageBuffer)
163 free(imageBuffer)
157 }
164 }
158−
159}
165}
160−