mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	Parallelize chunk generation using fan-out pattern.
This commit is contained in:
		@ -62,16 +62,32 @@ 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)
 | 
			
		||||
          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) {
 | 
			
		||||
    self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user