git.iko.soy/metal-metaballs
on master help sign in
Refactored texture allocation.
22a5e76vinivendra
PixelImage.xcodeproj/project.xcworkspace/xcuserdata/vini.xcuserdatad/UserInterfaceState.xcuserstate

Binary file not shown.

PixelImage.xcodeproj/xcuserdata/vini.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist+3−3
@@ -10,11 +10,11 @@
10 ignoreCount = "0"
10 ignoreCount = "0"
11 continueAfterRunningActions = "No"
11 continueAfterRunningActions = "No"
12 filePath = "PixelImage/MetalMetaballRenderer.swift"
12 filePath = "PixelImage/MetalMetaballRenderer.swift"
13− timestampString = "476814708.217603"
13+ timestampString = "476821456.932302"
14 startingColumnNumber = "9223372036854775807"
14 startingColumnNumber = "9223372036854775807"
15 endingColumnNumber = "9223372036854775807"
15 endingColumnNumber = "9223372036854775807"
16− startingLineNumber = "62"
16+ startingLineNumber = "69"
17− endingLineNumber = "62"
17+ endingLineNumber = "69"
18 landmarkName = "updateTargetView(_:dataSource:)"
18 landmarkName = "updateTargetView(_:dataSource:)"
19 landmarkType = "5">
19 landmarkType = "5">
20 </BreakpointContent>
20 </BreakpointContent>
PixelImage/MetalMetaballRenderer.swift+20−11
@@ -1,7 +1,12 @@
1−// TODO: Render metaballs on layer/view instead of image
1+// TODO: Optimize resources (don't allocate textures and images every frame)
2// TODO: Remove references to view size from shader
2// TODO: Remove references to view size from shader
3// TODO: Make shader ignore metaballs that are outside a certain area of tolerance (eg ±100 from borders)
3// TODO: Make shader ignore metaballs that are outside a certain area of tolerance (eg ±100 from borders)
4
4
5+// TODO: Metaballs from texture to buffer
6+// TODO: Use async dispatch to free up main queue
7+// TODO: Refactor update into 2 calls: metalRendering() and textureToImage()
8+// TODO: Use two textures/images to alternate the 2 calls (like OpenGL double buffers)
9+
5// TODO: Make graphics have edges only according to graph (instead of being a complete graph)
10// TODO: Make graphics have edges only according to graph (instead of being a complete graph)
6
11
7// TODO: Interpolate metaball colors
12// TODO: Interpolate metaball colors
@@ -16,30 +21,34 @@ class MetalMetaballRenderer: MetaballRenderer {
16 typealias TargetView = UIImageView
21 typealias TargetView = UIImageView
17
22
18 static let context = MTLContext()
23 static let context = MTLContext()
24+ static var internalTexture: MTLTexture!
19
25
20 static func updateTargetView(imageView: UIImageView, dataSource: MetaballDataSource) {
26 static func updateTargetView(imageView: UIImageView, dataSource: MetaballDataSource) {
21 let metaballs = dataSource.metaballs
27 let metaballs = dataSource.metaballs
22
28
29+ let width = Int(imageView.width)
30+ let height = Int(imageView.height)
31+
32+ if internalTexture == nil {
33+ let textureDescriptor =
34+ MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
35+
36+ internalTexture = context.device.newTextureWithDescriptor(textureDescriptor)
37+ }
38+
23 let kernelFunction: MTLFunction?
39 let kernelFunction: MTLFunction?
24 let pipeline: MTLComputePipelineState
40 let pipeline: MTLComputePipelineState
25− var internalTexture: MTLTexture! = nil
26−
27 do {
41 do {
42+ // Get long-lived objects
28 kernelFunction = context.library.newFunctionWithName("drawMetaballs")
43 kernelFunction = context.library.newFunctionWithName("drawMetaballs")
29 pipeline = try context.device.newComputePipelineStateWithFunction(kernelFunction!)
44 pipeline = try context.device.newComputePipelineStateWithFunction(kernelFunction!)
30
45
31− let width = Int(imageView.width)
46+ // Configure info for computing
32− let height = Int(imageView.height)
33−
34− let textureDescriptor =
35− MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
36−
37− internalTexture = context.device.newTextureWithDescriptor(textureDescriptor)
38 let threadGroupCounts = MTLSizeMake(8, 8, 1)
47 let threadGroupCounts = MTLSizeMake(8, 8, 1)
39 let threadGroups = MTLSizeMake(width / threadGroupCounts.width,
48 let threadGroups = MTLSizeMake(width / threadGroupCounts.width,
40 height / threadGroupCounts.height, 1)
49 height / threadGroupCounts.height, 1)
41
50
42− //
51+ // Create up-to-date metaball buffer
43 let metaballInfoTexture = metaballTexture(metaballs)
52 let metaballInfoTexture = metaballTexture(metaballs)
44
53
45 //
54 //