Skip to content

Commit

Permalink
Use flatmap internally
Browse files Browse the repository at this point in the history
Update readme
  • Loading branch information
andystanton committed Jun 14, 2024
1 parent ceea420 commit 2c36635
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ import SPMFractalNoise

let fractalNoise = FractalNoiseCPU()


let fractalNoiseParameters = FractalNoiseParameters(
noiseTypeParameters: .OpenSimplex2(
OpenSimplex2NoiseParameters(
seed: 42,
seed: 420,
noise3Variant: .xz)),
octaves: 8,
lacunarity: 2,
hurstExponent: 1.5,
hurstExponent: 1,
startingAmplitude: 1,
startingFrequency: 1)
startingFrequency: 0.0025,
coordinateScale: 0.5,
warpIterations: 3,
warpScale: 200)

let noise: Float = openSimplex2.noise3(
fractalNoiseParameters: fractalNoiseParameters,
Expand Down
31 changes: 14 additions & 17 deletions Sources/SPMFractalNoise/FractalNoiseMetal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class FractalNoiseMetal {
private let noise4Pipeline: MTLComputePipelineState?

public init(
device: MTLDevice? = nil,
device: MTLDevice = MTLCreateSystemDefaultDevice()!,
commandQueue: MTLCommandQueue? = nil
) {
self.device = device ?? MTLCreateSystemDefaultDevice()!
self.device = device
self.commandQueue = commandQueue ?? self.device.makeCommandQueue()

let kernel = """
Expand All @@ -28,21 +28,18 @@ public class FractalNoiseMetal {

let library = try! self.device.makeLibrary(source: kernel, options: nil)

if let noise2Function = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise2FunctionName) {
noise2Pipeline = try! self.device.makeComputePipelineState(function: noise2Function)
} else {
noise2Pipeline = nil
}
if let noise3Function = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise3FunctionName) {
noise3Pipeline = try! self.device.makeComputePipelineState(function: noise3Function)
} else {
noise3Pipeline = nil
}
if let noise4Function = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise4FunctionName) {
noise4Pipeline = try! self.device.makeComputePipelineState(function: noise4Function)
} else {
noise4Pipeline = nil
}
noise2Pipeline = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise2FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
noise3Pipeline = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise3FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
noise4Pipeline = library.makeFunction(name: FractalNoiseMetalShaderLoader.noise4FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
}

private func executeNoiseFunction<T>(
Expand Down
27 changes: 12 additions & 15 deletions Sources/SPMOpenSimplex2/OpenSimplex2Metal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ public class OpenSimplex2Metal {
"""

let library = try! device.makeLibrary(source: kernel, options: nil)
if let noise2Function = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise2FunctionName) {
noise2Pipeline = try! device.makeComputePipelineState(function: noise2Function)
} else {
noise2Pipeline = nil
}
if let noise3Function = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise3FunctionName) {
noise3Pipeline = try! device.makeComputePipelineState(function: noise3Function)
} else {
noise3Pipeline = nil
}
if let noise4Function = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise4FunctionName) {
noise4Pipeline = try! device.makeComputePipelineState(function: noise4Function)
} else {
noise4Pipeline = nil
}
noise2Pipeline = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise2FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
noise3Pipeline = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise3FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
noise4Pipeline = library.makeFunction(name: OpenSimplex2MetalShaderLoader.noise4FunctionName)
.flatMap {
try? device.makeComputePipelineState(function: $0)
}
}

private func executeNoiseFunction(
Expand Down

0 comments on commit 2c36635

Please sign in to comment.