mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-02 13:00:53 +00:00
Parallelize chunk generation using fan-out pattern.
This commit is contained in:
parent
fbf66585eb
commit
a149de885c
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user