Vectorized noise (but was it a bottleneck?)
This commit is contained in:
@@ -2,6 +2,12 @@ const std = @import("std");
|
||||
const math = @import("math.zig");
|
||||
|
||||
const Vector2 = math.Vector2;
|
||||
const Vector2x8 = math.Vector2x8;
|
||||
|
||||
const f32x8 = math.f32x8;
|
||||
const i32x8 = math.i32x8;
|
||||
|
||||
const ps = math.ps;
|
||||
|
||||
const Noise = struct {
|
||||
horizontal_scale: f32,
|
||||
@@ -11,6 +17,10 @@ const Noise = struct {
|
||||
pub fn sample(self: Noise, seed: u64, pos: Vector2) f32 {
|
||||
return math.noise2(seed, pos.divScalar(self.horizontal_scale)) * self.value_amplitude + self.value_median;
|
||||
}
|
||||
|
||||
pub fn samplev(self: Noise, seed: u64, pos: Vector2x8) f32x8 {
|
||||
return math.noise2x8(seed, pos.divScalar(self.horizontal_scale)) * ps(self.value_amplitude) + ps(self.value_median);
|
||||
}
|
||||
};
|
||||
|
||||
const noise_main: Noise = .{
|
||||
@@ -31,6 +41,16 @@ pub fn heightF(seed: u64, pos: Vector2) f32 {
|
||||
return @round(main + secondary);
|
||||
}
|
||||
|
||||
pub fn heightFv(seed: u64, pos: Vector2x8) f32x8 {
|
||||
const main = noise_main.samplev(seed, pos);
|
||||
const secondary = noise_secondary.samplev(seed, pos);
|
||||
return @round(main + secondary);
|
||||
}
|
||||
|
||||
pub fn heightI(seed: u64, pos: Vector2) i32 {
|
||||
return @intFromFloat(heightF(seed, pos));
|
||||
}
|
||||
|
||||
pub fn heightIv(seed: u64, pos: Vector2x8) i32x8 {
|
||||
return @intFromFloat(heightFv(seed, pos));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user