Split out algorithms into separate files
This commit is contained in:
parent
c3432af621
commit
8917289c05
3 changed files with 124 additions and 101 deletions
|
@ -12,6 +12,7 @@
|
|||
C018AD4021978E690094BE3C /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = C018AD3F21978E690094BE3C /* Queue.swift */; };
|
||||
C018AD422197907B0094BE3C /* QueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C018AD412197907B0094BE3C /* QueueTests.swift */; };
|
||||
C028A6A221A46796005DE718 /* Math.swift in Sources */ = {isa = PBXBuildFile; fileRef = C028A6A121A46796005DE718 /* Math.swift */; };
|
||||
C06F150121A8649000458C5B /* DiamondSquare.swift in Sources */ = {isa = PBXBuildFile; fileRef = C06F150021A8649000458C5B /* DiamondSquare.swift */; };
|
||||
C08C58A0218F46F000EAFC2D /* Algorithms.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08C589F218F46F000EAFC2D /* Algorithms.swift */; };
|
||||
C08C58A2218F474E00EAFC2D /* TerrainAlgorithms.metal in Sources */ = {isa = PBXBuildFile; fileRef = C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */; };
|
||||
C0C15A8E218DDD85007494E2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15A8D218DDD85007494E2 /* AppDelegate.swift */; };
|
||||
|
@ -52,6 +53,7 @@
|
|||
C018AD412197907B0094BE3C /* QueueTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueTests.swift; sourceTree = "<group>"; };
|
||||
C028A6A121A46796005DE718 /* Math.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Math.swift; sourceTree = "<group>"; };
|
||||
C04A27A421A6FB4B00FCABFB /* Random.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Random.hh; sourceTree = "<group>"; };
|
||||
C06F150021A8649000458C5B /* DiamondSquare.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiamondSquare.swift; sourceTree = "<group>"; };
|
||||
C08C589F218F46F000EAFC2D /* Algorithms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithms.swift; sourceTree = "<group>"; };
|
||||
C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = TerrainAlgorithms.metal; sourceTree = "<group>"; };
|
||||
C0C15A8A218DDD85007494E2 /* Terrain.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Terrain.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
|
@ -129,6 +131,15 @@
|
|||
path = Shaders;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C06F14FF21A8647100458C5B /* Algorithms */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
C08C589F218F46F000EAFC2D /* Algorithms.swift */,
|
||||
C06F150021A8649000458C5B /* DiamondSquare.swift */,
|
||||
);
|
||||
path = Algorithms;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C0C15A81218DDD85007494E2 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
|
@ -184,7 +195,7 @@
|
|||
C0C15AB6218E2A90007494E2 /* Renderer.swift */,
|
||||
C028A6A121A46796005DE718 /* Math.swift */,
|
||||
C0C15AC5218E32B2007494E2 /* Terrain.swift */,
|
||||
C08C589F218F46F000EAFC2D /* Algorithms.swift */,
|
||||
C06F14FF21A8647100458C5B /* Algorithms */,
|
||||
C018AD3F21978E690094BE3C /* Queue.swift */,
|
||||
C018AD3E219728890094BE3C /* Shaders */,
|
||||
C0C15ABB218E2A90007494E2 /* Assets.xcassets */,
|
||||
|
@ -355,6 +366,7 @@
|
|||
C0C15AB7218E2A90007494E2 /* Renderer.swift in Sources */,
|
||||
C0C15AB3218E2A90007494E2 /* AppDelegate.swift in Sources */,
|
||||
C018AD4021978E690094BE3C /* Queue.swift in Sources */,
|
||||
C06F150121A8649000458C5B /* DiamondSquare.swift in Sources */,
|
||||
C08C58A0218F46F000EAFC2D /* Algorithms.swift in Sources */,
|
||||
C028A6A221A46796005DE718 /* Math.swift in Sources */,
|
||||
);
|
||||
|
|
107
Terrain2/Algorithms/Algorithms.swift
Normal file
107
Terrain2/Algorithms/Algorithms.swift
Normal file
|
@ -0,0 +1,107 @@
|
|||
//
|
||||
// Algorithms.swift
|
||||
// Terrain2
|
||||
//
|
||||
// Created by Eryn Wells on 11/4/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Metal
|
||||
import os
|
||||
|
||||
let Log = OSLog(subsystem: "me.erynwells.Terrain2.Algorithms", category: "DiamondSquare")
|
||||
|
||||
enum KernelError: Error {
|
||||
case badFunction
|
||||
case badSize
|
||||
case textureCreationFailed
|
||||
}
|
||||
|
||||
protocol TerrainGenerator {
|
||||
var name: String { get }
|
||||
var needsGPU: Bool { get }
|
||||
var outTexture: MTLTexture { get }
|
||||
|
||||
func encode(in encoder: MTLComputeCommandEncoder)
|
||||
func render(progress: Progress) -> [Float]
|
||||
}
|
||||
|
||||
class Kernel {
|
||||
|
||||
class var textureSize: MTLSize {
|
||||
return MTLSize(width: 512, height: 512, depth: 1)
|
||||
}
|
||||
|
||||
class func buildTexture(device: MTLDevice, size: MTLSize) -> MTLTexture? {
|
||||
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: size.width, height: size.height, mipmapped: false)
|
||||
desc.usage = [.shaderRead, .shaderWrite]
|
||||
let tex = device.makeTexture(descriptor: desc)
|
||||
return tex
|
||||
}
|
||||
|
||||
let pipeline: MTLComputePipelineState
|
||||
let textures: [MTLTexture]
|
||||
let uniformBuffer: MTLBuffer?
|
||||
|
||||
var outTexture: MTLTexture {
|
||||
return textures[textureIndexes.out]
|
||||
}
|
||||
|
||||
private(set) var textureIndexes: (`in`: Int, out: Int) = (in: 0, out: 1)
|
||||
|
||||
init(device: MTLDevice, library: MTLLibrary, functionName: String, uniformBuffer: MTLBuffer? = nil) throws {
|
||||
guard let computeFunction = library.makeFunction(name: functionName) else {
|
||||
throw KernelError.badFunction
|
||||
}
|
||||
self.pipeline = try device.makeComputePipelineState(function: computeFunction)
|
||||
|
||||
// Create our input and output textures
|
||||
var textures = [MTLTexture]()
|
||||
for i in 0..<2 {
|
||||
guard let tex = Kernel.buildTexture(device: device, size: type(of: self).textureSize) else {
|
||||
print("Couldn't create heights texture i=\(i)")
|
||||
throw KernelError.textureCreationFailed
|
||||
}
|
||||
textures.append(tex)
|
||||
}
|
||||
self.textures = textures
|
||||
|
||||
self.uniformBuffer = uniformBuffer
|
||||
}
|
||||
|
||||
func encode(in encoder: MTLComputeCommandEncoder) {
|
||||
encoder.setComputePipelineState(pipeline)
|
||||
encoder.setTexture(textures[textureIndexes.in], index: textureIndexes.in)
|
||||
encoder.setTexture(textures[textureIndexes.out], index: textureIndexes.out)
|
||||
encoder.setBuffer(uniformBuffer, offset: 0, index: 0)
|
||||
encoder.dispatchThreads(type(of: self).textureSize, threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1))
|
||||
}
|
||||
}
|
||||
|
||||
/// "Compute" zero for every value of the height map.
|
||||
class ZeroAlgorithm: Kernel, TerrainGenerator {
|
||||
let name = "Zero"
|
||||
|
||||
let needsGPU: Bool = true
|
||||
|
||||
init?(device: MTLDevice, library: MTLLibrary) {
|
||||
do {
|
||||
try super.init(device: device, library: library, functionName: "zeroKernel")
|
||||
} catch let e {
|
||||
print("Couldn't create compute kernel. Error: \(e)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Algorithm
|
||||
|
||||
func render(progress: Progress) -> [Float] {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of the Circles algorithm.
|
||||
//class CirclesAlgorithm: Algorithm {
|
||||
// static let name = "Circles"
|
||||
//}
|
|
@ -1,8 +1,8 @@
|
|||
//
|
||||
// Algorithms.swift
|
||||
// DiamondSquare.swift
|
||||
// Terrain2
|
||||
//
|
||||
// Created by Eryn Wells on 11/4/18.
|
||||
// Created by Eryn Wells on 11/23/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
|
@ -10,97 +10,6 @@ import Foundation
|
|||
import Metal
|
||||
import os
|
||||
|
||||
let Log = OSLog(subsystem: "me.erynwells.Terrain2.Algorithms", category: "DiamondSquare")
|
||||
|
||||
enum KernelError: Error {
|
||||
case badFunction
|
||||
case badSize
|
||||
case textureCreationFailed
|
||||
}
|
||||
|
||||
protocol TerrainGenerator {
|
||||
var name: String { get }
|
||||
var needsGPU: Bool { get }
|
||||
var outTexture: MTLTexture { get }
|
||||
|
||||
func encode(in encoder: MTLComputeCommandEncoder)
|
||||
func render(progress: Progress) -> [Float]
|
||||
}
|
||||
|
||||
class Kernel {
|
||||
|
||||
class var textureSize: MTLSize {
|
||||
return MTLSize(width: 512, height: 512, depth: 1)
|
||||
}
|
||||
|
||||
class func buildTexture(device: MTLDevice, size: MTLSize) -> MTLTexture? {
|
||||
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: size.width, height: size.height, mipmapped: false)
|
||||
desc.usage = [.shaderRead, .shaderWrite]
|
||||
let tex = device.makeTexture(descriptor: desc)
|
||||
return tex
|
||||
}
|
||||
|
||||
let pipeline: MTLComputePipelineState
|
||||
let textures: [MTLTexture]
|
||||
let uniformBuffer: MTLBuffer?
|
||||
|
||||
var outTexture: MTLTexture {
|
||||
return textures[textureIndexes.out]
|
||||
}
|
||||
|
||||
private(set) var textureIndexes: (`in`: Int, out: Int) = (in: 0, out: 1)
|
||||
|
||||
init(device: MTLDevice, library: MTLLibrary, functionName: String, uniformBuffer: MTLBuffer? = nil) throws {
|
||||
guard let computeFunction = library.makeFunction(name: functionName) else {
|
||||
throw KernelError.badFunction
|
||||
}
|
||||
self.pipeline = try device.makeComputePipelineState(function: computeFunction)
|
||||
|
||||
// Create our input and output textures
|
||||
var textures = [MTLTexture]()
|
||||
for i in 0..<2 {
|
||||
guard let tex = Kernel.buildTexture(device: device, size: type(of: self).textureSize) else {
|
||||
print("Couldn't create heights texture i=\(i)")
|
||||
throw KernelError.textureCreationFailed
|
||||
}
|
||||
textures.append(tex)
|
||||
}
|
||||
self.textures = textures
|
||||
|
||||
self.uniformBuffer = uniformBuffer
|
||||
}
|
||||
|
||||
func encode(in encoder: MTLComputeCommandEncoder) {
|
||||
encoder.setComputePipelineState(pipeline)
|
||||
encoder.setTexture(textures[textureIndexes.in], index: textureIndexes.in)
|
||||
encoder.setTexture(textures[textureIndexes.out], index: textureIndexes.out)
|
||||
encoder.setBuffer(uniformBuffer, offset: 0, index: 0)
|
||||
encoder.dispatchThreads(type(of: self).textureSize, threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1))
|
||||
}
|
||||
}
|
||||
|
||||
/// "Compute" zero for every value of the height map.
|
||||
class ZeroAlgorithm: Kernel, TerrainGenerator {
|
||||
let name = "Zero"
|
||||
|
||||
let needsGPU: Bool = true
|
||||
|
||||
init?(device: MTLDevice, library: MTLLibrary) {
|
||||
do {
|
||||
try super.init(device: device, library: library, functionName: "zeroKernel")
|
||||
} catch let e {
|
||||
print("Couldn't create compute kernel. Error: \(e)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Algorithm
|
||||
|
||||
func render(progress: Progress) -> [Float] {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of the Diamond-Squares algorithm.
|
||||
/// - https://en.wikipedia.org/wiki/Diamond-square_algorithm
|
||||
public class DiamondSquareGenerator: TerrainGenerator {
|
||||
|
@ -260,7 +169,7 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
|||
}
|
||||
|
||||
os_signpost(.end, log: Log, name: "DiamondSquare.render")
|
||||
|
||||
|
||||
return heightMap
|
||||
}
|
||||
|
||||
|
@ -294,7 +203,7 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
|||
return pt.y * grid.size.w + pt.x
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let name = "Diamond-Square"
|
||||
let needsGPU: Bool = false
|
||||
|
||||
|
@ -387,8 +296,3 @@ extension DiamondSquareGenerator.Box: Equatable {
|
|||
return lhs.origin == rhs.origin && lhs.size == rhs.size
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of the Circles algorithm.
|
||||
//class CirclesAlgorithm: Algorithm {
|
||||
// static let name = "Circles"
|
||||
//}
|
Loading…
Add table
Add a link
Reference in a new issue