./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
62 / 63
98.4%
Branches:
44 / 49
89.8%
Functions:
8 / 8
100.0%

src/gpu/kernel.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "gpu/kernel.h"
4
5 #include "gpu/device.h"
6 #include "terraingen/util/bits.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <cstdint>
11 #include <vector>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// Most bytes a strip's buffer of the grid's cells holds, under the smallest storage buffer range
18 /// Vulkan allows (128 MiB).
19 constexpr std::size_t kMaxStripBytes = std::size_t{1} << 26;
20
21 /// Cells per workgroup side in kernels.
22 constexpr std::size_t kGroupSide = 16;
23
24 /// Most workgroups Vulkan guarantees in one dimension of a dispatch; CUDA and Metal allow more.
25 constexpr std::size_t kMaxGroups = 65535;
26
27 /// The no-data modes of `KernelParams::noData`, as `terraingen/gpu/kernel.slang` reads them: no
28 /// no-data value, NaN, or `noDataValue`.
29 constexpr std::uint32_t kNoDataNone = 0;
30 constexpr std::uint32_t kNoDataNan = 1;
31 constexpr std::uint32_t kNoDataValue = 2;
32
33 /// The push constants of kernels (`terraingen/gpu/kernel.slang`).
34 struct KernelParams {
35 std::uint32_t width = 0;
36 std::uint32_t rows = 0;
37 std::uint32_t noData = kNoDataNone; ///< How no-data cells are marked.
38 float noDataValue = 0.0f;
39 std::uint32_t firstRow = 0; ///< The strip's first row in the grid.
40 };
41
42 /// The grid in workgroups of `kGroupSide` cells, rounded up.
43 707 std::uint32_t Groups(std::size_t cells) {
44 707 return static_cast<std::uint32_t>(CeilDiv(cells, kGroupSide));
45 }
46
47 /// Whether kernels can run over `grid`: not rotated, not empty, and no wider than one dispatch.
48 552 bool Dispatchable(const Raster<float>& grid) {
49
2/2
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 1 time.
552 const GeoTransform& transform = grid.Transform();
50
4/6
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 551 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 551 times.
✗ Branch 5 not taken.
1103 return !transform.IsRotated() && grid.Width() > 0 && grid.Height() > 0 &&
51
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 550 times.
551 Groups(grid.Width()) <= kMaxGroups;
52 }
53
54 /// The x of each column's cell centers, measured from `origin`.
55 77 std::vector<float> ColumnOffsets(const Raster<float>& grid, const Vec2d& origin) {
56 77 std::vector<float> xs(grid.Width());
57
2/2
✓ Branch 0 taken 13822 times.
✓ Branch 1 taken 77 times.
13899 for (std::size_t col = 0; col < xs.size(); ++col) {
58 13822 xs[col] = static_cast<float>(grid.CellCenter(col, 0).x - origin.x);
59 }
60 77 return xs;
61 }
62
63 /// The y of each row's cell centers, measured from `origin`.
64 77 std::vector<float> RowOffsets(const Raster<float>& grid, const Vec2d& origin) {
65 77 std::vector<float> ys(grid.Height());
66
2/2
✓ Branch 0 taken 15659 times.
✓ Branch 1 taken 77 times.
15736 for (std::size_t row = 0; row < ys.size(); ++row) {
67 15659 ys[row] = static_cast<float>(grid.CellCenter(0, row).y - origin.y);
68 }
69 77 return ys;
70 }
71
72 /// The push constants shared by every strip of `grid`.
73 77 KernelParams GridParams(const Raster<float>& grid) {
74 77 KernelParams params;
75
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 57 times.
77 params.width = static_cast<std::uint32_t>(grid.Width());
76
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 57 times.
77 if (const auto& noData = grid.NoData()) {
77
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 19 times.
20 params.noData = std::isnan(*noData) ? kNoDataNan : kNoDataValue;
78 20 params.noDataValue = *noData;
79 }
80 77 return params;
81 }
82
83 /// The most bytes any buffer of `grid`'s cells holds per cell: its heights or one of `extra`.
84 77 std::size_t CellBytes(const Raster<float>& grid, Span<const KernelBuffer> extra) {
85 77 std::size_t bytes = sizeof(float);
86
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 77 times.
504 for (const KernelBuffer& buffer : extra) {
87
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 394 times.
427 if (buffer.perCell != 0) {
88
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 bytes = std::max(bytes, buffer.bytes.size() / grid.Size());
89 }
90 }
91 77 return bytes;
92 }
93
94 /// A whole grid's kernel run, one strip of rows at a time.
95 ✗ struct StripRun {
96 ComputeDevice& device;
97 const KernelProgram& program;
98 Span<const std::byte> kernel;
99 Span<const KernelBuffer> extra;
100 std::vector<float> xs;
101 std::vector<float> ys;
102 KernelParams params;
103
104 /// Runs the kernel over `rows` rows from `row0`, updating those rows of `values` in place.
105 78 void Run(Span<float> values, std::size_t row0, std::size_t rows) {
106 78 const std::size_t width = xs.size();
107 78 params.rows = static_cast<std::uint32_t>(rows);
108 78 params.firstRow = static_cast<std::uint32_t>(row0);
109 78 std::vector<DeviceBuffer> buffers{
110 78 OutputBuffer(AsWritableBytes(values.subspan(row0 * width, rows * width)), rows * width),
111 78 InputBuffer(AsBytes(Span(xs)), width),
112 78 InputBuffer(AsBytes(Span(ys).subspan(row0, rows)), rows), InputBuffer(kernel, 1)};
113
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 78 times.
505 for (const KernelBuffer& buffer : extra) {
114
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 33 times.
427 if (buffer.perCell == 0) {
115
1/1
✓ Branch 1 taken 394 times.
394 buffers.push_back(InputBuffer(buffer.bytes, buffer.count));
116 394 continue;
117 }
118 33 const std::size_t elements = buffer.perCell * width;
119 33 const std::size_t bytes = buffer.bytes.size() / buffer.count * elements;
120 66 buffers.push_back(
121
1/1
✓ Branch 2 taken 33 times.
33 InputBuffer(buffer.bytes.subspan(row0 * bytes, rows * bytes), rows * elements));
122 }
123 78 const Dispatch dispatch{{Groups(width), Groups(rows), 1u}, {kGroupSide, kGroupSide, 1u}};
124
1/1
✓ Branch 2 taken 78 times.
78 device.Run(program, buffers, AsBytes(Span(&params, 1)), dispatch);
125 78 }
126 };
127
128 } // namespace
129
130 552 bool RunKernel(Backend backend, const KernelProgram& program, Span<const std::byte> kernel,
131 Raster<float>& grid, const Vec2d& origin, Span<const KernelBuffer> extra) {
132
2/2
✓ Branch 0 taken 550 times.
✓ Branch 1 taken 2 times.
552 if (!Dispatchable(grid)) {
133 return false;
134 }
135 550 ComputeDevice* device = GpuDevice(backend);
136
4/4
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 471 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 77 times.
550 if (device == nullptr || !device->CanRun(program)) {
137 473 return false;
138 }
139 77 StripRun run{*device,
140 program,
141 kernel,
142 extra,
143 ColumnOffsets(grid, origin),
144 RowOffsets(grid, origin),
145
1/1
✓ Branch 2 taken 77 times.
77 GridParams(grid)};
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 const std::size_t height = grid.Height();
147 154 const std::size_t stripRows = std::clamp<std::size_t>(
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 kMaxStripBytes / (CellBytes(grid, extra) * grid.Width()), 1, kMaxGroups * kGroupSide);
149
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 77 times.
155 for (std::size_t row0 = 0; row0 < height; row0 += stripRows) {
150
3/3
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 78 times.
155 run.Run(grid.Values(), row0, std::min(stripRows, height - row0));
151 }
152 77 return true;
153 77 }
154
155 } // namespace terraingen
156