Skip to content

Commit

Permalink
Change noise interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
andystanton committed Jun 10, 2024
1 parent 1567035 commit 42f1e20
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 176 deletions.
38 changes: 18 additions & 20 deletions Sources/SPMFractalNoise/FractalNoise.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
@_exported import SPMOpenSimplex2

public struct FractalOpenSimplex2NoiseParameters {
let openSimplex2Variant: OpenSimplex2Noise2Variant
let openSimplex3Variant: OpenSimplex2Noise3Variant
let openSimplex4Variant: OpenSimplex2Noise4Variant

public init(
openSimplex2Variant: OpenSimplex2Noise2Variant = .standard,
openSimplex3Variant: OpenSimplex2Noise3Variant = .xy,
openSimplex4Variant: OpenSimplex2Noise4Variant = .xyz
) {
self.openSimplex2Variant = openSimplex2Variant
self.openSimplex3Variant = openSimplex3Variant
self.openSimplex4Variant = openSimplex4Variant
}
}

public enum FractalNoiseTypeParameters {
case OpenSimplex2(FractalOpenSimplex2NoiseParameters)
case OpenSimplex2(OpenSimplex2NoiseParameters)
}

public struct FractalNoiseParameters {
Expand All @@ -30,7 +14,14 @@ public struct FractalNoiseParameters {
let startingAmplitude: Float
let startingFrequency: Float

public init(noiseTypeParameters: FractalNoiseTypeParameters, octaves: Int32, lacunarity: Float, hurstExponent: Float, startingAmplitude: Float, startingFrequency: Float) {
public init(
noiseTypeParameters: FractalNoiseTypeParameters,
octaves: Int32,
lacunarity: Float,
hurstExponent: Float,
startingAmplitude: Float,
startingFrequency: Float
) {
self.noiseTypeParameters = noiseTypeParameters
self.octaves = octaves
self.lacunarity = lacunarity
Expand All @@ -41,7 +32,14 @@ public struct FractalNoiseParameters {
}

public protocol FractalNoise {
func noise3(seed: Int32, coord: SIMD3<Float>, fractalNoiseParameters: FractalNoiseParameters) -> Float
func noise3(seed: Int32, coords: [SIMD3<Float>], fractalNoiseParameters: FractalNoiseParameters) -> [Float]
func noise3(
fractalNoiseParameters: FractalNoiseParameters,
coord: SIMD3<Float>
) -> Float

func noise3(
fractalNoiseParameters: FractalNoiseParameters,
coords: [SIMD3<Float>]
) -> [Float]
}

32 changes: 13 additions & 19 deletions Sources/SPMFractalNoise/FractalNoiseCPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,33 @@ public class FractalNoiseCPU {
}

private func getNoise3Value(
seed: Int32,
coord: SIMD3<Float>,
noiseType: FractalNoiseTypeParameters
noiseTypeParameters: FractalNoiseTypeParameters,
coord: SIMD3<Float>
) -> Float {
switch noiseType {
switch noiseTypeParameters {
case .OpenSimplex2(let parameters):
return openSimplex2.noise3(
seed: seed,
coord: coord,
variant: parameters.openSimplex3Variant)
openSimplex2NoiseParameters: parameters,
coord: coord)
}
}
}

extension FractalNoiseCPU: FractalNoise {
public func noise3(
seed: Int32,
coords: [SIMD3<Float>],
fractalNoiseParameters: FractalNoiseParameters
fractalNoiseParameters: FractalNoiseParameters,
coords: [SIMD3<Float>]
) -> [Float] {
return coords.map {
noise3(
seed: seed,
coord: $0,
fractalNoiseParameters: fractalNoiseParameters)
fractalNoiseParameters: fractalNoiseParameters,
coord: $0)
}
}

public func noise3(
seed: Int32,
coord: SIMD3<Float>,
fractalNoiseParameters: FractalNoiseParameters
fractalNoiseParameters: FractalNoiseParameters,
coord: SIMD3<Float>
) -> Float {
var fractalNoise = Float.zero
var amplitude = fractalNoiseParameters.startingAmplitude
Expand All @@ -49,9 +44,8 @@ extension FractalNoiseCPU: FractalNoise {

for _ in 0..<fractalNoiseParameters.octaves {
fractalNoise += amplitude * getNoise3Value(
seed: seed,
coord: coord * frequency,
noiseType: fractalNoiseParameters.noiseTypeParameters)
noiseTypeParameters: fractalNoiseParameters.noiseTypeParameters,
coord: coord * frequency)

frequency *= fractalNoiseParameters.lacunarity
amplitude *= gain
Expand Down
25 changes: 8 additions & 17 deletions Sources/SPMFractalNoise/FractalNoiseMetal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class FractalNoiseMetal {

private func executeNoiseFunction<T>(
pipeline: MTLComputePipelineState,
seed: Int32,
fractalNoiseParameters: FractalNoiseParameters,
data: [T]
) -> [Float] {
Expand All @@ -64,12 +63,7 @@ public class FractalNoiseMetal {
let (noiseType, noiseTypeParameters) = switch fractalNoiseParameters.noiseTypeParameters {
case .OpenSimplex2(let params):
(FractalNoiseMetalType.OpenSimplex2,
FractalNoiseMetalTypeParameters.OpenSimplex2(
OpenSimplex2MetalParameters(
seed: seed,
noise2Variant: params.openSimplex2Variant.toMetalVariant(),
noise3Variant: params.openSimplex3Variant.toMetalVariant(),
noise4Variant: params.openSimplex4Variant.toMetalVariant())))
FractalNoiseMetalTypeParameters.OpenSimplex2(params.toMetal()))
}

var uniforms = FractalNoiseMetalParameters(
Expand Down Expand Up @@ -129,28 +123,25 @@ public class FractalNoiseMetal {

extension FractalNoiseMetal: FractalNoise {
public func noise3(
seed: Int32,
coord: SIMD3<Float>,
fractalNoiseParameters: FractalNoiseParameters
fractalNoiseParameters: FractalNoiseParameters,
coord: SIMD3<Float>
) -> Float {
return noise3(
seed: seed,
coords: [coord],
fractalNoiseParameters: fractalNoiseParameters)[0]
fractalNoiseParameters: fractalNoiseParameters,
coords: [coord])[0]
}

public func noise3(
seed: Int32,
coords: [SIMD3<Float>],
fractalNoiseParameters: FractalNoiseParameters
fractalNoiseParameters: FractalNoiseParameters,
coords: [SIMD3<Float>]
) -> [Float] {
guard let pipeline = noise3Pipeline else {
return []
}
return executeNoiseFunction(
pipeline: pipeline,
seed: seed,
fractalNoiseParameters: fractalNoiseParameters,
data: coords)
}
}

15 changes: 3 additions & 12 deletions Sources/SPMFractalNoise/metal/FractalNoiseMetalNoiseShader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public struct FractalNoiseMetalParameters {
}

public extension FractalNoiseMetalParameters {
init(
seed: Int32,
fractalNoiseParameters: FractalNoiseParameters
) {
init(fractalNoiseParameters: FractalNoiseParameters) {
self.lacunarity = fractalNoiseParameters.lacunarity
self.gain = exp2(-fractalNoiseParameters.hurstExponent)
self.startingAmplitude = fractalNoiseParameters.startingAmplitude
Expand All @@ -50,13 +47,7 @@ public extension FractalNoiseMetalParameters {
switch fractalNoiseParameters.noiseTypeParameters {
case .OpenSimplex2(let openSimplexParams):
self.noiseType = .OpenSimplex2
self.noiseTypeParameters = .OpenSimplex2(
OpenSimplex2MetalParameters(
seed: seed,
noise2Variant: openSimplexParams.openSimplex2Variant.toMetalVariant(),
noise3Variant: openSimplexParams.openSimplex3Variant.toMetalVariant(),
noise4Variant: openSimplexParams.openSimplex4Variant.toMetalVariant()))

self.noiseTypeParameters = .OpenSimplex2(openSimplexParams.toMetal())
}
}
}
Expand Down Expand Up @@ -117,7 +108,7 @@ public class FractalNoiseMetalShaderLoader {
struct FractalNoiseMetalParameters {
float lacunarity;
float gain;
float startingAmplitude;
float startingFrequency;
Expand Down
67 changes: 43 additions & 24 deletions Sources/SPMOpenSimplex2/OpenSimplex2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,65 @@ public enum OpenSimplex2Noise4Variant {
case fallback
}

public struct OpenSimplex2NoiseParameters {
let seed: Int32
let noise2Variant: OpenSimplex2Noise2Variant
let noise3Variant: OpenSimplex2Noise3Variant
let noise4Variant: OpenSimplex2Noise4Variant

public init(
seed: Int32 = 0,
noise2Variant: OpenSimplex2Noise2Variant = .standard,
noise3Variant: OpenSimplex2Noise3Variant = .xy,
noise4Variant: OpenSimplex2Noise4Variant = .xyz
) {
self.seed = seed
self.noise2Variant = noise2Variant
self.noise3Variant = noise3Variant
self.noise4Variant = noise4Variant
}
}

public protocol OpenSimplex2 {
func noise2(seed: Int32, coord: SIMD2<Float>, variant: OpenSimplex2Noise2Variant) -> Float
func noise3(seed: Int32, coord: SIMD3<Float>, variant: OpenSimplex2Noise3Variant) -> Float
func noise4(seed: Int32, coord: SIMD4<Float>, variant: OpenSimplex2Noise4Variant) -> Float
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD2<Float>) -> Float
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD3<Float>) -> Float
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD4<Float>) -> Float

func noise2(seed: Int32, coord: SIMD2<Double>, variant: OpenSimplex2Noise2Variant) -> Float
func noise3(seed: Int32, coord: SIMD3<Double>, variant: OpenSimplex2Noise3Variant) -> Float
func noise4(seed: Int32, coord: SIMD4<Double>, variant: OpenSimplex2Noise4Variant) -> Float
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD2<Double>) -> Float
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD3<Double>) -> Float
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD4<Double>) -> Float

func noise2(seed: Int32, coords: [SIMD2<Float>], variant: OpenSimplex2Noise2Variant) -> [Float]
func noise3(seed: Int32, coords: [SIMD3<Float>], variant: OpenSimplex2Noise3Variant) -> [Float]
func noise4(seed: Int32, coords: [SIMD4<Float>], variant: OpenSimplex2Noise4Variant) -> [Float]
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD2<Float>]) -> [Float]
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD3<Float>]) -> [Float]
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD4<Float>]) -> [Float]

func noise2(seed: Int32, coords: [SIMD2<Double>], variant: OpenSimplex2Noise2Variant) -> [Float]
func noise3(seed: Int32, coords: [SIMD3<Double>], variant: OpenSimplex2Noise3Variant) -> [Float]
func noise4(seed: Int32, coords: [SIMD4<Double>], variant: OpenSimplex2Noise4Variant) -> [Float]
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD2<Double>]) -> [Float]
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD3<Double>]) -> [Float]
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD4<Double>]) -> [Float]
}

