Attempt #1 at getting a texture of indexes

This commit is contained in:
Eryn Wells 2018-10-13 20:55:07 -07:00
parent f4a9f56fa7
commit 2ed72491b1
2 changed files with 51 additions and 1 deletions

View file

@ -26,12 +26,15 @@ class MarchingSquares {
func setupMetal(withDevice device: MTLDevice) {
let xSamples = Int(field.size.x / sampleGridSize.x)
let ySamples = Int(field.size.y / sampleGridSize.y)
guard xSamples > 1 && ySamples > 1 else {
return
}
let samplesDesc = MTLTextureDescriptor()
samplesDesc.textureType = .type2D
samplesDesc.width = xSamples
samplesDesc.height = ySamples
samplesDesc.pixelFormat = .depth32Float
samplesDesc.pixelFormat = .r32Float
samples = device.makeTexture(descriptor: samplesDesc)
let indexesDesc = MTLTextureDescriptor()
@ -61,4 +64,44 @@ class MarchingSquares {
}
}
}
func populateIndexes() {
guard let indexes = indexes else { return }
let bytesPerRow = indexes.width * MemoryLayout<UInt8>.stride
for x in 0..<indexes.width {
for y in 0..<indexes.height {
guard let samples = getSampleBlock(x: x, y: y) else {
continue
}
let index = (samples[0] > 1.0 ? 0b1000 : 0) +
(samples[1] > 1.0 ? 0b0100 : 0) +
(samples[2] > 1.0 ? 0b0001 : 0) +
(samples[3] > 1.0 ? 0b0010 : 0)
let origin = MTLOrigin(x: x, y: y, z: 0)
let size = MTLSize(width: 1, height: 1, depth: 1)
let region = MTLRegion(origin: origin, size: size)
let indexArr = [index]
indexes.replace(region: region, mipmapLevel: 0, withBytes: indexArr, bytesPerRow: bytesPerRow)
}
}
}
private func getSampleBlock(x: Int, y: Int) -> [Float]? {
guard let samples = samples else {
return nil
}
var block: [Float] = [0, 0, 0, 0]
let bytesPerRow = samples.width * MemoryLayout<Float>.stride
let origin = MTLOrigin(x: x, y: y, z: 0)
let size = MTLSize(width: 2, height: 2, depth: 1)
let region = MTLRegion(origin: origin, size: size)
samples.getBytes(&block, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
return block
}
}

View file

@ -119,6 +119,9 @@ public class Field {
public var defaults = UserDefaults.standard
private var parameters: Parameters
private lazy var marchingSquares: MarchingSquares = {
return MarchingSquares(field: self)
}()
internal var bounds: CGRect {
return CGRect(origin: CGPoint(), size: CGSize(size: size))
@ -155,7 +158,10 @@ public class Field {
}
}
}
populateBallBuffer()
marchingSquares.sampleField()
marchingSquares.populateIndexes()
}
func add(ballWithRadius radius: Float) {
@ -264,6 +270,7 @@ public class Field {
self.device = device
populateParametersBuffer()
populateBallBuffer()
marchingSquares.setupMetal(withDevice: device)
}
// MARK: - Notifications