src/gpu/kernel.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Running a kernel over a grid on a GPU backend. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/gpu/backend.h" | ||
| 9 | #include "terraingen/gpu/program.h" | ||
| 10 | #include "terraingen/raster/raster.h" | ||
| 11 | #include "terraingen/util/span.h" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// A read-only storage buffer a kernel binds after its parameter block: host bytes and the element | ||
| 19 | /// count its structured buffer reports. | ||
| 20 | struct KernelBuffer { | ||
| 21 | Span<const std::byte> bytes; ///< The host data. | ||
| 22 | std::size_t count = 0; ///< Number of elements. | ||
| 23 | /// Elements per grid cell, row by row, for a buffer bound one strip of rows at a time like | ||
| 24 | /// the heights; 0 for a buffer bound whole in every strip. | ||
| 25 | std::size_t perCell = 0; | ||
| 26 | }; | ||
| 27 | |||
| 28 | /// The buffer of `values`, which must outlive it. | ||
| 29 | /// @param perCell As `KernelBuffer::perCell`. | ||
| 30 | template <typename T> | ||
| 31 | 3677 | KernelBuffer MakeKernelBuffer(const std::vector<T>& values, std::size_t perCell = 0) { | |
| 32 | 3677 | return {AsBytes(Span(values)), values.size(), perCell}; | |
| 33 | } | ||
| 34 | |||
| 35 | /// Runs a kernel compiled by `terraingen_add_kernel` over every cell of `grid` holding data, in | ||
| 36 | /// strips of rows, on `backend` (`Vulkan`, `Cuda` or `Metal`). | ||
| 37 | /// | ||
| 38 | /// Each cell gets the kernel's `ApplyCell` at its center measured from `origin`, which the host | ||
| 39 | /// works out per column and per row in double, so cells see the same positions as on the CPU. | ||
| 40 | /// @param kernel The kernel's parameter block, laid out as in the kernel header. | ||
| 41 | /// @param extra Further buffers, bound in order after the parameter block: whole in every strip, | ||
| 42 | /// or the strip's rows of a buffer with `perCell` elements for each of the grid's cells. | ||
| 43 | /// @return false, leaving `grid` unchanged, when the backend is not a GPU one, its device is | ||
| 44 | /// unavailable, the build has no form of the kernel for it, or the grid cannot run there | ||
| 45 | /// (rotated, or wider than one dispatch reaches). | ||
| 46 | bool RunKernel(Backend backend, const KernelProgram& program, Span<const std::byte> kernel, | ||
| 47 | Raster<float>& grid, const Vec2d& origin, Span<const KernelBuffer> extra = {}); | ||
| 48 | |||
| 49 | } // namespace terraingen | ||
| 50 |