./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
642 / 654
98.2%
Branches:
316 / 346
91.3%
Functions:
58 / 58, 5 excl
100.0%

include/terraingen/layers/kernels/craters.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The kernel of crater shaping (`ApplyCraters`); a kernel header for C++ and Slang.
5 #pragma once
6
7 #include "terraingen/noise/fractal.h"
8 #include "terraingen/util/portable.h"
9
10 #ifndef __SLANG__
11 #include "terraingen/gpu/program.h"
12 #endif
13
14 namespace terraingen {
15
16 /// The fresh profile of simple craters, or a profile tabulated per crater.
17 enum class CraterProfileKind : uint32_t {
18 FassettThomson = 0, ///< `FassettThomsonProfile`.
19 MartinParkesDunstan = 1, ///< `MartinParkesDunstanProfile`.
20 /// Any other profile, tabulated per crater in its state: one row of heights over the
21 /// diameter, `tableCells` + 1 of them from the center to `tableExtent` diameters out.
22 Tabulated = 2,
23 };
24
25 /// A crater profile as the kernel evaluates it: a simple profile, complex craters from a
26 /// transition diameter (`ComplexCraterProfile`), and diffusion of the fresh shape by tables
27 /// (`DiffusedProfile`).
28 struct CraterProfileKernel {
29 CraterProfileKind simple = CraterProfileKind::FassettThomson; ///< The simple profile.
30 uint32_t rim = 0; ///< `FassettThomsonRim` as a number.
31 uint32_t complex = 0; ///< 1 when complex craters apply.
32 uint32_t diffused = 0; ///< 1 when the profile is diffused by tables.
33 float transitionDiameter = 0.0f; ///< `ComplexCraterShape::transitionDiameter` (m).
34 float floorCoefficient = 0.0f; ///< Coefficient of the floor diameter law.
35 float floorExponent = 0.0f; ///< Exponent of the floor diameter law.
36 float peakHeightCoefficient = 0.0f; ///< Coefficient of the peak height law.
37 float peakHeightExponent = 0.0f; ///< Exponent of the peak height law.
38 float peakWidthCoefficient = 0.0f; ///< Coefficient of the peak diameter or volume law.
39 float peakWidthExponent = 0.0f; ///< Exponent of the peak diameter or volume law.
40 uint32_t peakFromVolume = 0; ///< 1 when the peak's base follows from its volume.
41 float peakFitMaxDiameter = 0.0f; ///< Largest diameter of the peak fit (m); 0 for none.
42 float ejectaExponent = 3.0f; ///< `ComplexCraterShape::ejectaExponent`.
43 float complexReachRadii = 3.0f; ///< Reach of complex craters, in radii.
44 float tableExtent = 1.5f; ///< Where tabulated heights end, in diameters from the center.
45 uint32_t tableCells = 0; ///< Cells of a tabulated profile row; rows hold one more height.
46 uint32_t agesOffset = 0; ///< Where the tabulated diffusion ages start in `CraterData`.
47 uint32_t ageCount = 0; ///< Tabulated diffusion ages.
48 };
49
50 /// How craters are shaped, as `CraterApplyOptions` and the profile give it.
51 struct CraterShapeKernel {
52 CraterProfileKernel profile; ///< The profile.
53 float reachRadii = 1.5f; ///< `CraterReachRadii`.
54 float profileReachRadii = 1.5f; ///< The profile's own reach.
55 float blendStart = 0.8f; ///< `CraterBlend::startRadii`.
56 float blendEnd = 1.2f; ///< `CraterBlend::endRadii`.
57 uint32_t blendShape = 0; ///< `BlendShape` as a number.
58 float interiorReplacement = 0.0f; ///< `CraterApplyOptions::interiorReplacement`.
59 float replaceStart = 1.0f; ///< `CraterReplacement::startRadii`.
60 float replaceEnd = 1.5f; ///< `CraterReplacement::EndRadii` of the profile.
61 float mantling = 0.0f; ///< `CraterTexture::mantling`.
62 float ejectaReachRadii = 3.0f; ///< `CraterTexture::ejectaReachRadii`.
63 float rimSigma = 0.0f; ///< `RimSmoothing::sigmaRadii`.
64 float rimInner = 0.8f; ///< `RimSmoothing::innerRadii`.
65 float rimOuter = 1.2f; ///< `RimSmoothing::outerRadii`.
66 float rimStrength = 1.0f; ///< `RimSmoothing::strength`.
67 uint32_t rimShape = 0; ///< `RimSmoothing::shape` as a number.
68 float noiseAmplitude = 0.0f; ///< `CraterNoise::amplitude`.
69 float noiseWavelength = 0.5f; ///< `CraterNoise::wavelengthRadii`.
70 int noiseOctaves = 4; ///< `CraterNoise::octaves`.
71 float noiseGain = 0.5f; ///< `CraterNoise::gain`.
72 float noiseRimAmplitude = 0.0f; ///< `CraterNoise::rimAmplitude`.
73 float noiseRimWavelength = 0.1f; ///< `CraterNoise::rimWavelengthRadii`.
74 float noiseRimInner = 0.8f; ///< `CraterNoise::rimInnerRadii`.
75 float noiseRimOuter = 1.2f; ///< `CraterNoise::rimOuterRadii`.
76 float wallStreaks = 0.0f; ///< `CraterTexture::wallStreaks`.
77 float wallStreakWidth = 0.15f; ///< `CraterTexture::wallStreakWidthRadii`.
78 float ejecta = 0.0f; ///< `CraterTexture::ejecta`.
79 float ejectaLineation = 0.15f; ///< `CraterTexture::ejectaLineationRadii`.
80 float minFreshness = 0.5f; ///< `CraterTexture::minFreshness`.
81 };
82
83 /// One crater ready to shape.
84 struct CraterRecord {
85 float x = 0.0f; ///< Center east (m) as a float; `xLow` holds the remainder.
86 float y = 0.0f; ///< Center north (m) as a float; `yLow` holds the remainder.
87 float diameter = 0.0f; ///< Diameter (m).
88 float resolution = 1.0f; ///< `ResolutionWeight` at the sample spacing.
89 float freshDepth = 0.0f; ///< Fresh depth over diameter.
90 float freshRim = 0.0f; ///< Fresh rim height over diameter.
91 float currentDepth = 0.0f; ///< Current depth over diameter.
92 float currentRim = 0.0f; ///< Current rim height over diameter.
93 float diffusion = 0.0f; ///< `CraterState::diffusion`.
94 float ellipticity = 1.0f; ///< The outline's long over short axis.
95 float heading = 0.0f; ///< The outline's long axis, radians from east.
96 uint32_t firstHarmonic = 2; ///< Order of the outline's first harmonic.
97 uint32_t harmonicCount = 0; ///< The outline's harmonics.
98 uint32_t harmonicOffset = 0; ///< Amplitude and phase pairs in `CraterData`.
99 uint32_t tableCount = 0; ///< Entries of the outline's deviation table.
100 uint32_t tableOffset = 0; ///< The outline's deviations in `CraterData`.
101 uint32_t profileOffset = 0; ///< Its diffusion or profile table in `CraterData`.
102 uint32_t hasDatum = 0; ///< 1 when the crater has a datum plane.
103 float datumA = 0.0f; ///< `Plane::a` of the datum (m).
104 float datumB = 0.0f; ///< `Plane::b` of the datum.
105 float datumC = 0.0f; ///< `Plane::c` of the datum.
106 uint32_t noiseKey = 0; ///< Hash key of its noise.
107 uint32_t textureKey = 0; ///< Hash key of its texture.
108 uint32_t markCount = 0; ///< Knots of its outline marks; 0 for none.
109 /// The marks' inner, outer and fade radii, then their knots, in `CraterData`.
110 uint32_t markOffset = 0;
111 float shiftX = 0.0f; ///< `CraterSlopeShape::shift` east (m).
112 float shiftY = 0.0f; ///< `CraterSlopeShape::shift` north (m).
113 float upslopeX = 0.0f; ///< `CraterSlopeShape::upslope` east.
114 float upslopeY = 0.0f; ///< `CraterSlopeShape::upslope` north.
115 uint32_t terraceCount = 0; ///< Wall terrace benches; 0 for none.
116 uint32_t terraceOffset = 0; ///< The terraces' shape in `CraterData` (`KernelTerracedRadii`).
117 uint32_t peakCount = 0; ///< Massifs of the central peak; 0 keeps the profile's dome.
118 uint32_t peakOffset = 0; ///< The massifs in `CraterData` (`KernelPeakMassifs`).
119 uint32_t replace = 0; ///< 1 when the crater replaces the terrain (`Crater::replace`).
120 float xLow = 0.0f; ///< What `x` leaves of the center east (m).
121 float yLow = 0.0f; ///< What `y` leaves of the center north (m).
122 };
123
124 /// A point east and north of a crater's center, in meters, as the kernel reads it.
125 struct KernelOffset {
126 float dx = 0.0f; ///< East (m).
127 float dy = 0.0f; ///< North (m).
128 };
129
130 /// A crater's local frame on a body, from which the GPU works out each sample's offset
131 /// (`KernelFrameOffset`) as `Surface::Offset` does.
132 78936 struct CraterFrame {
133 float x = 0.0f; ///< Center's position from the dispatch's origin (m), as `Surface::Position`.
134 float y = 0.0f;
135 float z = 0.0f;
136 float eastX = 1.0f; ///< Unit vector east at the center.
137 float eastY = 0.0f;
138 float eastZ = 0.0f;
139 float northX = 0.0f; ///< Unit vector north at the center.
140 float northY = 1.0f;
141 float northZ = 0.0f;
142 /// The radius (m) of a sphere, whose offsets are azimuthal equidistant; 0 on an ellipsoid or
143 /// shape model, whose offsets are projected on the tangent plane.
144 float sphereRadius = 0.0f;
145 float xLow = 0.0f; ///< What `x`, `y` and `z` leave of the center's position (m).
146 float yLow = 0.0f;
147 float zLow = 0.0f;
148 };
149
150 /// `a` - `b` for coordinates held as a float and the float of its remainder: the same bits on
151 /// every device and for every query, with no origin to measure from.
152 53110539 inline float SplitDifference(float a, float aLow, float b, float bLow) {
153 53110449 return (a - b) + (aLow - bLow);
154 }
155
156 /// asin(`s`) / `s` for `s` in [0, 1]: by its series below 0.25, to within float rounding, since
157 /// GPUs may evaluate inverse trigonometric functions to only a few thousand ulp (Vulkan allows
158 /// 4096), which would be millimeters over a crater.
159 5895854 inline float KernelArcsineRatio(float s) {
160
2/2
✓ Branch 0 taken 5895103 times.
✓ Branch 1 taken 751 times.
5895854 if (s < 0.25f) {
161 5895103 const float s2 = s * s;
162 5895103 return 1.0f + s2 * (1.0f / 6.0f +
163 5895103 s2 * (3.0f / 40.0f + s2 * (5.0f / 112.0f + s2 * (35.0f / 1152.0f))));
164 }
165 751 return atan2(s, sqrt(1.0f - s * s)) / s;
166 }
167
168 /// The offset in crater frame `f` of the point (`dx`, `dy`, `dz`) meters from its center:
169 /// `Surface::Offset` in single precision.
170 19047726 inline KernelOffset KernelFrameOffset(KERNEL_IN(CraterFrame) f, float dx, float dy, float dz) {
171 19047726 KernelOffset offset;
172 19047726 offset.dx = dx * f.eastX + dy * f.eastY + dz * f.eastZ;
173 19047726 offset.dy = dx * f.northX + dy * f.northY + dz * f.northZ;
174
2/2
✓ Branch 0 taken 5895337 times.
✓ Branch 1 taken 13152389 times.
19047726 if (f.sphereRadius > 0.0f) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5895337 times.
5895337 const float chord = sqrt(dx * dx + dy * dy + dz * dz);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5895337 times.
5895337 const float arc = chord * KernelArcsineRatio(min(0.5f * chord / f.sphereRadius, 1.0f));
177
2/2
✓ Branch 0 taken 5894694 times.
✓ Branch 1 taken 376 times.
5895070 const float across = sqrt(offset.dx * offset.dx + offset.dy * offset.dy);
178
2/2
✓ Branch 0 taken 5894694 times.
✓ Branch 1 taken 376 times.
5895070 if (across > 0.0f) {
179 5894694 offset.dx *= arc / across;
180 5894694 offset.dy *= arc / across;
181 } else {
182 offset.dy = arc;
183 }
184 }
185 19047459 return offset;
186 }
187
188 /// A sample's coordinates (m), each a float and the float of its remainder (`SplitDifference`):
189 /// east and north on a plane, or its position (`Surface::Position`) on a body.
190 struct SplitPoint {
191 float x;
192 float xLow;
193 float y;
194 float yLow;
195 float z;
196 float zLow;
197 };
198
199 /// The offset of `p` from crater `c`'s center on a plane.
200 34062258 inline KernelOffset PlanarSplitOffset(KERNEL_IN(CraterRecord) c, KERNEL_IN(SplitPoint) p) {
201 34062258 KernelOffset offset;
202 34062258 offset.dx = SplitDifference(p.x, p.xLow, c.x, c.xLow);
203 34062258 offset.dy = SplitDifference(p.y, p.yLow, c.y, c.yLow);
204 34062258 return offset;
205 }
206
207 /// The offset of `p` in crater frame `f` on a body (`KernelFrameOffset`).
208 19048265 inline KernelOffset BodySplitOffset(KERNEL_IN(CraterFrame) f, KERNEL_IN(SplitPoint) p) {
209 19048265 return KernelFrameOffset(f, SplitDifference(p.x, p.xLow, f.x, f.xLow),
210 19048265 SplitDifference(p.y, p.yLow, f.y, f.yLow),
211 19048265 SplitDifference(p.z, p.zLow, f.z, f.zLow));
212 }
213
214 #ifdef __SLANG__
215 /// Where the variable-length values of craters are read from: a buffer the dispatch binds.
216 typedef StructuredBuffer<float> CraterData;
217
218 /// The float at `index` of `data`.
219 inline float CraterValue(CraterData data, uint32_t index) {
220 return data[index];
221 }
222
223 /// Crater records and frames, floats and unsigned integers a dispatch binds, indexed as arrays.
224 typedef StructuredBuffer<CraterRecord> CraterRecords;
225 typedef StructuredBuffer<CraterFrame> CraterFrames;
226 typedef StructuredBuffer<float> KernelFloats;
227 typedef StructuredBuffer<uint> KernelUints;
228 #else
229 /// Crater records and frames, floats and unsigned integers, indexed as arrays.
230 using CraterRecords = const CraterRecord*;
231 using CraterFrames = const CraterFrame*;
232 using KernelFloats = const float*;
233 using KernelUints = const uint32_t*;
234
235 /// Where the variable-length values of craters are read from.
236 struct CraterData {
237 const float* values = nullptr;
238 };
239
240 /// The float at `index` of `data`.
241 737009642 inline float CraterValue(CraterData data, uint32_t index) {
242 737009642 return data.values[index];
243 }
244 #endif
245
246 /// 1 - `t` shaped by `BlendShape` number `shape` (linear, smoothstep, smootherstep, Gaussian), for
247 /// `t` in [0, 1].
248 10629661 inline float KernelFalloffShape(float t, uint32_t shape) {
249
2/2
✓ Branch 0 taken 8719 times.
✓ Branch 1 taken 10620942 times.
10629661 if (shape == 0u) {
250 8719 return 1.0f - t;
251 }
252
2/2
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 10619142 times.
10620942 if (shape == 1u) {
253 1800 return 1.0f - t * t * (3.0f - 2.0f * t);
254 }
255
2/2
✓ Branch 0 taken 10617342 times.
✓ Branch 1 taken 1800 times.
10619142 if (shape == 2u) {
256 10617342 return 1.0f - t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
257 }
258 1800 const float tail = exp(-4.5f);
259 1800 const float z = 3.0f * t;
260 1800 return (exp(-0.5f * z * z) - tail) / (1.0f - tail);
261 }
262
263 /// 1 up to `start`, 0 from `end`, shaped by `shape` between (`Falloff`).
264 33632562 inline float KernelFalloff(float x, float start, float end, uint32_t shape) {
265
2/2
✓ Branch 0 taken 29906554 times.
✓ Branch 1 taken 3726008 times.
33632562 if (x <= start) {
266 return 1.0f;
267 }
268
2/2
✓ Branch 0 taken 10630111 times.
✓ Branch 1 taken 19276443 times.
29906554 if (x >= end) {
269 return 0.0f;
270 }
271 10630111 return KernelFalloffShape((x - start) / (end - start), shape);
272 }
273
274 /// Smoothstep of `t` clamped to [0, 1].
275 66304222 inline float KernelSmoothstep(float t) {
276
2/2
✓ Branch 0 taken 116523 times.
✓ Branch 1 taken 66187699 times.
66304222 const float u = clamp(t, 0.0f, 1.0f);
277 66304222 return u * u * (3.0f - 2.0f * u);
278 }
279
280 /// The direction of (`dx`, `dy`) in radians from east, 0 at the origin: fast math on some GPUs
281 /// leaves `atan2(0, 0)` undefined.
282 49366482 inline float KernelAngle(float dy, float dx) {
283
2/2
✓ Branch 0 taken 49379507 times.
✓ Branch 1 taken 599 times.
49366482 if (dx == 0.0f && dy == 0.0f) {
284 return 0.0f;
285 }
286 49379507 return atan2(dy, dx);
287 }
288
289 /// pi and 2 pi in single precision.
290 static const float kKernelPi = 3.14159265358979f;
291 static const float kKernelTwoPi = 6.28318530717959f;
292
293 /// A periodic table's knot below an angle and the fraction of the way to the next.
294 struct KernelPeriodicCell {
295 uint32_t index;
296 float t;
297 };
298
299 /// Where angle `turns`, in turns, falls in a periodic table of `size` knots around a crater.
300 180763 inline KernelPeriodicCell KernelPeriodicAt(float turns, uint32_t size) {
301
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 180759 times.
180763 const float position = (turns - floor(turns)) * float(size);
302 180763 KernelPeriodicCell cell;
303
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 180759 times.
180763 cell.index = min(uint32_t(position), size - 1u);
304 180763 cell.t = position - float(cell.index);
305 180763 return cell;
306 }
307
308 /// McGetchin et al.'s (1973) ejecta thickness (m) at the rim of a crater of `radius` meters.
309 9803964 inline float KernelEjectaScale(float radius) {
310 2798021 return 0.14f * pow(radius, 0.74f);
311 }
312
313 /// McGetchin et al.'s (1973) ejecta thickness at `radii` crater radii over that at the rim,
314 /// (r / R)^-3, tapered linearly in it to nothing at `reachRadii`; 1 within the rim.
315 7014944 inline float KernelEjectaShare(float radii, float reachRadii) {
316
1/2
✓ Branch 0 taken 7014944 times.
✗ Branch 1 not taken.
7014944 if (radii <= 1.0f) {
317 return 1.0f;
318 }
319
1/2
✓ Branch 0 taken 7014944 times.
✗ Branch 1 not taken.
7014944 if (radii >= reachRadii) {
320 return 0.0f;
321 }
322 7014945 const float atReach = 1.0f / (reachRadii * reachRadii * reachRadii);
323 7014944 return (1.0f / (radii * radii * radii) - atReach) / (1.0f - atReach);
324 }
325
326 /// Fassett and Thomson's exterior polynomial over the diameter at `x` radii, which falls to 0 at
327 /// 1.5, tapered by 1 - u^2 for u from 0 where it starts at `start` radii to 1 there, so its slope
328 /// also falls to 0 and the crater joins the ground smoothly; the taper leaves the height and slope
329 /// where it starts unchanged.
330 5095309 inline float FassettThomsonExterior(float x, float start) {
331 5095309 const float polynomial =
332 5095309 0.188253307f + x * (-0.187050452f + x * (0.01844746f + x * 0.01505647f));
333 5095309 const float u = (x - start) / (1.5f - start);
334 5095309 return polynomial * (1.0f - u * u);
335 }
336
337 /// `FassettThomsonProfile::Height` over the diameter.
338 15715861 inline float FassettThomsonShape(float x, uint32_t rim, float freshRim, float currentDepth) {
339
2/2
✓ Branch 0 taken 13906350 times.
✓ Branch 1 taken 1809511 times.
15715861 if (x > 1.5f) {
340 return 0.0f;
341 }
342 13906350 const float interior =
343 13906350 -0.228809953f + x * (0.227533882f + x * (0.083116795f - x * 0.039499407f));
344 13906350 float h = 0.0f;
345
2/2
✓ Branch 0 taken 2894576 times.
✓ Branch 1 taken 11011774 times.
13906350 if (rim == 2u) {
346
2/2
✓ Branch 0 taken 1885054 times.
✓ Branch 1 taken 1009522 times.
2894576 if (x <= 0.98f) {
347 1885054 h = interior;
348
2/2
✓ Branch 0 taken 78909 times.
✓ Branch 1 taken 930613 times.
1009522 } else if (x <= 1.02f) {
349 78909 h = freshRim;
350 } else {
351 930613 h = FassettThomsonExterior(x, 1.02f);
352 }
353 } else {
354
2/2
✓ Branch 0 taken 2890198 times.
✓ Branch 1 taken 8121576 times.
11011774 const float edge = rim == 0u ? 0.98f : 1.0f;
355
2/2
✓ Branch 0 taken 1162057 times.
✓ Branch 1 taken 9849717 times.
11011774 if (x <= 0.2f) {
356 1162057 h = -0.181f;
357
2/2
✓ Branch 0 taken 5685012 times.
✓ Branch 1 taken 4164705 times.
9849717 } else if (x <= edge) {
358 5685012 h = interior;
359 } else {
360 4164705 h = FassettThomsonExterior(x, edge);
361 }
362 }
363
2/2
✓ Branch 0 taken 4521449 times.
✓ Branch 1 taken 9384901 times.
18427799 return max(h, freshRim - currentDepth);
364 }
365
366 /// A profile's height (m) and its slope over the radius at a point.
367 struct KernelProfilePoint {
368 float height;
369 float slope;
370 };
371
372 /// Martin, Parkes and Dunstan's ejecta outside the rim, `x` radii beyond it in [0, 1]: the power
373 /// law of McGetchin et al. with a linear term that meets `rim` (m) at the crest and 0 at twice the
374 /// radius, tapered by 1 - x^2 so its slope also falls to 0 there and the crater joins the ground
375 /// smoothly; the taper leaves the height and slope at the crest unchanged.
376 2797857 inline KernelProfilePoint MartinParkesDunstanExterior(float x, float rim, float ejectaScale) {
377 2797857 const float offset = rim - ejectaScale;
378 2797857 const float gradient = -ejectaScale / 8.0f - offset;
379 2797857 const float r = 1.0f / (x + 1.0f);
380 2797857 const float law = ejectaScale * r * r * r + offset + gradient * x;
381 2797857 const float lawSlope = -3.0f * ejectaScale * r * r * r * r + gradient;
382 2797857 const float taper = 1.0f - x * x;
383 2797857 KernelProfilePoint point;
384 2797857 point.height = law * taper;
385 2797857 point.slope = lawSlope * taper - 2.0f * x * law;
386 2797857 return point;
387 }
388
389 /// `MartinParkesDunstanProfile::Height` (m); the host checks the state.
390 7912278 inline float MartinParkesDunstanHeight(float distance, float diameter, KERNEL_IN(CraterRecord) c) {
391 7912278 const float depth = c.currentDepth * diameter;
392 7912278 const float rim = c.currentRim * diameter;
393 7912278 const float freshRim = c.freshRim * diameter;
394 7912278 const float radius = 0.5f * diameter;
395 7912278 const float x = distance / radius - 1.0f;
396
2/2
✓ Branch 0 taken 7422648 times.
✓ Branch 1 taken 489630 times.
7912278 if (x >= 1.0f) {
397 return 0.0f;
398 }
399 7422648 const float k = freshRim - rim + depth;
400 7422648 const float alpha = (rim - freshRim) / k;
401
2/2
✓ Branch 0 taken 4387504 times.
✓ Branch 1 taken 3035144 times.
7422648 if (x <= alpha) {
402 4387504 return (k * x + 2.0f * k) * x + freshRim;
403 }
404
2/2
✓ Branch 0 taken 237124 times.
✓ Branch 1 taken 2798020 times.
3035144 if (x <= 0.0f) {
405 237124 return k * (alpha + 1.0f) / alpha * x * x + rim;
406 }
407 2798020 const float ejectaScale =
408
1/2
✓ Branch 0 taken 2798020 times.
✗ Branch 1 not taken.
2798020 freshRim > 0.0f ? KernelEjectaScale(radius) * rim / freshRim : KernelEjectaScale(radius);
409
1/2
✓ Branch 0 taken 2798057 times.
✗ Branch 1 not taken.
2798020 const float beta = min(1.0f, 1.5f * (freshRim - rim) / k);
410
2/2
✓ Branch 0 taken 321881 times.
✓ Branch 1 taken 2476139 times.
2798020 if (x <= beta) {
411 321881 const KernelProfilePoint join = MartinParkesDunstanExterior(beta, rim, ejectaScale);
412 321881 const float drop = rim - join.height;
413
3/4
✓ Branch 0 taken 321851 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 321851 times.
321881 const float slope = drop > 0.0f ? max(join.slope, -3.0f * drop / beta) : join.slope;
414 321881 const float t = x / beta;
415 321881 return rim + (join.height - rim) * t * t * (3.0f - 2.0f * t) +
416 321881 beta * slope * t * t * (t - 1.0f);
417 }
418 2476139 return MartinParkesDunstanExterior(x, rim, ejectaScale).height;
419 }
420
421 /// The simple profile's height (m) in crater `c`'s current state.
422 23616952 inline float SimpleProfileHeight(KERNEL_IN(CraterProfileKernel) p, float distance, float diameter,
423 KERNEL_IN(CraterRecord) c) {
424
2/2
✓ Branch 0 taken 7903310 times.
✓ Branch 1 taken 15713642 times.
23616952 if (p.simple == CraterProfileKind::MartinParkesDunstan) {
425 7903310 return MartinParkesDunstanHeight(distance, diameter, c);
426 }
427 15713642 return FassettThomsonShape(distance / (0.5f * diameter), p.rim, c.freshRim, c.currentDepth) *
428 15713642 diameter;
429 }
430
431 /// y (m) of a power law `coefficient` D^`exponent` in kilometers, or m^3 with `volume`.
432 4289884 inline float KilometerLaw(float coefficient, float exponent, float diameter, bool volume) {
433
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 501 times.
3962620 const float kilometers = coefficient * pow(diameter / 1000.0f, exponent);
434
4/6
terraingen::KilometerLaw(float, float, float, bool):
✓ Branch 0 taken 109193 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 501 times.
terraingen::SimpleProfileHeight(terraingen::CraterProfileKernel const&, float, float, terraingen::CraterRecord const&):
✓ Branch 0 taken 1271198 times.
✗ Branch 1 not taken.
1862518 return volume ? kilometers * 1e9f : kilometers * 1000.0f;
435 }
436
437 /// The height (m) and base radius (m) of a complex crater's fresh central peak.
438 struct KernelPeakSize {
439 float height;
440 float base;
441 };
442
443 /// `KernelPeakSize` of a complex crater `diameter` (m) across, from the profile's peak laws;
444 /// craters larger than the laws were fitted to keep that size's peak, scaled with the crater.
445 1454536 inline KernelPeakSize KernelComplexPeak(KERNEL_IN(CraterProfileKernel) p, float diameter) {
446 1454536 const float fitted =
447
3/4
✓ Branch 0 taken 1454536 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185033 times.
✓ Branch 3 taken 1269503 times.
1639569 p.peakFitMaxDiameter > 0.0f ? min(diameter, p.peakFitMaxDiameter) : diameter;
448 1454536 KernelPeakSize size;
449
2/2
✓ Branch 0 taken 973145 times.
✓ Branch 1 taken 481933 times.
1454536 size.height = KilometerLaw(p.peakHeightCoefficient, p.peakHeightExponent, fitted, false);
450
2/2
✓ Branch 0 taken 973145 times.
✓ Branch 1 taken 481933 times.
1454536 if (p.peakFromVolume == 1u) {
451 973145 const float volume =
452 973145 KilometerLaw(p.peakWidthCoefficient, p.peakWidthExponent, fitted, true);
453 973145 size.base = sqrt(3.0f * volume / (kKernelPi * size.height));
454 } else {
455 481933 size.base = 0.5f * KilometerLaw(p.peakWidthCoefficient, p.peakWidthExponent, fitted, false);
456 }
457 1454536 size.base *= diameter / fitted;
458 1454536 return size;
459 }
460
461 /// The round dome (m) of central peak `size` at `distance` (m) from its center.
462 1389330 inline float KernelPeakDome(KernelPeakSize size, float distance) {
463
4/4
✓ Branch 0 taken 6280 times.
✓ Branch 1 taken 111852 times.
✓ Branch 2 taken 407524 times.
✓ Branch 3 taken 863674 times.
1389330 if (!(distance < size.base)) {
464 return 0.0f;
465 }
466 413804 const float u = 1.0f - (distance / size.base) * (distance / size.base);
467 413804 return size.height * u * u;
468 }
469
470 /// The share of its fresh depth crater `c` keeps, which lowers its central peak as it wears.
471 1387869 inline float KernelKeptDepth(KERNEL_IN(CraterRecord) c) {
472 1387869 return c.freshDepth > 0.0f ? c.currentDepth / c.freshDepth : 1.0f;
473 }
474
475 /// `ComplexCraterProfile::Height` of a complex crater (m).
476 3482390 inline float ComplexProfileHeight(KERNEL_IN(CraterProfileKernel) p, float distance, float diameter,
477 KERNEL_IN(CraterRecord) c) {
478 3482390 const float radius = 0.5f * diameter;
479 3482390 const float x = distance / radius;
480 3482390 const float depth = c.currentDepth * diameter;
481 3482390 const float rim = c.currentRim * diameter;
482
2/2
✓ Branch 0 taken 2102080 times.
✓ Branch 1 taken 1380310 times.
3482390 if (x >= 1.0f) {
483
2/2
✓ Branch 0 taken 2097059 times.
✓ Branch 1 taken 5951 times.
2102080 if (x >= p.complexReachRadii) {
484 return 0.0f;
485 }
486 2097059 const float edge = pow(p.complexReachRadii, -p.ejectaExponent);
487 2097059 return rim * (pow(x, -p.ejectaExponent) - edge) / (1.0f - edge);
488 }
489 1380310 const float floorShare =
490
1/2
✓ Branch 0 taken 1380391 times.
✗ Branch 1 not taken.
1380310 min(0.9f, KilometerLaw(p.floorCoefficient, p.floorExponent, diameter, false) / diameter);
491 1380310 float height = rim - depth;
492
2/2
✓ Branch 0 taken 751370 times.
✓ Branch 1 taken 628940 times.
1380310 if (x > floorShare) {
493 751370 const float t = (x - floorShare) / (1.0f - floorShare);
494 751370 height += depth * t * t;
495 }
496
1/2
✓ Branch 0 taken 1380310 times.
✗ Branch 1 not taken.
4142188 return height + KernelKeptDepth(c) * KernelPeakDome(KernelComplexPeak(p, diameter), distance);
497 }
498
499 /// The fresh profile's height (m): simple or complex.
500 4740072 inline float FreshProfileHeight(KERNEL_IN(CraterProfileKernel) p, float distance, float diameter,
501 KERNEL_IN(CraterRecord) c) {
502
4/4
✓ Branch 0 taken 668074 times.
✓ Branch 1 taken 4071998 times.
✓ Branch 2 taken 467168 times.
✓ Branch 3 taken 202464 times.
4740072 if (p.complex == 1u && diameter >= p.transitionDiameter) {
503 467168 return ComplexProfileHeight(p, distance, diameter, c);
504 }
505 4272904 return SimpleProfileHeight(p, distance, diameter, c);
506 }
507
508 /// A table row's cell at a distance and the fraction of the way across it.
509 struct KernelTableCell {
510 uint32_t index;
511 float t;
512 };
513
514 /// Where `x` diameters from the center falls in a row of `p`'s tables.
515 30782279 inline KernelTableCell KernelTableAt(KERNEL_IN(CraterProfileKernel) p, float x) {
516 30782279 const float position = x / p.tableExtent * float(p.tableCells);
517 30782279 KernelTableCell cell;
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30782279 times.
30782279 cell.index = min(uint32_t(position), p.tableCells - 1u);
519 30782279 cell.t = position - float(cell.index);
520 30782279 return cell;
521 }
522
523 /// The value `t` of the way from `data[at]` to `data[at + 1]`.
524 61427182 inline float KernelLerp(CraterData data, uint32_t at, float t) {
525 61427182 return (1.0f - t) * CraterValue(data, at) + t * CraterValue(data, at + 1u);
526 }
527
528 /// The table row below a diffusion age and the weight of the row above it.
529 struct KernelAgeRow {
530 uint32_t row;
531 float weight;
532 };
533
534 /// Where diffusion age `tau` falls among `p`'s tabulated ages, interpolating in log tau (linearly
535 /// in tau before the first nonzero age), as `DiffusedProfile` does.
536 30723238 inline KernelAgeRow KernelAgeRowAt(KERNEL_IN(CraterProfileKernel) p, CraterData data, float tau) {
537 30723238 KernelAgeRow age;
538 30723238 age.row = 0u;
539 30723238 age.weight = 0.0f;
540 30723238 const uint32_t last = p.ageCount - 1u;
541 30723238 const float first = CraterValue(data, p.agesOffset + 1u);
542
2/2
✓ Branch 0 taken 30721753 times.
✓ Branch 1 taken 2070 times.
30723238 if (tau >= CraterValue(data, p.agesOffset + last)) {
543 age.row = last;
544
2/2
✓ Branch 0 taken 22084979 times.
✓ Branch 1 taken 8636774 times.
30721753 } else if (tau < first) {
545 8636774 age.weight = tau / first;
546 } else {
547 uint32_t low = 1u;
548 uint32_t high = last;
549
2/2
✓ Branch 0 taken 163426328 times.
✓ Branch 1 taken 22084979 times.
185511307 while (high - low > 1u) {
550 163426328 const uint32_t middle = (low + high) / 2u;
551
2/2
✓ Branch 0 taken 87781967 times.
✓ Branch 1 taken 75644361 times.
163426328 if (CraterValue(data, p.agesOffset + middle) <= tau) {
552 low = middle;
553 } else {
554 75644361 high = middle;
555 }
556 }
557 22084979 age.row = low;
558 22084979 const float below = CraterValue(data, p.agesOffset + low);
559 22084979 const float above = CraterValue(data, p.agesOffset + high);
560 22084979 age.weight = log(tau / below) / log(above / below);
561 }
562 30723238 return age;
563 }
564
565 /// A diffused profile's height over the diameter at `x` diameters from the center, below
566 /// `p.tableExtent`, at diffusion age `tau`: the ages read from `ages` and the crater's rows, one
567 /// per age, from `table` at `rows`, interpolated linearly in distance and log age.
568 30649963 inline float KernelDiffusedValue(KERNEL_IN(CraterProfileKernel) p, CraterData ages,
569 CraterData table, uint32_t rows, float x, float tau) {
570 30649963 const KernelTableCell cell = KernelTableAt(p, x);
571 30649963 const KernelAgeRow age = KernelAgeRowAt(p, ages, tau);
572 30648243 const uint32_t width = p.tableCells + 1u;
573 30648243 const uint32_t at = rows + age.row * width + cell.index;
574 30648243 float value = KernelLerp(table, at, cell.t);
575
2/2
✓ Branch 0 taken 30646254 times.
✓ Branch 1 taken 1989 times.
30648243 if (age.weight > 0.0f) {
576 30646254 value += age.weight * (KernelLerp(table, at + width, cell.t) - value);
577 }
578 30648243 return value;
579 }
580
581 /// `DiffusedProfile::Height` (m), reading the crater's table from `data`.
582 22252052 inline float DiffusedProfileHeight(KERNEL_IN(CraterProfileKernel) p, CraterData data,
583 float distance, float diameter, KERNEL_IN(CraterRecord) c) {
584
2/2
✓ Branch 0 taken 1129730 times.
✓ Branch 1 taken 21122322 times.
22252052 if (!(c.diffusion > 0.0f)) {
585 1129730 CraterRecord fresh = c;
586 1129730 fresh.currentDepth = c.freshDepth;
587 1129730 fresh.currentRim = c.freshRim;
588 1129730 return FreshProfileHeight(p, distance, diameter, fresh);
589 }
590 21122322 const float x = distance / diameter;
591
1/2
✓ Branch 0 taken 21124093 times.
✗ Branch 1 not taken.
21122322 if (x >= p.tableExtent) {
592 return 0.0f;
593 }
594 21124093 return diameter * KernelDiffusedValue(p, data, data, c.profileOffset, x, c.diffusion);
595 }
596
597 /// The height (m) of a profile tabulated for crater `c` (`CraterProfileKind::Tabulated`),
598 /// interpolated linearly along its row; 0 beyond the row.
599 134057 inline float TabulatedProfileHeight(KERNEL_IN(CraterProfileKernel) p, CraterData data,
600 float distance, float diameter, KERNEL_IN(CraterRecord) c) {
601 134057 const float x = distance / diameter;
602
2/2
✓ Branch 0 taken 132685 times.
✓ Branch 1 taken 1372 times.
134057 if (!(x < p.tableExtent)) {
603 return 0.0f;
604 }
605 132685 const KernelTableCell cell = KernelTableAt(p, x);
606 132685 return diameter * KernelLerp(data, c.profileOffset + cell.index, cell.t);
607 }
608
609 /// The profile's height (m) at `distance`.
610 25997783 inline float KernelProfileHeight(KERNEL_IN(CraterShapeKernel) k, CraterData data, float distance,
611 float diameter, KERNEL_IN(CraterRecord) c) {
612
2/2
✓ Branch 0 taken 133944 times.
✓ Branch 1 taken 25863839 times.
25997783 if (k.profile.simple == CraterProfileKind::Tabulated) {
613 133944 return TabulatedProfileHeight(k.profile, data, distance, diameter, c);
614 }
615
2/2
✓ Branch 0 taken 22254871 times.
✓ Branch 1 taken 3608968 times.
25863839 if (k.profile.diffused == 1u) {
616 22254871 return DiffusedProfileHeight(k.profile, data, distance, diameter, c);
617 }
618 3608968 return FreshProfileHeight(k.profile, distance, diameter, c);
619 }
620
621 /// The profile's height with its rim rounded (`RimSmoothing`): a Gaussian of the profile sampled on
622 /// a grid of half-sigma steps anchored at the rim, with weights that move smoothly with `distance`
623 /// and taper to zero between 3 and 4 sigma. Sampling at fixed offsets from `distance` instead would
624 /// copy the rim's kink to each offset, leaving terraces about a sigma from the crest.
625 21893316 inline float KernelRoundedHeight(KERNEL_IN(CraterShapeKernel) k, CraterData data, float distance,
626 float diameter, KERNEL_IN(CraterRecord) c) {
627 21893316 const float height = KernelProfileHeight(k, data, distance, diameter, c);
628
2/2
✓ Branch 0 taken 1794268 times.
✓ Branch 1 taken 20093036 times.
21887304 if (k.rimSigma == 0.0f) {
629 return height;
630 }
631 1794268 const float radius = 0.5f * diameter;
632 1794268 const float radii = distance / radius;
633 1794268 const float weight =
634 radii <= 1.0f
635
2/2
✓ Branch 0 taken 316828 times.
✓ Branch 1 taken 1477440 times.
3271702 ? k.rimStrength * KernelFalloff(2.0f - radii, 1.0f, 2.0f - k.rimInner, k.rimShape)
636 1477440 : k.rimStrength * KernelFalloff(radii, 1.0f, k.rimOuter, k.rimShape);
637
2/2
✓ Branch 0 taken 184553 times.
✓ Branch 1 taken 1609706 times.
1794259 if (weight == 0.0f) {
638 return height;
639 }
640 184553 const float sigma = k.rimSigma * radius;
641 184553 const float step = 0.5f * sigma;
642 184553 const float position = (distance - radius) / step;
643 184553 const float first = floor(position) - 7.0f;
644 184553 float smoothed = 0.0f;
645 184553 float total = 0.0f;
646
2/2
✓ Branch 0 taken 2950461 times.
✓ Branch 1 taken 184534 times.
3134995 for (int i = 0; i < 16; ++i) {
647 2950461 const float node = first + float(i);
648 2950461 const float z = 0.5f * (node - position);
649 2950461 const float w = exp(-0.5f * z * z) * KernelFalloff(abs(z), 3.0f, 4.0f, 2u);
650 2950805 smoothed += w * KernelProfileHeight(k, data, abs(radius + step * node), diameter, c);
651 2950442 total += w;
652 }
653 184534 return height + weight * (smoothed / total - height);
654 }
655
656 /// Fractal parameters with every field set, as kernels need: Slang does not apply a struct's
657 /// default values to a local.
658 15425419 inline FractalParams KernelFractalParams(uint32_t seed, float wavelength, int octaves, float gain) {
659 15425419 FractalParams params;
660 15425419 params.seed = seed;
661 15425419 params.wavelength = wavelength;
662 15425419 params.amplitude = 1.0f;
663 15425419 params.octaves = octaves;
664 15425419 params.lacunarity = 2.0f;
665 15425419 params.gain = gain;
666 15425419 params.basis = NoiseBasis::Perlin;
667 15425419 params.type = FractalType::Fbm;
668 15425419 params.offset = 1.0f;
669 15425419 return params;
670 }
671
672 /// The crater noise (`CraterNoise`) at `dx`, `dy` meters from the center and `radii` radii out.
673 21885553 inline float KernelNoiseHeight(KERNEL_IN(CraterShapeKernel) k, KERNEL_IN(CraterRecord) c, float dx,
674 float dy, float radii) {
675
3/4
✓ Branch 0 taken 13657304 times.
✓ Branch 1 taken 8228249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13666728 times.
21885553 if (k.noiseAmplitude == 0.0f && k.noiseRimAmplitude == 0.0f) {
676 return 0.0f;
677 }
678 8218825 const float radius = 0.5f * c.diameter;
679 8218825 const float footprint =
680
2/2
✓ Branch 0 taken 1194074 times.
✓ Branch 1 taken 7024751 times.
8218825 radii <= 1.0f ? 1.0f : KernelFalloff(radii, 1.0f, k.profileReachRadii, 2u);
681 1194074 const float rim = radii <= 1.0f ? KernelFalloff(2.0f - radii, 1.0f, 2.0f - k.noiseRimInner, 2u)
682 7024747 : KernelFalloff(radii, 1.0f, k.noiseRimOuter, 2u);
683 8218612 float height = 0.0f;
684
4/4
✓ Branch 0 taken 8218552 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 8202556 times.
✓ Branch 3 taken 15996 times.
8218612 if (k.noiseAmplitude > 0.0f && footprint > 0.0f) {
685 8202556 const FractalParams params = KernelFractalParams(c.noiseKey, k.noiseWavelength * radius,
686 8202556 k.noiseOctaves, k.noiseGain);
687 8202556 height += k.noiseAmplitude * footprint * Fractal2(dx, dy, params);
688 }
689
4/4
✓ Branch 0 taken 39014 times.
✓ Branch 1 taken 8179847 times.
✓ Branch 2 taken 11758 times.
✓ Branch 3 taken 27256 times.
8218843 if (k.noiseRimAmplitude > 0.0f && rim > 0.0f) {
690 11758 const FractalParams params = KernelFractalParams(
691 11758 c.noiseKey + 1u, k.noiseRimWavelength * radius, k.noiseOctaves, k.noiseGain);
692 11758 height += k.noiseRimAmplitude * rim * Fractal2(dx, dy, params);
693 }
694 8218844 return c.diameter * height;
695 }
696
697 /// Fractal noise streaked radially (`RadialStreaks`).
698 7190459 inline float KernelRadialStreaks(float radii, float angle, float across, float along, int octaves,
699 uint32_t seed) {
700 7190459 const FractalParams params = KernelFractalParams(seed, 1.0f, octaves, 0.5f);
701 7190459 const float ring = 1.0f / across;
702 7190459 return Fractal3Float(ring * cos(angle), ring * sin(angle), radii / along, params);
703 }
704
705 /// The wall gullies and ejecta texture (`CraterTexture`).
706 21884352 inline float KernelTextureHeight(KERNEL_IN(CraterShapeKernel) k, KERNEL_IN(CraterRecord) c,
707 float dx, float dy, float radii) {
708
4/4
✓ Branch 0 taken 20074299 times.
✓ Branch 1 taken 1810053 times.
✓ Branch 2 taken 6288403 times.
✓ Branch 3 taken 13794501 times.
21884352 if (k.wallStreaks == 0.0f && k.ejecta == 0.0f) {
709 return 0.0f;
710 }
711
1/2
✓ Branch 0 taken 8089851 times.
✗ Branch 1 not taken.
8089851 const float freshness = c.freshDepth > 0.0f ? c.currentDepth / c.freshDepth : 1.0f;
712 8089851 const float fade = KernelSmoothstep((freshness - k.minFreshness) / (1.0f - k.minFreshness));
713
2/2
✓ Branch 0 taken 8053953 times.
✓ Branch 1 taken 35898 times.
8089851 if (!(fade > 0.0f)) {
714 return 0.0f;
715 }
716 8053953 const float angle = KernelAngle(dy, dx);
717 8053951 float height = 0.0f;
718
6/6
✓ Branch 0 taken 1800781 times.
✓ Branch 1 taken 6253170 times.
✓ Branch 2 taken 1698350 times.
✓ Branch 3 taken 102431 times.
✓ Branch 4 taken 184517 times.
✓ Branch 5 taken 1513833 times.
8053951 if (k.wallStreaks > 0.0f && radii > 0.25f && radii < 1.0f) {
719 184517 const float band =
720 184517 KernelSmoothstep((radii - 0.25f) / 0.35f) * KernelSmoothstep((1.0f - radii) / 0.1f);
721 184517 height += k.wallStreaks * c.diameter * band *
722 184517 KernelRadialStreaks(radii, angle, k.wallStreakWidth, 0.5f, 2, c.textureKey);
723 }
724
5/6
✓ Branch 0 taken 8053735 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 7005942 times.
✓ Branch 3 taken 1047793 times.
✓ Branch 4 taken 7005944 times.
✗ Branch 5 not taken.
8053951 if (k.ejecta > 0.0f && radii > 1.0f && radii < k.ejectaReachRadii) {
725 7005944 const float radius = 0.5f * c.diameter;
726 7005944 const float thickness =
727 7005944 KernelEjectaScale(radius) * KernelEjectaShare(radii, k.ejectaReachRadii);
728 7005944 const float rise = KernelSmoothstep((radii - 1.0f) / 0.1f);
729 7005942 height += k.ejecta * thickness * rise *
730 7005944 KernelRadialStreaks(radii, angle, k.ejectaLineation, 2.0f * k.ejectaLineation, 3,
731 7005944 c.textureKey + 1u);
732 }
733 8053949 return fade * height;
734 }
735
736 /// How far an area-preserving ellipse of long over short axis `ellipticity`, its long axis at
737 /// `heading` radians, reaches at angle `theta`, over the round radius.
738 2466609 inline float KernelEllipseScale(float theta, float ellipticity, float heading) {
739 2466609 const float along = sqrt(ellipticity);
740 2466609 const float across = 1.0f / along;
741 2466609 const float u = cos(theta - heading) / along;
742 2466609 const float v = sin(theta - heading) / across;
743 2466609 return 1.0f / sqrt(u * u + v * v);
744 }
745
746 /// The periodic Catmull-Rom spline through knots `p0` to `p3` at `t` of the way from `p1` to `p2`.
747 158285 inline float KernelCatmullRom(float p0, float p1, float p2, float p3, float t) {
748 158285 return 0.5f * (2.0f * p1 + (p2 - p0) * t + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t * t +
749 158285 (3.0f * (p1 - p2) + p3 - p0) * t * t * t);
750 }
751
752 /// The strength of wall marks at `radii`: full from `inner` to `outer` radii, fading along
753 /// smoothsteps `fade` radii wide either side, 0 beyond.
754 1935322 inline float KernelMarksWindow(float radii, float inner, float outer, float fade) {
755
4/4
✓ Branch 0 taken 1833677 times.
✓ Branch 1 taken 101645 times.
✓ Branch 2 taken 158285 times.
✓ Branch 3 taken 1675392 times.
1935322 if (!(radii > inner - fade) || !(radii < outer + fade)) {
756 return 0.0f;
757 }
758 158285 return KernelSmoothstep((radii - inner + fade) / fade) *
759 158285 KernelSmoothstep((outer + fade - radii) / fade);
760 }
761
762 /// The power of the share a rim table keeps of its fine rest, past the harmonics it carries: half
763 /// the order of the first harmonic past them (13), as harmonic k keeps `share`^(k / 2). A smoothing
764 /// choice so fine rim roughness stays near the crest, not a measured value.
765 static const float kTableFineFade = 6.5f;
766
767 /// The outline's reach with its rough wobble kept by `share`, from 0 (none) to 1 (all): each
768 /// harmonic of order k by `share`^(k / 2), so the short ones fade first. A rim table comes with its
769 /// broad harmonics, which fade the same way, and the rest of the table, its fine roughness, fades
770 /// by `share`^`kTableFineFade`, so the fine rim wobble is not carried down the wall to the bowl;
771 /// with all of the wobble kept the table alone is read.
772 41047922 inline float KernelOutlineScaleAt(CraterData data, KERNEL_IN(CraterRecord) c, float dx, float dy,
773 float share) {
774 41047922 const float theta = KernelAngle(dy, dx);
775 41053962 const float scale =
776
2/2
✓ Branch 0 taken 2466989 times.
✓ Branch 1 taken 38586973 times.
41053962 c.ellipticity != 1.0f ? KernelEllipseScale(theta, c.ellipticity, c.heading) : 1.0f;
777 41053390 float wobble = 1.0f;
778
4/4
✓ Branch 0 taken 22478 times.
✓ Branch 1 taken 41030912 times.
✓ Branch 2 taken 22476 times.
✓ Branch 3 taken 2 times.
41053390 if (c.tableCount > 0u && share > 0.0f) {
779 22476 const KernelPeriodicCell cell = KernelPeriodicAt(theta / kKernelTwoPi, c.tableCount);
780 22476 const float rim =
781 22476 (1.0f - cell.t) * CraterValue(data, c.tableOffset + cell.index) +
782 22476 cell.t * CraterValue(data, c.tableOffset + (cell.index + 1u) % c.tableCount);
783
2/2
✓ Branch 0 taken 7690 times.
✓ Branch 1 taken 14786 times.
22476 if (share >= 1.0f) {
784 7690 wobble += share * rim;
785 } else {
786 float broad = 0.0f;
787
2/2
✓ Branch 0 taken 177432 times.
✓ Branch 1 taken 14786 times.
192218 for (uint32_t n = 0u; n < c.harmonicCount; ++n) {
788 177432 const float order = float(c.firstHarmonic + n);
789 177432 const float wave =
790 177432 CraterValue(data, c.harmonicOffset + 2u * n) *
791 177432 cos(order * theta + CraterValue(data, c.harmonicOffset + 2u * n + 1u));
792 177432 broad += wave;
793 177432 wobble += pow(share, 0.5f * order) * wave;
794 }
795 14786 wobble += pow(share, kTableFineFade) * (rim - broad);
796 }
797
2/2
✓ Branch 0 taken 41009623 times.
✓ Branch 1 taken 21289 times.
41053388 } else if (share > 0.0f) {
798
2/2
✓ Branch 0 taken 443894911 times.
✓ Branch 1 taken 41009623 times.
484904534 for (uint32_t n = 0u; n < c.harmonicCount; ++n) {
799 443894911 const float order = float(c.firstHarmonic + n);
800
2/2
✓ Branch 0 taken 414289901 times.
✓ Branch 1 taken 29605010 times.
443894911 const float kept = share < 1.0f ? pow(share, 0.5f * order) : 1.0f;
801 443894911 wobble += kept * CraterValue(data, c.harmonicOffset + 2u * n) *
802 443894911 cos(order * theta + CraterValue(data, c.harmonicOffset + 2u * n + 1u));
803 }
804 }
805 41053390 return scale * wobble;
806 }
807
808 /// How far the outline reaches toward (`dx`, `dy`), over the round radius (`CraterOutline::Scale`).
809 93249 inline float KernelOutlineScale(CraterData data, KERNEL_IN(CraterRecord) c, float dx, float dy) {
810 93249 return KernelOutlineScaleAt(data, c, dx, dy, 1.0f);
811 }
812
813 /// The share of an outline's rough wobble a crater's profile follows `radii` from its center:
814 /// none at the center, rising along a smoothstep to all of it at the rim and beyond, so the floor
815 /// stays round and rim kinks fade down the wall instead of running to the center.
816 41176550 inline float KernelOutlineShare(float radii) {
817 41176550 return KernelSmoothstep(radii);
818 }
819
820 /// The share of an outline's rough wobble crater `c` keeps as it wears: its current rim height over
821 /// its fresh one, so worn rims lose their short wiggles first and round out as they lower, as
822 /// diffusion rounds them; 1 without a fresh rim height.
823 40968915 inline float KernelWornOutlineShare(KERNEL_IN(CraterRecord) c) {
824
3/4
✓ Branch 0 taken 40292212 times.
✓ Branch 1 taken 676703 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40292212 times.
81261127 return c.freshRim > 0.0f ? clamp(c.currentRim / c.freshRim, 0.0f, 1.0f) : 1.0f;
825 }
826
827 /// The shift in radii crater `c`'s wall marks add at `radii` toward (`dx`, `dy`)
828 /// (`CraterOutline::MarkShift`).
829 1935322 inline float KernelMarkShift(CraterData data, KERNEL_IN(CraterRecord) c, float dx, float dy,
830 float radii) {
831 1935322 const float window = KernelMarksWindow(radii, CraterValue(data, c.markOffset),
832 CraterValue(data, c.markOffset + 1u),
833 1935322 CraterValue(data, c.markOffset + 2u));
834
2/2
✓ Branch 0 taken 158285 times.
✓ Branch 1 taken 1777037 times.
1935322 if (window == 0.0f) {
835 return 0.0f;
836 }
837 158285 const uint32_t size = c.markCount;
838 158285 const KernelPeriodicCell cell = KernelPeriodicAt(KernelAngle(dy, dx) / kKernelTwoPi, size);
839 158285 const uint32_t index = cell.index;
840 158285 const uint32_t knots = c.markOffset + 3u;
841 158285 return window * KernelCatmullRom(CraterValue(data, knots + (index + size - 1u) % size),
842 CraterValue(data, knots + index),
843 158285 CraterValue(data, knots + (index + 1u) % size),
844 158285 CraterValue(data, knots + (index + 2u) % size), cell.t);
845 }
846
847 /// Floats of a crater's terraces in `CraterData` before its scarps: the floor edge in radii, the
848 /// bench's share of the wall's slope, the strength, the scarps' rounding share, the arcs' cut and
849 /// width, (amplitude, phase) pairs of harmonics 2 to 6 of the arcs, and the rim crest's scallop
850 /// height in radii, number and phase, and the share of slump blocks missing from the benches.
851 static const uint32_t kTerraceHeader = 20u;
852
853 /// Floats of each scarp of a crater's terraces after the header: mean position across the wall
854 /// (0 floor edge, 1 rim crest), scallop height, scallop count and phase, wobble amplitude and
855 /// phase, and each scallop block's step and hash key.
856 static const uint32_t kTerraceScarpValues = 8u;
857
858 /// Floats of each step of a crater's terraces, after its scarps: its bench level, its scarp share
859 /// and how much of its bench it keeps, less on walls too steep for one.
860 static const uint32_t kTerraceStepValues = 3u;
861
862 /// Share of a scallop's arc on each side of a cusp over which neighboring blocks' values blend.
863 static const float kTerraceBlockBlend = 0.3f;
864
865 /// The value of block `cell` (taken modulo `count`) of a scarp hashed from `key`: the hash in
866 /// [0, 1) for a negative `gaps`, else 1 when it stands and 0 when it is one of the `gaps` share
867 /// of blocks missing.
868 4455251 inline float KernelTerraceBlockValue(uint32_t key, int cell, int count, float gaps) {
869 4455251 const int wrapped = ((cell % count) + count) % count;
870
2/2
✓ Branch 0 taken 1474762 times.
✓ Branch 1 taken 2980489 times.
4455251 const float hash = HashToUnitFloat(PcgHash(key + uint32_t(wrapped) * 0x9E3779B9u));
871
4/4
✓ Branch 0 taken 1474762 times.
✓ Branch 1 taken 2980489 times.
✓ Branch 2 taken 306708 times.
✓ Branch 3 taken 1168054 times.
4455251 return gaps < 0.0f ? hash : (hash >= gaps ? 1.0f : 0.0f);
872 }
873
874 /// `KernelTerraceBlockValue` of the block of scarp `s` (its floats' offset in `data`) toward
875 /// `theta`, blended with its neighbor's across each cusp so the value is continuous round the rim.
876 2788001 inline float KernelTerraceBlocks(CraterData data, uint32_t s, float theta, uint32_t salt,
877 float gaps) {
878 2788001 const float turns =
879 2788001 CraterValue(data, s + 2u) * theta / kKernelTwoPi + CraterValue(data, s + 3u);
880 2788001 const int count = int(CraterValue(data, s + 2u));
881 2788001 const uint32_t key = uint32_t(CraterValue(data, s + 7u)) ^ (salt * 0x85EBCA6Bu);
882
2/2
✓ Branch 0 taken 852703 times.
✓ Branch 1 taken 1935298 times.
2788001 const float cellStart = floor(turns);
883 2788001 const int cell = int(cellStart);
884 2788001 const float u = turns - cellStart;
885 2788001 const float value = KernelTerraceBlockValue(key, cell, count, gaps);
886
2/2
✓ Branch 0 taken 852703 times.
✓ Branch 1 taken 1935298 times.
2788001 if (u < kTerraceBlockBlend) {
887 852703 const float b = KernelSmoothstep(0.5f + 0.5f * u / kTerraceBlockBlend);
888 852703 return KernelTerraceBlockValue(key, cell - 1, count, gaps) * (1.0f - b) + value * b;
889 }
890
2/2
✓ Branch 0 taken 817568 times.
✓ Branch 1 taken 1117730 times.
1935298 if (u > 1.0f - kTerraceBlockBlend) {
891 817568 const float b = KernelSmoothstep(0.5f + 0.5f * (1.0f - u) / kTerraceBlockBlend);
892 817568 return KernelTerraceBlockValue(key, cell + 1, count, gaps) * (1.0f - b) + value * b;
893 }
894 return value;
895 }
896
897 /// A sum of `count` cosine harmonics of `theta` from order `first`, their (amplitude, phase) pairs
898 /// at `offset` in `data`, each phase turned by `turn` times its order.
899 278848 inline float KernelHarmonics(CraterData data, uint32_t offset, uint32_t first, uint32_t count,
900 float theta, float turn) {
901 278848 float sum = 0.0f;
902
2/2
✓ Branch 0 taken 1393420 times.
✓ Branch 1 taken 278848 times.
1672268 for (uint32_t n = 0u; n < count; ++n) {
903 1393420 const float order = float(first + n);
904 1393420 sum += CraterValue(data, offset + 2u * n) *
905 1393420 cos(order * (theta + turn) + CraterValue(data, offset + 2u * n + 1u));
906 }
907 278848 return sum;
908 }
909
910 /// How much of bench `step` of crater `c`'s terraces stands toward `theta`, in [0, 1]: each bench
911 /// covers its own arcs of the wall, the arcs' harmonics turned by a golden angle from one bench
912 /// to the next; 1 everywhere when the benches go all the way round.
913 956960 inline float KernelTerraceArc(CraterData data, uint32_t o, float theta, float step) {
914 956960 const float cut = CraterValue(data, o + 4u);
915
2/2
✓ Branch 0 taken 278802 times.
✓ Branch 1 taken 678158 times.
956960 if (!(cut > -1.5f)) {
916 return 1.0f;
917 }
918 278802 const float arcs = KernelHarmonics(data, o + 6u, 2u, 5u, theta, 2.39996323f * step);
919 278802 return KernelSmoothstep((arcs - cut) / CraterValue(data, o + 5u));
920 }
921
922 /// The mean position across the wall of scarp `k` of crater `c`'s terraces, 0 at the floor edge
923 /// and 1 at the rim crest, for `k` from 0 (the floor edge) to `terraceCount` (the rim crest).
924 2071769 inline float KernelTerraceBase(CraterData data, KERNEL_IN(CraterRecord) c, uint32_t k) {
925
2/2
✓ Branch 0 taken 1634534 times.
✓ Branch 1 taken 437235 times.
2071769 if (k == 0u) {
926 return 0.0f;
927 }
928
2/2
✓ Branch 0 taken 1321989 times.
✓ Branch 1 taken 312545 times.
1634534 if (k >= c.terraceCount) {
929 return 1.0f;
930 }
931 1321989 return CraterValue(data, c.terraceOffset + kTerraceHeader + kTerraceScarpValues * (k - 1u));
932 }
933
934 /// Where scarp `k` of crater `c`'s terraces stands toward `theta`, across the wall: its mean
935 /// position moved by scallops, arcs bowing toward the rim between inward cusps as slump blocks'
936 /// headscarps do, by a slow wobble, and in or out block by block, the offsets blending across
937 /// each cusp so the slump blocks step en echelon; the floor edge and rim crest for `k` 0 and
938 /// `terraceCount`.
939 2016545 inline float KernelTerraceScarp(CraterData data, KERNEL_IN(CraterRecord) c, uint32_t k,
940 float theta) {
941
4/4
✓ Branch 0 taken 2016101 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 155877 times.
✓ Branch 3 taken 1860224 times.
2016545 if (k == 0u || k >= c.terraceCount) {
942 156321 return KernelTerraceBase(data, c, k);
943 }
944 1860224 const uint32_t s = c.terraceOffset + kTerraceHeader + kTerraceScarpValues * (k - 1u);
945 1860224 const float turns =
946 1860224 CraterValue(data, s + 2u) * theta / kKernelTwoPi + CraterValue(data, s + 3u);
947 1860224 const float f = 2.0f * (turns - floor(turns)) - 1.0f;
948 1860224 const float scallop = CraterValue(data, s + 1u) * (1.0f - f * f - 2.0f / 3.0f);
949 1860224 const float wobble = CraterValue(data, s + 4u) * cos(3.0f * theta + CraterValue(data, s + 5u));
950 1860224 const float block = 2.0f * KernelTerraceBlocks(data, s, theta, 0u, -1.0f) - 1.0f;
951 1860224 return CraterValue(data, s) + scallop + wobble + CraterValue(data, s + 6u) * block;
952 }
953
954 /// How far crater `c`'s rim crest reaches toward `theta` for its terraces, over the round radius:
955 /// scalloped, bowing outward between inward cusps as the headscarps of the outermost slump blocks
956 /// do, so the rim of a terraced crater is polygonal (Pike 1980, USGS PP 1046-C, p. C38).
957 99546 inline float KernelTerraceRimScale(CraterData data, KERNEL_IN(CraterRecord) c, float theta) {
958 99546 const uint32_t o = c.terraceOffset;
959 99546 const float turns =
960 99546 CraterValue(data, o + 17u) * theta / kKernelTwoPi + CraterValue(data, o + 18u);
961 99546 const float f = 2.0f * (turns - floor(turns)) - 1.0f;
962 99546 return 1.0f + CraterValue(data, o + 16u) * (1.0f - f * f - 2.0f / 3.0f);
963 }
964
965 /// A half-scarp rising from 0 to 1 as `v` goes from its brink at 0 to the step's edge at 1: its
966 /// slope grows from 0 over the first `rounding` of it, then holds, so scarps rise at an even slope
967 /// between rounded brinks and toes, 1 / (1 - rounding / 2) times their mean.
968 618100 inline float KernelTerraceEase(float v, float rounding) {
969 618100 const float steepest = 1.0f / (1.0f - 0.5f * rounding);
970
6/6
<unknown function 968>:
✓ Branch 0 taken 21530 times.
✓ Branch 1 taken 33206 times.
✓ Branch 2 taken 17954 times.
✓ Branch 3 taken 28081 times.
terraingen::KernelTerraceEase(float, float):
✓ Branch 0 taken 207754 times.
✓ Branch 1 taken 309575 times.
618100 if (v < rounding) {
971 247238 return steepest * v * v / (2.0f * rounding);
972 }
973 370862 return steepest * (v - 0.5f * rounding);
974 }
975
976 /// `radii` from crater `c`'s center toward `theta` with its wall remapped into `terraceCount`
977 /// nearly level benches between scarps; the map is continuous and monotonic and keeps the floor
978 /// edge and rim crest in place.
979 1031448 inline float KernelTerracedRadii(CraterData data, KERNEL_IN(CraterRecord) c, float radii,
980 float theta) {
981 1031448 const uint32_t o = c.terraceOffset;
982 1031448 const float inner = CraterValue(data, o);
983
4/4
✓ Branch 0 taken 1022019 times.
✓ Branch 1 taken 9429 times.
✓ Branch 2 taken 958312 times.
✓ Branch 3 taken 63707 times.
1031448 if (!(radii > inner) || !(radii < 1.0f)) {
984 return radii;
985 }
986 958312 const float t = (radii - inner) / (1.0f - inner);
987 958312 uint32_t step = 0u;
988 958312 float low = 0.0f;
989 958312 float high = KernelTerraceScarp(data, c, 1u, theta);
990
4/4
✓ Branch 0 taken 1861873 times.
✓ Branch 1 taken 157069 times.
✓ Branch 2 taken 1060630 times.
✓ Branch 3 taken 801243 times.
2018942 while (step + 1u < c.terraceCount && t >= high) {
991 1060630 ++step;
992 1060630 low = high;
993 1060630 high = KernelTerraceScarp(data, c, step + 1u, theta);
994 }
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 958312 times.
958312 const float within = clamp((t - low) / (high - low), 0.0f, 1.0f);
996 958312 float strength = CraterValue(data, o + 2u) * KernelTerraceArc(data, o, theta, float(step));
997
2/2
✓ Branch 0 taken 927794 times.
✓ Branch 1 taken 30518 times.
958312 if (c.terraceCount > 1u) {
998 927794 const bool top = step + 1u >= c.terraceCount;
999
2/2
✓ Branch 0 taken 801878 times.
✓ Branch 1 taken 125916 times.
927794 const uint32_t k = top ? step : step + 1u;
1000 927794 const uint32_t s = o + kTerraceHeader + kTerraceScarpValues * (k - 1u);
1001
2/2
✓ Branch 0 taken 801819 times.
✓ Branch 1 taken 125975 times.
1729613 strength *= KernelTerraceBlocks(data, s, theta, top ? 2u : 1u, CraterValue(data, o + 19u));
1002 }
1003 958312 const float rounding = CraterValue(data, o + 3u);
1004 958312 const float slope = CraterValue(data, o + 1u);
1005 958312 const uint32_t s = o + kTerraceHeader + kTerraceScarpValues * (c.terraceCount - 1u) +
1006 958312 kTerraceStepValues * step;
1007 958312 const float level = CraterValue(data, s);
1008 958312 const float scarp = CraterValue(data, s + 1u);
1009 958312 strength *= CraterValue(data, s + 2u);
1010 958312 const float lower = scarp * level;
1011 958312 const float upper = scarp * (1.0f - level);
1012 958312 float stair = level;
1013
2/2
✓ Branch 0 taken 278258 times.
✓ Branch 1 taken 680054 times.
958312 if (within < lower) {
1014 278258 stair = level * (1.0f - KernelTerraceEase(1.0f - within / lower, rounding));
1015
2/2
✓ Branch 0 taken 239126 times.
✓ Branch 1 taken 440928 times.
680054 } else if (within > 1.0f - upper) {
1016 239126 stair =
1017 239126 level + (1.0f - level) * KernelTerraceEase((within - (1.0f - upper)) / upper, rounding);
1018 }
1019 958312 const float rise = slope * within + (1.0f - slope) * stair;
1020 958312 const float shaped = within + strength * (rise - within);
1021 958312 const float from = KernelTerraceBase(data, c, step);
1022 958312 const float to = KernelTerraceBase(data, c, step + 1u);
1023 958312 return inner + (from + shaped * (to - from)) * (1.0f - inner);
1024 }
1025
1026 /// The distance (m) crater `c`'s round profile is read at, `dx` east and `dy` north of its center
1027 /// (`CraterProfileDistance`).
1028 48506704 inline float KernelProfileDistance(CraterData data, KERNEL_IN(CraterRecord) c, float dx, float dy) {
1029
2/2
✓ Branch 0 taken 46042980 times.
✓ Branch 1 taken 2463724 times.
48506704 float distance = sqrt(dx * dx + dy * dy);
1030
7/8
✓ Branch 0 taken 46042980 times.
✓ Branch 1 taken 2463724 times.
✓ Branch 2 taken 7461644 times.
✓ Branch 3 taken 38581336 times.
✓ Branch 4 taken 7464741 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38464 times.
✓ Branch 7 taken 7426277 times.
48506704 if (c.ellipticity != 1.0f || c.harmonicCount > 0u || c.tableCount > 0u || c.markCount > 0u) {
1031 41080427 const float share =
1032 41080427 KernelOutlineShare(distance / (0.5f * c.diameter)) * KernelWornOutlineShare(c);
1033 41080427 distance /= KernelOutlineScaleAt(data, c, dx, dy, share);
1034 }
1035
2/2
✓ Branch 0 taken 1906045 times.
✓ Branch 1 taken 46820749 times.
48726794 if (c.markCount > 0u) {
1036 1906045 const float radius = 0.5f * c.diameter;
1037 1906045 distance += radius * KernelMarkShift(data, c, dx, dy, distance / radius);
1038 }
1039
2/2
✓ Branch 0 taken 95725 times.
✓ Branch 1 taken 48631069 times.
48726794 if (c.terraceCount > 0u) {
1040 95725 const float radius = 0.5f * c.diameter;
1041 95725 const float theta = KernelAngle(dy, dx);
1042 96123 const float scale = KernelTerraceRimScale(data, c, theta);
1043 96123 distance /= 1.0f + (scale - 1.0f) * KernelOutlineShare(distance / radius);
1044 96123 distance = radius * KernelTerracedRadii(data, c, distance / radius, theta);
1045 }
1046 48727496 return distance;
1047 }
1048
1049 /// How much of crater `c`'s floor shift applies at `dx` east and `dy` north of its center
1050 /// (`CraterSlopeShape::shift`): all within 0.3 radii, fading along a smoothstep to none at the rim.
1051 7480374 inline float KernelSlopeShiftWeight(KERNEL_IN(CraterRecord) c, float dx, float dy) {
1052 7480374 const float radii = sqrt(dx * dx + dy * dy) / (0.5f * c.diameter);
1053 7480374 return 1.0f - KernelSmoothstep((radii - 0.3f) / 0.7f);
1054 }
1055
1056 /// The share of crater `c`'s raised relief, rim and ejecta, kept at `dx` east and `dy` north of
1057 /// its center: 1 less the removed share times the squared cosine of the angle from upslope.
1058 2683344 inline float KernelSlopeRaisedShare(KERNEL_IN(CraterRecord) c, float dx, float dy) {
1059
2/2
✓ Branch 0 taken 2683343 times.
✓ Branch 1 taken 1 time.
2683344 const float distance = sqrt(dx * dx + dy * dy);
1060
2/2
✓ Branch 0 taken 2683343 times.
✓ Branch 1 taken 1 time.
2683344 if (!(distance > 0.0f)) {
1061 return 1.0f;
1062 }
1063
2/2
✓ Branch 0 taken 1336632 times.
✓ Branch 1 taken 1346711 times.
2683343 const float removed = sqrt(c.upslopeX * c.upslopeX + c.upslopeY * c.upslopeY);
1064
2/2
✓ Branch 0 taken 1336632 times.
✓ Branch 1 taken 1346711 times.
2683343 const float cosine = max(0.0f, (c.upslopeX * dx + c.upslopeY * dy) / (removed * distance));
1065 2683343 return 1.0f - removed * cosine * cosine;
1066 }
1067
1068 /// `KernelProfileDistance` at `dx` east and `dy` north of crater `c`'s center after its floor
1069 /// shift (`CraterSlopeShape::shift`).
1070 47829418 inline float KernelShiftedDistance(CraterData data, KERNEL_IN(CraterRecord) c, float dx, float dy) {
1071 47829418 const float shift =
1072
4/4
✓ Branch 0 taken 40507951 times.
✓ Branch 1 taken 7321467 times.
✓ Branch 2 taken 158904 times.
✓ Branch 3 taken 40349047 times.
47829418 c.shiftX != 0.0f || c.shiftY != 0.0f ? KernelSlopeShiftWeight(c, dx, dy) : 0.0f;
1073 47829418 return KernelProfileDistance(data, c, dx - shift * c.shiftX, dy - shift * c.shiftY);
1074 }
1075
1076 /// Profile height `height` (m) at `dx` east and `dy` north of crater `c`'s center with its raised
1077 /// relief lowered upslope (`CraterSlopeShape::upslope`).
1078 23060859 inline float KernelUpslopeHeight(KERNEL_IN(CraterRecord) c, float height, float dx, float dy) {
1079
6/6
✓ Branch 0 taken 16925186 times.
✓ Branch 1 taken 6135673 times.
✓ Branch 2 taken 14333589 times.
✓ Branch 3 taken 2591597 times.
✓ Branch 4 taken 91742 times.
✓ Branch 5 taken 14241847 times.
23060859 if (height > 0.0f && (c.upslopeX != 0.0f || c.upslopeY != 0.0f)) {
1080 2683339 return height * KernelSlopeRaisedShare(c, dx, dy);
1081 }
1082 return height;
1083 }
1084
1085 /// Floats of a crater's central peak in `CraterData` before its massifs: the ridged roughness
1086 /// over the peak's height, its wavelength and the warp of the massifs' outlines in peak base
1087 /// radii, the noise's hash key, and the massifs' height and width over the profile's peak.
1088 static const uint32_t kPeakHeader = 6u;
1089
1090 /// Floats of each massif of a central peak, after the header: its center east and north and its
1091 /// radius in peak base radii, the square root of its aspect ratio, the cosine and sine of its
1092 /// long axis's heading, its height over the peak's, the exponent of its flanks, and its summit's
1093 /// offset east and north within its footprint, and the power of its flanks.
1094 static const uint32_t kPeakMassifValues = 11u;
1095
1096 /// How far a central peak's massifs reach, warp included, in peak base radii.
1097 static const float kPeakReach = 2.6f;
1098
1099 /// The height of massif `m` of a central peak, over the peak's height, at `px` east and `py`
1100 /// north of the crater's center in peak base radii: an elliptical mountain of height h and
1101 /// flanks (1 - r^e)^q, sharp-crested for e under 1 and domed at 2, meeting the floor in a slope
1102 /// break for q near 1 and easing into it at 2, its summit pulled off center toward its offset so
1103 /// one flank is steeper than the other; 0 outside its footprint.
1104 242720 inline float KernelPeakMassif(CraterData data, uint32_t m, float px, float py) {
1105 242720 const float ex = px - CraterValue(data, m);
1106 242720 const float ey = py - CraterValue(data, m + 1u);
1107 242720 const float radius = CraterValue(data, m + 2u);
1108 242720 const float stretch = CraterValue(data, m + 3u);
1109 242720 const float cosine = CraterValue(data, m + 4u);
1110 242720 const float sine = CraterValue(data, m + 5u);
1111 242720 const float qx = (ex * cosine + ey * sine) / (radius * stretch);
1112 242720 const float qy = (ey * cosine - ex * sine) * stretch / radius;
1113 242720 const float squared = qx * qx + qy * qy;
1114
2/2
✓ Branch 0 taken 34132 times.
✓ Branch 1 taken 208588 times.
242720 if (!(squared < 1.0f)) {
1115 return 0.0f;
1116 }
1117 34132 const float pull = 1.0f - squared;
1118 34132 const float sx = qx - CraterValue(data, m + 8u) * pull;
1119 34132 const float sy = qy - CraterValue(data, m + 9u) * pull;
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34132 times.
34132 const float r = min(sqrt(sx * sx + sy * sy), 1.0f);
1121 34132 const float flank = 1.0f - pow(r, CraterValue(data, m + 7u));
1122 34132 return CraterValue(data, m + 6u) * pow(flank, CraterValue(data, m + 10u));
1123 }
1124
1125 /// Crater `c`'s central peak (m) of `size` at `dx` east and `dy` north of its center: the highest
1126 /// of its massifs, their outlines warped and their slopes roughened by ridged noise into spurs,
1127 /// gullies and lesser summits, as on the peaks of Copernicus and Tycho.
1128 199108 inline float KernelPeakMassifs(CraterData data, KERNEL_IN(CraterRecord) c, KernelPeakSize size,
1129 float dx, float dy) {
1130 199108 const uint32_t o = c.peakOffset;
1131 199108 float px = dx / size.base;
1132 199108 float py = dy / size.base;
1133 199108 const float roughness = CraterValue(data, o);
1134 199108 const float wavelength = CraterValue(data, o + 1u);
1135 199108 const float warp = CraterValue(data, o + 2u);
1136 199108 const uint32_t key = uint32_t(CraterValue(data, o + 3u));
1137
2/2
✓ Branch 0 taken 18940 times.
✓ Branch 1 taken 180200 times.
199108 if (warp > 0.0f) {
1138 18940 const FractalParams shape = KernelFractalParams(key + 1u, 2.0f * wavelength, 3, 0.5f);
1139 18940 const float wx = Fractal2(px, py, shape);
1140 18884 const float wy = Fractal2(px + 17.31f, py - 9.73f, shape);
1141 18888 px += warp * wx;
1142 18888 py += warp * wy;
1143 }
1144 199056 float height = 0.0f;
1145
2/2
✓ Branch 0 taken 242795 times.
✓ Branch 1 taken 198937 times.
441732 for (uint32_t n = 0u; n < c.peakCount; ++n) {
1146 485471 height =
1147
2/2
✓ Branch 1 taken 33576 times.
✓ Branch 2 taken 209100 times.
276371 max(height, KernelPeakMassif(data, o + kPeakHeader + kPeakMassifValues * n, px, py));
1148 }
1149
4/4
✓ Branch 0 taken 18737 times.
✓ Branch 1 taken 180200 times.
✓ Branch 2 taken 1706 times.
✓ Branch 3 taken 17031 times.
198937 if (roughness > 0.0f && height > 0.0f) {
1150 1706 FractalParams ridges = KernelFractalParams(key, wavelength, 5, 0.5f);
1151 1706 ridges.type = FractalType::Ridged;
1152 1706 height *= 1.0f + roughness * Fractal2(px, py, ridges);
1153 }
1154 198941 return size.height * height;
1155 }
1156
1157 /// What crater `c`'s central peak adds (m) at `dx` east and `dy` north of its center to its
1158 /// profile height at `distance` (m): its massifs, moved with the floor's shift, in place of the
1159 /// profile's round dome, both lowered as the crater wears. 0 for craters without massifs, and for
1160 /// profiles that tabulate or diffuse their peak, which keep their dome.
1161 23034328 inline float KernelPeakRelief(KERNEL_IN(CraterShapeKernel) k, CraterData data,
1162 KERNEL_IN(CraterRecord) c, float distance, float dx, float dy) {
1163
4/6
✓ Branch 0 taken 74277 times.
✓ Branch 1 taken 22960051 times.
✓ Branch 2 taken 74360 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74360 times.
✗ Branch 5 not taken.
23034328 if (c.peakCount == 0u || k.profile.complex != 1u || k.profile.diffused == 1u ||
1164
1/2
✓ Branch 0 taken 74360 times.
✗ Branch 1 not taken.
74360 k.profile.simple == CraterProfileKind::Tabulated ||
1165
1/2
✓ Branch 0 taken 74679 times.
✗ Branch 1 not taken.
74360 c.diameter < k.profile.transitionDiameter) {
1166 return 0.0f;
1167 }
1168 74679 const KernelPeakSize size = KernelComplexPeak(k.profile, c.diameter);
1169 74589 KernelPeakSize scaled = size;
1170 74589 scaled.height *= CraterValue(data, c.peakOffset + 4u);
1171 74589 scaled.base *= CraterValue(data, c.peakOffset + 5u);
1172 74589 const float shift =
1173
2/4
✓ Branch 0 taken 74589 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 74589 times.
74589 c.shiftX != 0.0f || c.shiftY != 0.0f ? KernelSlopeShiftWeight(c, dx, dy) : 0.0f;
1174 75241 const float sx = dx - shift * c.shiftX;
1175 75241 const float sy = dy - shift * c.shiftY;
1176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75241 times.
75241 const float reach = kPeakReach * max(size.base, scaled.base);
1177
2/2
✓ Branch 0 taken 7559 times.
✓ Branch 1 taken 67682 times.
75241 if (!(sx * sx + sy * sy < reach * reach)) {
1178 return 0.0f;
1179 }
1180
1/2
✓ Branch 0 taken 7559 times.
✗ Branch 1 not taken.
7559 return KernelKeptDepth(c) *
1181 15321 (KernelPeakMassifs(data, c, scaled, sx, sy) - KernelPeakDome(size, distance));
1182 }
1183
1184 /// `height` after crater `c`, at `dx` east and `dy` north of its center in meters, as
1185 /// `ApplyCraters` shapes a cell there; unchanged beyond its reach.
1186 45801421 inline float ApplyCraterKernel(KERNEL_IN(CraterShapeKernel) k, CraterData data,
1187 KERNEL_IN(CraterRecord) c, float height, float dx, float dy) {
1188 45801421 const float diameter = c.diameter;
1189 45801421 const float radius = 0.5f * diameter;
1190 45801421 const float distance = KernelShiftedDistance(data, c, dx, dy);
1191
2/2
✓ Branch 0 taken 21905037 times.
✓ Branch 1 taken 24241408 times.
46146445 if (distance >= k.reachRadii * radius) {
1192 return height;
1193 }
1194 21905037 const float radii = distance / radius;
1195 21905037 float replaced = 0.0f;
1196
2/2
✓ Branch 0 taken 13643908 times.
✓ Branch 1 taken 8261129 times.
21905037 if (c.hasDatum == 1u) {
1197 13643908 float weight = c.replace == 1u
1198
2/2
✓ Branch 0 taken 192736 times.
✓ Branch 1 taken 13451172 times.
27094885 ? KernelFalloff(radii, k.replaceStart, k.replaceEnd, k.blendShape)
1199 13451172 : k.interiorReplacement *
1200 13451172 KernelFalloff(radii, k.blendStart, k.blendEnd, k.blendShape);
1201
4/4
✓ Branch 0 taken 13501 times.
✓ Branch 1 taken 13628634 times.
✓ Branch 2 taken 9001 times.
✓ Branch 3 taken 4500 times.
13642135 if (k.mantling > 0.0f && radii > 1.0f) {
1202
1/2
✓ Branch 0 taken 9001 times.
✗ Branch 1 not taken.
18002 weight = max(weight, k.mantling * KernelEjectaShare(radii, k.ejectaReachRadii));
1203 }
1204 13642135 replaced = weight * (c.datumA + c.datumB * dx + c.datumC * dy - height);
1205 }
1206 21903264 const float bare =
1207 21903264 KernelUpslopeHeight(c, KernelRoundedHeight(k, data, distance, diameter, c), dx, dy) +
1208 21881695 KernelPeakRelief(k, data, c, distance, dx, dy);
1209 21879198 const float shaped =
1210 21879198 bare + KernelNoiseHeight(k, c, dx, dy, radii) + KernelTextureHeight(k, c, dx, dy, radii);
1211 21877749 return height + c.resolution * shaped + c.resolution * replaced;
1212 }
1213
1214 /// What fitting crater datums takes: the craters' shapes, the fit footprint and the lattice the
1215 /// ground is sampled on (`FitCraterDatum`), and the size bands that index the craters.
1216 struct CraterDatumKernel {
1217 CraterShapeKernel shape;
1218 float fitRadii = 1.0f; ///< The fit footprint, in crater radii.
1219 uint32_t latticeSide = 7u; ///< Lattice points per side.
1220 uint32_t bandCount = 0u; ///< Size bands of the crater index.
1221 };
1222
1223 /// The relief (m) crater `c` adds at `dx` east and `dy` north of its center without its datum,
1224 /// as datums are fitted to it: its resolution times the bare profile, within the profile's
1225 /// reach. `bound` (m) is at least that reach stretched by the crater's outline.
1226 7294014 inline float CraterRelief(KERNEL_IN(CraterShapeKernel) k, CraterData data,
1227 KERNEL_IN(CraterRecord) c, float bound, float dx, float dy) {
1228 7294014 const float squared = dx * dx + dy * dy;
1229
2/2
✓ Branch 0 taken 2071184 times.
✓ Branch 1 taken 5222830 times.
7294014 if (!(squared < bound * bound)) {
1230 return 0.0f;
1231 }
1232 2071184 const float distance = KernelShiftedDistance(data, c, dx, dy);
1233
2/2
✓ Branch 0 taken 1157143 times.
✓ Branch 1 taken 914048 times.
2071191 if (!(distance < k.profileReachRadii * 0.5f * c.diameter)) {
1234 return 0.0f;
1235 }
1236 1157143 return c.resolution *
1237 1157143 (KernelUpslopeHeight(c, KernelProfileHeight(k, data, distance, c.diameter, c), dx, dy) +
1238 1157144 KernelPeakRelief(k, data, c, distance, dx, dy));
1239 }
1240
1241 /// The offset (m) of lattice point `index` of `side` across a footprint of `fitRadius`.
1242 90809 inline float KernelDatumOffset(uint32_t index, uint32_t side, float fitRadius) {
1243 90809 return fitRadius * ((2.0f * float(index) + 1.0f) / float(side) - 1.0f);
1244 }
1245
1246 /// Relative float error allowed for, in cell lookups, at a position's magnitude.
1247 static const float kDatumCellSlack = 4.8e-7f;
1248
1249 /// The first and last cells of a band's row or column within one cell of position `p`.
1250 struct KernelCellSpan {
1251 int32_t first;
1252 int32_t last;
1253 };
1254
1255 /// The cells of side `size`, `count` of them from absolute cell `start`, within one cell plus
1256 /// float slack `slack` of position `p`, clamped to the band.
1257 805048 inline KernelCellSpan KernelCellsNear(float p, float size, float slack, float start,
1258 int32_t count) {
1259 805048 KernelCellSpan span;
1260
2/2
✓ Branch 0 taken 112776 times.
✓ Branch 1 taken 692272 times.
805048 span.first = max(int32_t(floor((p - size - slack) / size) - start), 0);
1261
2/2
✓ Branch 0 taken 97765 times.
✓ Branch 1 taken 707283 times.
805048 span.last = min(int32_t(floor((p + size + slack) / size) - start), count - 1);
1262 805048 return span;
1263 }
1264
1265 /// The relief (m) of the craters before crater `k` of `records` at lattice point `point` of `k`'s
1266 /// datum footprint, numbered row by row; 0 outside the footprint. The index is laid out as
1267 /// `DatumKernelIndex` (shaping/datum_fit.cpp) describes.
1268 90809 inline float KernelDatumRelief(KERNEL_IN(CraterDatumKernel) kernel, CraterData data,
1269 CraterRecords records, KernelFloats bandFloats,
1270 KernelUints bandCells, KernelUints cells, KernelUints entries,
1271 uint32_t k, uint32_t point) {
1272 90809 const uint32_t side = kernel.latticeSide;
1273 90809 const float fitRadius = kernel.fitRadii * 0.5f * records[k].diameter;
1274 90809 const float dx = KernelDatumOffset(point % side, side, fitRadius);
1275 90809 const float dy = KernelDatumOffset(point / side, side, fitRadius);
1276
2/2
✓ Branch 0 taken 90792 times.
✓ Branch 1 taken 17 times.
90809 if (dx * dx + dy * dy >= fitRadius * fitRadius) {
1277 return 0.0f;
1278 }
1279 90792 SplitPoint at;
1280 90792 at.x = records[k].x;
1281 90792 at.xLow = records[k].xLow + dx;
1282 90792 at.y = records[k].y;
1283 90792 at.yLow = records[k].yLow + dy;
1284 90792 at.z = 0.0f;
1285 90792 at.zLow = 0.0f;
1286 90792 const float px = at.x + at.xLow;
1287 90792 const float py = at.y + at.yLow;
1288 90792 const float slackX = kDatumCellSlack * abs(px);
1289 90792 const float slackY = kDatumCellSlack * abs(py);
1290 90792 float relief = 0.0f;
1291
2/2
✓ Branch 0 taken 404085 times.
✓ Branch 1 taken 90809 times.
494894 for (uint32_t b = 0u; b < kernel.bandCount; ++b) {
1292 404085 const float size = bandFloats[3u * b];
1293 404085 const int32_t across = int32_t(bandCells[3u * b]);
1294 404085 const KernelCellSpan columns =
1295 404085 KernelCellsNear(px, size, slackX, bandFloats[3u * b + 1u], across);
1296 404085 const KernelCellSpan rows = KernelCellsNear(py, size, slackY, bandFloats[3u * b + 2u],
1297 404085 int32_t(bandCells[3u * b + 1u]));
1298
2/2
✓ Branch 0 taken 1082645 times.
✓ Branch 1 taken 404102 times.
1486747 for (int32_t j = rows.first; j <= rows.last; ++j) {
1299
2/2
✓ Branch 0 taken 3030045 times.
✓ Branch 1 taken 1082662 times.
4112707 for (int32_t i = columns.first; i <= columns.last; ++i) {
1300 3030045 const uint32_t cell = bandCells[3u * b + 2u] + uint32_t(j * across + i);
1301 3030045 const uint32_t first = cells[2u * cell];
1302 3030045 const uint32_t end = first + cells[2u * cell + 1u];
1303
2/2
✓ Branch 0 taken 4750234 times.
✓ Branch 1 taken 2154955 times.
6905189 for (uint32_t e = first; e < end; ++e) {
1304 4750234 const uint32_t n = entries[e];
1305
2/2
✓ Branch 0 taken 3875127 times.
✓ Branch 1 taken 875107 times.
4750234 if (n >= k) {
1306 break;
1307 }
1308 3875127 const KernelOffset offset = PlanarSplitOffset(records[n], at);
1309 3875144 relief +=
1310 3875127 CraterRelief(kernel.shape, data, records[n], size, offset.dx, offset.dy);
1311 }
1312 }
1313 }
1314 }
1315 return relief;
1316 }
1317
1318 /// What fitting crater datums on a body takes on the GPU: the craters' shapes.
1319 struct CraterBodyDatumKernel {
1320 CraterShapeKernel shape;
1321 };
1322
1323 /// The relief (m) of craters `entries[first]` to `entries[first + count - 1]` of `records`, in
1324 /// that order, at `p` on a body; each crater read in its `frames` (`BodySplitOffset`) within its
1325 /// `bounds` (m), as `CraterRelief` takes them.
1326 inline float KernelBodyDatumRelief(KERNEL_IN(CraterShapeKernel) k, CraterData data,
1327 CraterRecords records, CraterFrames frames, KernelFloats bounds,
1328 KernelUints entries, uint32_t first, uint32_t count,
1329 KERNEL_IN(SplitPoint) p) {
1330 float relief = 0.0f;
1331 for (uint32_t e = first; e < first + count; ++e) {
1332 const uint32_t n = entries[e];
1333 const KernelOffset at = BodySplitOffset(frames[n], p);
1334 relief += CraterRelief(k, data, records[n], bounds[n], at.dx, at.dy);
1335 }
1336 return relief;
1337 }
1338
1339 /// Where the first entry and count of the tile holding cell (`col`, `row`) sit in `tiles`: tiles
1340 /// across, cells per tile side and 1 on a body, then each tile's first entry and count.
1341 31274 inline uint32_t CraterTileHead(KernelUints tiles, uint32_t col, uint32_t row) {
1342 31274 const uint32_t side = tiles[1];
1343
2/2
✓ Branch 0 taken 23522 times.
✓ Branch 1 taken 7752 times.
31274 return 3u + 2u * ((row / side) * tiles[0] + col / side);
1344 }
1345
1346 /// The height after craters `entries[first]` to `entries[first + count - 1]` of `records`, in
1347 /// that order, from `height` at `p` on a plane.
1348 4551285 inline float ShapeCraters(KERNEL_IN(CraterShapeKernel) k, CraterData data, CraterRecords records,
1349 KernelUints entries, uint32_t first, uint32_t count, float height,
1350 KERNEL_IN(SplitPoint) p) {
1351
2/2
✓ Branch 0 taken 30064160 times.
✓ Branch 1 taken 4559307 times.
34623467 for (uint32_t e = first; e < first + count; ++e) {
1352 30064160 const uint32_t n = entries[e];
1353 30064160 const KernelOffset at = PlanarSplitOffset(records[n], p);
1354 30064160 height = ApplyCraterKernel(k, data, records[n], height, at.dx, at.dy);
1355 }
1356 4559307 return height;
1357 }
1358
1359 /// `ShapeCraters` on a body, each crater read in its `frames`.
1360 1660124 inline float ShapeCratersOnBody(KERNEL_IN(CraterShapeKernel) k, CraterData data,
1361 CraterRecords records, CraterFrames frames, KernelUints entries,
1362 uint32_t first, uint32_t count, float height,
1363 KERNEL_IN(SplitPoint) p) {
1364
2/2
✓ Branch 0 taken 15648851 times.
✓ Branch 1 taken 1659833 times.
17308684 for (uint32_t e = first; e < first + count; ++e) {
1365 15648851 const uint32_t n = entries[e];
1366 15648851 const KernelOffset at = BodySplitOffset(frames[n], p);
1367 15646987 height = ApplyCraterKernel(k, data, records[n], height, at.dx, at.dy);
1368 }
1369 1659833 return height;
1370 }
1371
1372 #ifndef __SLANG__
1373 static_assert(sizeof(CraterProfileKernel) == 76,
1374 "CraterProfileKernel must match its std430 layout");
1375 static_assert(sizeof(CraterShapeKernel) == 188, "CraterShapeKernel must match its std430 layout");
1376 static_assert(sizeof(CraterRecord) == 144, "CraterRecord must match its std430 layout");
1377 static_assert(sizeof(CraterFrame) == 52, "CraterFrame must match its std430 layout");
1378 static_assert(sizeof(CraterDatumKernel) == 200, "CraterDatumKernel must match its std430 layout");
1379 static_assert(sizeof(CraterBodyDatumKernel) == 188,
1380 "CraterBodyDatumKernel must match its std430 layout");
1381
1382 /// The compiled forms of the crater kernel.
1383 template <> KernelProgram CompiledKernel<CraterShapeKernel>();
1384
1385 /// The compiled forms of the crater datum kernel.
1386 template <> KernelProgram CompiledKernel<CraterDatumKernel>();
1387
1388 /// The compiled forms of the crater datum kernel on bodies.
1389 template <> KernelProgram CompiledKernel<CraterBodyDatumKernel>();
1390 #endif
1391
1392 } // namespace terraingen
1393