Move the render queue to the Diamond-Square object

This commit is contained in:
Eryn Wells 2018-11-11 22:12:25 -05:00
parent e62dd1b0f2
commit f0d6177567
2 changed files with 24 additions and 15 deletions

View file

@ -22,7 +22,7 @@ protocol TerrainGenerator {
func updateUniforms() func updateUniforms()
func encode(in encoder: MTLComputeCommandEncoder) func encode(in encoder: MTLComputeCommandEncoder)
func render() func render(completion: @escaping () -> Void)
} }
class Kernel { class Kernel {
@ -96,7 +96,7 @@ class ZeroAlgorithm: Kernel, TerrainGenerator {
func updateUniforms() { } func updateUniforms() { }
func render() { } func render(completion: @escaping () -> Void) { }
} }
/// Randomly generate heights that are independent of all others. /// Randomly generate heights that are independent of all others.
@ -130,7 +130,7 @@ class RandomAlgorithm: Kernel, TerrainGenerator {
RandomAlgorithmUniforms_refreshRandoms(uniforms) RandomAlgorithmUniforms_refreshRandoms(uniforms)
} }
func render() { } func render(completion: @escaping () -> Void) { }
} }
/// Implementation of the Diamond-Squares algorithm. /// Implementation of the Diamond-Squares algorithm.
@ -234,6 +234,8 @@ public class DiamondSquareGenerator: TerrainGenerator {
struct Algorithm { struct Algorithm {
let grid: Box let grid: Box
private let queue = DispatchQueue(label: "Diamond-Square Algorithm Queue")
var roughness: Float = 1.0 { var roughness: Float = 1.0 {
didSet { didSet {
randomRange = -roughness...roughness randomRange = -roughness...roughness
@ -248,7 +250,14 @@ public class DiamondSquareGenerator: TerrainGenerator {
} }
/// Run the algorithm and return the genreated height map. /// Run the algorithm and return the genreated height map.
func render() -> [Float] { func render(completion: @escaping ([Float]) -> Void) {
queue.async {
let heightMap = self.queue_render()
completion(heightMap)
}
}
func queue_render() -> [Float] {
var heightMap = [Float](repeating: 0, count: grid.size.w * grid.size.h) var heightMap = [Float](repeating: 0, count: grid.size.w * grid.size.h)
// 0. Set the corners to initial values if they haven't been set yet. // 0. Set the corners to initial values if they haven't been set yet.
@ -351,12 +360,15 @@ public class DiamondSquareGenerator: TerrainGenerator {
algorithm = Algorithm(grid: Box(origin: Point(), size: Size(w: DiamondSquareGenerator.textureSize.width, h: DiamondSquareGenerator.textureSize.height))) algorithm = Algorithm(grid: Box(origin: Point(), size: Size(w: DiamondSquareGenerator.textureSize.width, h: DiamondSquareGenerator.textureSize.height)))
} }
func render() { func render(completion: @escaping () -> Void) {
let heightMap = algorithm.render() algorithm.render { (heightMap: [Float]) in
let region = MTLRegion(origin: MTLOrigin(), size: DiamondSquareGenerator.textureSize) let region = MTLRegion(origin: MTLOrigin(), size: DiamondSquareGenerator.textureSize)
let newActiveTexture = (activeTexture + 1) % textures.count let newActiveTexture = (self.activeTexture + 1) % self.textures.count
textures[newActiveTexture].replace(region: region, mipmapLevel: 0, withBytes: heightMap, bytesPerRow: MemoryLayout<Float>.stride * DiamondSquareGenerator.textureSize.width) self.textures[newActiveTexture].replace(region: region, mipmapLevel: 0, withBytes: heightMap, bytesPerRow: MemoryLayout<Float>.stride * DiamondSquareGenerator.textureSize.width)
activeTexture = newActiveTexture self.activeTexture = newActiveTexture
completion()
}
} }
// MARK: Algorithm // MARK: Algorithm

View file

@ -38,8 +38,6 @@ class Renderer: NSObject, MTKViewDelegate {
let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight) let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight)
let regenerationSemaphore = DispatchSemaphore(value: 1) let regenerationSemaphore = DispatchSemaphore(value: 1)
let generatorQueue = DispatchQueue(label: "me.erynwells.Terrain.generatorQueue")
var uniformBufferOffset = 0 var uniformBufferOffset = 0
var uniformBufferIndex = 0 var uniformBufferIndex = 0
var uniforms: UnsafeMutablePointer<Uniforms> var uniforms: UnsafeMutablePointer<Uniforms>
@ -171,9 +169,8 @@ class Renderer: NSObject, MTKViewDelegate {
func scheduleAlgorithmIteration() { func scheduleAlgorithmIteration() {
regenerationSemaphore.wait() regenerationSemaphore.wait()
if !terrain.generator.needsGPU { if !terrain.generator.needsGPU {
generatorQueue.async { print("Rendering terrain...")
print("Rendering terrain...") self.terrain.generator.render {
self.terrain.generator.render()
print("Rendering terrain...complete!") print("Rendering terrain...complete!")
self.didUpdateTerrain = true self.didUpdateTerrain = true
} }