score-plugin-gfx/Gfx/Graph/Node.hpp
1 #pragma once
2 #include <Gfx/Graph/CommonUBOs.hpp>
3 #include <Gfx/Graph/Mesh.hpp>
4 #include <Gfx/Graph/RenderState.hpp>
5 #include <Gfx/Graph/Uniforms.hpp>
6 #include <Gfx/Graph/Utils.hpp>
7 
8 #include <ossia/dataflow/geometry_port.hpp>
9 #include <ossia/dataflow/nodes/media.hpp>
10 #include <ossia/dataflow/token_request.hpp>
11 #include <ossia/detail/flat_map.hpp>
12 #include <ossia/detail/variant.hpp>
13 #include <ossia/network/value/value.hpp>
14 
15 #include <score_plugin_gfx_export.h>
16 
17 #include <algorithm>
18 #include <optional>
19 #include <vector>
20 
21 namespace score::gfx
22 {
23 class RenderList;
24 struct Graph;
25 class GenericNodeRenderer;
26 class NodeRenderer;
27 using FunctionMessage = std::function<void(score::gfx::Node&)>;
28 #if BOOST_VERSION < 107900
29 // Old boost: small_vector was not nothrow-move-constructible so we remove the check there.
30 using gfx_input = ossia::slow_variant<
31  ossia::monostate, ossia::value, ossia::audio_vector, ossia::geometry_spec,
32  ossia::transform3d, FunctionMessage>;
33 #else
34 using gfx_input = ossia::variant<
35  ossia::monostate, ossia::value, ossia::audio_vector, ossia::geometry_spec,
36  ossia::transform3d, FunctionMessage>;
37 #endif
38 
44 struct Timings
45 {
46  ossia::time_value date{};
47  ossia::time_value parent_duration{};
48 };
49 
50 struct Message
51 {
52  int32_t node_id{};
53  Timings token{};
54  std::vector<gfx_input> input;
55 };
59 class SCORE_PLUGIN_GFX_EXPORT Node
60 {
61 public:
62  explicit Node();
63  virtual ~Node();
64 
65  Node(const Node&) = delete;
66  Node(Node&&) = delete;
67  Node& operator=(const Node&) = delete;
68  Node& operator=(Node&&) = delete;
69 
73  virtual NodeRenderer* createRenderer(RenderList& r) const noexcept = 0;
74 
78  virtual void renderedNodesChanged();
79 
83  virtual void process(Message&& msg);
84  virtual void update();
85 
89  std::vector<Port*> input;
95  ossia::small_pod_vector<Port*, 1> output;
96 
100  ossia::flat_map<RenderList*, score::gfx::NodeRenderer*> renderedNodes;
101 
102  int32_t id = -1;
103  bool addedToGraph{};
104 };
105 
109 class SCORE_PLUGIN_GFX_EXPORT ProcessNode : public Node
110 {
111 public:
112  using Node::Node;
113 
117  void materialChange() noexcept
118  {
119  materialChanged.fetch_add(1, std::memory_order_release);
120  }
121  bool hasMaterialChanged(int64_t& renderer) const noexcept
122  {
123  int64_t res = materialChanged.load(std::memory_order_acquire);
124  if(renderer != res)
125  {
126  renderer = res;
127  return true;
128  }
129  return false;
130  }
131  std::atomic_int64_t materialChanged{0};
132 
136  void geometryChange() noexcept
137  {
138  geometryChanged.fetch_add(1, std::memory_order_release);
139  }
140  bool hasGeometryChanged(int64_t& renderer) const noexcept
141  {
142  int64_t res = geometryChanged.load(std::memory_order_acquire);
143  if(renderer != res)
144  {
145  renderer = res;
146  return true;
147  }
148  return false;
149  }
150  std::atomic_int64_t geometryChanged{-1};
151 
157  ProcessUBO standardUBO{};
158 
165  ossia::geometry_spec geometry;
166 
167  void process(Message&& msg) override;
168  void process(Timings tk);
169  void process(int32_t port, const ossia::value& v);
170  void process(int32_t port, const ossia::audio_vector& v);
171  void process(int32_t port, const ossia::geometry_spec& v);
172  void process(int32_t port, const ossia::transform3d& v);
173  void process(int32_t port, ossia::monostate) const noexcept { }
174  void process(int32_t port, const FunctionMessage&);
175 };
176 
180 class SCORE_PLUGIN_GFX_EXPORT NodeModel : public score::gfx::ProcessNode
181 {
182 public:
183  explicit NodeModel();
184  virtual ~NodeModel();
185 
186  virtual score::gfx::NodeRenderer*
187  createRenderer(RenderList& r) const noexcept override;
188 
189 protected:
190  std::unique_ptr<char[]> m_materialData;
191 
192  friend class GenericNodeRenderer;
193 };
194 }
195 
196 // QDebug operator<<(QDebug, const score::gfx::gfx_input&);
197 QDebug operator<<(QDebug, const std::vector<score::gfx::gfx_input>&);
Generic renderer.
Definition: NodeRenderer.hpp:61
Root data model for visual nodes.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:60
std::vector< Port * > input
Input ports of that node.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:89
virtual NodeRenderer * createRenderer(RenderList &r) const noexcept=0
Create a renderer in a given context for this node.
ossia::flat_map< RenderList *, score::gfx::NodeRenderer * > renderedNodes
Map associating each RenderList to a Renderer for this model.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:100
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:95
Common base class for most single-pass, simple nodes.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:181
Renderer for a given node.
Definition: NodeRenderer.hpp:11
Common base class for nodes that map to score processes.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:110
ossia::geometry_spec geometry
The geometry to use.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:165
void geometryChange() noexcept
Used to notify a geometry change from the model to the renderers.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:136
void materialChange() noexcept
Used to notify a material change from the model to the renderers.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:117
List of nodes to be rendered to an output.
Definition: RenderList.hpp:19
Graphics rendering pipeline for ossia score.
Definition: Filter/PreviewWidget.hpp:12
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:51
UBO specific to individual processes / nodes.
Definition: CommonUBOs.hpp:12
Messages sent from the execution thread to the rendering thread.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:45