scene/tables/meshes.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <compare> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <map> | ||
| 7 | #include <memory> | ||
| 8 | #include <optional> | ||
| 9 | #include <set> | ||
| 10 | #include <unordered_map> | ||
| 11 | #include <utility> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | #include "chrono_batch_render/algorithms/radix_sort.h" | ||
| 15 | #include "chrono_batch_render/algorithms/scan.h" | ||
| 16 | #include "chrono_batch_render/bvh/builder.h" | ||
| 17 | #include "chrono_batch_render/device/buffer.h" | ||
| 18 | #include "chrono_batch_render/device/program.h" | ||
| 19 | #include "chrono_batch_render/device/ref.h" | ||
| 20 | #include "chrono_batch_render/scene/records/meshes.h" | ||
| 21 | #include "chrono_batch_render/scene/tables/storage.h" | ||
| 22 | #include "chrono_batch_render/scene/tables/table.h" | ||
| 23 | #include "chrono_batch_render/stage/structure/structure.h" | ||
| 24 | |||
| 25 | namespace chrono::batch_render { | ||
| 26 | |||
| 27 | 399 | struct GeometryStats { | |
| 28 | uint32_t meshes = 0; | ||
| 29 | uint32_t shapes = 0; | ||
| 30 | uint32_t topologies = 0; | ||
| 31 | uint32_t built = 0; | ||
| 32 | uint32_t refit = 0; | ||
| 33 | uint32_t triangulated = 0; | ||
| 34 | }; | ||
| 35 | |||
| 36 | 39 | struct MeshShape { | |
| 37 | Ref<bvh::Blas> blas; | ||
| 38 | uint32_t points = 0; | ||
| 39 | uint32_t triangles = 0; | ||
| 40 | uint32_t users = 0; | ||
| 41 | }; | ||
| 42 | |||
| 43 | class MeshTable : public RenderTable { | ||
| 44 | public: | ||
| 45 | 399 | MeshTable(Structure& structure, bvh::Builder& builder) | |
| 46 | 399 | : m_structure(structure), m_builder(builder) {} | |
| 47 | |||
| 48 | 1013 | std::string_view Name() const override { | |
| 49 | 1013 | return "geometry"; | |
| 50 | } | ||
| 51 | |||
| 52 | std::span<const ColumnRef> Inputs() const override; | ||
| 53 | std::span<const TableId> TableOutputs() const override; | ||
| 54 | void Describe(RenderTables& tables) const override; | ||
| 55 | void Prepare() override; | ||
| 56 | void Run(const ChangeSet& changes, TableVersions& versions) override; | ||
| 57 | |||
| 58 | const GeometryStats& Stats() const { | ||
| 59 | 1 | return m_stats; | |
| 60 | } | ||
| 61 | |||
| 62 | std::optional<MeshShape> Find(uint32_t prim) const; | ||
| 63 | |||
| 64 | 40 | const MeshRecord& Record(uint32_t meshRow) const { | |
| 65 | 49 | return m_records.Row(meshRow); | |
| 66 | } | ||
| 67 | |||
| 68 | void Hold(std::vector<Ref<const RefCounted>>& held) const override; | ||
| 69 | |||
| 70 | private: | ||
| 71 | struct TopologyKey { | ||
| 72 | 1772 | uint64_t indices = 0; | |
| 73 | 1732 | uint32_t indexCount = 0; | |
| 74 | 1732 | uint64_t counts = 0; | |
| 75 | 1732 | uint32_t faceCount = 0; | |
| 76 | 1732 | uint64_t holes = 0; | |
| 77 | 1732 | uint32_t holeCount = 0; | |
| 78 | |||
| 79 | 1772 | auto operator<=>(const TopologyKey&) const = default; | |
| 80 | }; | ||
| 81 | |||
| 82 | struct ShapeKey { | ||
| 83 | 522 | uint64_t points = 0; | |
| 84 | 434 | uint32_t pointCount = 0; | |
| 85 | 434 | TopologyKey topology; | |
| 86 | |||
| 87 | 522 | auto operator<=>(const ShapeKey&) const = default; | |
| 88 | }; | ||
| 89 | |||
| 90 | 98 | struct Topology { | |
| 91 | ArrayRef triangles; | ||
| 92 | ArrayRef corners; | ||
| 93 | ArrayRef faces; | ||
| 94 | ArrayRef starts; | ||
| 95 | ArrayRef adjacency; | ||
| 96 | uint32_t triangleCount = 0; | ||
| 97 | uint32_t covered = 0; | ||
| 98 | uint32_t users = 0; | ||
| 99 | bool ready = false; | ||
| 100 | }; | ||
| 101 | |||
| 102 | 222 | struct Shape { | |
| 103 | Ref<bvh::Blas> blas; | ||
| 104 | ArrayRef normals; | ||
| 105 | ArrayRef bounds; | ||
| 106 | uint32_t pointCount = 0; | ||
| 107 | uint32_t users = 0; | ||
| 108 | uint32_t updated = 0; | ||
| 109 | bool build = false; | ||
| 110 | bool refit = false; | ||
| 111 | }; | ||
| 112 | |||
| 113 | 136 | struct Mesh { | |
| 114 | uint32_t prim = kNoPrim; | ||
| 115 | uint32_t generation = 0; | ||
| 116 | uint32_t pointsRow = kNoRow; | ||
| 117 | uint32_t meshRow = kNoRow; | ||
| 118 | std::optional<ShapeKey> key; | ||
| 119 | Shape* shape = nullptr; | ||
| 120 | }; | ||
| 121 | |||
| 122 | void Place(uint32_t prim); | ||
| 123 | void Settle(); | ||
| 124 | void Prune(); | ||
| 125 | void Triangulate(); | ||
| 126 | void Release(Mesh& mesh); | ||
| 127 | ShapeKey Key(const Mesh& mesh) const; | ||
| 128 | void Link(const Mesh& mesh); | ||
| 129 | void Update(std::vector<uint32_t>& updated); | ||
| 130 | |||
| 131 | Structure& m_structure; | ||
| 132 | bvh::Builder& m_builder; | ||
| 133 | device::Program& Kernels(); | ||
| 134 | |||
| 135 | Ref<device::Program> m_program; | ||
| 136 | std::unique_ptr<algorithms::Scan> m_scan; | ||
| 137 | std::unique_ptr<algorithms::RadixSort> m_sort; | ||
| 138 | uint32_t m_scanCapacity = 0; | ||
| 139 | uint32_t m_sortCapacity = 0; | ||
| 140 | std::vector<std::pair<uint32_t, ShapeKey>> m_placing; | ||
| 141 | std::unordered_map<uint32_t, Mesh> m_meshes; | ||
| 142 | std::unordered_map<uint32_t, uint32_t> m_byPointsRow; | ||
| 143 | std::unordered_map<uint32_t, uint32_t> m_byMeshRow; | ||
| 144 | std::map<TopologyKey, Topology> m_topologies; | ||
| 145 | std::map<ShapeKey, Shape> m_shapes; | ||
| 146 | GeometryStats m_stats; | ||
| 147 | RecordTable<MeshRecord> m_records{"mesh records"}; | ||
| 148 | std::vector<uint32_t> m_relinked; | ||
| 149 | bool m_stale = false; | ||
| 150 | }; | ||
| 151 | |||
| 152 | } | ||
| 153 |