public extension OpenSimplex2 {
func noise2(seed: Int32, coord: SIMD2<Float>, variant: OpenSimplex2Noise2Variant) -> Float {
return noise2(seed: seed, coord: SIMD2<Double>(coord), variant: variant)
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD2<Float>) -> Float {
return noise2(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: SIMD2<Double>(coord))
}

func noise3(seed: Int32, coord: SIMD3<Float>, variant: OpenSimplex2Noise3Variant) -> Float {
return noise3(seed: seed, coord: SIMD3<Double>(coord), variant: variant)
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD3<Float>) -> Float {
return noise3(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: SIMD3<Double>(coord))
}

func noise4(seed: Int32, coord: SIMD4<Float>, variant: OpenSimplex2Noise4Variant) -> Float {
return noise4(seed: seed, coord: SIMD4<Double>(coord), variant: variant)
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD4<Float>) -> Float {
return noise4(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: SIMD4<Double>(coord))
}

func noise2(seed: Int32, coords: [SIMD2<Float>], variant: OpenSimplex2Noise2Variant) -> [Float] {
return noise2(seed: seed, coords: coords.map(SIMD2<Double>.init), variant: variant)
func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD2<Float>]) -> [Float] {
return noise2(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coords: coords.map(SIMD2<Double>.init))
}

