graph/graph.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <compare> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <functional> | ||
| 7 | #include <span> | ||
| 8 | #include <string> | ||
| 9 | #include <string_view> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace chrono::batch_render::graph { | ||
| 13 | |||
| 14 | inline constexpr uint32_t kNoNode = ~0u; | ||
| 15 | |||
| 16 | struct Edge { | ||
| 17 | uint32_t input = 0; | ||
| 18 | uint32_t source = kNoNode; | ||
| 19 | uint32_t output = 0; | ||
| 20 | }; | ||
| 21 | |||
| 22 | 1764 | struct Node { | |
| 23 | uint32_t kind = 0; | ||
| 24 | std::vector<Edge> inputs; | ||
| 25 | }; | ||
| 26 | |||
| 27 | 122 | struct Ordering { | |
| 28 | std::vector<uint32_t> order; | ||
| 29 | std::vector<uint32_t> cycle; | ||
| 30 | }; | ||
| 31 | |||
| 32 | 127 | class Graph { | |
| 33 | public: | ||
| 34 | uint32_t Add(uint32_t kind); | ||
| 35 | void Connect(uint32_t node, uint32_t input, uint32_t source, uint32_t output); | ||
| 36 | |||
| 37 | 196 | std::span<const Node> Nodes() const { | |
| 38 | 196 | return m_nodes; | |
| 39 | } | ||
| 40 | |||
| 41 | 764 | const Node& operator[](uint32_t node) const { | |
| 42 | 764 | return m_nodes[node]; | |
| 43 | } | ||
| 44 | |||
| 45 | Ordering Order() const; | ||
| 46 | |||
| 47 | private: | ||
| 48 | std::vector<Node> m_nodes; | ||
| 49 | }; | ||
| 50 | |||
| 51 | 483 | class ShapeKey { | |
| 52 | public: | ||
| 53 | std::string_view Bytes() const { | ||
| 54 | return m_bytes; | ||
| 55 | } | ||
| 56 | |||
| 57 | uint64_t Hash() const; | ||
| 58 | |||
| 59 | 19 | bool operator==(const ShapeKey&) const = default; | |
| 60 | auto operator<=>(const ShapeKey&) const = default; | ||
| 61 | |||
| 62 | private: | ||
| 63 | friend class ShapeWriter; | ||
| 64 | |||
| 65 | 19 | std::string m_bytes; | |
| 66 | }; | ||
| 67 | |||
| 68 | 234 | class ShapeWriter { | |
| 69 | public: | ||
| 70 | void Add(uint32_t value); | ||
| 71 | void Add(std::string_view text); | ||
| 72 | void Add(std::span<const std::byte> bytes); | ||
| 73 | |||
| 74 | ShapeKey Finish() &&; | ||
| 75 | |||
| 76 | private: | ||
| 77 | ShapeKey m_key; | ||
| 78 | }; | ||
| 79 | |||
| 80 | using NodeUniforms = std::function<void(uint32_t node, ShapeWriter& writer)>; | ||
| 81 | |||
| 82 | ShapeKey GraphShape(const Graph& graph, std::span<const uint32_t> order, | ||
| 83 | const NodeUniforms& uniforms = {}); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | template <> struct std::hash<::chrono::batch_render::graph::ShapeKey> { | ||
| 88 | 80 | size_t operator()(const ::chrono::batch_render::graph::ShapeKey& key) const { | |
| 89 | 80 | return size_t(key.Hash()); | |
| 90 | } | ||
| 91 | }; | ||
| 92 |