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 | 552 | const GeoTransform& transform = grid.Transform(); | |
| 50 | 1103 | return !transform.IsRotated() && grid.Width() > 0 && grid.Height() > 0 && | |
| 51 | 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 | 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 | 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 | 77 | params.width = static_cast<std::uint32_t>(grid.Width()); | |
| 76 | 77 | if (const auto& noData = grid.NoData()) { | |
| 77 | 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 | 504 | for (const KernelBuffer& buffer : extra) { | |
| 87 | 427 | if (buffer.perCell != 0) { | |
| 88 | 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 | 505 | for (const KernelBuffer& buffer : extra) { | |
| 114 | 427 | if (buffer.perCell == 0) { | |
| 115 | 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 | 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 | 78 | device.Run(program, buffers, AsBytes(Span(¶ms, 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 | 552 | if (!Dispatchable(grid)) { | |
| 133 | return false; | ||
| 134 | } | ||
| 135 | 550 | ComputeDevice* device = GpuDevice(backend); | |
| 136 | 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 | 77 | GridParams(grid)}; | |
| 146 | 77 | const std::size_t height = grid.Height(); | |
| 147 | 154 | const std::size_t stripRows = std::clamp<std::size_t>( | |
| 148 | 77 | kMaxStripBytes / (CellBytes(grid, extra) * grid.Width()), 1, kMaxGroups * kGroupSide); | |
| 149 | 155 | for (std::size_t row0 = 0; row0 < height; row0 += stripRows) { | |
| 150 | 155 | run.Run(grid.Values(), row0, std::min(stripRows, height - row0)); | |
| 151 | } | ||
| 152 | 77 | return true; | |
| 153 | 77 | } | |
| 154 | |||
| 155 | } // namespace terraingen | ||
| 156 |