Loading...
Searching...
No Matches
CpuAnalysisNode.hpp
1#pragma once
2
3#if SCORE_PLUGIN_GFX
4#include <Crousti/GfxNode.hpp>
5
6namespace oscr
7{
8
9template <typename Node_T>
10 requires(
11 avnd::texture_input_introspection<Node_T>::size > 0
12 && avnd::texture_output_introspection<Node_T>::size == 0)
13struct GfxRenderer<Node_T> final : score::gfx::OutputNodeRenderer
14{
15 using texture_inputs = avnd::texture_input_introspection<Node_T>;
16 std::shared_ptr<Node_T> state;
17 score::gfx::Message m_last_message{};
18 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
19 m_rts;
20
21 std::vector<QRhiReadbackResult> m_readbacks;
22 ossia::time_value m_last_time{-1};
23
24 const GfxNode<Node_T>& node() const noexcept
25 {
26 return static_cast<const GfxNode<Node_T>&>(score::gfx::NodeRenderer::node);
27 }
28 GfxRenderer(const GfxNode<Node_T>& p)
29 : score::gfx::OutputNodeRenderer{p}
30 , state{std::make_shared<Node_T>()}
31 , m_readbacks(texture_inputs::size)
32 {
33 prepareNewState<Node_T>(state, p);
34 }
35
37 renderTargetForInput(const score::gfx::Port& p) override
38 {
39 auto it = m_rts.find(&p);
40 SCORE_ASSERT(it != m_rts.end());
41 return it->second;
42 }
43
44 template <typename Tex>
45 void createInput(
46 score::gfx::RenderList& renderer, int k, const Tex& texture_spec,
48 {
49 auto port = this->node().input[k];
50 static constexpr auto flags
51 = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
52 auto texture = renderer.state.rhi->newTexture(
53 gpp::qrhi::textureFormat(texture_spec), spec.size, 1, flags);
54 SCORE_ASSERT(texture->create());
56 renderer.state, texture, renderer.samples(), renderer.requiresDepth());
57 }
58
59 QRhiTexture* texture(int k) const noexcept
60 {
61 auto port = this->node().input[k];
62 auto it = m_rts.find(port);
63 SCORE_ASSERT(it != m_rts.end());
64 SCORE_ASSERT(it->second.texture);
65 return it->second.texture;
66 }
67
68 void loadInputTexture(QRhi& rhi, avnd::cpu_texture auto& cpu_tex, int k)
69 {
70 oscr::loadInputTexture(rhi, m_readbacks, cpu_tex, k);
71 }
72
73 void init(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) override
74 {
75 if constexpr(requires { state->prepare(); })
76 {
77 this->node().processControlIn(
78 *this, *state, m_last_message, this->node().last_message, this->node().m_ctx);
79 state->prepare();
80 }
81
82 // Init input render targets
83 int k = 0;
84 avnd::cpu_texture_input_introspection<Node_T>::for_all(
85 avnd::get_inputs<Node_T>(*state),
86 [&]<typename F>(F& t) {
87 // FIXME k isn't the port index, it's the texture port index
88 auto spec = this->node().resolveRenderTargetSpecs(k, renderer);
89 if constexpr(requires {
90 t.request_width;
91 t.request_height;
92 })
93 {
94 spec.size.rwidth() = t.request_width;
95 spec.size.rheight() = t.request_height;
96 }
97 createInput(renderer, k, t.texture, spec);
98 if constexpr(avnd::cpu_fixed_format_texture<decltype(t.texture)>)
99 {
100 t.texture.width = spec.size.width();
101 t.texture.height = spec.size.height();
102 }
103 k++;
104 });
105 }
106
107 void update(
108 score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res,
109 score::gfx::Edge* edge) override
110 {
111 bool updated = false;
112 /*
113 int k = 0;
114 avnd::cpu_texture_input_introspection<Node_T>::for_all(
115 avnd::get_inputs<Node_T>(state), [&]<typename F>(F& t) {
116 if constexpr(requires {
117 t.request_width;
118 t.request_height;
119 })
120 {
121 const auto tex = this->texture(k)->pixelSize();
122 if(tex.width() != t.request_width || tex.height() != t.request_height)
123 {
124 QSize sz{t.request_width, t.request_height};
125
126 // Release
127 auto port = parent.input[k];
128
129 m_rts[port].release();
130 createInput(renderer, k, t.texture, sz);
131
132 t.texture.width = sz.width();
133 t.texture.height = sz.height();
134
135 updated = true;
136 }
137 }
138 k++;
139 });
140*/
141 if(updated)
142 {
143 // We must notify the graph that the previous nodes have to be recomputed
144 }
145 }
146
147 void release(score::gfx::RenderList& r) override
148 {
149 // Free inputs
150 // TODO investigate why reference does not work here:
151 for(auto [port, rt] : m_rts)
152 rt.release();
153 m_rts.clear();
154 }
155
156 void inputAboutToFinish(
157 score::gfx::RenderList& renderer, const score::gfx::Port& p,
158 QRhiResourceUpdateBatch*& res) override
159 {
160 auto& parent = this->node();
161 res = renderer.state.rhi->nextResourceUpdateBatch();
162 const auto& inputs = parent.input;
163 auto index_of_port = ossia::find(inputs, &p) - inputs.begin();
164 SCORE_ASSERT(index_of_port == 0);
165 {
166 auto tex = m_rts[&p].texture;
167 auto& readback = m_readbacks[index_of_port];
168 readback = {};
169 res->readBackTexture(QRhiReadbackDescription{tex}, &readback);
170 }
171 }
172
173 void runInitialPasses(
174 score::gfx::RenderList& renderer, QRhiCommandBuffer& commands,
175 QRhiResourceUpdateBatch*& res, score::gfx::Edge& edge) override
176 {
177 auto& parent = this->node();
178 auto& rhi = *renderer.state.rhi;
179
180 // Insert a synchronisation point to allow readbacks to complete
181 rhi.finish();
182
183 // If we are paused, we don't run the processor implementation.
184 if(parent.last_message.token.date == m_last_time)
185 {
186 return;
187 }
188 m_last_time = parent.last_message.token.date;
189
190 // Fetch input textures (if any)
191 {
192 // Copy the readback output inside the structure
193 // TODO it would be much better to do this inside the readback's
194 // "completed" callback.
195 int k = 0;
196 avnd::cpu_texture_input_introspection<Node_T>::for_all(
197 avnd::get_inputs<Node_T>(*state), [&](auto& t) {
198 loadInputTexture(rhi, t.texture, k);
199 k++;
200 });
201 }
202
203 parent.processControlIn(
204 *this, *state, m_last_message, parent.last_message, parent.m_ctx);
205
206 // Run the processor
207 (*state)();
208
209 // Copy the data to the model node
210 parent.processControlOut(*this->state);
211
212 // Copy the geometry
213 // FIXME we need something such as port_run_{pre,post}process for GPU nodes
214 avnd::geometry_output_introspection<Node_T>::for_all_n2(
215 state->outputs, [&]<std::size_t F, std::size_t P>(
216 auto& t, avnd::predicate_index<P>, avnd::field_index<F>) {
217 postprocess_geometry(t);
218 });
219 }
220 template <avnd::geometry_port Field>
221 void postprocess_geometry(Field& ctrl)
222 {
223 using namespace avnd;
224 // bool mesh_dirty{};
225 // bool tform_dirty{};
226 // mesh_dirty = ctrl.dirty_mesh;
227
228 if(ctrl.dirty_mesh)
229 {
230 auto meshes = std::make_shared<ossia::mesh_list>();
231 auto& ossia_meshes = *meshes;
232 if constexpr(static_geometry_type<Field> || dynamic_geometry_type<Field>)
233 {
234 ossia_meshes.meshes.resize(1);
235 oscr::load_geometry(ctrl, ossia_meshes.meshes[0]);
236 }
237 else if constexpr(
238 static_geometry_type<decltype(Field::mesh)>
239 || dynamic_geometry_type<decltype(Field::mesh)>)
240 {
241 ossia_meshes.meshes.resize(1);
242 oscr::load_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
243 }
244 else
245 {
246 oscr::load_geometry(ctrl, ossia_meshes);
247 }
248 }
249 ctrl.dirty_mesh = false;
250
251 // if constexpr(requires { ctrl.transform; })
252 // {
253 // if(ctrl.dirty_transform)
254 // {
255 // std::copy_n(
256 // ctrl.transform, std::ssize(ctrl.transform), port.data.transform.matrix);
257 // tform_dirty = true;
258 // ctrl.dirty_transform = false;
259 // }
260 // }
261 //
262 // port.data.flags = {};
263 // if(mesh_dirty)
264 // port.data.flags = port.data.flags | ossia::geometry_port::dirty_meshes;
265 // if(tform_dirty)
266 // port.data.flags = port.data.flags | ossia::geometry_port::dirty_transform;
267 }
268};
269
270template <typename Node_T>
271 requires(avnd::texture_input_introspection<Node_T>::size > 0
272 && avnd::texture_output_introspection<Node_T>::size == 0)
273struct GfxNode<Node_T> final
274 : CustomGpuOutputNodeBase
275 , GpuNodeElements<Node_T>
276{
277 oscr::ProcessModel<Node_T>& processModel;
278 GfxNode(
280 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
281 int64_t id, const score::DocumentContext& ctx)
282 : CustomGpuOutputNodeBase{std::move(q), std::move(ctls), ctx}
283 , processModel{element}
284 {
285 this->instance = id;
286
287 initGfxPorts<Node_T>(this, this->input, this->output);
288 }
289
291 createRenderer(score::gfx::RenderList& r) const noexcept override
292 {
293 return new GfxRenderer<Node_T>{*this};
294 }
295};
296}
297#endif
Definition score-plugin-avnd/Crousti/ProcessModel.hpp:86
Definition OutputNode.hpp:11
List of nodes to be rendered to an output.
Definition RenderList.hpp:19
bool requiresDepth() const noexcept
Whether this list of rendering actions requires depth testing at all.
Definition RenderList.hpp:137
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:89
Definition Factories.hpp:19
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples, bool depth)
Create a render target from a texture.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:10
Base toolkit upon which the software is built.
Definition Application.cpp:97
STL namespace.
Definition DocumentContext.hpp:18
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:70
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:50
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:52
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:57
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:115