scene/tables/semantics.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <map> | ||
| 7 | #include <memory> | ||
| 8 | #include <mutex> | ||
| 9 | #include <set> | ||
| 10 | #include <span> | ||
| 11 | #include <string> | ||
| 12 | #include <string_view> | ||
| 13 | #include <unordered_map> | ||
| 14 | #include <utility> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | #include "chrono_batch_render/device/buffer.h" | ||
| 18 | #include "chrono_batch_render/device/ref.h" | ||
| 19 | #include "chrono_batch_render/device/tensor.h" | ||
| 20 | #include "chrono_batch_render/scene/records/prims.h" | ||
| 21 | #include "chrono_batch_render/scene/records/render_vars.h" | ||
| 22 | #include "chrono_batch_render/scene/records/semantics.h" | ||
| 23 | #include "chrono_batch_render/scene/records/transforms.h" | ||
| 24 | #include "chrono_batch_render/scene/tables/table.h" | ||
| 25 | #include "chrono_batch_render/stage/structure/structure.h" | ||
| 26 | #include "chrono_batch_render/stage/types.h" | ||
| 27 | |||
| 28 | namespace chrono::batch_render { | ||
| 29 | |||
| 30 | 1 | struct IdEntry { | |
| 31 | StableId id; | ||
| 32 | std::string text; | ||
| 33 | std::string path; | ||
| 34 | std::vector<std::pair<std::string, std::vector<std::string>>> labels; | ||
| 35 | }; | ||
| 36 | |||
| 37 | using IdEntries = std::vector<IdEntry>; | ||
| 38 | |||
| 39 | StableId HashPath(std::string_view path); | ||
| 40 | std::vector<std::byte> PackIdMap(std::span<const std::pair<StableId, std::string>> entries); | ||
| 41 | |||
| 42 | struct SemanticMap { | ||
| 43 | std::shared_ptr<const IdMap> map; | ||
| 44 | device::Tensor packed; | ||
| 45 | }; | ||
| 46 | |||
| 47 | 361 | struct SemanticStats { | |
| 48 | uint32_t resolved = 0; | ||
| 49 | bool reallocated = false; | ||
| 50 | }; | ||
| 51 | |||
| 52 | class SemanticTable : public RenderTable { | ||
| 53 | public: | ||
| 54 | 361 | explicit SemanticTable(Structure& structure) : m_structure(structure) {} | |
| 55 | |||
| 56 | 942 | std::string_view Name() const override { | |
| 57 | 942 | return "semantics"; | |
| 58 | } | ||
| 59 | |||
| 60 | std::span<const ColumnRef> Inputs() const override; | ||
| 61 | bool Pending() const override; | ||
| 62 | void Run(const ChangeSet& changes, TableVersions& versions) override; | ||
| 63 | |||
| 64 | void Require(std::span<const Token> taxonomies); | ||
| 65 | |||
| 66 | const SemanticsData& Data() const { | ||
| 67 | return m_data; | ||
| 68 | } | ||
| 69 | |||
| 70 | const SemanticStats& Stats() const { | ||
| 71 | return m_stats; | ||
| 72 | } | ||
| 73 | |||
| 74 | const Ref<device::Buffer>& StableIds() const { | ||
| 75 | 1 | return m_stableBuffer; | |
| 76 | } | ||
| 77 | |||
| 78 | void Describe(RenderTables& tables) const override; | ||
| 79 | |||
| 80 | 942 | void Hold(std::vector<Ref<const RefCounted>>& held) const override { | |
| 81 | 4710 | HoldBuffers(held, {m_stableBuffer, m_idBuffer, m_primBuffer}); | |
| 82 | 1884 | } | |
| 83 | |||
| 84 | uint32_t SemanticId(Token taxonomy, uint32_t prim) const; | ||
| 85 | uint32_t LabeledPrim(Token taxonomy, uint32_t prim) const; | ||
| 86 | |||
| 87 | SemanticMap Map(RenderVarSource source, Token taxonomy); | ||
| 88 | |||
| 89 | private: | ||
| 90 | IdEntries Entries(RenderVarSource source, Token taxonomy) const; | ||
| 91 | IdEntries SemanticIdEntries(Token taxonomy) const; | ||
| 92 | IdEntries StableIdEntries() const; | ||
| 93 | using Labels = std::vector<uint32_t>; | ||
| 94 | |||
| 95 | uint32_t TaxonomyIndex(Token taxonomy) const; | ||
| 96 | std::set<uint32_t> LabeledPrims(Token taxonomy) const; | ||
| 97 | std::vector<std::vector<std::string>> SetTexts(uint32_t taxonomy) const; | ||
| 98 | IdEntries PrimEntries(Token taxonomy, bool stable) const; | ||
| 99 | const Labels* FindLabels(uint32_t prim, Token taxonomy, | ||
| 100 | std::map<uint64_t, Labels>& reads) const; | ||
| 101 | void TrackRows(uint32_t prim); | ||
| 102 | void Resolve(uint32_t prim, std::map<uint64_t, Labels>& reads); | ||
| 103 | void Grow(uint32_t prims); | ||
| 104 | void Upload(std::span<const uint32_t> prims); | ||
| 105 | |||
| 106 | Structure& m_structure; | ||
| 107 | std::vector<Token> m_wanted; | ||
| 108 | std::vector<Token> m_taxonomies; | ||
| 109 | uint32_t m_stride = 0; | ||
| 110 | std::vector<StableId> m_stableIds; | ||
| 111 | std::vector<uint32_t> m_semanticIds; | ||
| 112 | std::vector<uint32_t> m_semanticPrims; | ||
| 113 | std::vector<std::map<Labels, uint32_t>> m_sets; | ||
| 114 | std::unordered_map<uint32_t, uint32_t> m_byLabelRow; | ||
| 115 | std::unordered_map<uint32_t, std::vector<uint32_t>> m_labelRows; | ||
| 116 | Ref<device::Buffer> m_stableBuffer; | ||
| 117 | Ref<device::Buffer> m_idBuffer; | ||
| 118 | Ref<device::Buffer> m_primBuffer; | ||
| 119 | SemanticsData m_data{}; | ||
| 120 | SemanticStats m_stats; | ||
| 121 | bool m_built = false; | ||
| 122 | std::mutex m_mapMutex; | ||
| 123 | std::map<std::pair<RenderVarSource, uint32_t>, SemanticMap> m_maps; | ||
| 124 | }; | ||
| 125 | |||
| 126 | } | ||
| 127 |