func noise3(seed: Int32, coords: [SIMD3<Float>], variant: OpenSimplex2Noise3Variant) -> [Float] {
return noise3(seed: seed, coords: coords.map(SIMD3<Double>.init), variant: variant)
func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD3<Float>]) -> [Float] {
return noise3(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coords: coords.map(SIMD3<Double>.init))
}

func noise4(seed: Int32, coords: [SIMD4<Float>], variant: OpenSimplex2Noise4Variant) -> [Float] {
return noise4(seed: seed, coords: coords.map(SIMD4<Double>.init), variant: variant)
func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD4<Float>]) -> [Float] {
return noise4(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coords: coords.map(SIMD4<Double>.init))
}
}
44 changes: 22 additions & 22 deletions Sources/SPMOpenSimplex2/OpenSimplex2CPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -792,50 +792,50 @@ public class OpenSimplex2CPU {
}

extension OpenSimplex2CPU: OpenSimplex2 {
public func noise2(seed: Int32, coord: SIMD2<Double>, variant: OpenSimplex2Noise2Variant) -> Float {
return switch variant {
public func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD2<Double>) -> Float {
return switch openSimplex2NoiseParameters.noise2Variant {
case .standard:
noise2_Standard(seed: Int(seed), x: coord.x, y: coord.y)
noise2_Standard(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y)
case .x:
noise2_ImproveX(seed: Int(seed), x: coord.x, y: coord.y)
noise2_ImproveX(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y)
}
}

public func noise3(seed: Int32, coord: SIMD3<Double>, variant: OpenSimplex2Noise3Variant) -> Float {
return switch variant {
public func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD3<Double>) -> Float {
return switch openSimplex2NoiseParameters.noise3Variant {
case .xy:
noise3_ImproveXY(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z)
noise3_ImproveXY(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z)
case .xz:
noise3_ImproveXZ(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z)
noise3_ImproveXZ(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z)
case .fallback:
noise3_Fallback(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z)
noise3_Fallback(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z)
}
}

public func noise4(seed: Int32, coord: SIMD4<Double>, variant: OpenSimplex2Noise4Variant) -> Float {
return switch variant {
public func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coord: SIMD4<Double>) -> Float {
return switch openSimplex2NoiseParameters.noise4Variant {
case .xyz:
noise4_ImproveXYZ(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
noise4_ImproveXYZ(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
case .xyz_xy:
noise4_ImproveXYZ_ImproveXY(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
noise4_ImproveXYZ_ImproveXY(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
case .xyz_xz:
noise4_ImproveXYZ_ImproveXZ(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
noise4_ImproveXYZ_ImproveXZ(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
case .xy_zw:
noise4_ImproveXY_ImproveZW(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
noise4_ImproveXY_ImproveZW(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
case .fallback:
noise4_Fallback(seed: Int(seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
noise4_Fallback(seed: Int(openSimplex2NoiseParameters.seed), x: coord.x, y: coord.y, z: coord.z, w: coord.w)
}
}

public func noise2(seed: Int32, coords: [SIMD2<Double>], variant: OpenSimplex2Noise2Variant) -> [Float] {
return coords.map { noise2(seed: seed, coord: $0, variant: variant) }
public func noise2(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD2<Double>]) -> [Float] {
return coords.map { noise2(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: $0) }
}

public func noise3(seed: Int32, coords: [SIMD3<Double>], variant: OpenSimplex2Noise3Variant) -> [Float] {
return coords.map { noise3(seed: seed, coord: $0, variant: variant) }
public func noise3(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD3<Double>]) -> [Float] {
return coords.map { noise3(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: $0) }
}

public func noise4(seed: Int32, coords: [SIMD4<Double>], variant: OpenSimplex2Noise4Variant) -> [Float] {
return coords.map { noise4(seed: seed, coord: $0, variant: variant) }
public func noise4(openSimplex2NoiseParameters: OpenSimplex2NoiseParameters, coords: [SIMD4<Double>]) -> [Float] {
return coords.map { noise4(openSimplex2NoiseParameters: openSimplex2NoiseParameters, coord: $0) }
}
}
Loading

0 comments on commit 42f1e20

Please sign in to comment.