device/commands.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <memory> | ||
| 7 | #include <optional> | ||
| 8 | #include <span> | ||
| 9 | #include <type_traits> | ||
| 10 | #include <variant> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "chrono_batch_render/device/accel.h" | ||
| 14 | #include "chrono_batch_render/device/buffer.h" | ||
| 15 | #include "chrono_batch_render/device/program.h" | ||
| 16 | #include "chrono_batch_render/device/ref.h" | ||
| 17 | #include "chrono_batch_render/device/texture.h" | ||
| 18 | #include "chrono_batch_render/export.h" | ||
| 19 | #include "chrono_batch_render/util/profiler.h" | ||
| 20 | |||
| 21 | namespace chrono::batch_render::device { | ||
| 22 | |||
| 23 | class Queue; | ||
| 24 | using profiler::ZoneId; | ||
| 25 | |||
| 26 | struct CopyCommand { | ||
| 27 | Buffer* src; | ||
| 28 | uint64_t srcOffset; | ||
| 29 | Buffer* dst; | ||
| 30 | uint64_t dstOffset; | ||
| 31 | uint64_t bytes; | ||
| 32 | }; | ||
| 33 | |||
| 34 | struct TextureCopyCommand { | ||
| 35 | Buffer* src; | ||
| 36 | uint64_t srcOffset; | ||
| 37 | Texture* dst; | ||
| 38 | uint32_t level; | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct FillCommand { | ||
| 42 | Buffer* dst; | ||
| 43 | uint64_t offset; | ||
| 44 | uint64_t bytes; | ||
| 45 | uint32_t value; | ||
| 46 | }; | ||
| 47 | |||
| 48 | struct UpdateCommand { | ||
| 49 | Buffer* dst; | ||
| 50 | uint64_t offset; | ||
| 51 | uint64_t bytes; | ||
| 52 | size_t data; | ||
| 53 | }; | ||
| 54 | |||
| 55 | struct DispatchCommand { | ||
| 56 | const Kernel* kernel; | ||
| 57 | Size3 groups; | ||
| 58 | size_t data; | ||
| 59 | uint32_t paramBytes; | ||
| 60 | Buffer* counts = nullptr; | ||
| 61 | uint64_t countsOffset = 0; | ||
| 62 | }; | ||
| 63 | |||
| 64 | struct BuildAccelCommand { | ||
| 65 | Accel* dst; | ||
| 66 | AccelInput input; | ||
| 67 | Buffer* scratch; | ||
| 68 | uint64_t scratchOffset; | ||
| 69 | bool update = false; | ||
| 70 | }; | ||
| 71 | |||
| 72 | struct BarrierCommand {}; | ||
| 73 | |||
| 74 | struct ZoneCommand { | ||
| 75 | uint32_t index; | ||
| 76 | bool begin; | ||
| 77 | }; | ||
| 78 | |||
| 79 | using Command = std::variant<CopyCommand, TextureCopyCommand, FillCommand, UpdateCommand, | ||
| 80 | DispatchCommand, BuildAccelCommand, BarrierCommand, ZoneCommand>; | ||
| 81 | |||
| 82 | template <class... Lambdas> struct Overloaded : Lambdas... { | ||
| 83 | using Lambdas::operator()...; | ||
| 84 | }; | ||
| 85 | |||
| 86 | struct FinishDesc { | ||
| 87 | bool reusable = false; | ||
| 88 | }; | ||
| 89 | |||
| 90 | class CHRONO_BATCH_RENDER_API CommandBuffer { | ||
| 91 | public: | ||
| 92 | CommandBuffer(CommandBuffer&& other) noexcept; | ||
| 93 | CommandBuffer& operator=(CommandBuffer&& other) noexcept; | ||
| 94 | ~CommandBuffer(); | ||
| 95 | |||
| 96 | 115543 | bool Empty() const { | |
| 97 | 115543 | return m_commands.empty(); | |
| 98 | } | ||
| 99 | |||
| 100 | 80101 | bool Reusable() const { | |
| 101 | 80101 | return m_reusable; | |
| 102 | } | ||
| 103 | |||
| 104 | 160209 | std::span<const Command> Commands() const { | |
| 105 | 160209 | return m_commands; | |
| 106 | } | ||
| 107 | |||
| 108 | 49196 | std::span<const ZoneId> Zones() const { | |
| 109 | 49196 | return m_zones; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | std::span<const std::byte> Data() const { | |
| 113 | ✗ | return m_data; | |
| 114 | } | ||
| 115 | |||
| 116 | 442601 | template <class DataCommand> const std::byte* Data(const DataCommand& command) const { | |
| 117 | 442601 | return m_data.data() + command.data; | |
| 118 | } | ||
| 119 | |||
| 120 | private: | ||
| 121 | friend class CommandEncoder; | ||
| 122 | friend class Queue; | ||
| 123 | |||
| 124 | CommandBuffer(Queue& queue, bool own); | ||
| 125 | void Release(); | ||
| 126 | void Close(); | ||
| 127 | void HoldResources(); | ||
| 128 | |||
| 129 | Queue* m_queue; | ||
| 130 | Ref<Device> m_device; | ||
| 131 | std::vector<Command> m_commands; | ||
| 132 | std::vector<std::byte> m_data; | ||
| 133 | std::vector<ZoneId> m_zones; | ||
| 134 | bool m_open = false; | ||
| 135 | bool m_reusable = false; | ||
| 136 | std::vector<Ref<Resource>> m_held; | ||
| 137 | mutable uint64_t m_submission = 0; | ||
| 138 | |||
| 139 | 238556 | struct Recorded { | |
| 140 | std::shared_ptr<void> timestamps; | ||
| 141 | std::shared_ptr<void> native; | ||
| 142 | }; | ||
| 143 | |||
| 144 | mutable Recorded m_recorded; | ||
| 145 | }; | ||
| 146 | |||
| 147 | 66522 | class CHRONO_BATCH_RENDER_API CommandEncoder { | |
| 148 | public: | ||
| 149 | void Copy(BufferRange src, BufferRange dst); | ||
| 150 | |||
| 151 | void Copy(BufferRange src, Texture& dst, uint32_t level); | ||
| 152 | |||
| 153 | void Fill(BufferRange dst, uint32_t value = 0); | ||
| 154 | |||
| 155 | void Update(BufferRange dst, const void* data); | ||
| 156 | |||
| 157 | template <class Params> | ||
| 158 | 232582 | void Dispatch(Kernel& kernel, const Params& params, Size3 groups, | |
| 159 | std::optional<BufferRange> counts = std::nullopt) { | ||
| 160 | static_assert(std::is_trivially_copyable_v<Params>, "parameters are copied as bytes"); | ||
| 161 | 232582 | Dispatch(kernel, ¶ms, sizeof(Params), groups, counts); | |
| 162 | 215180 | } | |
| 163 | |||
| 164 | void Dispatch(Kernel& kernel, Size3 groups, std::optional<BufferRange> counts = std::nullopt) { | ||
| 165 | Dispatch(kernel, nullptr, 0, groups, counts); | ||
| 166 | } | ||
| 167 | |||
| 168 | void BuildAccel(Accel& dst, const AccelInput& input, BufferRange scratch, bool update = false); | ||
| 169 | |||
| 170 | void Dispatch(Kernel& kernel, const void* params, size_t bytes, Size3 groups, | ||
| 171 | std::optional<BufferRange> counts = std::nullopt); | ||
| 172 | |||
| 173 | void Concurrent(bool concurrent); | ||
| 174 | |||
| 175 | void Barrier(); | ||
| 176 | |||
| 177 | void BeginZone(ZoneId zone); | ||
| 178 | |||
| 179 | void EndZone(); | ||
| 180 | |||
| 181 | CommandBuffer Finish(const FinishDesc& desc = {}); | ||
| 182 | |||
| 183 | static constexpr uint64_t kMaxUpdateBytes = 65536; | ||
| 184 | |||
| 185 | private: | ||
| 186 | friend class Queue; | ||
| 187 | |||
| 188 | CommandEncoder(Queue& queue, bool own); | ||
| 189 | |||
| 190 | void Append(const CommandBuffer& commands); | ||
| 191 | void Require(const Resource& resource, const char* what) const; | ||
| 192 | size_t Store(const void* data, size_t bytes, size_t alignment); | ||
| 193 | void Record(const Command& command); | ||
| 194 | |||
| 195 | CommandBuffer m_recording; | ||
| 196 | std::vector<uint32_t> m_open; | ||
| 197 | bool m_concurrent = false; | ||
| 198 | bool m_barrier = false; | ||
| 199 | bool m_own; | ||
| 200 | }; | ||
| 201 | |||
| 202 | class CHRONO_BATCH_RENDER_API ZoneScope { | ||
| 203 | public: | ||
| 204 | 44741 | ZoneScope(CommandEncoder& encoder, ZoneId zone) : m_encoder(encoder) { | |
| 205 | 44741 | encoder.BeginZone(zone); | |
| 206 | 44701 | } | |
| 207 | |||
| 208 | ~ZoneScope(); | ||
| 209 | ZoneScope(const ZoneScope&) = delete; | ||
| 210 | ZoneScope& operator=(const ZoneScope&) = delete; | ||
| 211 | |||
| 212 | private: | ||
| 213 | CommandEncoder& m_encoder; | ||
| 214 | }; | ||
| 215 | |||
| 216 | } | ||
| 217 |