overlay/overlay_impl.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <array> | ||
| 5 | #include <condition_variable> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <map> | ||
| 8 | #include <memory> | ||
| 9 | #include <mutex> | ||
| 10 | #include <optional> | ||
| 11 | #include <span> | ||
| 12 | #include <string> | ||
| 13 | #include <string_view> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #include "chrono_batch_render/algorithms/radix_sort.h" | ||
| 17 | #include "chrono_batch_render/algorithms/scan.h" | ||
| 18 | #include "chrono_batch_render/device/buffer.h" | ||
| 19 | #include "chrono_batch_render/device/commands.h" | ||
| 20 | #include "chrono_batch_render/device/program.h" | ||
| 21 | #include "chrono_batch_render/overlay/kernels/composite.h" | ||
| 22 | #include "chrono_batch_render/overlay/kernels/groups.h" | ||
| 23 | #include "chrono_batch_render/overlay/kernels/overlay.h" | ||
| 24 | #include "chrono_batch_render/overlay/kernels/selection.h" | ||
| 25 | #include "chrono_batch_render/overlay/kernels/shapes.h" | ||
| 26 | #include "chrono_batch_render/overlay/overlay.h" | ||
| 27 | #include "chrono_batch_render/stage/types.h" | ||
| 28 | |||
| 29 | namespace chrono::batch_render { | ||
| 30 | |||
| 31 | 15 | struct OverlayData { | |
| 32 | struct Batch { | ||
| 33 | OverlayKind kind = OverlayKind::Lines; | ||
| 34 | OverlayStyle style; | ||
| 35 | device::Tensor points; | ||
| 36 | device::Tensor ends; | ||
| 37 | device::Tensor colors; | ||
| 38 | device::Tensor sizes; | ||
| 39 | device::Tensor worlds; | ||
| 40 | device::Tensor itemCount; | ||
| 41 | device::Tensor rotations; | ||
| 42 | device::Tensor scalars; | ||
| 43 | device::Tensor extras; | ||
| 44 | device::Tensor chars; | ||
| 45 | device::Tensor prims; | ||
| 46 | device::Tensor groups; | ||
| 47 | device::Tensor pixels; | ||
| 48 | uint32_t groupCount = 0; | ||
| 49 | OverlayPrimShape primShape = OverlayPrimShape::Frames; | ||
| 50 | float length = 0; | ||
| 51 | float fade = 0; | ||
| 52 | std::optional<float> vectorScale; | ||
| 53 | std::optional<ScreenPlacement> placement; | ||
| 54 | OverlayNumberFormat format; | ||
| 55 | std::shared_ptr<const void> owner; | ||
| 56 | std::vector<Float3> hostPoints; | ||
| 57 | std::vector<Float3> hostEnds; | ||
| 58 | std::vector<uint32_t> hostColors; | ||
| 59 | std::vector<float> hostSizes; | ||
| 60 | std::vector<uint32_t> hostWorlds; | ||
| 61 | std::vector<Float4> hostRotations; | ||
| 62 | std::vector<float> hostScalars; | ||
| 63 | std::vector<uint32_t> hostExtras; | ||
| 64 | std::vector<uint8_t> hostCodes; | ||
| 65 | std::vector<uint32_t> hostPrims; | ||
| 66 | std::vector<uint32_t> hostGroups; | ||
| 67 | std::vector<uint32_t> hostIds; | ||
| 68 | uint32_t maxChars = 0; | ||
| 69 | }; | ||
| 70 | |||
| 71 | std::vector<Batch> batches; | ||
| 72 | |||
| 73 | bool Outlined() const; | ||
| 74 | }; | ||
| 75 | |||
| 76 | struct OverlayTarget { | ||
| 77 | uint32_t camera = kNoPrim; | ||
| 78 | uint32_t product = kNoPrim; | ||
| 79 | uint32_t layer = 0; | ||
| 80 | uint32_t width = 0; | ||
| 81 | uint32_t height = 0; | ||
| 82 | float missDistance = 0; | ||
| 83 | uint64_t overlay = 0; | ||
| 84 | uint64_t depth = 0; | ||
| 85 | uint64_t overlays = ~uint64_t(0); | ||
| 86 | uint64_t primIds = 0; | ||
| 87 | }; | ||
| 88 | |||
| 89 | 8 | struct OverlayPickRequest { | |
| 90 | uint32_t target = 0; | ||
| 91 | Float2 pixel; | ||
| 92 | float radius = 0; | ||
| 93 | Ref<device::Buffer> readback; | ||
| 94 | }; | ||
| 95 | |||
| 96 | struct OverlayBatchSource { | ||
| 97 | uint32_t overlay = 0; | ||
| 98 | uint32_t batch = 0; | ||
| 99 | }; | ||
| 100 | |||
| 101 | struct OverlayPickState { | ||
| 102 | std::mutex mutex; | ||
| 103 | std::condition_variable served; | ||
| 104 | bool recorded = false; | ||
| 105 | device::Operation done; | ||
| 106 | Ref<device::Buffer> readback; | ||
| 107 | std::vector<std::string> names; | ||
| 108 | std::vector<OverlayBatchSource> sources; | ||
| 109 | std::string product; | ||
| 110 | uint32_t layer = 0; | ||
| 111 | Float2 pixel; | ||
| 112 | float radius = 0; | ||
| 113 | }; | ||
| 114 | |||
| 115 | 48 | struct OverlayCameraSource { | |
| 116 | uint32_t camera = kNoPrim; | ||
| 117 | device::Tensor pixels; | ||
| 118 | }; | ||
| 119 | |||
| 120 | using OverlayContents = std::shared_ptr<const OverlayData>; | ||
| 121 | |||
| 122 | 48 | struct OverlayOutput { | |
| 123 | std::string_view product; | ||
| 124 | const std::vector<std::string>* layers = nullptr; | ||
| 125 | const device::Tensor* image = nullptr; | ||
| 126 | std::vector<OverlayTarget> targets; | ||
| 127 | }; | ||
| 128 | |||
| 129 | ✗ | struct OverlayFont { | |
| 130 | std::array<OverlayGlyph, 256> glyphs{}; | ||
| 131 | std::vector<int8_t> kerning; | ||
| 132 | std::vector<uint8_t> field; | ||
| 133 | float textHeight = 0; | ||
| 134 | }; | ||
| 135 | |||
| 136 | struct OverlaysImpl { | ||
| 137 | 7026 | static OverlaysImpl& Get(Overlays& overlays) { | |
| 138 | 7026 | return *overlays.m_impl; | |
| 139 | } | ||
| 140 | |||
| 141 | bool Outlined() const; | ||
| 142 | std::vector<std::shared_ptr<OverlayPickState>> TakePicks(std::span<const std::string> products); | ||
| 143 | |||
| 144 | device::Device& device; | ||
| 145 | mutable std::mutex mutex; | ||
| 146 | std::map<std::string, OverlayContents, std::less<>> layers; | ||
| 147 | std::vector<std::shared_ptr<OverlayPickState>> picks; | ||
| 148 | }; | ||
| 149 | |||
| 150 | const OverlayFont& OverlayTextFont(); | ||
| 151 | std::vector<uint8_t> OverlayCodes(std::string_view text); | ||
| 152 | |||
| 153 | class OverlayPass { | ||
| 154 | public: | ||
| 155 | explicit OverlayPass(device::Device& device); | ||
| 156 | |||
| 157 | static OverlayContents Share(const Overlay& overlay); | ||
| 158 | static void Serve(std::span<const std::shared_ptr<OverlayPickState>> picks, | ||
| 159 | const device::Operation& done); | ||
| 160 | |||
| 161 | std::vector<OverlayContents> Draw(device::CommandEncoder& encoder, const Snapshot& snapshot, | ||
| 162 | uint32_t worldCount, const OverlaysImpl& overlays, | ||
| 163 | std::span<const OverlayOutput> outputs, | ||
| 164 | std::span<const OverlayCameraSource> cameraImages, | ||
| 165 | std::span<const std::shared_ptr<OverlayPickState>> picks); | ||
| 166 | static void CheckDevice(const OverlayData& overlay, device::Device& device); | ||
| 167 | |||
| 168 | void Record(device::CommandEncoder& encoder, const Snapshot& snapshot, uint32_t worldCount, | ||
| 169 | std::span<const OverlayTarget> targets, std::span<const OverlayContents> overlays, | ||
| 170 | std::span<const OverlayCameraSource> cameraImages = {}, | ||
| 171 | std::span<const OverlayPickRequest> picks = {}); | ||
| 172 | |||
| 173 | const std::vector<OverlayBatchSource>& BatchSources() const { | ||
| 174 | return m_batchSources; | ||
| 175 | } | ||
| 176 | |||
| 177 | uint32_t PairCapacity() const { | ||
| 178 | return m_pairCapacity; | ||
| 179 | } | ||
| 180 | |||
| 181 | private: | ||
| 182 | 12 | struct Uploaded { | |
| 183 | std::weak_ptr<const OverlayData> contents; | ||
| 184 | std::vector<Ref<device::Buffer>> buffers; | ||
| 185 | }; | ||
| 186 | |||
| 187 | ✗ | struct Grouping { | |
| 188 | Ref<device::Buffer> keys; | ||
| 189 | Ref<device::Buffer> order; | ||
| 190 | Ref<device::Buffer> starts; | ||
| 191 | }; | ||
| 192 | |||
| 193 | using GroupKey = std::pair<uint32_t, uint32_t>; | ||
| 194 | |||
| 195 | void AddBatches(const OverlayContents& overlay, uint32_t index, uint32_t worldCount, | ||
| 196 | std::vector<OverlayBatch>& batches, uint32_t& firstItem); | ||
| 197 | void Unite(OverlayBatch& gpu, uint32_t rows, uint32_t groups, uint64_t prims, | ||
| 198 | uint32_t primStride, uint64_t groupIds, uint32_t groupStride); | ||
| 199 | void RecordUnions(device::CommandEncoder& encoder, const Snapshot& snapshot); | ||
| 200 | void Group(OverlayBatch& gpu, GroupKey key, uint32_t rows, uint32_t worldCount, uint64_t worlds, | ||
| 201 | uint32_t worldStride, uint64_t itemCounts); | ||
| 202 | void RecordGroups(device::CommandEncoder& encoder); | ||
| 203 | void Reserve(Ref<device::Buffer>& buffer, uint64_t bytes, const char* label); | ||
| 204 | |||
| 205 | device::Device& m_device; | ||
| 206 | Ref<device::Program> m_program; | ||
| 207 | Ref<device::Program> m_maskProgram; | ||
| 208 | Ref<device::Buffer> m_unused; | ||
| 209 | std::optional<algorithms::Scan> m_scan; | ||
| 210 | std::optional<algorithms::RadixSort> m_sort; | ||
| 211 | uint32_t m_workCapacity = 0; | ||
| 212 | uint32_t m_pairCapacity = 0; | ||
| 213 | std::map<const OverlayData*, Uploaded> m_uploads; | ||
| 214 | Ref<device::Buffer> m_views; | ||
| 215 | Ref<device::Buffer> m_batches; | ||
| 216 | Ref<device::Buffer> m_records; | ||
| 217 | Ref<device::Buffer> m_tileCounts; | ||
| 218 | Ref<device::Buffer> m_offsets; | ||
| 219 | Ref<device::Buffer> m_keys; | ||
| 220 | Ref<device::Buffer> m_values; | ||
| 221 | Ref<device::Buffer> m_occupied; | ||
| 222 | Ref<device::Buffer> m_pairCount; | ||
| 223 | Ref<device::Buffer> m_pairsNeeded; | ||
| 224 | std::optional<algorithms::RadixSort> m_groupSort; | ||
| 225 | uint32_t m_groupSortCapacity = 0; | ||
| 226 | std::vector<Grouping> m_groupings; | ||
| 227 | std::vector<OverlayGroupParams> m_groupParams; | ||
| 228 | std::vector<GroupKey> m_groupKeys; | ||
| 229 | std::vector<GroupKey> m_measuredKeys; | ||
| 230 | std::map<GroupKey, uint32_t> m_groupStrides; | ||
| 231 | Ref<device::Buffer> m_groupNeeds; | ||
| 232 | Ref<device::Buffer> m_groupNeedsRead; | ||
| 233 | Ref<device::Buffer> m_fontField; | ||
| 234 | Ref<device::Buffer> m_fontGlyphs; | ||
| 235 | Ref<device::Buffer> m_fontKerning; | ||
| 236 | std::vector<OverlayUniteParams> m_uniteParams; | ||
| 237 | std::vector<Ref<device::Buffer>> m_boxes; | ||
| 238 | std::vector<OverlayOutline> m_outlines; | ||
| 239 | Ref<device::Buffer> m_outlineBuffer; | ||
| 240 | Ref<device::Buffer> m_outlineMasks; | ||
| 241 | Ref<device::Buffer> m_outlineDepths; | ||
| 242 | Ref<device::Buffer> m_outlineTiles; | ||
| 243 | Ref<device::Buffer> m_fills; | ||
| 244 | Ref<device::Buffer> m_fillCount; | ||
| 245 | Ref<device::Buffer> m_cameraImages; | ||
| 246 | Ref<device::Buffer> m_pickQueries; | ||
| 247 | Ref<device::Buffer> m_pickResults; | ||
| 248 | std::vector<OverlayBatchSource> m_batchSources; | ||
| 249 | }; | ||
| 250 | |||
| 251 | } | ||
| 252 |