Loading...
Searching...
No Matches
GpuUtils.hpp
1#pragma once
2
3#include <avnd/introspection/gfx.hpp>
4#if SCORE_PLUGIN_GFX
5#include <Process/ExecutionContext.hpp>
6
7#include <Crousti/File.hpp>
8#include <Crousti/GppCoroutines.hpp>
9#include <Crousti/GppShaders.hpp>
10#include <Crousti/MessageBus.hpp>
11#include <Crousti/TextureConversion.hpp>
12#include <Crousti/TextureFormat.hpp>
13#include <Gfx/GfxExecNode.hpp>
14#include <Gfx/Graph/Node.hpp>
15#include <Gfx/Graph/OutputNode.hpp>
16#include <Gfx/Graph/RenderList.hpp>
17#include <Gfx/Graph/RenderState.hpp>
18
19#include <score/tools/ThreadPool.hpp>
20
21#include <ossia/detail/small_flat_map.hpp>
22
23#include <ossia-qt/invoke.hpp>
24
25#include <QCoreApplication>
26#include <QTimer>
27#include <QtGui/private/qrhi_p.h>
28
29#include <avnd/binding/ossia/metadatas.hpp>
30#include <avnd/binding/ossia/port_run_postprocess.hpp>
31#include <avnd/binding/ossia/port_run_preprocess.hpp>
32#include <avnd/binding/ossia/soundfiles.hpp>
33#include <avnd/concepts/parameter.hpp>
34#include <avnd/introspection/input.hpp>
35#include <avnd/introspection/output.hpp>
36#include <fmt/format.h>
37#include <gpp/layout.hpp>
38
39#include <score_plugin_avnd_export.h>
40
41namespace oscr
42{
43struct GpuWorker
44{
45 template <typename T>
46 void initWorker(this auto& self, std::shared_ptr<T>& state) noexcept
47 {
48 if constexpr(avnd::has_worker<T>)
49 {
50 auto ptr = QPointer{&self};
51 auto& tq = score::TaskPool::instance();
52 using worker_type = decltype(state->worker);
53
54 auto wk_state = std::weak_ptr{state};
55 state->worker.request = [ptr, &tq, wk_state]<typename... Args>(Args&&... f) {
56 using type_of_result = decltype(worker_type::work(std::forward<Args>(f)...));
57 tq.post([... ff = std::forward<Args>(f), wk_state, ptr]() mutable {
58 if constexpr(std::is_void_v<type_of_result>)
59 {
60 worker_type::work(std::forward<decltype(ff)>(ff)...);
61 }
62 else
63 {
64 // If the worker returns a std::function, it
65 // is to be invoked back in the processor DSP thread
66 auto res = worker_type::work(std::forward<decltype(ff)>(ff)...);
67 if(!res || !ptr)
68 return;
69
70 ossia::qt::run_async(
71 QCoreApplication::instance(),
72 [res = std::move(res), wk_state, ptr]() mutable {
73 if(ptr)
74 if(auto state = wk_state.lock())
75 res(*state);
76 });
77 }
78 });
79 };
80 }
81 }
82};
83
84template <typename GpuNodeRenderer, typename Node>
85struct GpuProcessIns
86{
87 GpuNodeRenderer& gpu;
88 Node& state;
89 const score::gfx::Message& prev_mess;
90 const score::gfx::Message& mess;
91 const score::DocumentContext& ctx;
92
93 bool can_process_message(std::size_t N)
94 {
95 if(mess.input.size() <= N)
96 return false;
97
98 if(prev_mess.input.size() == mess.input.size())
99 {
100 auto& prev = prev_mess.input[N];
101 auto& next = mess.input[N];
102 if(prev.index() == 1 && next.index() == 1)
103 {
104 if(ossia::get<ossia::value>(prev) == ossia::get<ossia::value>(next))
105 {
106 return false;
107 }
108 }
109 }
110 return true;
111 }
112
113 void operator()(avnd::parameter auto& t, auto field_index)
114 {
115 if(!can_process_message(field_index))
116 return;
117
118 if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
119 {
120 oscr::from_ossia_value(t, *val, t.value);
121 if_possible(t.update(state));
122 }
123 }
124
125#if OSCR_HAS_MMAP_FILE_STORAGE
126 template <avnd::raw_file_port Field, std::size_t NField>
127 void operator()(Field& t, avnd::field_index<NField> field_index)
128 {
129 // FIXME we should be loading a file there
130 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
131 using file_ports = avnd::raw_file_input_introspection<Node>;
132
133 if(!can_process_message(field_index))
134 return;
135
136 auto val = ossia::get_if<ossia::value>(&mess.input[field_index]);
137 if(!val)
138 return;
139
140 static constexpr bool has_text = requires { decltype(Field::file)::text; };
141 static constexpr bool has_mmap = requires { decltype(Field::file)::mmap; };
142
143 // First we can load it directly since execution hasn't started yet
144 if(auto hdl = loadRawfile(*val, ctx, has_text, has_mmap))
145 {
146 static constexpr auto N = file_ports::field_index_to_index(NField);
147 if constexpr(avnd::port_can_process<Field>)
148 {
149 // FIXME also do it when we get a run-time message from the exec engine,
150 // OSC, etc
151 auto func = executePortPreprocess<Field>(*hdl);
152 const_cast<node_type&>(gpu.node())
153 .file_loaded(
154 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
155 if(func)
156 func(state);
157 }
158 else
159 {
160 const_cast<node_type&>(gpu.node())
161 .file_loaded(
162 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
163 }
164 }
165 }
166#endif
167
168 template <avnd::buffer_port Field, std::size_t NField>
169 void operator()(Field& t, avnd::field_index<NField> field_index)
170 {
171 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
172 auto& node = const_cast<node_type&>(gpu.node());
173 auto val = ossia::get_if<ossia::render_target_spec>(&mess.input[field_index]);
174 if(!val)
175 return;
176 node.process(NField, *val);
177 }
178
179 template <avnd::texture_port Field, std::size_t NField>
180 void operator()(Field& t, avnd::field_index<NField> field_index)
181 {
182 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
183 auto& node = const_cast<node_type&>(gpu.node());
184 auto val = ossia::get_if<ossia::render_target_spec>(&mess.input[field_index]);
185 if(!val)
186 return;
187 node.process(NField, *val);
188 }
189
190 template <avnd::geometry_port Field, std::size_t NField>
191 void operator()(Field& t, avnd::field_index<NField> field_index)
192 {
193 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
194 auto& node = const_cast<node_type&>(gpu.node());
195
196 // FIXME
197 }
198
199 void operator()(auto& t, auto field_index) = delete;
200};
201
202struct GpuControlIns
203{
204 template <typename Self, typename Node_T>
205 static void processControlIn(
206 Self& self, Node_T& state, score::gfx::Message& renderer_mess,
207 const score::gfx::Message& mess, const score::DocumentContext& ctx) noexcept
208 {
209 // Apply the controls
210 avnd::input_introspection<Node_T>::for_all_n(
211 avnd::get_inputs<Node_T>(state),
212 GpuProcessIns<Self, Node_T>{self, state, renderer_mess, mess, ctx});
213 renderer_mess = mess;
214 }
215};
216
217struct GpuControlOuts
218{
219 std::weak_ptr<Execution::ExecutionCommandQueue> queue;
220 Gfx::exec_controls control_outs;
221
222 int64_t instance{};
223
224 template <typename Node_T>
225 void processControlOut(Node_T& state) const noexcept
226 {
227 if(!this->control_outs.empty())
228 {
229 auto q = this->queue.lock();
230 if(!q)
231 return;
232 auto& qq = *q;
233 int parm_k = 0;
234 avnd::parameter_output_introspection<Node_T>::for_all(
235 avnd::get_outputs(state), [&]<avnd::parameter T>(const T& t) {
236 qq.enqueue([v = oscr::to_ossia_value(t, t.value),
237 port = control_outs[parm_k]]() mutable {
238 std::swap(port->value, v);
239 port->changed = true;
240 });
241
242 parm_k++;
243 });
244 }
245 }
246};
247
248template <typename T>
249struct SCORE_PLUGIN_AVND_EXPORT GpuNodeElements
250{
251 [[no_unique_address]] oscr::soundfile_storage<T> soundfiles;
252
253 [[no_unique_address]] oscr::midifile_storage<T> midifiles;
254
255#if defined(OSCR_HAS_MMAP_FILE_STORAGE)
256 [[no_unique_address]] oscr::raw_file_storage<T> rawfiles;
257#endif
258
259 template <std::size_t N, std::size_t NField>
260 void file_loaded(
261 auto& state, const std::shared_ptr<oscr::raw_file_data>& hdl,
262 avnd::predicate_index<N>, avnd::field_index<NField>)
263 {
264 this->rawfiles.load(
265 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
266 }
267};
268
269struct SCORE_PLUGIN_AVND_EXPORT CustomGfxNodeBase : score::gfx::NodeModel
270{
271 explicit CustomGfxNodeBase(const score::DocumentContext& ctx)
272 : score::gfx::NodeModel{}
273 , m_ctx{ctx}
274 {
275 }
276 virtual ~CustomGfxNodeBase();
277 const score::DocumentContext& m_ctx;
278 score::gfx::Message last_message;
279 void process(score::gfx::Message&& msg) override;
281};
282struct SCORE_PLUGIN_AVND_EXPORT CustomGfxOutputNodeBase : score::gfx::OutputNode
283{
284 virtual ~CustomGfxOutputNodeBase();
285
286 score::gfx::Message last_message;
287 void process(score::gfx::Message&& msg) override;
288};
289struct CustomGpuNodeBase
291 , GpuWorker
292 , GpuControlIns
293 , GpuControlOuts
294{
295 CustomGpuNodeBase(
296 std::weak_ptr<Execution::ExecutionCommandQueue>&& q, Gfx::exec_controls&& ctls,
297 const score::DocumentContext& ctx)
298 : GpuControlOuts{std::move(q), std::move(ctls)}
299 , m_ctx{ctx}
300 {
301 }
302
303 virtual ~CustomGpuNodeBase() = default;
304
305 const score::DocumentContext& m_ctx;
306 QString vertex, fragment, compute;
307 score::gfx::Message last_message;
308 void process(score::gfx::Message&& msg) override;
309};
310
311struct SCORE_PLUGIN_AVND_EXPORT CustomGpuOutputNodeBase
313 , GpuWorker
314 , GpuControlIns
315 , GpuControlOuts
316{
317 CustomGpuOutputNodeBase(
318 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls&& ctls,
319 const score::DocumentContext& ctx);
320 virtual ~CustomGpuOutputNodeBase();
321
322 const score::DocumentContext& m_ctx;
323 std::weak_ptr<score::gfx::RenderList> m_renderer{};
324 std::shared_ptr<score::gfx::RenderState> m_renderState{};
325
326 QString vertex, fragment, compute;
327 score::gfx::Message last_message;
328 void process(score::gfx::Message&& msg) override;
330
331 void setRenderer(std::shared_ptr<score::gfx::RenderList>) override;
332 score::gfx::RenderList* renderer() const override;
333
334 void startRendering() override;
335 void render() override;
336 void stopRendering() override;
337 bool canRender() const override;
338 void onRendererChange() override;
339
340 void createOutput(score::gfx::OutputConfiguration) override;
341
342 void destroyOutput() override;
343 std::shared_ptr<score::gfx::RenderState> renderState() const override;
344
345 Configuration configuration() const noexcept override;
346};
347
348template <typename Node_T, typename Node>
349void prepareNewState(std::shared_ptr<Node_T>& eff, const Node& parent)
350{
351 if constexpr(avnd::has_worker<Node_T>)
352 {
353 parent.initWorker(eff);
354 }
355 if constexpr(avnd::has_processor_to_gui_bus<Node_T>)
356 {
357 auto& process = parent.processModel;
358 eff->send_message = [ptr = QPointer{&process}](auto&& b) mutable {
359 // FIXME right now all the rendering is done in the UI thread, which is very MEH
360 // this->in_edit([&process, bb = std::move(b)]() mutable {
361
362 if(ptr && ptr->to_ui)
363 MessageBusSender{ptr->to_ui}(std::move(b));
364 // });
365 };
366
367 // FIXME GUI -> engine. See executor.hpp
368 }
369
370 avnd::init_controls(*eff);
371
372 if constexpr(avnd::can_prepare<Node_T>)
373 {
374 if constexpr(avnd::function_reflection<&Node_T::prepare>::count == 1)
375 {
376 using prepare_type = avnd::first_argument<&Node_T::prepare>;
377 prepare_type t;
378 if_possible(t.instance = parent.instance);
379 eff->prepare(t);
380 }
381 else
382 {
383 eff->prepare();
384 }
385 }
386}
387
388struct port_to_type_enum
389{
390 template <std::size_t I, avnd::buffer_port F>
391 constexpr auto operator()(avnd::field_reflection<I, F> p)
392 {
393 return score::gfx::Types::Buffer;
394 }
395
396 template <std::size_t I, avnd::cpu_texture_port F>
397 constexpr auto operator()(avnd::field_reflection<I, F> p)
398 {
399 using texture_type = std::remove_cvref_t<decltype(F::texture)>;
400 return avnd::cpu_fixed_format_texture<texture_type> ? score::gfx::Types::Image
401 : score::gfx::Types::Buffer;
402 }
403
404 template <std::size_t I, avnd::sampler_port F>
405 constexpr auto operator()(avnd::field_reflection<I, F> p)
406 {
407 return score::gfx::Types::Image;
408 }
409 template <std::size_t I, avnd::image_port F>
410 constexpr auto operator()(avnd::field_reflection<I, F> p)
411 {
412 return score::gfx::Types::Image;
413 }
414 template <std::size_t I, avnd::attachment_port F>
415 constexpr auto operator()(avnd::field_reflection<I, F> p)
416 {
417 return score::gfx::Types::Image;
418 }
419
420 template <std::size_t I, avnd::geometry_port F>
421 constexpr auto operator()(avnd::field_reflection<I, F> p)
422 {
423 return score::gfx::Types::Geometry;
424 }
425 template <std::size_t I, avnd::mono_audio_port F>
426 constexpr auto operator()(avnd::field_reflection<I, F> p)
427 {
428 return score::gfx::Types::Audio;
429 }
430 template <std::size_t I, avnd::poly_audio_port F>
431 constexpr auto operator()(avnd::field_reflection<I, F> p)
432 {
433 return score::gfx::Types::Audio;
434 }
435 template <std::size_t I, avnd::int_parameter F>
436 constexpr auto operator()(avnd::field_reflection<I, F> p)
437 {
438 return score::gfx::Types::Int;
439 }
440 template <std::size_t I, avnd::enum_parameter F>
441 constexpr auto operator()(avnd::field_reflection<I, F> p)
442 {
443 return score::gfx::Types::Int;
444 }
445 template <std::size_t I, avnd::float_parameter F>
446 constexpr auto operator()(avnd::field_reflection<I, F> p)
447 {
448 return score::gfx::Types::Float;
449 }
450 template <std::size_t I, avnd::parameter F>
451 constexpr auto operator()(avnd::field_reflection<I, F> p)
452 {
453 using value_type = std::remove_cvref_t<decltype(F::value)>;
454
455 if constexpr(std::is_aggregate_v<value_type>)
456 {
457 constexpr int sz = boost::pfr::tuple_size_v<value_type>;
458 if constexpr(sz == 2)
459 {
460 return score::gfx::Types::Vec2;
461 }
462 else if constexpr(sz == 3)
463 {
464 return score::gfx::Types::Vec3;
465 }
466 else if constexpr(sz == 4)
467 {
468 return score::gfx::Types::Vec4;
469 }
470 }
471 return score::gfx::Types::Empty;
472 }
473 template <std::size_t I, typename F>
474 constexpr auto operator()(avnd::field_reflection<I, F> p)
475 {
476 return score::gfx::Types::Empty;
477 }
478};
479
480template <typename Node_T>
481inline void initGfxPorts(auto* self, auto& input, auto& output)
482{
483 avnd::input_introspection<Node_T>::for_all(
484 [self, &input]<typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
485 static constexpr auto type = port_to_type_enum{}(f);
486 input.push_back(new score::gfx::Port{self, {}, type, {}});
487 });
488 avnd::output_introspection<Node_T>::for_all(
489 [self,
490 &output]<typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
491 static constexpr auto type = port_to_type_enum{}(f);
492 output.push_back(new score::gfx::Port{self, {}, type, {}});
493 });
494}
495
496static score::gfx::BufferView getInputBuffer(
497 score::gfx::RenderList& renderer, const score::gfx::Node& parent, int port_index)
498{
499 const auto& inputs = parent.input;
500 // SCORE_ASSERT(port_index == 0);
501 {
502 score::gfx::Port* p = inputs[port_index];
503 for(auto& edge : p->edges)
504 {
505 auto src_node = edge->source->node;
506 score::gfx::NodeRenderer* src_renderer = src_node->renderedNodes.at(&renderer);
507 if(src_renderer)
508 {
509 return src_renderer->bufferForOutput(*edge->source);
510 }
511 break;
512 }
513 }
514 return {};
515}
516
517
518static void readbackInputBuffer(
519 score::gfx::RenderList& renderer
520 , QRhiResourceUpdateBatch& res
521 , const score::gfx::Node& parent
522 , QRhiBufferReadbackResult& readback
523 , int port_index
524 )
525{
526 // FIXME: instead of doing this we could do the readback in the
527 // producer node and just read its bytearray once...
528 if(auto buf = getInputBuffer(renderer, parent, port_index))
529 {
530 readback = {};
531 res.readBackBuffer(buf.handle, buf.byte_offset, buf.byte_size, &readback);
532 }
533}
534
535static void recreateOutputBuffer(
536 score::gfx::RenderList& renderer, avnd::cpu_buffer auto& cpu_buf,
537 QRhiResourceUpdateBatch& res, score::gfx::BufferView& buf)
538{
539 const auto bytesize = avnd::get_bytesize(cpu_buf);
540 if(!buf.handle)
541 {
542 if(bytesize > 0)
543 {
544 buf.handle = renderer.state.rhi->newBuffer(
545 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
546 bytesize);
547 buf.handle->setName("GpuUtils::recreateOutputBuffer");
548 buf.byte_offset = 0;
549 buf.byte_size = bytesize;
550
551 buf.handle->create();
552 }
553 else
554 {
555 cpu_buf.changed = false;
556 return;
557 }
558 }
559 else if(buf.handle->size() != bytesize)
560 {
561 buf.handle->destroy();
562 buf.handle->setSize(bytesize);
563 buf.handle->create();
564 buf.byte_size = bytesize;
565 }
566}
567
568static void uploadOutputBuffer(
569 score::gfx::RenderList& renderer, avnd::cpu_buffer auto& cpu_buf,
570 QRhiResourceUpdateBatch& res, score::gfx::BufferView& rhi_buf)
571{
572 if(cpu_buf.changed)
573 {
574 const auto bytesize = avnd::get_bytesize(cpu_buf);
575 recreateOutputBuffer(renderer, cpu_buf, res, rhi_buf);
577 &res, rhi_buf.handle, 0, cpu_buf.byte_size,
578 (const char*)avnd::get_bytes(cpu_buf));
579 cpu_buf.changed = false;
580 }
581}
582
583static void uploadOutputBuffer(
584 score::gfx::RenderList& renderer, avnd::gpu_buffer auto& gpu_buf,
585 QRhiResourceUpdateBatch& res, score::gfx::BufferView& rhi_buf)
586{
587 rhi_buf.handle = reinterpret_cast<QRhiBuffer*>(gpu_buf.handle);
588 rhi_buf.byte_size = gpu_buf.byte_size;
589 rhi_buf.byte_offset = gpu_buf.byte_offset;
590}
591
592template <typename T>
593struct geometry_inputs_storage;
594
595struct mesh_input_storage
596{
597 std::vector<QRhiBufferReadbackResult> readbacks;
598 std::vector<QRhiBuffer*> buffers;
599};
600struct geometry_input_storage
601{
602 ossia::geometry_spec spec;
603 std::vector<mesh_input_storage> meshes;
604};
605
606template <typename T>
607 requires(avnd::geometry_input_introspection<T>::size > 0)
608struct geometry_inputs_storage<T>
609{
610 // FIXME in Gfx/Graph/NodeRenderer.hpp
611 static_assert(avnd::geometry_input_introspection<T>::size == 1);
612
613 geometry_input_storage inputs[avnd::geometry_input_introspection<T>::size];
614 ossia::small_vector<QRhiBuffer*, 4> allocated;
615
616 void readInputGeometries(
617 score::gfx::RenderList& renderer, const ossia::geometry_spec& spec, auto& parent,
618 auto& state)
619 {
620 // Copy the readback output inside the structure
621 // TODO it would be much better to do this inside the readback's
622 // "completed" callback.
623 avnd::geometry_input_introspection<T>::for_all_n(
624 avnd::get_inputs<T>(state),
625 [&]<typename Field, std::size_t N>(Field& t, avnd::predicate_index<N> np) {
626 this->inputs[N].spec = spec; // FIXME multiple geometry input ports
627 this->inputs[N].meshes.resize(1); // FIXME
628
629 // Here we fetch the readbacks results
630 auto& meshes = this->inputs[N].meshes[0];
631
632 oscr::meshes_from_ossia(
633 spec.meshes, t.mesh,
634 [&](auto& write_buf, int buffer_index, void* data, int64_t bytesize) {
635 // CPU input geometry, upload was done before
636 SCORE_ASSERT(buffer_index >= 0);
637 if(buffer_index < meshes.readbacks.size())
638 {
639 QRhiBuffer* handle = meshes.buffers[buffer_index];
640 write_buf.handle = handle;
641 write_buf.byte_size = handle->size();
642 }
643 }, [&](auto& write_buf, int buffer_index, void* handle) {
644 // GPU input buffer, CPU output buffer: need to fetch our readback
645 SCORE_ASSERT(buffer_index >= 0);
646 if(buffer_index < meshes.readbacks.size())
647 {
648 // FIXME investigate why runInitialPasses is called before inputAboutToFinish
649 auto& readback = meshes.readbacks[buffer_index].data;
650 write_buf.raw_data = reinterpret_cast<unsigned char*>(readback.data());
651 write_buf.byte_size = readback.size();
652 }
653 });
654 });
655 }
656
657 void inputAboutToFinish(
658 score::gfx::RenderList& renderer, QRhiResourceUpdateBatch*& res,
659 const ossia::geometry_spec& spec, auto& state, auto& parent)
660 {
661 avnd::geometry_input_introspection<T>::for_all_n2(
662 avnd::get_inputs<T>(state),
663 [&]<typename Field, std::size_t N, std::size_t NField>(
664 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
665 this->inputs[N].spec = spec; // FIXME multiple geometry input ports
666 this->inputs[N].meshes.resize(1); // FIXME
667 // Here we request readbacks if necessary
668
669 auto& meshes = this->inputs[N].meshes[0];
670 oscr::meshes_from_ossia(
671 spec.meshes, t.mesh,
672 [&](auto& write_buf, int buffer_index, void* data, int64_t bytesize) {
673 // cpu -> gpu
674 if(meshes.buffers.size() <= buffer_index)
675 {
676 meshes.buffers.resize(buffer_index + 1);
677 meshes.readbacks.resize(buffer_index + 1);
678
679 auto buf = renderer.state.rhi->newBuffer(
680 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
681 bytesize);
682 buf->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(t));
683 buf->create();
684 allocated.push_back(buf);
685 meshes.buffers[buffer_index] = buf;
686 }
687
688 res->uploadStaticBuffer(meshes.buffers[buffer_index], 0, bytesize, data);
689 }, [&](auto& write_buf, int buffer_index, void* handle) {
690 // gpu -> cpu
691 if(meshes.readbacks.size() <= buffer_index)
692 {
693 meshes.buffers.resize(buffer_index + 1);
694 meshes.readbacks.resize(buffer_index + 1);
695 }
696
697 meshes.readbacks[buffer_index] = {};
698 if(auto buf = static_cast<QRhiBuffer*>(handle))
699 {
700 meshes.buffers[buffer_index] = buf;
701 res->readBackBuffer(buf, 0, buf->size(), &meshes.readbacks[buffer_index]);
702 }
703 else
704 {
705 meshes.buffers[buffer_index] = {};
706 meshes.readbacks[buffer_index] = {};
707 }
708 });
709 });
710 }
711
712 void release(score::gfx::RenderList& renderer)
713 {
714 for(auto& buf : allocated)
715 renderer.releaseBuffer(buf);
716 allocated.clear();
717 }
718};
719
720template <typename T>
721 requires(avnd::geometry_input_introspection<T>::size == 0)
722struct geometry_inputs_storage<T>
723{
724 static void readInputBuffers(auto&&...) { }
725
726 static void inputAboutToFinish(auto&&...) { }
727};
728
729template<typename T>
730struct buffer_inputs_storage;
731
732template<typename T>
733 requires (avnd::buffer_input_introspection<T>::size > 0)
734struct buffer_inputs_storage<T>
735{
736 // +1 because of zero-array-size unsupported
737 QRhiBufferReadbackResult
738 m_readbacks[avnd::cpu_buffer_input_introspection<T>::size + 1];
739 score::gfx::BufferView m_gpubufs[avnd::gpu_buffer_input_introspection<T>::size + 1];
740
741 void readInputBuffers(
742 score::gfx::RenderList& renderer, auto& parent, auto& state)
743 {
744 if constexpr(avnd::cpu_buffer_input_introspection<T>::size > 0)
745 {
746 // Copy the readback output inside the structure
747 // TODO it would be much better to do this inside the readback's
748 // "completed" callback.
749 avnd::cpu_buffer_input_introspection<T>::for_all_n(
750 avnd::get_inputs<T>(state),
751 [&]<typename Field, std::size_t N>
752 (Field& t, avnd::predicate_index<N> np)
753 {
754 auto& readback = m_readbacks[N].data;
755 t.buffer.raw_data = reinterpret_cast<unsigned char*>(readback.data());
756 t.buffer.byte_size = readback.size();
757 t.buffer.byte_offset = 0; // FIXME
758 t.buffer.changed = true;
759 });
760 }
761
762 if constexpr(avnd::gpu_buffer_input_introspection<T>::size > 0)
763 {
764 // Copy the readback output inside the structure
765 // TODO it would be much better to do this inside the readback's
766 // "completed" callback.
767 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
768 avnd::get_inputs<T>(state),
769 [&]<typename Field, std::size_t N, std::size_t NField>(
770 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
771 score::gfx::BufferView& buf = m_gpubufs[N];
772 if(!buf)
773 buf = getInputBuffer(renderer, parent, nf);
774 if(!buf)
775 return;
776 t.buffer.handle = buf.handle;
777 t.buffer.byte_size = buf.byte_size;
778 t.buffer.byte_offset = buf.byte_offset;
779 // t.buffer.changed = true; FIXME
780 });
781 }
782 }
783
784 void inputAboutToFinish(
785 score::gfx::RenderList& renderer,
786 QRhiResourceUpdateBatch*& res,
787 auto& state,
788 auto& parent)
789 {
790 avnd::cpu_buffer_input_introspection<T>::for_all_n2(
791 avnd::get_inputs<T>(state),
792 [&]<typename Field, std::size_t N, std::size_t NField>
793 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
794 readbackInputBuffer(renderer, *res, parent, m_readbacks[N], nf);
795 });
796 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
797 avnd::get_inputs<T>(state),
798 [&]<typename Field, std::size_t N, std::size_t NField>
799 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
800 m_gpubufs[N] = getInputBuffer(renderer, parent, nf);
801 });
802 }
803};
804
805template<typename T>
806 requires (avnd::buffer_input_introspection<T>::size == 0)
807struct buffer_inputs_storage<T>
808{
809 static void readInputBuffers(auto&&...)
810 {
811
812 }
813
814 static void inputAboutToFinish(auto&&...)
815 {
816
817 }
818};
819
820struct MaybeOwnedBuffer : score::gfx::BufferView
821{
822 bool owned{false};
823};
824
825template<typename T>
826struct buffer_outputs_storage;
827
828template<typename T>
829 requires (avnd::buffer_output_introspection<T>::size > 0)
830struct buffer_outputs_storage<T>
831{
832 std::pair<const score::gfx::Port*, MaybeOwnedBuffer>
833 m_buffers[avnd::buffer_output_introspection<T>::size];
834
835 QRhiResourceUpdateBatch* currentResourceUpdateBatch{};
836
837 template <typename Field, std::size_t N, std::size_t NField>
838 requires avnd::cpu_buffer<std::decay_t<decltype(Field::buffer)>>
839 void createOutput(
840 score::gfx::RenderList& renderer, auto& parent, Field& port,
841 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
842 {
843 auto& [gfx_port, buf] = m_buffers[N];
844 gfx_port = parent.output[nf];
845 buf.handle = renderer.state.rhi->newBuffer(
846 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer, 1);
847 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
848 buf.byte_offset = 0;
849 buf.byte_size = 1;
850 buf.owned = true;
851
852 buf.handle->create();
853
854 port.buffer.upload
855 = [this, &renderer, &port](const char* data, int64_t offset, int64_t bytesize) {
856 // FIXME is offset and bytesize relative to the input or the output data ?
857 SCORE_ASSERT(currentResourceUpdateBatch);
858 auto& [gfx_port, buf] = m_buffers[N];
859
860 if(!buf.handle)
861 {
862 if(bytesize > 0)
863 {
864 buf.handle = renderer.state.rhi->newBuffer(
865 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
866 bytesize);
867 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
868 buf.byte_offset = 0;
869 buf.byte_size = bytesize;
870 buf.owned = true;
871
872 buf.handle->create();
873 }
874 else
875 {
876 buf.handle = renderer.state.rhi->newBuffer(
877 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
878 1);
879 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
880 buf.byte_offset = 0;
881 buf.byte_size = 1;
882 buf.owned = true;
883
884 buf.handle->create();
885 return;
886 }
887 }
888 else if(buf.handle->size() != bytesize)
889 {
890 buf.handle->destroy();
891 buf.handle->setSize(bytesize);
892 buf.handle->create();
893 buf.byte_size = bytesize;
894 }
895
897 currentResourceUpdateBatch, buf.handle, offset, bytesize, data);
898 };
899 }
900
901 template <typename Field, std::size_t N, std::size_t NField>
902 requires avnd::gpu_buffer<std::decay_t<decltype(Field::buffer)>>
903 void createOutput(
904 score::gfx::RenderList& renderer, auto& parent, Field& port,
905 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
906 {
907 auto& [gfx_port, buf] = m_buffers[N];
908 gfx_port = parent.output[nf];
909 buf.handle = reinterpret_cast<QRhiBuffer*>(port.buffer.handle);
910 buf.byte_size = port.buffer.byte_size;
911 buf.byte_offset = port.buffer.byte_offset;
912 buf.owned = false;
913 }
914
915 void init(score::gfx::RenderList& renderer, auto& state, auto& parent)
916 {
917 // Init buffers for the outputs
918 avnd::buffer_output_introspection<T>::for_all_n2(
919 avnd::get_outputs<T>(state), [&]<typename Field, std::size_t N, std::size_t NField>
920 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
921 SCORE_ASSERT(parent.output.size() > nf);
922 SCORE_ASSERT(parent.output[nf]->type == score::gfx::Types::Buffer);
923 using buffer_type = std::decay_t<decltype(port.buffer)>;
924
925 if constexpr(avnd::cpu_raw_buffer<buffer_type> && requires {
926 port.buffer.upload(nullptr, 0, 0);
927 })
928 {
929 createOutput(renderer, parent, port, np, nf);
930 }
931 else if constexpr(avnd::gpu_buffer<buffer_type>)
932 {
933 createOutput(renderer, parent, port, np, nf);
934 }
935 else
936 {
937 // m_buffers[N] = createOutput(renderer, *parent.output[nf], port.buffer);
938 static_assert(std::is_same_v<T, void>, "unsupported");
939 }
940 });
941 }
942
943 void prepareUpload(QRhiResourceUpdateBatch& res)
944 {
945 currentResourceUpdateBatch = &res;
946 }
947
948 void upload(score::gfx::RenderList& renderer, auto& state, QRhiResourceUpdateBatch& res)
949 {
950 avnd::buffer_output_introspection<T>::for_all_n(
951 avnd::get_outputs<T>(state), [&]<std::size_t N>(auto& t, avnd::predicate_index<N> idx) {
952 auto& [port, buf] = m_buffers[N];
953 uploadOutputBuffer(renderer, t.buffer, res, buf);
954 });
955 }
956
957 void release(score::gfx::RenderList& renderer)
958 {
959 // Free outputs
960 for(auto& [p, buf] : m_buffers)
961 {
962 if(buf.owned)
963 renderer.releaseBuffer(buf.handle);
964 buf.handle = nullptr;
965 buf.owned = false;
966 }
967 }
968};
969
970template<typename T>
971 requires (avnd::buffer_output_introspection<T>::size == 0)
972struct buffer_outputs_storage<T>
973{
974 static void init(auto&&...)
975 {
976
977 }
978
979 static void prepareUpload(auto&&...)
980 {
981 }
982
983 static void upload(auto&&...)
984 {
985 }
986
987 static void release(auto&&...)
988 {
989 }
990};
991
992
993template <typename Tex>
994static auto
995createOutputTexture(score::gfx::RenderList& renderer, const Tex& texture_spec, QSize size)
996{
997 auto& rhi = *renderer.state.rhi;
998 QRhiTexture* texture = &renderer.emptyTexture();
999 if(size.width() > 0 && size.height() > 0)
1000 {
1001 texture = rhi.newTexture(
1002 gpp::qrhi::textureFormat(texture_spec), size, 1, QRhiTexture::Flag{});
1003
1004 texture->create();
1005 }
1006
1007 auto sampler = rhi.newSampler(
1008 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1009 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
1010
1011 sampler->create();
1012 return score::gfx::Sampler{sampler, texture};
1013}
1014
1015
1016template<typename T>
1017struct texture_inputs_storage;
1018
1019template<typename T>
1020 requires (avnd::texture_input_introspection<T>::size > 0)
1021struct texture_inputs_storage<T>
1022{
1023 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
1024 m_rts;
1025
1026 QRhiReadbackResult m_readbacks[avnd::texture_input_introspection<T>::size];
1027
1028 template <typename Tex>
1029 void createInput(
1030 score::gfx::RenderList& renderer, score::gfx::Port* port, const Tex& texture_spec,
1032 {
1033 static constexpr auto flags
1034 = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
1035 auto texture = renderer.state.rhi->newTexture(
1036 gpp::qrhi::textureFormat(texture_spec), spec.size, 1, flags);
1037 SCORE_ASSERT(texture->create());
1038 m_rts[port] = score::gfx::createRenderTarget(
1039 renderer.state, texture, renderer.samples(), renderer.requiresDepth(*port));
1040 }
1041
1042 void init(auto& self, score::gfx::RenderList& renderer)
1043 {
1044 // Init input render targets
1045 avnd::cpu_texture_input_introspection<T>::for_all_n(
1046 avnd::get_inputs<T>(*self.state),
1047 [&]<typename F, std::size_t K>(F& t, avnd::predicate_index<K>) {
1048 // FIXME k isn't the port index, it's the texture port index
1049 auto& parent = self.node();
1050 auto spec = parent.resolveRenderTargetSpecs(K, renderer);
1051 if constexpr(requires {
1052 t.request_width;
1053 t.request_height;
1054 })
1055 {
1056 spec.size.rwidth() = t.request_width;
1057 spec.size.rheight() = t.request_height;
1058 }
1059
1060 createInput(renderer, parent.input[K], t.texture, spec);
1061
1062 if constexpr(avnd::cpu_fixed_format_texture<decltype(t.texture)>)
1063 {
1064 t.texture.width = spec.size.width();
1065 t.texture.height = spec.size.height();
1066 }
1067 });
1068 }
1069
1070 void runInitialPasses(auto& self, QRhi& rhi)
1071 {
1072 // Fetch input textures (if any)
1073 // Copy the readback output inside the structure
1074 // TODO it would be much better to do this inside the readback's
1075 // "completed" callback.
1076 avnd::cpu_texture_input_introspection<T>::for_all_n(
1077 avnd::get_inputs<T>(*self.state), [&]<std::size_t K>(auto& t, avnd::predicate_index<K>) {
1078 oscr::loadInputTexture(rhi, m_readbacks, t.texture, K);
1079 });
1080 }
1081
1082 void release()
1083 {
1084 // Free inputs
1085 // TODO investigate why reference does not work here:
1086 for(auto [port, rt] : m_rts)
1087 rt.release();
1088 m_rts.clear();
1089 }
1090
1091 void inputAboutToFinish(auto& parent, const score::gfx::Port& p, QRhiResourceUpdateBatch*& res)
1092 {
1093 const auto& inputs = parent.input;
1094 auto index_of_port = ossia::find(inputs, &p) - inputs.begin();
1095 {
1096 auto tex = m_rts[&p].texture;
1097 auto& readback = m_readbacks[index_of_port];
1098 readback = {};
1099 res->readBackTexture(QRhiReadbackDescription{tex}, &readback);
1100 }
1101 }
1102
1103};
1104template<typename T>
1105 requires (avnd::texture_input_introspection<T>::size == 0)
1106struct texture_inputs_storage<T>
1107{
1108 static void init(auto&&...) { }
1109 static void runInitialPasses(auto&&...) { }
1110 static void release(auto&&...) { }
1111 static void inputAboutToFinish(auto&&...) { }
1112};
1113
1114
1115
1116template <avnd::cpu_texture Tex>
1117static QRhiTexture* updateTexture(auto& self, score::gfx::RenderList& renderer, int k, const Tex& cpu_tex)
1118{
1119 auto& [sampler, texture] = self.m_samplers[k];
1120 if(texture)
1121 {
1122 auto sz = texture->pixelSize();
1123 if(cpu_tex.width == sz.width() && cpu_tex.height == sz.height())
1124 return texture;
1125 }
1126
1127 // Check the texture size
1128 if(cpu_tex.width > 0 && cpu_tex.height > 0)
1129 {
1130 QRhiTexture* oldtex = texture;
1131 QRhiTexture* newtex = renderer.state.rhi->newTexture(
1132 gpp::qrhi::textureFormat(cpu_tex), QSize{cpu_tex.width, cpu_tex.height}, 1,
1133 QRhiTexture::Flag{});
1134 newtex->create();
1135 for(auto& [edge, pass] : self.m_p)
1136 if(pass.srb)
1137 score::gfx::replaceTexture(*pass.srb, sampler, newtex);
1138 texture = newtex;
1139
1140 if(oldtex && oldtex != &renderer.emptyTexture())
1141 {
1142 oldtex->deleteLater();
1143 }
1144
1145 return newtex;
1146 }
1147 else
1148 {
1149 for(auto& [edge, pass] : self.m_p)
1150 if(pass.srb)
1151 score::gfx::replaceTexture(*pass.srb, sampler, &renderer.emptyTexture());
1152
1153 return &renderer.emptyTexture();
1154 }
1155}
1156
1157template <avnd::cpu_texture Tex>
1158static void uploadOutputTexture(auto& self,
1159 score::gfx::RenderList& renderer, int k, Tex& cpu_tex,
1160 QRhiResourceUpdateBatch* res)
1161{
1162 if(cpu_tex.changed)
1163 {
1164 if(auto texture = updateTexture(self, renderer, k, cpu_tex))
1165 {
1166 QByteArray buf
1167 = QByteArray::fromRawData((const char*)cpu_tex.bytes, cpu_tex.bytesize());
1168 if constexpr(requires { Tex::RGB; })
1169 {
1170 // RGB -> RGBA
1171 // FIXME other conversions
1172 const QByteArray rgb = buf;
1173 QByteArray rgba;
1174 rgba.resize(cpu_tex.width * cpu_tex.height * 4);
1175 auto src = (const unsigned char*)rgb.constData();
1176 auto dst = (unsigned char*)rgba.data();
1177 for(int rgb_byte = 0, rgba_byte = 0, N = rgb.size(); rgb_byte < N;)
1178 {
1179 dst[rgba_byte + 0] = src[rgb_byte + 0];
1180 dst[rgba_byte + 1] = src[rgb_byte + 1];
1181 dst[rgba_byte + 2] = src[rgb_byte + 2];
1182 dst[rgba_byte + 3] = 255;
1183 rgb_byte += 3;
1184 rgba_byte += 4;
1185 }
1186 buf = rgba;
1187 }
1188
1189 // Upload it (mirroring is done in shader generic_texgen_fs if necessary)
1190 {
1191 QRhiTextureSubresourceUploadDescription sd(buf);
1192 QRhiTextureUploadDescription desc{QRhiTextureUploadEntry{0, 0, sd}};
1193
1194 res->uploadTexture(texture, desc);
1195 }
1196
1197 cpu_tex.changed = false;
1198 }
1199 }
1200}
1201
1202static const constexpr auto generic_texgen_vs = R"_(#version 450
1203layout(location = 0) in vec2 position;
1204layout(location = 1) in vec2 texcoord;
1205
1206layout(binding=3) uniform sampler2D y_tex;
1207layout(location = 0) out vec2 v_texcoord;
1208
1209layout(std140, binding = 0) uniform renderer_t {
1210 mat4 clipSpaceCorrMatrix;
1211 vec2 renderSize;
1212} renderer;
1213
1214out gl_PerVertex { vec4 gl_Position; };
1215
1216void main()
1217{
1218#if defined(QSHADER_SPIRV) || defined(QSHADER_GLSL)
1219 v_texcoord = vec2(texcoord.x, 1. - texcoord.y);
1220#else
1221 v_texcoord = texcoord;
1222#endif
1223 gl_Position = renderer.clipSpaceCorrMatrix * vec4(position.xy, 0.0, 1.);
1224}
1225)_";
1226
1227static const constexpr auto generic_texgen_fs = R"_(#version 450
1228layout(location = 0) in vec2 v_texcoord;
1229layout(location = 0) out vec4 fragColor;
1230
1231layout(std140, binding = 0) uniform renderer_t {
1232mat4 clipSpaceCorrMatrix;
1233vec2 renderSize;
1234} renderer;
1235
1236layout(binding=3) uniform sampler2D y_tex;
1237
1238void main ()
1239{
1240 fragColor = texture(y_tex, v_texcoord);
1241}
1242)_";
1243
1244template<typename T>
1245struct texture_outputs_storage;
1246
1247// If we have texture outs we need the whole rendering infrastructure
1248template<typename T>
1249 requires (avnd::texture_output_introspection<T>::size > 0)
1250struct texture_outputs_storage<T>
1251{
1252 void init(auto& self, score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
1253 {
1254 const auto& mesh = renderer.defaultTriangle();
1255 self.defaultMeshInit(renderer, mesh, res);
1256 self.processUBOInit(renderer);
1257 // Not needed here as we do not have a GPU pass:
1258 // this->m_material.init(renderer, this->node.input, this->m_samplers);
1259
1260 std::tie(self.m_vertexS, self.m_fragmentS)
1261 = score::gfx::makeShaders(renderer.state, generic_texgen_vs, generic_texgen_fs);
1262
1263 avnd::cpu_texture_output_introspection<T>::for_all(
1264 avnd::get_outputs<T>(*self.state), [&](auto& t) {
1265 self.m_samplers.push_back(
1266 createOutputTexture(renderer, t.texture, QSize{t.texture.width, t.texture.height}));
1267 });
1268
1269 self.defaultPassesInit(renderer, mesh);
1270 }
1271
1272 void runInitialPasses(auto& self,
1273 score::gfx::RenderList& renderer,
1274 QRhiResourceUpdateBatch*& res)
1275 {
1276 avnd::cpu_texture_output_introspection<T>::for_all_n(
1277 avnd::get_outputs<T>(*self.state), [&]<std::size_t N>(auto& t, avnd::predicate_index<N>) {
1278 uploadOutputTexture(self, renderer, N, t.texture, res);
1279 });
1280 }
1281
1282 void release(auto& self, score::gfx::RenderList& r)
1283 {
1284 // Free outputs
1285 for(auto& [sampl, texture] : self.m_samplers)
1286 {
1287 if(texture != &r.emptyTexture())
1288 texture->deleteLater();
1289 texture = nullptr;
1290 }
1291 }
1292
1293};
1294
1295template<typename T>
1296 requires (avnd::texture_output_introspection<T>::size == 0)
1297struct texture_outputs_storage<T>
1298{
1299 static void init(auto& self, score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
1300 {
1301 }
1302
1303 static void runInitialPasses(auto& self,
1304 score::gfx::RenderList& renderer,
1305 QRhiResourceUpdateBatch*& res)
1306 {
1307 }
1308
1309 static void release(auto& self, score::gfx::RenderList& r)
1310 {
1311 }
1312};
1313template<typename T>
1314struct geometry_outputs_storage;
1315
1316template<typename T>
1317 requires (avnd::geometry_output_introspection<T>::size > 0)
1318struct geometry_outputs_storage<T>
1319{
1320 ossia::geometry_spec specs[avnd::geometry_output_introspection<T>::size];
1321
1322 template <avnd::geometry_port Field>
1323 void reload_mesh(Field& ctrl, ossia::geometry_spec& spc)
1324 {
1325 spc.meshes = std::make_shared<ossia::mesh_list>();
1326 auto& ossia_meshes = *spc.meshes;
1327 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
1328 {
1329 ossia_meshes.meshes.resize(1);
1330 load_geometry(ctrl, ossia_meshes.meshes[0]);
1331 }
1332 else if constexpr(
1333 avnd::static_geometry_type<decltype(Field::mesh)>
1334 || avnd::dynamic_geometry_type<decltype(Field::mesh)>)
1335 {
1336 ossia_meshes.meshes.resize(1);
1337 load_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
1338 }
1339 else
1340 {
1341 load_geometry(ctrl, ossia_meshes);
1342 }
1343 }
1344
1345 template <avnd::geometry_port Field, std::size_t N>
1346 void upload(
1347 score::gfx::RenderList& renderer, Field& ctrl, score::gfx::Edge& edge,
1348 avnd::predicate_index<N>)
1349 {
1350 auto edge_sink = edge.sink;
1351 if(auto pnode = edge_sink->node)
1352 {
1353 ossia::geometry_spec& spc = specs[N];
1354
1355 // 1. Reload mesh
1356 {
1357 if(ctrl.dirty_mesh)
1358 {
1359 reload_mesh(ctrl, spc);
1360 }
1361 else
1362 {
1363 if(spc.meshes)
1364 {
1365 auto& ossia_meshes = *spc.meshes;
1366
1367 bool any_need_reload = false;
1368 bool any_need_upload = false;
1369 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
1370 {
1371 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
1372 auto [need_reload, need_upload]
1373 = update_geometry(ctrl, ossia_meshes.meshes[0]);
1374 any_need_reload = need_reload;
1375 any_need_upload = need_upload;
1376 }
1377 else if constexpr(
1378 avnd::static_geometry_type<decltype(Field::mesh)>
1379 || avnd::dynamic_geometry_type<decltype(Field::mesh)>)
1380 {
1381 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
1382 auto [need_reload, need_upload]
1383 = update_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
1384 any_need_reload = need_reload;
1385 any_need_upload = need_upload;
1386 }
1387 else
1388 {
1389 auto [need_reload, need_upload] = update_geometry(ctrl, ossia_meshes);
1390 any_need_reload = need_reload;
1391 any_need_upload = need_upload;
1392 }
1393
1394 if(any_need_reload)
1395 {
1396 reload_mesh(ctrl, spc);
1397 }
1398 }
1399 }
1400 ctrl.dirty_mesh = false;
1401 }
1402
1403 // 2. Push to next node
1404 // FIXME this should be for the renderer of edge, not the node, since
1405 // geometries can have gpu buffers
1406 auto rendered_node = pnode->renderedNodes.find(&renderer);
1407 SCORE_ASSERT(rendered_node != pnode->renderedNodes.end());
1408
1409 auto it = std::find(
1410 edge_sink->node->input.begin(), edge_sink->node->input.end(), edge_sink);
1411 SCORE_ASSERT(it != edge_sink->node->input.end());
1412 int n = it - edge_sink->node->input.begin();
1413
1414 rendered_node->second->process(n, spc);
1415
1416 // 3. Same for transform3d
1417
1418 if constexpr(requires { ctrl.transform; })
1419 {
1420 if(ctrl.dirty_transform)
1421 {
1422 ossia::transform3d transform;
1423 std::copy_n(ctrl.transform, std::ssize(ctrl.transform), transform.matrix);
1424 ctrl.dirty_transform = false;
1425
1426 if(auto pnode = dynamic_cast<score::gfx::ProcessNode*>(edge_sink->node))
1427 pnode->process(n, transform);
1428 }
1429 }
1430 }
1431 }
1432
1433 void upload(score::gfx::RenderList& renderer, auto& state, score::gfx::Edge& edge)
1434 {
1435 // FIXME we need something such as port_run_{pre,post}process for GPU nodes
1436 avnd::geometry_output_introspection<T>::for_all_n(
1437 avnd::get_outputs(state),
1438 [&](auto& field, auto pred) { this->upload(renderer, field, edge, pred); });
1439 }
1440};
1441
1442
1443template<typename T>
1444 requires (avnd::geometry_output_introspection<T>::size == 0)
1445struct geometry_outputs_storage<T>
1446{
1447 static void upload(auto&&...)
1448 {
1449
1450 }
1451};
1452}
1453
1454#endif
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:71
std::vector< Port * > input
Input ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:100
virtual void process(Message &&msg)
Process a message from the execution engine.
Definition Node.cpp:25
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:106
Common base class for most single-pass, simple nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:200
Renderer for a given node.
Definition NodeRenderer.hpp:11
Base class for sink nodes (QWindow, spout, syphon, NDI output, ...)
Definition OutputNode.hpp:31
Common base class for nodes that map to score processes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:173
List of nodes to be rendered to an output.
Definition RenderList.hpp:19
bool requiresDepth(score::gfx::Port &p) const noexcept
Whether this list of rendering actions requires depth testing at all.
Definition RenderList.cpp:344
const score::gfx::Mesh & defaultTriangle() const noexcept
A triangle mesh correct for this API.
Definition RenderList.cpp:383
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:94
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing.
Definition RenderList.hpp:117
TreeNode< DeviceExplorerNode > Node
Definition DeviceNode.hpp:74
Definition Factories.hpp:19
void uploadStaticBufferWithStoredData(QRhiResourceUpdateBatch *ub, QRhiBuffer *buf, int offset, int64_t bytesize, const char *data)
Schedule a Static buffer update when we can guarantee the buffer outlives the frame.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:338
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:394
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:99
STL namespace.
Definition DocumentContext.hpp:18
Definition Mesh.hpp:15
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:71
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:46
Definition OutputNode.hpp:11
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:53
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:53
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:26