OGLplus  (0.59.0) a C++ wrapper for rendering APIs

render_graph.hpp
Go to the documentation of this file.
1 #ifndef OGLPLUS_TEXGEN_RENDER_GRAPH_HPP
9 #define OGLPLUS_TEXGEN_RENDER_GRAPH_HPP
10 
11 #include "render_node.hpp"
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 namespace eagine::oglp::texgen {
18 
19 template <typename Node>
20 class render_graph_node : public Node {
21 public:
22  using Node::Node;
23 
24  // TODO throw connect error
25  render_graph_node<Node>& connect(string_view inp_name, output_intf& out) {
26  if(auto inp = this->input_by_name(inp_name)) {
27  connect_output_to_input(out, inp);
28  }
29  return *this;
30  }
31 
32  render_graph_node<Node>&
33  connect(string_view inp_name, node_intf& out_node, span_size_t oidx) {
34  if(oidx < out_node.output_count()) {
35  return connect(inp_name, out_node.output(oidx));
36  }
37  return *this;
38  }
39 
40  render_graph_node<Node>& connect(string_view inp_name, node_intf& out_node) {
41  return connect(inp_name, out_node, 0);
42  }
43 
44  render_graph_node<Node>& connect(span_size_t iidx, output_intf& out) {
45  if(iidx < this->input_count()) {
46  connect_output_to_input(out, this->input(iidx));
47  }
48  return *this;
49  }
50 
51  render_graph_node<Node>&
52  connect(span_size_t iidx, node_intf& out_node, span_size_t oidx) {
53  if(oidx < out_node.output_count()) {
54  return connect(iidx, out_node.output(oidx));
55  }
56  return *this;
57  }
58 
59  render_graph_node<Node>& connect(span_size_t iidx, node_intf& out_node) {
60  return connect(iidx, out_node, 0);
61  }
62 
63  render_graph_node<Node>& connect(output_intf& out) {
64  return connect(0, out);
65  }
66 
67  render_graph_node<Node>& connect(node_intf& out_node) {
68  return connect(0, out_node);
69  }
70 };
71 
72 class render_graph {
73 private:
74  using _node_ptr_t = std::unique_ptr<node_intf>;
75  std::vector<_node_ptr_t> _anon_nodes;
76  std::map<std::string, _node_ptr_t> _nodes;
77  std::unique_ptr<render_node> _render_node;
78 
79 public:
80  render_graph();
81  render_graph(render_graph&&) noexcept = default;
82  render_graph(const render_graph&) = delete;
83  render_graph& operator=(render_graph&&) noexcept = default;
84  render_graph& operator=(const render_graph&) = delete;
85  ~render_graph();
86 
87  void disconnect_all();
88 
89  void add_anonymous_node(std::unique_ptr<node_intf>&&);
90 
91  void add_node(std::string name, std::unique_ptr<node_intf>&&);
92 
93  template <typename NodeType, typename... P>
94  render_graph_node<NodeType>& add_new_anon(P&&... p) {
95  auto* ptr = new render_graph_node<NodeType>(std::forward<P>(p)...);
96  EAGINE_ASSERT(ptr);
97  add_anonymous_node(_node_ptr_t(ptr));
98  return *ptr;
99  }
100 
101  template <typename NodeType, typename... P>
102  render_graph_node<NodeType>& add_new(std::string name, P&&... p) {
103  auto* ptr = new render_graph_node<NodeType>(std::forward<P>(p)...);
104  EAGINE_ASSERT(ptr);
105  add_node(std::move(name), _node_ptr_t(ptr));
106  return *ptr;
107  }
108 
109  render_node& renderer();
110 
111  void set_dimensions(
112  const valid_if_positive<int>& w,
113  const valid_if_positive<int>& h);
114 
115  void render();
116 
117  // find node
118  optional_reference_wrapper<node_intf>
119  find_node(const std::string& node_name);
120 
121  // find input / output
122  optional_reference_wrapper<input_intf>
123  find_node_input(node_intf& node, span_size_t index);
124 
125  optional_reference_wrapper<output_intf>
126  find_node_output(node_intf& node, span_size_t index);
127 
128  optional_reference_wrapper<input_intf>
129  find_node_input(node_intf& node, string_view iname);
130 
131  optional_reference_wrapper<output_intf>
132  find_node_output(node_intf& node, string_view oname);
133 
134  optional_reference_wrapper<input_intf>
135  find_node_input(const std::string& node_name, span_size_t index);
136 
137  optional_reference_wrapper<output_intf>
138  find_node_output(const std::string& node_name, span_size_t index);
139 
140  optional_reference_wrapper<input_intf>
141  find_node_input(const std::string& node_name, string_view iname);
142 
143  optional_reference_wrapper<output_intf>
144  find_node_output(const std::string& node_name, string_view oname);
145 
146  // connect to renderer
147  bool connect_to_renderer(output_intf& output);
148 
149  bool connect_to_renderer(node_intf&, span_size_t index = 0);
150 
151  bool connect_to_renderer(const std::string& node_name, span_size_t index);
152 
153  bool connect_to_renderer(const std::string& node_name) {
154  return connect_to_renderer(node_name, 0);
155  }
156 
157  // connect
158  bool connect(output_intf& out, input_intf& inp);
159 
160  bool connect(
161  node_intf& output_node,
162  span_size_t oindex,
163  node_intf& input_node,
164  span_size_t iindex);
165 
166  bool connect(
167  node_intf& output_node,
168  string_view oname,
169  node_intf& input_node,
170  string_view iname);
171 
172  bool
173  connect(node_intf& output_node, node_intf& input_node, string_view iname);
174 
175  bool connect(
176  const std::string& output_node_name,
177  span_size_t oindex,
178  const std::string& input_node_name,
179  span_size_t iindex);
180 
181  bool connect(
182  const std::string& output_node_name,
183  const std::string& input_node_name,
184  span_size_t iindex);
185 
186  bool connect(
187  const std::string& output_node_name,
188  string_view oname,
189  const std::string& input_node_name,
190  string_view iname);
191 
192  bool connect(
193  const std::string& output_node_name,
194  const std::string& input_node_name,
195  string_view iname);
196 
197  bool connect(
198  const std::string& output_node_name,
199  const std::string& input_node_name);
200 
201  bool finalize();
202 };
203 
204 } // namespace eagine::oglp::texgen
205 
206 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
207 #include <oglplus/texgen/render_graph.inl>
208 #endif
209 
210 #endif // OGLPLUS_TEXGEN_RENDER_GRAPH_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).