Parallelize chunk generation using fan-out pattern.

This commit is contained in:
Alex Zenla 2024-09-01 19:41:22 -04:00
parent fbf66585eb
commit a149de885c
No known key found for this signature in database
GPG Key ID: 067B238899B51269

View File

@ -62,15 +62,31 @@ public class World {
func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
self._generator.reset(seed: seed)
let orig = SIMD3(width, height, depth) / 2
var localChunks: [SIMD3<Int>: Chunk] = [:]
let localChunksLock = NSLock()
let queue = OperationQueue()
queue.qualityOfService = .userInitiated
for z in 0..<depth {
for y in 0..<height {
for x in 0..<width {
let chunkID = SIMD3(x, y, z) &- orig
self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
self._chunkDamage.insert(chunkID)
queue.addOperation {
let chunk = self._generator.makeChunk(id: chunkID)
localChunksLock.lock()
defer {
localChunksLock.unlock()
}
localChunks[chunkID] = chunk
}
}
}
}
queue.waitUntilAllOperationsAreFinished()
for (chunkID, chunk) in localChunks {
self._chunks[chunkID] = chunk
self._chunkDamage.insert(chunkID)
}
}
func generate(chunkID: ChunkID) {