include/terraingen/layers/kernels/noise_layer.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The kernel of `NoiseLayer`; a kernel header for C++ and Slang. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/layers/kernels/cell.h" | ||
| 8 | #include "terraingen/noise/fractals.h" | ||
| 9 | |||
| 10 | #ifndef __SLANG__ | ||
| 11 | #include "terraingen/gpu/program.h" | ||
| 12 | |||
| 13 | #include <vector> | ||
| 14 | #endif | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// The enabled fractals of a noise layer, in order. | ||
| 19 | 392 | struct NoiseLayerKernel { | |
| 20 | FractalStack stack; ///< Only enabled fractals, so every `enabled` flag is set. | ||
| 21 | }; | ||
| 22 | |||
| 23 | #ifdef __SLANG__ | ||
| 24 | /// A cell's height after the noise layer: `height` plus the stacked fractals at `position`. | ||
| 25 | inline float ApplyCell(NoiseLayerKernel kernel, float height, CellPosition position) { | ||
| 26 | return height + EvaluateFractals(position.x, position.y, kernel.stack); | ||
| 27 | } | ||
| 28 | #else | ||
| 29 | /// A cell's height after the noise layer: `height` plus the stacked fractals at `position`. The | ||
| 30 | /// same stack as on the GPU, taken by reference so no cell copies the block. | ||
| 31 | inline float ApplyCell(const NoiseLayerKernel& kernel, float height, CellPosition position) { | ||
| 32 | return height + | ||
| 33 | EvaluateFractals(position.x, position.y, kernel.stack.fractals, kernel.stack.count); | ||
| 34 | } | ||
| 35 | |||
| 36 | static_assert(sizeof(NoiseLayerKernel) == 420, "NoiseLayerKernel must match its std430 layout"); | ||
| 37 | |||
| 38 | /// The compiled forms of the noise layer kernel. | ||
| 39 | template <> KernelProgram CompiledKernel<NoiseLayerKernel>(); | ||
| 40 | |||
| 41 | /// The kernel of `fractals`, at most `kMaxFractals`, keeping the enabled ones in order; skipping | ||
| 42 | /// a disabled fractal leaves the stack as it was, so the heights are the same. | ||
| 43 | 390 | inline NoiseLayerKernel MakeNoiseLayerKernel(const std::vector<Fractal>& fractals) { | |
| 44 | 390 | NoiseLayerKernel kernel; | |
| 45 | 804 | for (const Fractal& fractal : fractals) { | |
| 46 | 414 | if (fractal.enabled && kernel.stack.count < kMaxFractals) { | |
| 47 | 411 | kernel.stack.fractals[kernel.stack.count++] = fractal; | |
| 48 | } | ||
| 49 | } | ||
| 50 | 390 | return kernel; | |
| 51 | } | ||
| 52 | #endif | ||
| 53 | |||
| 54 | } // namespace terraingen | ||
| 55 |