src/gpu/device.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `ComputeDevice`, the interface of the GPU devices, and `LazyDevice`, their shared | ||
| 5 | /// opening. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/gpu/backend.h" | ||
| 9 | #include "terraingen/gpu/program.h" | ||
| 10 | #include "terraingen/util/log.h" | ||
| 11 | #include "terraingen/util/span.h" | ||
| 12 | |||
| 13 | #include <array> | ||
| 14 | #include <cstddef> | ||
| 15 | #include <cstdint> | ||
| 16 | #include <exception> | ||
| 17 | #include <memory> | ||
| 18 | #include <mutex> | ||
| 19 | #include <string> | ||
| 20 | |||
| 21 | namespace terraingen { | ||
| 22 | |||
| 23 | /// A storage buffer bound to a kernel, in binding order. | ||
| 24 | struct DeviceBuffer { | ||
| 25 | Span<const std::byte> bytes; ///< The host data uploaded before the run. | ||
| 26 | std::size_t count = 0; ///< Number of elements its structured buffer reports. | ||
| 27 | std::byte* output = nullptr; ///< Where its `bytes.size()` bytes are copied back after the run; | ||
| 28 | ///< null for a buffer the kernel only reads. | ||
| 29 | }; | ||
| 30 | |||
| 31 | /// A buffer the kernel only reads. | ||
| 32 | 586 | inline DeviceBuffer InputBuffer(Span<const std::byte> bytes, std::size_t count) { | |
| 33 | 586 | return {bytes, count, nullptr}; | |
| 34 | } | ||
| 35 | |||
| 36 | /// A buffer the kernel reads and writes, copied back in place after the run. | ||
| 37 | 86 | inline DeviceBuffer OutputBuffer(Span<std::byte> bytes, std::size_t count) { | |
| 38 | 85 | return {bytes, count, bytes.data()}; | |
| 39 | } | ||
| 40 | |||
| 41 | /// The work of one kernel run: workgroup counts and threads per workgroup, in x, y and z. | ||
| 42 | struct Dispatch { | ||
| 43 | std::array<std::uint32_t, 3> groups{1u, 1u, 1u}; ///< Workgroup counts. | ||
| 44 | std::array<std::uint32_t, 3> groupSize{1u, 1u, 1u}; ///< Threads per workgroup, as the kernel | ||
| 45 | ///< declares them. | ||
| 46 | }; | ||
| 47 | |||
| 48 | /// A GPU that runs kernels compiled by `terraingen_add_kernel`. Thread-safe; runs are serialized. | ||
| 49 | 32 | class ComputeDevice { | |
| 50 | public: | ||
| 51 | ✗ | virtual ~ComputeDevice() = default; | |
| 52 | |||
| 53 | /// The device name the driver or system reports. | ||
| 54 | virtual const std::string& DeviceName() const = 0; | ||
| 55 | |||
| 56 | /// Whether `program` has a form this device runs. | ||
| 57 | virtual bool CanRun(const KernelProgram& program) const = 0; | ||
| 58 | |||
| 59 | /// Runs `program`, binding `buffers` in order and the push constants after them, then copies | ||
| 60 | /// the output buffers back. | ||
| 61 | /// @throws std::runtime_error for a device failure or a program without a form for it. | ||
| 62 | virtual void Run(const KernelProgram& program, Span<const DeviceBuffer> buffers, | ||
| 63 | Span<const std::byte> pushConstants, const Dispatch& dispatch) = 0; | ||
| 64 | }; | ||
| 65 | |||
| 66 | /// The device of a GPU backend (`Vulkan`, `Cuda` or `Metal`), opened on first use; null when it is | ||
| 67 | /// unavailable or `backend` is not a GPU backend. | ||
| 68 | ComputeDevice* GpuDevice(Backend backend); | ||
| 69 | |||
| 70 | /// The process-wide instance of a device type, opened by `Device::Open` on the first `Get`. | ||
| 71 | /// | ||
| 72 | /// The device is never destroyed, so the driver is not torn down during exit. | ||
| 73 | template <typename Device> class LazyDevice { | ||
| 74 | public: | ||
| 75 | /// @param api The API name log messages show, such as "Vulkan". | ||
| 76 | 80 | explicit LazyDevice(const char* api) : m_api(api) {} | |
| 77 | |||
| 78 | /// The device, opened on the first call; null when it cannot open (see `Reason`). | ||
| 79 | 434 | Device* Get() { | |
| 80 | 80 | std::call_once(m_once, [this] { | |
| 81 | try { | ||
| 82 | 80 | m_device = Device::Open().release(); | |
| 83 | 32 | LogInfo("gpu", m_api, " compute on ", m_device->DeviceName()); | |
| 84 | 48 | } catch (const std::exception& error) { | |
| 85 | 48 | m_reason = error.what(); | |
| 86 | 48 | LogInfo("gpu", m_api, " unavailable: ", m_reason); | |
| 87 | } | ||
| 88 | }); | ||
| 89 | 307 | return m_device; | |
| 90 | } | ||
| 91 | |||
| 92 | /// Why the device cannot open; empty when it is open. | ||
| 93 | 127 | const std::string& Reason() { | |
| 94 | 127 | Get(); | |
| 95 | 127 | return m_reason; | |
| 96 | } | ||
| 97 | |||
| 98 | private: | ||
| 99 | const char* m_api; | ||
| 100 | std::once_flag m_once; | ||
| 101 | Device* m_device = nullptr; | ||
| 102 | std::string m_reason; | ||
| 103 | }; | ||
| 104 | |||
| 105 | /// A `ComputeDevice` with one process-wide instance, opened by `Device::Open` on first use and | ||
| 106 | /// named `Device::kApi` in log messages. | ||
| 107 | 32 | template <typename Device> class SharedComputeDevice : public ComputeDevice { | |
| 108 | public: | ||
| 109 | /// The shared device, opened on the first call; null when it is unavailable (see `Reason`). | ||
| 110 | 307 | static Device* Get() { | |
| 111 | 307 | return Shared().Get(); | |
| 112 | } | ||
| 113 | |||
| 114 | /// Why the device is unavailable; empty when it is available. | ||
| 115 | 127 | static std::string Reason() { | |
| 116 | 127 | return Shared().Reason(); | |
| 117 | } | ||
| 118 | |||
| 119 | private: | ||
| 120 | 434 | static LazyDevice<Device>& Shared() { | |
| 121 | 434 | static LazyDevice<Device> device(Device::kApi); | |
| 122 | 434 | return device; | |
| 123 | } | ||
| 124 | }; | ||
| 125 | |||
| 126 | } // namespace terraingen | ||
| 127 |