From 83978aff8ca3df90b5639c6033c74d07c4ca6bd8 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 27 Oct 2018 10:25:26 -0700 Subject: [PATCH] Create Variants and set up its buffer --- MetaballsKit/MarchingSquares.swift | 34 +++++++++++++----------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/MetaballsKit/MarchingSquares.swift b/MetaballsKit/MarchingSquares.swift index ab17cec..05896a5 100644 --- a/MetaballsKit/MarchingSquares.swift +++ b/MetaballsKit/MarchingSquares.swift @@ -34,6 +34,8 @@ class MarchingSquares { private(set) var gridGeometry: MTLBuffer? + private let variants = Variants() + private var xSamples: Int { let xSize = field.size.x / sampleGridSize.x let xRem = field.size.x % sampleGridSize.x @@ -67,6 +69,7 @@ class MarchingSquares { func setupMetal(withDevice device: MTLDevice, library: MTLLibrary) { samplingPipeline = createComputePipeline(withFunctionNamed: "samplingKernel", device: device, library: library) contouringPipeline = createComputePipeline(withFunctionNamed: "contouringKernel", device: device, library: library) + variants.setupMetal(withDevice: device) createParametersBuffer(withDevice: device) createSamplesBuffer(withDevice: device) createContourIndexesBuffer(withDevice: device) @@ -232,7 +235,7 @@ class MarchingSquares { } } -struct Variants { +fileprivate class Variants { static let geometry: [Float] = [ // 0: no triangles // 1: lower left corner, 1 triangle @@ -365,26 +368,19 @@ struct Variants { } static func startingIndex(for variation: UInt) -> UInt { - switch variation { - case 0: return 0 - case 1: return 0 - case 2: return 3 - case 3: return 6 - case 4: return 10 - case 5: return 13 - case 6: return 19 - case 7: return 23 - case 8: return 28 - case 9: return 31 - case 10: return 35 - case 11: return 41 - case 12: return 46 - case 13: return 50 - case 14: return 55 - case 15: return 60 - default: return 0 + var idx = UInt(0) + for i in 0...stride * Variants.geometry.count + if let buffer = device.makeBuffer(bytes: Variants.geometry, length: bufferLength, options: .storageModeShared) { + self.buffer = buffer + } + } }