implement infinite worlds with threaded chunk generation

This commit is contained in:
Alex Zenla 2024-09-03 09:18:35 -04:00
parent 6f985ce1c9
commit 5e40e12c8b
No known key found for this signature in database
GPG Key ID: 067B238899B51269
7 changed files with 133 additions and 18 deletions

View File

@ -53,6 +53,7 @@ add_executable(Voxelotl MACOSX_BUNDLE
# Game logic classes
Chunk.swift
ChunkGeneration.swift
WorldGenerator.swift
CubeMeshBuilder.swift
ChunkMeshBuilder.swift

View File

@ -0,0 +1,92 @@
import Foundation
public struct ChunkGeneration {
private let queue: OperationQueue
private let localReadyChunks = ConcurrentDictionary<SIMD3<Int>, Chunk>()
private var generatingChunkSet = Set<SIMD3<Int>>()
weak var world: World?
init(queue: DispatchQueue) {
self.queue = OperationQueue()
self.queue.underlyingQueue = queue
self.queue.maxConcurrentOperationCount = 8
self.queue.qualityOfService = .userInitiated
}
public mutating func generate(chunkID: SIMD3<Int>) {
if !generatingChunkSet.insert(chunkID).inserted {
return
}
self.queueGenerateJob(chunkID: chunkID)
}
func queueGenerateJob(chunkID: SIMD3<Int>) {
self.queue.addOperation {
guard let world = self.world else {
return
}
let chunk = world.generateSingleChunkUncommitted(chunkID: chunkID)
self.localReadyChunks[chunkID] = chunk
}
}
public mutating func generateAdjacentIfNeeded(position: SIMD3<Float>) {
guard let world = self.world else {
return
}
let centerChunkID = World.makeID(position: position)
for offset in ChunkGeneration.chunkGenerateNeighbors {
let chunkID = centerChunkID &+ offset
if world.getChunk(id: chunkID) == nil {
self.generate(chunkID: chunkID)
}
}
}
public mutating func acceptReadyChunks() {
guard let world = self.world else {
return
}
if self.generatingChunkSet.isEmpty {
return
}
for (chunkID, chunk) in self.localReadyChunks.take() {
world.addChunk(chunkID: chunkID, chunk: chunk)
self.generatingChunkSet.remove(chunkID)
}
}
private static let chunkGenerateNeighbors: [SIMD3<Int>] = [
SIMD3<Int>(-1, -1, -1),
SIMD3<Int>(0, -1, -1),
SIMD3<Int>(1, -1, -1),
SIMD3<Int>(-1, 0, -1),
SIMD3<Int>(0, 0, -1),
SIMD3<Int>(1, 0, -1),
SIMD3<Int>(-1, 1, -1),
SIMD3<Int>(0, 1, -1),
SIMD3<Int>(1, 1, -1),
SIMD3<Int>(-1, -1, 0),
SIMD3<Int>(0, -1, 0),
SIMD3<Int>(1, -1, 0),
SIMD3<Int>(-1, 0, 0),
SIMD3<Int>(0, 0, 0),
SIMD3<Int>(1, 0, 0),
SIMD3<Int>(-1, 1, 0),
SIMD3<Int>(0, 1, 0),
SIMD3<Int>(1, 1, 0),
SIMD3<Int>(-1, -1, 1),
SIMD3<Int>(0, -1, 1),
SIMD3<Int>(1, -1, 1),
SIMD3<Int>(-1, 0, 1),
SIMD3<Int>(0, 0, 1),
SIMD3<Int>(1, 0, 1),
SIMD3<Int>(-1, 1, 1),
SIMD3<Int>(0, 1, 1),
SIMD3<Int>(1, 1, 1),
]
}

View File

@ -62,6 +62,14 @@ public class ConcurrentDictionary<V: Hashable, T>: Collection {
}
}
public func take() -> Dictionary<V, T> {
self.locked {
let current = self.inner
self.inner = [:]
return current
}
}
fileprivate func locked<X>(_ perform: () -> X) -> X {
self.lock.lock()
defer {

View File

@ -87,8 +87,13 @@ class Game: GameDelegate {
// Regenerate current chunk
if regenChunk {
self.world.generate(chunkID: World.makeID(position: self.player.position))
let chunkID = World.makeID(position: self.player.position)
let chunk = self.world.generateSingleChunkUncommitted(chunkID: chunkID)
self.world.addChunk(chunkID: chunkID, chunk: chunk)
}
self.world.generateAdjacentChunksIfNeeded(position: self.player.position)
self.world.update()
}
func draw(_ renderer: Renderer, _ time: GameTime) {

View File

@ -8,7 +8,7 @@ struct Player {
to: .init(Self.radius, Self.height, Self.radius))
static let eyeLevel: Float = 1.4
static let epsilon = Float.ulpOfOne * 20
static let epsilon = Float.ulpOfOne * 2000
static let accelerationCoeff: Float = 75
static let airAccelCoeff: Float = 3

View File

@ -10,11 +10,14 @@ public class World {
private var _chunks: Dictionary<ChunkID, Chunk>
private var _chunkDamage: Set<ChunkID>
private var _generator: WorldGenerator
private var _chunkGeneration: ChunkGeneration
public init() {
self._chunks = [:]
self._chunkDamage = []
self._generator = WorldGenerator()
self._generator = StandardWorldGenerator()
self._chunkGeneration = ChunkGeneration(queue: .global(qos: .userInitiated))
self._chunkGeneration.world = self
}
func getBlock(at position: SIMD3<Int>) -> Block {
@ -69,29 +72,26 @@ public class World {
self._generator.reset(seed: seed)
let orig = SIMD3(width, height, depth) / 2
let localChunks = ConcurrentDictionary<ChunkID, Chunk>()
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
queue.addOperation {
let chunk = self._generator.makeChunk(id: chunkID)
localChunks[chunkID] = chunk
}
self._chunkGeneration.generate(chunkID: chunkID)
}
}
}
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)
func generateSingleChunkUncommitted(chunkID: SIMD3<Int>) -> Chunk {
self._generator.makeChunk(id: chunkID)
}
public func generateAdjacentChunksIfNeeded(position: SIMD3<Float>) {
self._chunkGeneration.generateAdjacentIfNeeded(position: position)
}
public func addChunk(chunkID: ChunkID, chunk: Chunk) {
self._chunks[chunkID] = chunk
self._chunkDamage.insert(chunkID)
for i: ChunkID in [ .X, .Y, .Z ] {
for otherID in [ chunkID &- i, chunkID &+ i ] {
@ -102,6 +102,10 @@ public class World {
}
}
public func update() {
self._chunkGeneration.acceptReadyChunks()
}
func handleRenderDamagedChunks(_ body: (_ id: ChunkID, _ chunk: Chunk) -> Void) {
for id in self._chunkDamage {
body(id, self._chunks[id]!)

View File

@ -1,4 +1,9 @@
struct WorldGenerator {
protocol WorldGenerator {
mutating func reset(seed: UInt64)
func makeChunk(id: SIMD3<Int>) -> Chunk
}
struct StandardWorldGenerator: WorldGenerator {
var noise: ImprovedPerlin<Float>!, noise2: SimplexNoise<Float>!
public mutating func reset(seed: UInt64) {