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,15 +62,31 @@ public class World {
 | 
				
			|||||||
  func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
 | 
					  func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
 | 
				
			||||||
    self._generator.reset(seed: seed)
 | 
					    self._generator.reset(seed: seed)
 | 
				
			||||||
    let orig = SIMD3(width, height, depth) / 2
 | 
					    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 z in 0..<depth {
 | 
				
			||||||
      for y in 0..<height {
 | 
					      for y in 0..<height {
 | 
				
			||||||
        for x in 0..<width {
 | 
					        for x in 0..<width {
 | 
				
			||||||
          let chunkID = SIMD3(x, y, z) &- orig
 | 
					          let chunkID = SIMD3(x, y, z) &- orig
 | 
				
			||||||
          self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
					          queue.addOperation {
 | 
				
			||||||
          self._chunkDamage.insert(chunkID)
 | 
					            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) {
 | 
					  func generate(chunkID: ChunkID) {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user