./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
66 / 66
100.0%
Branches:
36 / 40
90.0%
Functions:
10 / 10
100.0%

include/terraingen/noise/perlin.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Improved Perlin gradient noise in 2D and 3D.
5 #pragma once
6
7 #include "terraingen/noise/hash.h"
8 #include "terraingen/noise/lattice.h"
9
10 namespace terraingen {
11
12 namespace detail {
13
14 /// Dot product of an offset with one of eight unit gradients, the axes and the diagonals.
15 /// @param hash Corner hash; its low three bits pick x, -x, y, -y, then (x + y), (y - x), (x - y)
16 /// and -(x + y) over sqrt(2).
17 643102070 inline float PerlinGradient2(uint32_t hash, float x, float y) {
18 643102070 constexpr float kInvSqrt2 = 0.707106781186547524f;
19 643102070 const uint32_t h = hash & 7u;
20
2/2
✓ Branch 0 taken 380746327 times.
✓ Branch 1 taken 262355743 times.
643102070 const float axis = (h & 2u) != 0u ? y : x;
21
2/2
✓ Branch 0 taken 93092430 times.
✓ Branch 1 taken 550009640 times.
643102070 const float sx = h == 5u ? -x : x;
22
2/2
✓ Branch 0 taken 80529109 times.
✓ Branch 1 taken 562572961 times.
643102070 const float sy = h == 6u ? -y : y;
23 643102070 const float sum = sx + sy;
24
2/2
✓ Branch 0 taken 73666989 times.
✓ Branch 1 taken 569435081 times.
643102070 const float diagonal = (h == 7u ? -sum : sum) * kInvSqrt2;
25
4/4
✓ Branch 0 taken 351956704 times.
✓ Branch 1 taken 291145366 times.
✓ Branch 2 taken 185888941 times.
✓ Branch 3 taken 166067763 times.
643102070 return h < 4u ? ((h & 1u) != 0u ? -axis : axis) : diagonal;
26 }
27
28 /// The hash of a 2D lattice corner, shared by Perlin and simplex noise.
29 /// @param ix Corner x, wrapped to 32 bits.
30 /// @param iy Corner y, wrapped to 32 bits.
31 177283169 inline uint32_t PerlinCornerHash2(uint32_t ix, uint32_t iy, uint32_t seed) {
32
8/8
✓ Branch 0 taken 2281658 times.
✓ Branch 1 taken 242108 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 140 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
137269164 return Pcg3dHash(SeededLatticeKey2(ix, iy, seed)).x;
33 }
34
35 /// Dot product of an offset with one of Perlin's twelve cube-edge gradients (sixteen with
36 /// repeats).
37 /// @param hash Corner hash; its low four bits pick the gradient.
38 269191023 inline float PerlinGradient3(uint32_t hash, float x, float y, float z) {
39 269191030 const uint32_t h = hash & 15u;
40
2/2
✓ Branch 0 taken 140401103 times.
✓ Branch 1 taken 128789920 times.
269191023 const float u = h < 8u ? x : y;
41
7/8
✓ Branch 0 taken 209912443 times.
✓ Branch 1 taken 60095820 times.
✓ Branch 2 taken 174961619 times.
✓ Branch 3 taken 34950824 times.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 1 time.
269191030 const float v = h < 4u ? y : (h == 12u || h == 14u ? x : z);
42
4/4
✓ Branch 0 taken 140063390 times.
✓ Branch 1 taken 129127633 times.
✓ Branch 2 taken 139830939 times.
✓ Branch 3 taken 129360084 times.
269191023 return ((h & 1u) == 0u ? u : -u) + ((h & 2u) == 0u ? v : -v);
43 }
44
45 /// The hash of a 3D Perlin lattice corner.
46 /// @param ix Corner x, wrapped to 32 bits.
47 /// @param iy Corner y, wrapped to 32 bits.
48 /// @param iz Corner z, wrapped to 32 bits.
49 268376483 inline uint32_t PerlinCornerHash3(uint32_t ix, uint32_t iy, uint32_t iz, uint32_t seed) {
50 268376483 return Pcg3dHash(PerlinLatticeKey3(ix, iy, iz, seed)).x;
51 }
52
53 /// The gradient term of one corner of `point`'s cell: its gradient dotted with the offset from
54 /// it.
55 /// @param corner The corner as bits: 1 for +x, 2 for +y and 4 for +z.
56 263054881 inline float PerlinCorner3(LatticePoint3 point, uint32_t corner, uint32_t seed) {
57 263054881 const uint32_t dx = corner & 1u;
58 263054881 const uint32_t dy = (corner >> 1u) & 1u;
59 263054881 const uint32_t dz = corner >> 2u;
60 263054881 const uint32_t hash = PerlinCornerHash3(uint32_t(point.ix) + dx, uint32_t(point.iy) + dy,
61 263054881 uint32_t(point.iz) + dz, seed);
62 263054881 return PerlinGradient3(hash, point.fx - float(dx), point.fy - float(dy), point.fz - float(dz));
63 }
64
65 } // namespace detail
66
67 /// 2D improved Perlin gradient noise at a lattice cell plus fraction; zero at lattice
68 /// points. Passing the cell separately keeps precision far from the origin.
69 /// @param fx Fraction in x, in [0, 1).
70 /// @param fy Fraction in y, in [0, 1).
71 /// @return Noise in [-1, 1].
72 174759225 inline float PerlinNoise2(int32_t ix, int32_t iy, float fx, float fy, uint32_t seed) {
73 174759225 const uint32_t x0 = uint32_t(ix);
74 174759225 const uint32_t y0 = uint32_t(iy);
75 174759225 const uint32_t x1 = x0 + 1u;
76 174759225 const uint32_t y1 = y0 + 1u;
77
78 174759225 const float n00 = detail::PerlinGradient2(detail::PerlinCornerHash2(x0, y0, seed), fx, fy);
79 174759225 const float n10 =
80 174759225 detail::PerlinGradient2(detail::PerlinCornerHash2(x1, y0, seed), fx - 1.0f, fy);
81 174759225 const float n01 =
82 174759225 detail::PerlinGradient2(detail::PerlinCornerHash2(x0, y1, seed), fx, fy - 1.0f);
83 174759225 const float n11 =
84 174759225 detail::PerlinGradient2(detail::PerlinCornerHash2(x1, y1, seed), fx - 1.0f, fy - 1.0f);
85
86 174759225 const float u = detail::PerlinFade(fx);
87 174759225 const float v = detail::PerlinFade(fy);
88
89 174759225 constexpr float kSqrt2f = 1.41421356237309505f;
90 174759225 return kSqrt2f * detail::Lerp(v, detail::Lerp(u, n00, n10), detail::Lerp(u, n01, n11));
91 }
92
93 /// 2D improved Perlin gradient noise at a point in lattice units.
94 /// @return Noise in [-1, 1].
95 107858887 inline float PerlinNoise2(float x, float y, uint32_t seed) {
96 107858887 const LatticePoint2 point = SplitLattice2(x, y);
97 107858887 return PerlinNoise2(point.ix, point.iy, point.fx, point.fy, seed);
98 }
99
100 /// 2D improved Perlin gradient noise at a double-precision point in lattice units, split
101 /// into cell and fraction before narrowing so it stays exact far from the origin.
102 /// @return Noise in [-1, 1].
103 #ifndef __SLANG__
104 66593074 inline float PerlinNoise2Precise(double x, double y, uint32_t seed) {
105 66593074 const LatticePoint2 point = SplitLatticePrecise2(x, y);
106 66593074 return PerlinNoise2(point.ix, point.iy, point.fx, point.fy, seed);
107 }
108 #endif
109
110 /// 3D improved Perlin gradient noise at a lattice cell and fraction; zero at lattice
111 /// points and clamped to [-1, 1].
112 /// @return Noise in [-1, 1].
113 34400656 inline float PerlinNoise3(LatticePoint3 point, uint32_t seed) {
114 34400656 const float c000 = detail::PerlinCorner3(point, 0u, seed);
115 34400656 const float c100 = detail::PerlinCorner3(point, 1u, seed);
116 34400656 const float c010 = detail::PerlinCorner3(point, 2u, seed);
117 34400656 const float c110 = detail::PerlinCorner3(point, 3u, seed);
118 34400656 const float c001 = detail::PerlinCorner3(point, 4u, seed);
119 34400656 const float c101 = detail::PerlinCorner3(point, 5u, seed);
120 34400656 const float c011 = detail::PerlinCorner3(point, 6u, seed);
121 34400656 const float c111 = detail::PerlinCorner3(point, 7u, seed);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34400656 times.
34400656 const float u = detail::PerlinFade(point.fx);
123 34400656 const float v = detail::PerlinFade(point.fy);
124 34400656 const float w = detail::PerlinFade(point.fz);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34400656 times.
34400656 const float near = detail::Lerp(v, detail::Lerp(u, c000, c100), detail::Lerp(u, c010, c110));
126 34400656 const float far = detail::Lerp(v, detail::Lerp(u, c001, c101), detail::Lerp(u, c011, c111));
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34400656 times.
34400656 return clamp(detail::Lerp(w, near, far), -1.0f, 1.0f);
128 }
129
130 /// 3D improved Perlin gradient noise at a double-precision point in lattice units, split
131 /// into cell and fraction before narrowing so it stays exact on a planet's surface.
132 /// @return Noise in [-1, 1].
133 #ifndef __SLANG__
134 99853 inline float PerlinNoise3Precise(double x, double y, double z, uint32_t seed) {
135 99853 return PerlinNoise3(SplitLatticePrecise3(x, y, z), seed);
136 }
137 #endif
138
139 /// 3D improved Perlin gradient noise at a point in lattice units.
140 /// @return Noise in [-1, 1].
141 6940472 inline float PerlinNoise3(float x, float y, float z, uint32_t seed) {
142 6940472 return PerlinNoise3(SplitLattice3(x, y, z), seed);
143 }
144
145 } // namespace terraingen
146