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/SceneConcepts.hpp>
12#include <Crousti/TextureConversion.hpp>
13#include <Crousti/TextureFormat.hpp>
14#include <Crousti/WorkerBinding.hpp>
15#include <Gfx/GfxExecNode.hpp>
16#include <Gfx/Graph/Node.hpp>
17#include <Gfx/Graph/OutputNode.hpp>
18#include <Gfx/Graph/RenderList.hpp>
19#include <Gfx/Graph/RenderState.hpp>
20#include <Gfx/Graph/RowOrder.hpp>
21
22#include <score/tools/ThreadPool.hpp>
23
24#include <ossia/detail/hash_map.hpp>
25#include <ossia/detail/lockfree_queue.hpp>
26#include <ossia/detail/small_flat_map.hpp>
27#include <ossia/detail/small_vector.hpp>
28
29#include <ossia-qt/invoke.hpp>
30
31#include <QCoreApplication>
32#include <QTimer>
33#include <QtGui/private/qrhi_p.h>
34
35#include <avnd/binding/ossia/metadatas.hpp>
36#include <avnd/binding/ossia/port_run_postprocess.hpp>
37#include <avnd/binding/ossia/port_run_preprocess.hpp>
38#include <avnd/binding/ossia/soundfiles.hpp>
39#include <avnd/concepts/parameter.hpp>
40#include <avnd/introspection/input.hpp>
41#include <avnd/introspection/output.hpp>
42#include <fmt/format.h>
43#include <gpp/layout.hpp>
44#include <halp/texture.hpp>
45
46#include <score_plugin_avnd_export.h>
47
48#include <algorithm>
49#include <atomic>
50
51namespace oscr
52{
64struct GpuWorker
65{
66 struct WorkerResults
67 {
68 struct Result
69 {
70 uint64_t order{};
71 std::function<void()> apply;
72 };
73 ossia::mpmc_queue<Result> queue;
74 std::atomic<uint64_t> pushed{};
75 std::atomic<int> inflight{};
76
77 void push(std::function<void()> f)
78 {
79 queue.enqueue(Result{pushed.fetch_add(1, std::memory_order_relaxed), std::move(f)});
80 }
81
82 void drain()
83 {
84 ossia::small_vector<Result, 4> batch;
85 Result r;
86 while(queue.try_dequeue(r))
87 batch.push_back(std::move(r));
88 std::ranges::sort(batch, {}, &Result::order);
89 for(auto& res : batch)
90 res.apply();
91 }
92
93 // Waits for the jobs in flight, and for those that the results applied
94 // meanwhile requested.
95 void settle()
96 {
97 for(;;)
98 {
99 for(int n = inflight.load(); n > 0; n = inflight.load())
100 inflight.wait(n);
101 if(queue.size_approx() == 0)
102 return;
103 drain();
104 }
105 }
106 };
107
110 struct Delivery
111 {
112 std::weak_ptr<WorkerResults> results;
113
114 struct Job
115 {
116 std::weak_ptr<WorkerResults> results;
117 explicit Job(std::weak_ptr<WorkerResults> r) noexcept
118 : results{std::move(r)}
119 {
120 if(auto p = results.lock())
121 p->inflight.fetch_add(1);
122 }
123 Job(Job&& other) noexcept = default;
124 Job& operator=(Job&&) = delete;
125 ~Job()
126 {
127 if(auto p = results.lock())
128 {
129 p->inflight.fetch_sub(1);
130 p->inflight.notify_all();
131 }
132 }
133
134 template <typename F>
135 void operator()(F&& apply)
136 {
137 auto r = results.lock();
138 if(!r)
139 return;
140 r->push(std::forward<F>(apply));
141 ossia::qt::run_async(QCoreApplication::instance(), [wr = results] {
142 if(auto r = wr.lock())
143 r->drain();
144 });
145 }
146 };
147 Job begin() const noexcept { return Job{results}; }
148 };
149
152 void drainWorker(bool settle = false) const
153 {
154 if(!m_workerResults)
155 return;
156 if(settle)
157 m_workerResults->settle();
158 else
159 m_workerResults->drain();
160 }
161
162 template <typename T>
163 void initWorker(this auto& self, std::shared_ptr<T>& state) noexcept
164 {
165 if constexpr(avnd::has_worker<T>)
166 {
167 if(!self.m_workerResults)
168 self.m_workerResults = std::make_shared<WorkerResults>();
169
170 bind_worker(*state, std::weak_ptr{state}, Delivery{self.m_workerResults});
171 }
172 }
173
174 mutable std::shared_ptr<WorkerResults> m_workerResults;
175};
176
177#if defined(OSCR_HAS_MMAP_FILE_STORAGE)
178// The file ports keep string_views into the raw_file_data, and there is one
179// object instance per renderer (one renderer per RenderList, that is per
180// output) and per instance in CustomGpuRenderer. Storing the handles on the
181// node would thus free, on every load, the memory that the ports of all the
182// other instances still point to.
183template <typename T>
184struct GpuRendererFiles
185{
186 template <std::size_t N, std::size_t NField>
187 void file_loaded(
188 T& state, const std::shared_ptr<oscr::raw_file_data>& hdl,
189 avnd::predicate_index<N> pred, avnd::field_index<NField> field)
190 {
191 m_rawfiles[&state].load(state, hdl, pred, field);
192 }
193
194 void releaseFiles() noexcept { m_rawfiles.clear(); }
195
196private:
197 ossia::hash_map<const T*, oscr::raw_file_storage<T>> m_rawfiles;
198};
199
200template <typename T>
201 requires(avnd::raw_file_input_introspection<T>::size == 0)
202struct GpuRendererFiles<T>
203{
204 void releaseFiles() noexcept { }
205};
206#else
207template <typename T>
208struct GpuRendererFiles
209{
210 void releaseFiles() noexcept { }
211};
212#endif
213
221struct SCORE_PLUGIN_AVND_EXPORT GpuMessageState
222{
223 score::gfx::Message message;
224 std::vector<uint32_t> generation;
225
226 void process(score::gfx::Message&& msg) noexcept;
227};
228
229template <typename GpuNodeRenderer, typename Node>
230struct GpuProcessIns
231{
232 GpuProcessIns(
233 GpuNodeRenderer& gpu, Node& state, const GpuMessageState& prev,
234 const GpuMessageState& cur, const score::DocumentContext& ctx) noexcept
235 : gpu{gpu}
236 , state{state}
237 , prev_mess{prev.message}
238 , mess{cur.message}
239 , prev_gen{prev.generation}
240 , gen{cur.generation}
241 , ctx{ctx}
242 {
243 }
244
245 GpuNodeRenderer& gpu;
246 Node& state;
247 const score::gfx::Message& prev_mess;
248 const score::gfx::Message& mess;
249 const std::vector<uint32_t>& prev_gen;
250 const std::vector<uint32_t>& gen;
251 const score::DocumentContext& ctx;
252
253 bool can_process_message(std::size_t N)
254 {
255 if(mess.input.size() <= N)
256 return false;
257
258 if(prev_mess.input.size() == mess.input.size())
259 {
260 auto& prev = prev_mess.input[N];
261 auto& next = mess.input[N];
262 if(prev.index() == 1 && next.index() == 1)
263 {
264 if(ossia::get<ossia::value>(prev) == ossia::get<ossia::value>(next))
265 {
266 return false;
267 }
268 }
269 }
270 return true;
271 }
272
273 // The node's message keeps the last value of every input, so the one-shot values in it are
274 // applied once per arrival rather than once per value: a bang equal to the previous one is a
275 // new event, the same one still sitting in the message is not.
276 bool received_event(std::size_t N) const noexcept
277 {
278 if(mess.input.size() <= N || gen.size() <= N)
279 return false;
280 return prev_gen.size() <= N || prev_gen[N] != gen[N];
281 }
282
283 template <avnd::parameter_port Field, std::size_t NField>
284 void operator()(Field& t, avnd::field_index<NField> field_index)
285 {
286 if constexpr(avnd::optional_ish<decltype(Field::value)>)
287 {
288 // Mirrors the CPU path, which sees each port's data stream once per tick.
289 if(!received_event(NField))
290 return;
291 }
292 else if(!can_process_message(field_index))
293 {
294 return;
295 }
296
297 if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
298 {
299 oscr::from_ossia_value(t, *val, t.value);
300 if_possible(t.update(state));
301 }
302 }
303
304#if OSCR_HAS_MMAP_FILE_STORAGE
305 template <avnd::raw_file_port Field, std::size_t NField>
306 void operator()(Field& t, avnd::field_index<NField> field_index)
307 {
308 // FIXME we should be loading a file there
309 using file_ports = avnd::raw_file_input_introspection<Node>;
310
311 if(!can_process_message(field_index))
312 return;
313
314 auto val = ossia::get_if<ossia::value>(&mess.input[field_index]);
315 if(!val)
316 return;
317
318 static constexpr bool has_text = requires { decltype(Field::file)::text; };
319 static constexpr bool has_mmap = requires { decltype(Field::file)::mmap; };
320
321 // First we can load it directly since execution hasn't started yet
322 if(auto hdl = loadRawfile(*val, ctx, has_text, has_mmap))
323 {
324 static constexpr auto N = file_ports::field_index_to_index(NField);
325 if constexpr(avnd::port_can_process<Field>)
326 {
327 // FIXME also do it when we get a run-time message from the exec engine,
328 // OSC, etc
329 auto func = executePortPreprocess<Field>(*hdl);
330 gpu.file_loaded(
331 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
332 if(func)
333 func(state);
334 }
335 else
336 {
337 gpu.file_loaded(
338 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
339 }
340 }
341 }
342#endif
343
344 template <avnd::buffer_port Field, std::size_t NField>
345 void operator()(Field& t, avnd::field_index<NField> field_index)
346 {
347 if(!can_process_message(field_index))
348 return;
349
350 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
351 auto& node = const_cast<node_type&>(gpu.node());
352 if(field_index >= mess.input.size())
353 return;
354 auto val = ossia::get_if<ossia::render_target_spec>(&mess.input[field_index]);
355 if(!val)
356 return;
357 static_cast<score::gfx::Node&>(node).process(int32_t(NField), *val);
358 }
359
360 template <avnd::texture_port Field, std::size_t NField>
361 void operator()(Field& t, avnd::field_index<NField> field_index)
362 {
363 if(!can_process_message(field_index))
364 return;
365
366 using node_type = std::remove_cvref_t<decltype(gpu.node())>;
367 auto& node = const_cast<node_type&>(gpu.node());
368 if(field_index >= mess.input.size())
369 return;
370 auto val = ossia::get_if<ossia::render_target_spec>(&mess.input[field_index]);
371 if(!val)
372 return;
373 static_cast<score::gfx::Node&>(node).process(int32_t(NField), *val);
374 }
375
376 template <avnd::geometry_port Field, std::size_t NField>
377 void operator()(Field& t, avnd::field_index<NField> field_index)
378 {
379 // Intentional no-op: geometry is not in the control message, it flows
380 // through geometry_inputs_storage::readInputGeometries. The empty body
381 // keeps GpuProcessIns instantiable for nodes whose input list contains
382 // geometry fields, which would otherwise hit the `= delete` catch-all
383 // below.
384 }
385
386 template <scene_port Field, std::size_t NField>
387 void operator()(Field& t, avnd::field_index<NField> field_index)
388 {
389 // Intentional no-op — same reasoning as the geometry_port overload above.
390 // Scene data flows through scene_inputs_storage / scene_outputs_storage.
391 }
392
393 void operator()(auto& t, auto field_index) = delete;
394};
395
396struct GpuControlIns
397{
398 template <typename Self, typename Node_T>
399 static void processControlIn(
400 Self& self, Node_T& state, GpuMessageState& renderer_mess,
401 const GpuMessageState& mess, const score::DocumentContext& ctx) noexcept
402 {
403 // Apply the controls
404 avnd::input_introspection<Node_T>::for_all_n(
405 avnd::get_inputs<Node_T>(state),
406 GpuProcessIns<Self, Node_T>{self, state, renderer_mess, mess, ctx});
407 renderer_mess = mess;
408 }
409
410 // Once the processor has run, the impulses and other one-shot values it received this tick are
411 // consumed, exactly as oscr::process_after_run does on the CPU path. Without this an impulse
412 // would stay set until the next message touches the port.
413 template <typename Node_T>
414 static void clearControlIn(Node_T& state) noexcept
415 {
416 avnd::parameter_input_introspection<Node_T>::for_all(
417 avnd::get_inputs<Node_T>(state), []<typename Field>(Field& t) {
418 if constexpr(avnd::optional_ish<decltype(Field::value)>)
419 t.value.reset();
420 });
421 }
422};
423
424struct GpuControlOuts
425{
426 std::weak_ptr<Execution::ExecutionCommandQueue> queue;
427 Gfx::exec_controls control_outs;
428
429 int64_t instance{};
430
436 static bool
437 drivesControlOuts(const score::gfx::RenderList& r, const score::gfx::Node& n) noexcept
438 {
439 const score::gfx::RenderList* first{};
440 for(const auto& [rl, renderer] : n.renderedNodes)
441 {
442 if(!rl->output.preview)
443 return rl == &r;
444 if(!first)
445 first = rl;
446 }
447 return !first || first == &r;
448 }
449
450 template <typename Node_T>
451 void processControlOut(
452 Node_T& state, const score::gfx::RenderList& r,
453 const score::gfx::Node& n) const noexcept
454 {
455 if(!this->control_outs.empty() && drivesControlOuts(r, n))
456 {
457 auto q = this->queue.lock();
458 if(!q)
459 return;
460 auto& qq = *q;
461 int parm_k = 0;
462 avnd::parameter_output_introspection<Node_T>::for_all(
463 avnd::get_outputs(state), [&]<avnd::parameter_port T>(const T& t) {
464 qq.enqueue([v = oscr::to_ossia_value(t, t.value),
465 port = control_outs[parm_k]]() mutable {
466 std::swap(port->value, v);
467 port->changed = true;
468 });
469
470 parm_k++;
471 });
472 }
473 }
474};
475
476template <typename T>
477struct SCORE_PLUGIN_AVND_EXPORT GpuNodeElements
478{
479 [[no_unique_address]] oscr::soundfile_storage<T> soundfiles;
480
481 [[no_unique_address]] oscr::midifile_storage<T> midifiles;
482
483 // Raw file handles are stored per-state in the renderers, see GpuRendererFiles
484};
485
486struct SCORE_PLUGIN_AVND_EXPORT CustomGfxNodeBase : score::gfx::NodeModel
487{
488 explicit CustomGfxNodeBase(const score::DocumentContext& ctx)
489 : score::gfx::NodeModel{}
490 , m_ctx{ctx}
491 {
492 }
493 virtual ~CustomGfxNodeBase();
494 const score::DocumentContext& m_ctx;
495 GpuMessageState last_message;
496 void process(score::gfx::Message&& msg) override;
498};
499struct SCORE_PLUGIN_AVND_EXPORT CustomGfxOutputNodeBase : score::gfx::OutputNode
500{
501 virtual ~CustomGfxOutputNodeBase();
502
503 GpuMessageState last_message;
504 void process(score::gfx::Message&& msg) override;
505};
506struct SCORE_PLUGIN_AVND_EXPORT CustomGpuNodeBase
508 , GpuWorker
509 , GpuControlIns
510 , GpuControlOuts
511{
512 CustomGpuNodeBase(
513 std::weak_ptr<Execution::ExecutionCommandQueue>&& q, Gfx::exec_controls&& ctls,
514 const score::DocumentContext& ctx)
515 : GpuControlOuts{std::move(q), std::move(ctls)}
516 , m_ctx{ctx}
517 {
518 }
519
520 virtual ~CustomGpuNodeBase() = default;
521
522 const score::DocumentContext& m_ctx;
523 QString vertex, fragment, compute;
524 GpuMessageState last_message;
525 void process(score::gfx::Message&& msg) override;
526};
527
528struct SCORE_PLUGIN_AVND_EXPORT CustomGpuOutputNodeBase
530 , GpuWorker
531 , GpuControlIns
532 , GpuControlOuts
533{
537 static constexpr QSize defaultRenderSize{200, 200};
538
539 CustomGpuOutputNodeBase(
540 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls&& ctls,
541 const score::DocumentContext& ctx);
542 virtual ~CustomGpuOutputNodeBase();
543
544 const score::DocumentContext& m_ctx;
545 std::weak_ptr<score::gfx::RenderList> m_renderer{};
546 std::shared_ptr<score::gfx::RenderState> m_renderState{};
547
548 QString vertex, fragment, compute;
549 GpuMessageState last_message;
550 void process(score::gfx::Message&& msg) override;
552
553 void setRenderer(std::shared_ptr<score::gfx::RenderList>) override;
554 score::gfx::RenderList* renderer() const override;
555
556 void startRendering() override;
557 void render() override;
558 void stopRendering() override;
559 bool canRender() const override;
560 void onRendererChange() override;
561
562 void createOutput(score::gfx::OutputConfiguration) override;
563
564 void destroyOutput() override;
565 std::shared_ptr<score::gfx::RenderState> renderState() const override;
566
567 Configuration configuration() const noexcept override;
568};
569
570template <typename Node_T, typename Node>
571void prepareNewState(std::shared_ptr<Node_T>& eff, const Node& parent)
572{
573 if constexpr(avnd::has_worker<Node_T>)
574 {
575 parent.initWorker(eff);
576 }
577 if constexpr(avnd::has_processor_to_gui_bus<Node_T>)
578 {
579 eff->send_message = [ptr = parent.processModel](auto&& b) mutable {
580 // FIXME right now all the rendering is done in the UI thread, which is very MEH
581 // this->in_edit([&process, bb = std::move(b)]() mutable {
582
583 if(ptr && ptr->to_ui)
584 MessageBusSender{ptr->to_ui}(std::move(b));
585 // });
586 };
587
588 // FIXME GUI -> engine. See executor.hpp
589 }
590
591 avnd::init_controls(*eff);
592
593 // The monitor of the process, shared by every renderer of the node
594 if constexpr(requires { eff->monitor; parent.processModel; })
595 {
596 using monitor_type = typename decltype(Node_T::monitor)::element_type;
597 if(parent.processModel)
598 if(auto m = std::dynamic_pointer_cast<monitor_type>(parent.processModel->monitor()))
599 eff->monitor = std::move(m);
600 }
601
602 if constexpr(avnd::can_prepare<Node_T>)
603 {
604 if constexpr(avnd::function_reflection<&Node_T::prepare>::count == 1)
605 {
606 using prepare_type = avnd::first_argument<&Node_T::prepare>;
607 prepare_type t;
608 if_possible(t.instance = parent.instance);
609 eff->prepare(t);
610 }
611 else
612 {
613 eff->prepare();
614 }
615 }
616}
617
618struct port_to_type_enum
619{
620 template <std::size_t I, avnd::buffer_port F>
621 constexpr auto operator()(avnd::field_reflection<I, F> p)
622 {
623 return score::gfx::Types::Buffer;
624 }
625
626 template <std::size_t I, avnd::cpu_texture_port F>
627 constexpr auto operator()(avnd::field_reflection<I, F> p)
628 {
629 using texture_type = std::remove_cvref_t<decltype(F::texture)>;
630 return (avnd::cpu_fixed_format_texture<texture_type> || avnd::cpu_dynamic_format_texture<texture_type>)
631 ? score::gfx::Types::Image
632 : score::gfx::Types::Buffer;
633 }
634
635 template <std::size_t I, avnd::gpu_texture_port F>
636 constexpr auto operator()(avnd::field_reflection<I, F> p)
637 {
638 return score::gfx::Types::Image;
639 }
640
641 template <std::size_t I, avnd::sampler_port F>
642 constexpr auto operator()(avnd::field_reflection<I, F> p)
643 {
644 return score::gfx::Types::Image;
645 }
646 template <std::size_t I, avnd::image_port F>
647 constexpr auto operator()(avnd::field_reflection<I, F> p)
648 {
649 return score::gfx::Types::Image;
650 }
651 template <std::size_t I, avnd::attachment_port F>
652 constexpr auto operator()(avnd::field_reflection<I, F> p)
653 {
654 return score::gfx::Types::Image;
655 }
656 template <std::size_t I, avnd::gpu_render_target_output_port F>
657 constexpr auto operator()(avnd::field_reflection<I, F> p)
658 {
659 return score::gfx::Types::Image;
660 }
661
662 template <std::size_t I, avnd::geometry_port F>
663 constexpr auto operator()(avnd::field_reflection<I, F> p)
664 {
665 return score::gfx::Types::Geometry;
666 }
667 // Scene ports reuse Types::Geometry — a scene is a richer form of geometry.
668 template <std::size_t I, scene_port F>
669 requires(!avnd::geometry_port<F>)
670 constexpr auto operator()(avnd::field_reflection<I, F> p)
671 {
672 return score::gfx::Types::Geometry;
673 }
674 template <std::size_t I, avnd::mono_audio_port F>
675 constexpr auto operator()(avnd::field_reflection<I, F> p)
676 {
677 return score::gfx::Types::Audio;
678 }
679 template <std::size_t I, avnd::poly_audio_port F>
680 constexpr auto operator()(avnd::field_reflection<I, F> p)
681 {
682 return score::gfx::Types::Audio;
683 }
684 template <std::size_t I, avnd::int_parameter F>
685 constexpr auto operator()(avnd::field_reflection<I, F> p)
686 {
687 return score::gfx::Types::Int;
688 }
689 template <std::size_t I, avnd::enum_parameter F>
690 constexpr auto operator()(avnd::field_reflection<I, F> p)
691 {
692 return score::gfx::Types::Int;
693 }
694 template <std::size_t I, avnd::float_parameter F>
695 constexpr auto operator()(avnd::field_reflection<I, F> p)
696 {
697 return score::gfx::Types::Float;
698 }
699 template <std::size_t I, avnd::parameter_port F>
700 constexpr auto operator()(avnd::field_reflection<I, F> p)
701 {
702 using value_type = std::remove_cvref_t<decltype(F::value)>;
703
704 if constexpr(std::is_array_v<value_type>)
705 {
706 static constexpr int sz = sizeof(value_type) / sizeof(value_type{}[0]);
707 if constexpr(sz == 2)
708 {
709 return score::gfx::Types::Vec2;
710 }
711 else if constexpr(sz == 3)
712 {
713 return score::gfx::Types::Vec3;
714 }
715 else if constexpr(sz == 4)
716 {
717 return score::gfx::Types::Vec4;
718 }
719 }
720 else if constexpr(std::is_aggregate_v<value_type>)
721 {
722 static constexpr int sz = avnd::pfr::tuple_size_v<value_type>;
723 if constexpr(sz == 2)
724 {
725 return score::gfx::Types::Vec2;
726 }
727 else if constexpr(sz == 3)
728 {
729 return score::gfx::Types::Vec3;
730 }
731 else if constexpr(sz == 4)
732 {
733 return score::gfx::Types::Vec4;
734 }
735 }
736 return score::gfx::Types::Empty;
737 }
738 template <std::size_t I, typename F>
739 constexpr auto operator()(avnd::field_reflection<I, F> p)
740 {
741 return score::gfx::Types::Empty;
742 }
743};
744
745// Compile-time port flags derived from a field's declarative metadata.
746// Inspects:
747// - `texture_target` (texture_kind_of) — non-2D textures bypass the
748// local-RT allocation and grab the upstream texture directly.
749// - `samplable_depth` (samplable_depth_of) — opt-in to having the
750// framework allocate a sampleable depth attachment on the producing
751// edge's RT and expose its handle through `texture.depth_handle`,
752// mirroring the semantics CSF/ISF shaders get via "IS_DEPTH": true.
753template <typename Field>
754constexpr score::gfx::Flag port_flags_for_field() noexcept
755{
756 if constexpr(avnd::gpu_texture_port<Field>)
757 {
758 constexpr auto kind = halp::texture_kind_of<Field>();
759 constexpr bool nonD2 = (kind != halp::texture_kind::texture_2d);
760 constexpr bool depth = halp::samplable_depth_of<Field>();
761 static_assert(
762 !(single_cable_port<Field> && (depth || nonD2)),
763 "single_cable is only supported on 2D texture inputs without depth");
764 if constexpr(single_cable_port<Field>)
765 return score::gfx::Flag::SingleCable;
766 else if constexpr(nonD2 && depth)
767 return score::gfx::Flag::GrabsFromSource | score::gfx::Flag::SamplableDepth;
768 else if constexpr(nonD2)
769 return score::gfx::Flag::GrabsFromSource;
770 else if constexpr(depth)
771 return score::gfx::Flag::SamplableDepth;
772 }
773 return score::gfx::Flag{};
774}
775
776// Map QRhi's depth-format taxonomy onto halp's depth_format_t.
777// The 4-arg subset matches every depth format score's createRenderTarget
778// can produce (today always D32F, but the API accepts the others).
779inline constexpr halp::gpu_texture::depth_format_t qrhiToHalpDepthFormat(
780 QRhiTexture::Format f) noexcept
781{
782 using D = halp::gpu_texture::depth_format_t;
783 switch(f)
784 {
785 case QRhiTexture::D16: return D::D16;
786 case QRhiTexture::D24: return D::D24;
787 case QRhiTexture::D24S8: return D::D24S8;
788 case QRhiTexture::D32F: return D::D32F;
789 default: break;
790 }
791 return D::D32F;
792}
793
794template <typename Node_T>
795inline void initGfxPorts(auto* self, auto& input, auto& output)
796{
797 avnd::input_introspection<Node_T>::for_all(
798 [self, &input]<typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
799 static constexpr auto type = port_to_type_enum{}(f);
800 static constexpr auto flags = port_flags_for_field<Field>();
801 input.push_back(new score::gfx::Port{self, {}, type, flags, {}});
802 });
803 avnd::output_introspection<Node_T>::for_all(
804 [self,
805 &output]<typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
806 static constexpr auto type = port_to_type_enum{}(f);
807 // port_flags_for_field encodes INPUT-side sink semantics
808 // (GrabsFromSource → "sample the upstream's texture directly";
809 // SamplableDepth → "ask the producer for a sampleable depth
810 // attachment"). Neither has any meaning on an OUTPUT port — emitting
811 // them here would make the graph treat this node's own output as if it
812 // grabbed from / sampled some upstream source. Outputs carry no such
813 // flags.
814 output.push_back(new score::gfx::Port{self, {}, type, score::gfx::Flag{}, {}});
815 });
816}
817
818static score::gfx::BufferView getInputBuffer(
819 score::gfx::RenderList& renderer, const score::gfx::Node& parent, int port_index)
820{
821 const auto& inputs = parent.input;
822 // SCORE_ASSERT(port_index == 0);
823 {
824 score::gfx::Port* p = inputs[port_index];
825 for(auto& edge : p->edges)
826 {
827 auto src_node = edge->source->node;
828 score::gfx::NodeRenderer* src_renderer = src_node->renderedNodes.at(&renderer);
829 if(src_renderer)
830 {
831 return src_renderer->bufferForOutput(*edge->source);
832 }
833 break;
834 }
835 }
836 return {};
837}
838
839
840static void readbackInputBuffer(
841 score::gfx::RenderList& renderer
842 , QRhiResourceUpdateBatch& res
843 , const score::gfx::Node& parent
844 , QRhiBufferReadbackResult& readback
845 , int port_index
846 )
847{
848 // FIXME: instead of doing this we could do the readback in the
849 // producer node and just read its bytearray once...
850 if(auto buf = getInputBuffer(renderer, parent, port_index))
851 {
852 readback = {};
853 res.readBackBuffer(buf.handle, buf.byte_offset, buf.byte_size, &readback);
854 }
855}
856
857static void recreateOutputBuffer(
858 score::gfx::RenderList& renderer, avnd::cpu_buffer auto& cpu_buf,
859 QRhiResourceUpdateBatch& res, score::gfx::BufferView& buf)
860{
861 const auto bytesize = avnd::get_bytesize(cpu_buf);
862 if(!buf.handle)
863 {
864 if(bytesize > 0)
865 {
866 buf.handle = renderer.state.rhi->newBuffer(
867 QRhiBuffer::Static,
869 *renderer.state.rhi,
870 QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer),
871 bytesize);
872 buf.handle->setName("GpuUtils::recreateOutputBuffer");
873 buf.byte_offset = 0;
874 buf.byte_size = bytesize;
875
876 buf.handle->create();
877 }
878 else
879 {
880 cpu_buf.changed = false;
881 return;
882 }
883 }
884 else if(buf.handle->size() != bytesize)
885 {
886 buf.handle->destroy();
887 buf.handle->setSize(bytesize);
888 buf.handle->create();
889 buf.byte_size = bytesize;
890 }
891}
892
893static void uploadOutputBuffer(
894 score::gfx::RenderList& renderer, avnd::cpu_buffer auto& cpu_buf,
895 QRhiResourceUpdateBatch& res, score::gfx::BufferView& rhi_buf)
896{
897 if(cpu_buf.changed)
898 {
899 recreateOutputBuffer(renderer, cpu_buf, res, rhi_buf);
901 &res, rhi_buf.handle, 0, cpu_buf.byte_size,
902 (const char*)avnd::get_bytes(cpu_buf));
903 cpu_buf.changed = false;
904 }
905}
906
907static void uploadOutputBuffer(
908 score::gfx::RenderList& renderer, avnd::gpu_buffer auto& gpu_buf,
909 QRhiResourceUpdateBatch& res, score::gfx::BufferView& rhi_buf)
910{
911 rhi_buf.handle = reinterpret_cast<QRhiBuffer*>(gpu_buf.handle);
912 rhi_buf.byte_size = gpu_buf.byte_size;
913 rhi_buf.byte_offset = gpu_buf.byte_offset;
914}
915
916template <typename T>
917struct geometry_inputs_storage;
918
919struct mesh_input_storage
920{
921 std::vector<QRhiBufferReadbackResult> readbacks;
922 std::vector<QRhiBuffer*> buffers;
923};
924struct geometry_input_storage
925{
926 ossia::geometry_spec spec;
927 std::vector<mesh_input_storage> meshes;
928};
929
930template <typename T>
931 requires(avnd::geometry_input_introspection<T>::size > 0)
932struct geometry_inputs_storage<T>
933{
934 // FIXME in Gfx/Graph/NodeRenderer.hpp
935 static_assert(avnd::geometry_input_introspection<T>::size == 1);
936
937 geometry_input_storage inputs[avnd::geometry_input_introspection<T>::size];
938 ossia::small_vector<QRhiBuffer*, 4> allocated;
939
940 struct pending_transform
941 {
942 ossia::transform3d value;
943 bool dirty{};
944 };
945 pending_transform transforms[avnd::geometry_input_introspection<T>::size];
946
947 void setTransform(int32_t port, const ossia::transform3d& v, auto& state)
948 {
949 avnd::geometry_input_introspection<T>::for_all_n2(
950 avnd::get_inputs<T>(state),
951 [&]<typename Field, std::size_t N, std::size_t NField>(
952 Field&, avnd::predicate_index<N>, avnd::field_index<NField>) {
953 if(int32_t(NField) == port)
954 transforms[N] = {v, true};
955 });
956 }
957
958 void readInputGeometries(
959 score::gfx::RenderList& renderer, const ossia::geometry_spec& spec, auto& parent,
960 auto& state)
961 {
962 // Copy the readback output inside the structure
963 // TODO it would be much better to do this inside the readback's
964 // "completed" callback.
965 avnd::geometry_input_introspection<T>::for_all_n(
966 avnd::get_inputs<T>(state),
967 [&]<typename Field, std::size_t N>(Field& t, avnd::predicate_index<N> np) {
968 this->inputs[N].spec = spec; // FIXME multiple geometry input ports
969 this->inputs[N].meshes.resize(1); // FIXME
970
971 // Here we fetch the readbacks results
972 auto& meshes = this->inputs[N].meshes[0];
973
974 // No geometry: the producer's edge was removed (removeInputEdge resets
975 // the spec). meshes_from_ossia returns early on a null mesh list and
976 // would leave t.mesh holding the departed producer's GPU handles, freed
977 // a frame later, with nothing marking it changed: Threedim::Instancer
978 // kept baking from a freed CSF buffer
979 // (examples/3d/instancing-full.score).
980 if(!spec.meshes)
981 {
982 t.mesh = {};
983 if constexpr(requires { t.dirty_mesh; })
984 t.dirty_mesh = true;
985 meshes.buffers.clear();
986 meshes.readbacks.clear();
987 return;
988 }
989
990 oscr::meshes_from_ossia(
991 spec.meshes, t.mesh,
992 [&](auto& write_buf, int buffer_index, void* data, int64_t bytesize) {
993 // CPU input geometry, upload was done before
994 SCORE_ASSERT(buffer_index >= 0);
995 if(buffer_index < meshes.readbacks.size())
996 {
997 QRhiBuffer* handle = meshes.buffers[buffer_index];
998 write_buf.handle = handle;
999 write_buf.byte_size = handle->size();
1000 }
1001 }, [&](auto& write_buf, int buffer_index, void* handle) {
1002 // GPU input buffer, CPU output buffer: need to fetch our readback
1003 SCORE_ASSERT(buffer_index >= 0);
1004 if(buffer_index < meshes.readbacks.size())
1005 {
1006 // FIXME investigate why runInitialPasses is called before inputAboutToFinish
1007 auto& readback = meshes.readbacks[buffer_index].data;
1008 write_buf.raw_data = reinterpret_cast<unsigned char*>(readback.data());
1009 write_buf.byte_size = readback.size();
1010 }
1011 });
1012
1013 if constexpr(requires {
1014 t.transform[0];
1015 t.dirty_transform;
1016 })
1017 {
1018 auto& pending = transforms[N];
1019 if(pending.dirty)
1020 {
1021 static_assert(std::extent_v<std::remove_cvref_t<decltype(t.transform)>> == 16);
1022 std::copy_n(pending.value.matrix, 16, t.transform);
1023 pending.dirty = false;
1024 t.dirty_transform = true;
1025 }
1026 else
1027 {
1028 t.dirty_transform = false;
1029 }
1030 }
1031
1032 if constexpr(requires { t.mesh.buffers[0].handle; })
1033 {
1034 if(spec.meshes && !spec.meshes->meshes.empty())
1035 {
1036 const auto& src = spec.meshes->meshes[0].buffers;
1037 const std::size_t n
1038 = std::min({src.size(), t.mesh.buffers.size(), meshes.buffers.size()});
1039 for(std::size_t i = 0; i < n; i++)
1040 {
1041 if(!ossia::get_if<ossia::geometry::cpu_buffer>(&src[i].data))
1042 continue;
1043 if(QRhiBuffer* handle = meshes.buffers[i])
1044 {
1045 t.mesh.buffers[i].handle = handle;
1046 t.mesh.buffers[i].byte_size = handle->size();
1047 }
1048 }
1049 }
1050 }
1051 });
1052 }
1053
1054 void inputAboutToFinish(
1055 score::gfx::RenderList& renderer, QRhiResourceUpdateBatch*& res,
1056 const ossia::geometry_spec& spec, auto& state, auto& parent)
1057 {
1058 avnd::geometry_input_introspection<T>::for_all_n2(
1059 avnd::get_inputs<T>(state),
1060 [&]<typename Field, std::size_t N, std::size_t NField>(
1061 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
1062 this->inputs[N].spec = spec; // FIXME multiple geometry input ports
1063 this->inputs[N].meshes.resize(1); // FIXME
1064 // Here we request readbacks if necessary
1065
1066 auto& meshes = this->inputs[N].meshes[0];
1067 auto upload = [&](int buffer_index, const void* data, int64_t bytesize) {
1068 if(meshes.buffers.size() <= std::size_t(buffer_index))
1069 {
1070 meshes.buffers.resize(buffer_index + 1);
1071 meshes.readbacks.resize(buffer_index + 1);
1072 }
1073 QRhiBuffer*& buf = meshes.buffers[buffer_index];
1074 if(!buf || !ossia::contains(allocated, buf))
1075 {
1076 buf = renderer.state.rhi->newBuffer(
1077 QRhiBuffer::Static,
1079 *renderer.state.rhi,
1080 QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer),
1081 bytesize);
1082 buf->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(t));
1083 buf->create();
1085 allocated.push_back(buf);
1086 }
1087 else if(buf->size() < bytesize)
1088 {
1089 // Buffer exists but is too small — resize it.
1090 buf->setSize(bytesize);
1091 buf->create();
1092 }
1093
1094 res->uploadStaticBuffer(buf, 0, bytesize, data);
1095 };
1096 oscr::meshes_from_ossia(
1097 spec.meshes, t.mesh,
1098 [&](auto& write_buf, int buffer_index, void* data, int64_t bytesize) {
1099 // cpu -> gpu
1100 upload(buffer_index, data, bytesize);
1101 }, [&](auto& write_buf, int buffer_index, void* handle) {
1102 // gpu -> cpu
1103 if(meshes.readbacks.size() <= buffer_index)
1104 {
1105 meshes.buffers.resize(buffer_index + 1);
1106 meshes.readbacks.resize(buffer_index + 1);
1107 }
1108
1109 meshes.readbacks[buffer_index] = {};
1110 if(auto buf = static_cast<QRhiBuffer*>(handle))
1111 {
1112 meshes.buffers[buffer_index] = buf;
1113 res->readBackBuffer(buf, 0, buf->size(), &meshes.readbacks[buffer_index]);
1114 }
1115 else
1116 {
1117 meshes.buffers[buffer_index] = {};
1118 meshes.readbacks[buffer_index] = {};
1119 }
1120 });
1121
1122 // A CPU buffer is flagged dirty only on the frame it changed: a consumer
1123 // that first sees it later (inserted into a running graph, or rebuilt)
1124 // still has to upload it once.
1125 if constexpr(requires { t.mesh.buffers[0].handle; })
1126 {
1127 if(spec.meshes && !spec.meshes->meshes.empty())
1128 {
1129 const auto& src = spec.meshes->meshes[0].buffers;
1130 for(std::size_t i = 0; i < src.size(); i++)
1131 {
1132 auto* cpu = ossia::get_if<ossia::geometry::cpu_buffer>(&src[i].data);
1133 if(!cpu || !cpu->raw_data || cpu->byte_size <= 0)
1134 continue;
1135 if(i < meshes.buffers.size() && meshes.buffers[i]
1136 && ossia::contains(allocated, meshes.buffers[i]))
1137 continue;
1138 upload(int(i), cpu->raw_data.get(), cpu->byte_size);
1139 }
1140 }
1141 }
1142 });
1143 }
1144
1145 void release(score::gfx::RenderList& renderer)
1146 {
1147 for(auto& buf : allocated)
1148 renderer.releaseBuffer(buf);
1149 allocated.clear();
1150 for(auto& in : inputs)
1151 in.meshes.clear();
1152 }
1153};
1154
1155template <typename T>
1156 requires(avnd::geometry_input_introspection<T>::size == 0)
1157struct geometry_inputs_storage<T>
1158{
1159 static void readInputGeometries(auto&&...) { }
1160
1161 static void inputAboutToFinish(auto&&...) { }
1162
1163 static void release(auto&&...) { }
1164};
1165
1166template<typename T>
1167struct buffer_inputs_storage;
1168
1169template<typename T>
1170 requires (avnd::buffer_input_introspection<T>::size > 0)
1171struct buffer_inputs_storage<T>
1172{
1173 // +1 because of zero-array-size unsupported
1174 QRhiBufferReadbackResult
1175 m_readbacks[avnd::cpu_buffer_input_introspection<T>::size + 1];
1176 score::gfx::BufferView m_gpubufs[avnd::gpu_buffer_input_introspection<T>::size + 1];
1177
1178 void readInputBuffers(
1179 score::gfx::RenderList& renderer, auto& parent, auto& state)
1180 {
1181 if constexpr(avnd::cpu_buffer_input_introspection<T>::size > 0)
1182 {
1183 // Copy the readback output inside the structure
1184 // TODO it would be much better to do this inside the readback's
1185 // "completed" callback.
1186 avnd::cpu_buffer_input_introspection<T>::for_all_n(
1187 avnd::get_inputs<T>(state),
1188 [&]<typename Field, std::size_t N>
1189 (Field& t, avnd::predicate_index<N> np)
1190 {
1191 auto& readback = m_readbacks[N].data;
1192 t.buffer.raw_data = reinterpret_cast<unsigned char*>(readback.data());
1193 t.buffer.byte_size = readback.size();
1194 t.buffer.byte_offset = 0; // FIXME
1195 t.buffer.changed = true;
1196 });
1197 }
1198
1199 if constexpr(avnd::gpu_buffer_input_introspection<T>::size > 0)
1200 {
1201 // Copy the readback output inside the structure
1202 // TODO it would be much better to do this inside the readback's
1203 // "completed" callback.
1204 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
1205 avnd::get_inputs<T>(state),
1206 [&]<typename Field, std::size_t N, std::size_t NField>(
1207 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
1208 // Re-fetched every frame: the cached view is only refreshed at the end
1209 // of a frame, and the upstream renderer may have been released or
1210 // recreated in between (a live graph edit deletes its buffers at once),
1211 // which left a freed QRhiBuffer* published to the node and bound as a
1212 // vertex input (examples/3d/example-big-cube.score).
1213 score::gfx::BufferView& buf = m_gpubufs[N];
1214 buf = getInputBuffer(renderer, parent, nf);
1215 if(!buf)
1216 {
1217 t.buffer.handle = nullptr;
1218 t.buffer.byte_size = 0;
1219 t.buffer.byte_offset = 0;
1220 return;
1221 }
1222 t.buffer.handle = buf.handle;
1223 t.buffer.byte_size = buf.byte_size;
1224 t.buffer.byte_offset = buf.byte_offset;
1225 // t.buffer.changed = true; FIXME
1226 });
1227 }
1228 }
1229
1230 void inputAboutToFinish(
1231 score::gfx::RenderList& renderer,
1232 QRhiResourceUpdateBatch*& res,
1233 auto& state,
1234 auto& parent)
1235 {
1236 avnd::cpu_buffer_input_introspection<T>::for_all_n2(
1237 avnd::get_inputs<T>(state),
1238 [&]<typename Field, std::size_t N, std::size_t NField>
1239 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
1240 readbackInputBuffer(renderer, *res, parent, m_readbacks[N], nf);
1241 });
1242 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
1243 avnd::get_inputs<T>(state),
1244 [&]<typename Field, std::size_t N, std::size_t NField>
1245 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
1246 m_gpubufs[N] = getInputBuffer(renderer, parent, nf);
1247 });
1248 }
1249};
1250
1251template<typename T>
1252 requires (avnd::buffer_input_introspection<T>::size == 0)
1253struct buffer_inputs_storage<T>
1254{
1255 static void readInputBuffers(auto&&...)
1256 {
1257
1258 }
1259
1260 static void inputAboutToFinish(auto&&...)
1261 {
1262
1263 }
1264};
1265
1266struct MaybeOwnedBuffer : score::gfx::BufferView
1267{
1268 bool owned{false};
1269};
1270
1271template<typename T>
1272struct buffer_outputs_storage;
1273
1274template<typename T>
1275 requires (avnd::buffer_output_introspection<T>::size > 0)
1276struct buffer_outputs_storage<T>
1277{
1278 std::pair<const score::gfx::Port*, MaybeOwnedBuffer>
1279 m_buffers[avnd::buffer_output_introspection<T>::size];
1280
1281 QRhiResourceUpdateBatch* currentResourceUpdateBatch{};
1282
1285 score::gfx::RenderList* m_renderer{};
1286
1287 template <typename Field, std::size_t N, std::size_t NField>
1288 requires avnd::cpu_buffer<std::decay_t<decltype(Field::buffer)>>
1289 void createOutput(
1290 score::gfx::RenderList& renderer, auto& parent, Field& port,
1291 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
1292 {
1293 auto& [gfx_port, buf] = m_buffers[N];
1294 gfx_port = parent.output[nf];
1295 buf.handle = renderer.state.rhi->newBuffer(
1296 QRhiBuffer::Static,
1298 *renderer.state.rhi,
1299 QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer), 1);
1300 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
1301 buf.byte_offset = 0;
1302 buf.byte_size = 1;
1303 buf.owned = true;
1304
1305 buf.handle->create();
1306
1307 m_renderer = &renderer;
1308 bindUpload(port, np);
1309 }
1310
1318 template <typename Field, std::size_t N>
1319 void bindUpload(Field& port, avnd::predicate_index<N>)
1320 {
1321 port.buffer.upload
1322 = [this, &port](const char* data, int64_t offset, int64_t bytesize) {
1323 // FIXME is offset and bytesize relative to the input or the output data ?
1324 SCORE_ASSERT(currentResourceUpdateBatch);
1325 SCORE_ASSERT(m_renderer);
1326 auto& rhi = *m_renderer->state.rhi;
1327 auto& [gfx_port, buf] = m_buffers[N];
1328
1329 if(!buf.handle)
1330 {
1331 if(bytesize > 0)
1332 {
1333 buf.handle = rhi.newBuffer(
1334 QRhiBuffer::Static,
1336 rhi, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer),
1337 bytesize);
1338 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
1339 buf.byte_offset = 0;
1340 buf.byte_size = bytesize;
1341 buf.owned = true;
1342
1343 buf.handle->create();
1344 }
1345 else
1346 {
1347 buf.handle = rhi.newBuffer(
1348 QRhiBuffer::Static,
1350 rhi, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer),
1351 1);
1352 buf.handle->setName(oscr::getUtf8Name<T>() + "::" + oscr::getUtf8Name(port));
1353 buf.byte_offset = 0;
1354 buf.byte_size = 1;
1355 buf.owned = true;
1356
1357 buf.handle->create();
1358 return;
1359 }
1360 }
1361 else if(buf.handle->size() != bytesize)
1362 {
1363 buf.handle->destroy();
1364 buf.handle->setSize(bytesize);
1365 buf.handle->create();
1366 buf.byte_size = bytesize;
1367 }
1368
1370 currentResourceUpdateBatch, buf.handle, offset, bytesize, data);
1371 };
1372 }
1373
1376 void bindUploads(auto& state)
1377 {
1378 avnd::buffer_output_introspection<T>::for_all_n(
1379 avnd::get_outputs<T>(state),
1380 [this]<typename Field, std::size_t N>(Field& port, avnd::predicate_index<N> np) {
1381 if constexpr(avnd::cpu_buffer<std::decay_t<decltype(Field::buffer)>> && requires {
1382 port.buffer.upload(nullptr, 0, 0);
1383 })
1384 {
1385 bindUpload(port, np);
1386 }
1387 });
1388 }
1389
1390 template <typename Field, std::size_t N, std::size_t NField>
1391 requires avnd::gpu_buffer<std::decay_t<decltype(Field::buffer)>>
1392 void createOutput(
1393 score::gfx::RenderList& renderer, auto& parent, Field& port,
1394 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
1395 {
1396 auto& [gfx_port, buf] = m_buffers[N];
1397 gfx_port = parent.output[nf];
1398 buf.handle = reinterpret_cast<QRhiBuffer*>(port.buffer.handle);
1399 buf.byte_size = port.buffer.byte_size;
1400 buf.byte_offset = port.buffer.byte_offset;
1401 buf.owned = false;
1402 }
1403
1404 void init(score::gfx::RenderList& renderer, auto& state, auto& parent)
1405 {
1406 // Init buffers for the outputs
1407 avnd::buffer_output_introspection<T>::for_all_n2(
1408 avnd::get_outputs<T>(state), [&]<typename Field, std::size_t N, std::size_t NField>
1409 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
1410 SCORE_ASSERT(parent.output.size() > nf);
1411 SCORE_ASSERT(parent.output[nf]->type == score::gfx::Types::Buffer);
1412 using buffer_type = std::decay_t<decltype(port.buffer)>;
1413
1414 if constexpr(avnd::cpu_raw_buffer<buffer_type> && requires {
1415 port.buffer.upload(nullptr, 0, 0);
1416 })
1417 {
1418 createOutput(renderer, parent, port, np, nf);
1419 }
1420 else if constexpr(avnd::gpu_buffer<buffer_type>)
1421 {
1422 createOutput(renderer, parent, port, np, nf);
1423 }
1424 else
1425 {
1426 // m_buffers[N] = createOutput(renderer, *parent.output[nf], port.buffer);
1427 static_assert(std::is_same_v<T, void>, "unsupported");
1428 }
1429 });
1430 }
1431
1432 void prepareUpload(QRhiResourceUpdateBatch& res)
1433 {
1434 currentResourceUpdateBatch = &res;
1435 }
1436
1437 void upload(score::gfx::RenderList& renderer, auto& state, QRhiResourceUpdateBatch& res)
1438 {
1439 avnd::buffer_output_introspection<T>::for_all_n(
1440 avnd::get_outputs<T>(state), [&]<std::size_t N>(auto& t, avnd::predicate_index<N> idx) {
1441 auto& [port, buf] = m_buffers[N];
1442 uploadOutputBuffer(renderer, t.buffer, res, buf);
1443 });
1444 }
1445
1446 void release(score::gfx::RenderList& renderer)
1447 {
1448 // Free outputs
1449 for(auto& [p, buf] : m_buffers)
1450 {
1451 if(buf.owned)
1452 renderer.releaseBuffer(buf.handle);
1453 buf.handle = nullptr;
1454 buf.owned = false;
1455 }
1456 }
1457};
1458
1459template<typename T>
1460 requires (avnd::buffer_output_introspection<T>::size == 0)
1461struct buffer_outputs_storage<T>
1462{
1463 static void init(auto&&...)
1464 {
1465
1466 }
1467
1468 static void prepareUpload(auto&&...)
1469 {
1470 }
1471
1472 static void bindUploads(auto&&...)
1473 {
1474 }
1475
1476 static void upload(auto&&...)
1477 {
1478 }
1479
1480 static void release(auto&&...)
1481 {
1482 }
1483};
1484
1485
1486template <typename Tex>
1487static auto
1488createOutputTexture(score::gfx::RenderList& renderer, const Tex& texture_spec, QSize size)
1489{
1490 auto& rhi = *renderer.state.rhi;
1491 QRhiTexture* texture = &renderer.emptyTexture();
1492 if(size.width() > 0 && size.height() > 0)
1493 {
1494 // Published through textureForOutput: a CPU texture inlet downstream
1495 // (Texture to buffer) reads it back.
1496 texture = rhi.newTexture(
1497 gpp::qrhi::textureFormat(texture_spec), size, 1,
1498 QRhiTexture::UsedAsTransferSource);
1499
1500 texture->create();
1501 }
1502
1503 auto sampler = rhi.newSampler(
1504 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1505 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
1506
1507 sampler->create();
1508 return score::gfx::Sampler{sampler, texture};
1509}
1510
1511
1512template<typename T>
1513struct texture_inputs_storage;
1514
1515template<typename T>
1516 requires (avnd::texture_input_introspection<T>::size > 0)
1517struct texture_inputs_storage<T>
1518{
1519 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
1520 m_rts;
1521 ossia::small_flat_map<const score::gfx::Port*, QRhiSampler*, 2> m_samplers;
1522
1523 QRhiReadbackResult m_readbacks[avnd::texture_input_introspection<T>::size];
1524 bool m_readbackPremultiplied[avnd::texture_input_introspection<T>::size]{};
1525
1526 template <typename Tex>
1527 QRhiTexture* createInput(
1528 score::gfx::RenderList& renderer, score::gfx::Port* port, Tex& texture_spec,
1529 const score::gfx::RenderTargetSpecs& spec, bool wantsSamplableDepth = false,
1530 bool mipmapped = false)
1531 {
1532 QRhiTexture::Flags flags
1533 = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
1534 if(mipmapped)
1535 flags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips;
1536 QRhiTexture::Format fmt{};
1537 if constexpr(requires (Tex tex) { tex.format = {}; } && !requires (Tex tex) { tex.request_format; })
1538 {
1539 // Format freely assignable: we use what the user sets in the GUI
1540 fmt = spec.format;
1541 gpp::qrhi::toTextureFormat(fmt, texture_spec);
1542 }
1543 else
1544 {
1545 fmt = gpp::qrhi::textureFormat(texture_spec);
1546 }
1547
1548 QRhiTexture* texture = renderer.state.rhi->newTexture(
1549 fmt, spec.size, 1, flags);
1550
1551 SCORE_ASSERT(texture->create());
1552 // wantsSamplableDepth implies wantsDepth: createRenderTarget allocates
1553 // a sampleable single-sample depth texture (with MSAA-resolve when
1554 // available) instead of a renderbuffer / non-resolve depth target.
1555 // Same shape ISF/CSF inputs get when their port has SamplableDepth.
1556 const bool wantsDepth = renderer.requiresDepth(*port) || wantsSamplableDepth;
1557 m_rts[port] = score::gfx::createRenderTarget(
1558 renderer.state, texture, renderer.samples(),
1559 wantsDepth, wantsSamplableDepth);
1560 return texture;
1561 }
1562
1563 void init(auto& self, score::gfx::RenderList& renderer)
1564 {
1565 // Init input render targets
1566 avnd::texture_input_introspection<T>::for_all_n2(
1567 avnd::get_inputs<T>(*self.state),
1568 [&]<typename F, std::size_t K, std::size_t N>(F& t, avnd::predicate_index<K>, avnd::field_index<N>) {
1569 // Non-2D GPU texture inputs (cube / array / 3D) don't get a local
1570 // render target — the port carries Flag::GrabsFromSource (set by
1571 // initGfxPorts via texture_kind_of<F>()), the graph will populate
1572 // t.texture.handle through updateInputTexture when the edge
1573 // resolves. Skipping the allocation here avoids wasting a 2D
1574 // colour attachment that would never be rendered into anyway.
1575 if constexpr(avnd::gpu_texture_port<F>
1576 && halp::texture_kind_of<F>() != halp::texture_kind::texture_2d)
1577 {
1578 t.texture.kind = halp::texture_kind_of<F>();
1579 // Handle + size populated later by updateInputTexture once the
1580 // upstream is resolved.
1581 return;
1582 }
1583
1584 auto& parent = self.node();
1585 auto spec = renderer.resolveInputRenderTargetSpecs(parent, N);
1586 if constexpr(requires {
1587 t.request_width;
1588 t.request_height;
1589 })
1590 {
1591 spec.size.rwidth() = t.request_width;
1592 spec.size.rheight() = t.request_height;
1593 }
1594
1595 constexpr bool wantsSamplableDepth
1596 = avnd::gpu_texture_port<F> && halp::samplable_depth_of<F>();
1597 if constexpr(avnd::gpu_texture_port<F>)
1598 {
1599 if(!single_cable_port<F> || singleCableNeedsTarget(renderer, *parent.input[N]))
1600 createInput(
1601 renderer, parent.input[N], t.texture, spec, wantsSamplableDepth,
1602 spec.mipmap_mode != QRhiSampler::None);
1603 auto* sampler = renderer.state.rhi->newSampler(
1604 spec.mag_filter, spec.min_filter, spec.mipmap_mode, spec.address_u,
1605 spec.address_v, spec.address_w);
1606 sampler->setName("texture_inputs_storage::sampler");
1607 sampler->create();
1608 m_samplers[parent.input[N]] = sampler;
1609 }
1610 else
1611 {
1612 createInput(renderer, parent.input[N], t.texture, spec, wantsSamplableDepth);
1613 }
1614 if constexpr(avnd::cpu_texture_port<F>)
1615 {
1616 t.texture.width = spec.size.width();
1617 t.texture.height = spec.size.height();
1618 }
1619 });
1620
1621 refreshGpuInputs(self, renderer);
1622 }
1623
1624 static std::pair<bool, QRhiTexture*> upstreamTexture(
1625 score::gfx::RenderList& renderer, const score::gfx::Node& node, int32_t index,
1626 const score::gfx::Port& port, bool needsOwnTarget = false)
1627 {
1628 int wired = 0;
1629 QRhiTexture* direct = nullptr;
1630 for(auto* edge : port.edges)
1631 {
1632 if(!edge || !edge->source || !edge->source->node)
1633 continue;
1634 auto& rendered = edge->source->node->renderedNodes;
1635 auto it = rendered.find(&renderer);
1636 if(it == rendered.end() || !it->second)
1637 continue;
1638 if(wired++ == 0)
1639 direct = it->second->textureForOutput(*edge->source);
1640 }
1641 if(wired != 1 || needsOwnTarget || node.hasExplicitRenderTargetSpecs(index))
1642 direct = nullptr;
1643 return {wired > 0, direct};
1644 }
1645
1646 static QRhiTexture*
1647 singleCableTexture(score::gfx::RenderList& renderer, const score::gfx::Port& port)
1648 {
1649 for(auto* edge : port.edges)
1650 {
1651 if(!edge || !edge->source || !edge->source->node)
1652 continue;
1653 auto& rendered = edge->source->node->renderedNodes;
1654 auto it = rendered.find(&renderer);
1655 if(it == rendered.end() || !it->second)
1656 continue;
1657 return it->second->textureForOutput(*edge->source);
1658 }
1659 return nullptr;
1660 }
1661
1662 static bool
1663 singleCableNeedsTarget(score::gfx::RenderList& renderer, const score::gfx::Port& port)
1664 {
1665 return !port.edges.empty() && !singleCableTexture(renderer, port);
1666 }
1667
1668 static bool
1669 composited(score::gfx::RenderList& renderer, const score::gfx::Port& port)
1670 {
1671 for(auto* edge : port.edges)
1672 {
1673 if(!edge || !edge->source || !edge->source->node)
1674 continue;
1675 auto& rendered = edge->source->node->renderedNodes;
1676 if(auto it = rendered.find(&renderer);
1677 it != rendered.end() && it->second && it->second->hasOutputPassForEdge(*edge))
1678 return true;
1679 }
1680 return false;
1681 }
1682
1683 void releaseTarget(score::gfx::RenderList& renderer, const score::gfx::Port* port)
1684 {
1685 if(auto it = m_rts.find(port); it != m_rts.end())
1686 {
1687 renderer.releaseOwnedTarget(it->second);
1688 m_rts.erase(it);
1689 }
1690 }
1691
1692 QRhiSampler* refreshSampler(
1693 const score::gfx::Node& node, int32_t index, score::gfx::RenderList& renderer,
1694 const score::gfx::Port* port)
1695 {
1696 auto it = m_samplers.find(port);
1697 if(it == m_samplers.end() || !it->second)
1698 return nullptr;
1699 auto* sampler = it->second;
1700 const auto spec = node.resolveRenderTargetSpecs(index, renderer);
1701 if(sampler->magFilter() != spec.mag_filter || sampler->minFilter() != spec.min_filter
1702 || sampler->mipmapMode() != spec.mipmap_mode
1703 || sampler->addressU() != spec.address_u || sampler->addressV() != spec.address_v
1704 || sampler->addressW() != spec.address_w)
1705 {
1706 sampler->destroy();
1707 sampler->setMagFilter(spec.mag_filter);
1708 sampler->setMinFilter(spec.min_filter);
1709 sampler->setMipmapMode(spec.mipmap_mode);
1710 sampler->setAddressU(spec.address_u);
1711 sampler->setAddressV(spec.address_v);
1712 sampler->setAddressW(spec.address_w);
1713 sampler->create();
1714 }
1715 return sampler;
1716 }
1717
1718 static void describeTexture(halp::gpu_texture& dst, QRhiTexture& tex)
1719 {
1720 const auto sz = tex.pixelSize();
1721 dst.handle = &tex;
1722 dst.width = sz.width();
1723 dst.height = sz.height();
1724 gpp::qrhi::toTextureFormat(tex.format(), dst);
1725 const auto flags = tex.flags();
1726 if(flags & QRhiTexture::CubeMap)
1727 {
1728 dst.kind = halp::texture_kind::cubemap;
1729 dst.layers_or_depth = 6;
1730 }
1731 else if(flags & QRhiTexture::ThreeDimensional)
1732 {
1733 dst.kind = halp::texture_kind::texture_3d;
1734 dst.layers_or_depth = tex.depth();
1735 }
1736 else if(flags & QRhiTexture::TextureArray)
1737 {
1738 dst.kind = halp::texture_kind::texture_array;
1739 dst.layers_or_depth = tex.arraySize();
1740 }
1741 else
1742 {
1743 dst.kind = halp::texture_kind::texture_2d;
1744 dst.layers_or_depth = 1;
1745 }
1746 }
1747
1748 void refreshGpuInputs(auto& self, score::gfx::RenderList& renderer)
1749 {
1750 avnd::texture_input_introspection<T>::for_all_n2(
1751 avnd::get_inputs<T>(*self.state),
1752 [&]<typename F, std::size_t K, std::size_t N>(
1753 F& t, avnd::predicate_index<K>, avnd::field_index<N>) {
1754 if constexpr(
1755 avnd::gpu_texture_port<F>
1756 && halp::texture_kind_of<F>() == halp::texture_kind::texture_2d)
1757 {
1758 constexpr bool wantsSamplableDepth = halp::samplable_depth_of<F>();
1759 auto* port = self.node().input[N];
1760 auto& tex = t.texture;
1761 const auto rt_it = m_rts.find(port);
1762 const bool mipmapped
1763 = rt_it != m_rts.end() && rt_it->second.texture
1764 && rt_it->second.texture->flags().testFlag(QRhiTexture::MipMapped);
1765 tex.sampler_handle = refreshSampler(self.node(), N, renderer, port);
1766
1767 QRhiTexture* src = nullptr;
1768 if constexpr(single_cable_port<F>)
1769 {
1770 src = singleCableTexture(renderer, *port);
1771 if(src)
1772 {
1773 if(rt_it != m_rts.end() && !composited(renderer, *port))
1774 releaseTarget(renderer, port);
1775 }
1776 else if(!port->edges.empty() && rt_it != m_rts.end())
1777 {
1778 src = rt_it->second.texture;
1779 }
1780 }
1781 else if(auto [wired, direct]
1782 = upstreamTexture(renderer, self.node(), N, *port, mipmapped);
1783 wired)
1784 {
1785 if constexpr(!wantsSamplableDepth)
1786 src = direct;
1787 if(!src && rt_it != m_rts.end())
1788 src = rt_it->second.texture;
1789 }
1790
1791 if(!src)
1792 {
1793 tex.handle = nullptr;
1794 tex.width = 0;
1795 tex.height = 0;
1796 tex.kind = halp::texture_kind::texture_2d;
1797 tex.layers_or_depth = 1;
1798 if constexpr(wantsSamplableDepth)
1799 tex.depth_handle = nullptr;
1800 return;
1801 }
1802
1803 describeTexture(tex, *src);
1804 if constexpr(wantsSamplableDepth)
1805 {
1806 auto* depth = rt_it->second.depthTexture;
1807 tex.depth_handle = depth;
1808 if(depth)
1809 tex.depth_format = qrhiToHalpDepthFormat(depth->format());
1810 }
1811 }
1812 });
1813 }
1814
1815 bool update(auto& self,
1816 score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
1817 {
1818 avnd::texture_input_introspection<T>::for_all_n2(
1819 avnd::get_inputs<T>(*self.state),
1820 [&]<typename F, std::size_t K, std::size_t N>(
1821 F& t, avnd::predicate_index<K>, avnd::field_index<N>) {
1822 if constexpr(single_cable_port<F> && avnd::gpu_texture_port<F>)
1823 {
1824 auto& parent = self.node();
1825 auto* port = parent.input[N];
1826 if(m_rts.find(port) != m_rts.end() || !singleCableNeedsTarget(renderer, *port))
1827 return;
1828 auto spec = renderer.resolveInputRenderTargetSpecs(parent, N);
1829 if constexpr(requires {
1830 t.request_width;
1831 t.request_height;
1832 })
1833 {
1834 spec.size.rwidth() = t.request_width;
1835 spec.size.rheight() = t.request_height;
1836 }
1837 createInput(
1838 renderer, port, t.texture, spec, false,
1839 spec.mipmap_mode != QRhiSampler::None);
1840 for(auto* edge : port->edges)
1841 {
1842 auto& rendered = edge->source->node->renderedNodes;
1843 if(auto it = rendered.find(&renderer); it != rendered.end() && it->second)
1844 {
1845 it->second->removeOutputPass(renderer, *edge);
1846 it->second->addOutputPass(renderer, *edge, res);
1847 }
1848 }
1849 }
1850 });
1851#if 0
1852 bool need_update = false;
1853 avnd::texture_input_introspection<T>::for_all_n2(
1854 avnd::get_inputs<T>(*self.state),
1855 [&]<typename F, std::size_t K, std::size_t N>(F& t, avnd::predicate_index<K>, avnd::field_index<N>) {
1856 if constexpr(requires {
1857 t.request_width;
1858 t.request_height;
1859 })
1860 {
1861 auto& parent = self.node();
1862 auto port = parent.input[N];
1863 const score::gfx::TextureRenderTarget& texture = m_rts[port];
1864 QSizeF sz{};
1865 if(texture.texture)
1866 sz = texture.texture->pixelSize();
1867 if(sz.width() != t.request_width || sz.height() != t.request_height)
1868 {
1869 // FIXME right now this doesn't work because
1870 // the render target spec is stored in the node.
1871 // Also the RenderList just recomputes everything anyways,
1872 // so we should just emit a "need to change" signal and abort as
1873 // long as things aren't more optimized and actually follow the graph
1874
1875 // m_rts[port].release();
1876
1877 // auto spec = parent.resolveRenderTargetSpecs(N, renderer);
1878 // spec.size.rwidth() = t.request_width;
1879 // spec.size.rheight() = t.request_height;
1880
1881 // createInput(renderer, port, t.texture, sz);
1882
1883 // t.texture.width = spec.size.width();
1884 // t.texture.height = spec.size.height();
1885 // need_update = true;
1886 //
1887 }
1888 }
1889 });
1890 return need_update;
1891#endif
1892 return false;
1893 }
1894
1895 template <typename F>
1896 static constexpr bool reads_upstream_directly() noexcept
1897 {
1898 using Tex = std::decay_t<decltype(F::texture)>;
1899 return avnd::cpu_texture_port<F>
1900 && requires(Tex tex) { tex.format = {}; }
1901 && !requires(Tex tex) { tex.request_format; }
1902 && !requires(F f) { f.request_width; };
1903 }
1904
1905 void runInitialPasses(auto& self, score::gfx::RenderList& renderer)
1906 {
1907 auto& rhi = *renderer.state.rhi;
1908 refreshGpuInputs(self, renderer);
1909
1910 // Fetch input textures (if any)
1911 // Copy the readback output inside the structure
1912 // TODO it would be much better to do this inside the readback's
1913 // "completed" callback.
1914 if constexpr(avnd::cpu_texture_input_introspection<T>::size > 0)
1915 {
1916 avnd::texture_input_introspection<T>::for_all_n(
1917 avnd::get_inputs<T>(*self.state), [&]<typename F, std::size_t K>(F& t, avnd::predicate_index<K>) {
1918 if constexpr(avnd::cpu_texture_port<F>)
1919 {
1920 if constexpr(reads_upstream_directly<F>())
1921 {
1922 const auto& rb = m_readbacks[K];
1923 if(!rb.pixelSize.isEmpty())
1924 {
1925 t.texture.width = rb.pixelSize.width();
1926 t.texture.height = rb.pixelSize.height();
1927 gpp::qrhi::toTextureFormat(rb.format, t.texture);
1928 }
1929 }
1930 if(auto& rb = m_readbacks[K]; m_readbackPremultiplied[K] && !rb.data.isEmpty())
1931 {
1932 gpp::qrhi::unpremultiply(
1933 rb.format, rb.data, rb.pixelSize.width(), rb.pixelSize.height());
1934 m_readbackPremultiplied[K] = false;
1935 }
1936 oscr::loadInputTexture(rhi, m_readbacks, t.texture, K);
1937 }
1938 });
1939 }
1940 }
1941
1942 void release()
1943 {
1944 // Free inputs
1945 // TODO investigate why reference does not work here:
1946 for(auto [port, rt] : m_rts)
1947 rt.release();
1948 m_rts.clear();
1949 for(auto [port, sampler] : m_samplers)
1950 sampler->deleteLater();
1951 m_samplers.clear();
1952 }
1953
1954 void inputAboutToFinish(
1955 auto& self, score::gfx::RenderList& renderer, const score::gfx::Port& p,
1956 QRhiResourceUpdateBatch*& res)
1957 {
1958 if constexpr(avnd::cpu_texture_input_introspection<T>::size > 0)
1959 {
1960 const auto& inputs = self.node().input;
1961 avnd::texture_input_introspection<T>::for_all_n2(
1962 avnd::get_inputs<T>(*self.state),
1963 [&]<typename F, std::size_t K, std::size_t N>(
1964 F&, avnd::predicate_index<K>, avnd::field_index<N>) {
1965 if constexpr(avnd::cpu_texture_port<F>)
1966 {
1967 if(N >= inputs.size() || inputs[N] != &p)
1968 return;
1969 auto rt_it = m_rts.find(&p);
1970 if(rt_it == m_rts.end())
1971 return;
1972 QRhiTexture* tex = rt_it->second.texture;
1973 if constexpr(reads_upstream_directly<F>())
1974 {
1975 auto [wired, direct] = upstreamTexture(renderer, self.node(), N, p);
1976 if(direct && direct->sampleCount() <= 1
1977 && (direct->flags() & QRhiTexture::UsedAsTransferSource)
1978 && !(direct->flags()
1979 & (QRhiTexture::CubeMap | QRhiTexture::ThreeDimensional
1980 | QRhiTexture::TextureArray)))
1981 tex = direct;
1982 }
1983 if(!tex)
1984 return;
1985 auto& readback = m_readbacks[K];
1986 readback = {};
1987 m_readbackPremultiplied[K] = tex == rt_it->second.texture;
1988 res->readBackTexture(QRhiReadbackDescription{tex}, &readback);
1989 }
1990 });
1991 }
1992 }
1993
1994};
1995template<typename T>
1996 requires (avnd::texture_input_introspection<T>::size == 0)
1997struct texture_inputs_storage<T>
1998{
1999 static void init(auto&&...) { }
2000 static void runInitialPasses(auto&&...) { }
2001 static void release(auto&&...) { }
2002 static void inputAboutToFinish(auto&&...) { }
2003};
2004
2005
2006
2007template <avnd::cpu_texture Tex>
2008static QRhiTexture* updateTexture(auto& self, score::gfx::RenderList& renderer, int k, const Tex& cpu_tex)
2009{
2010 auto& sampler = self.m_samplers[k].sampler;
2011 auto& texture = self.m_samplers[k].texture;
2012 if(texture)
2013 {
2014 auto sz = texture->pixelSize();
2015 if(cpu_tex.width == sz.width() && cpu_tex.height == sz.height())
2016 return texture;
2017 }
2018
2019 // Check the texture size
2020 if(cpu_tex.width > 0 && cpu_tex.height > 0)
2021 {
2022 QRhiTexture* oldtex = texture;
2023 QRhiTexture* newtex = renderer.state.rhi->newTexture(
2024 gpp::qrhi::textureFormat(cpu_tex), QSize{cpu_tex.width, cpu_tex.height}, 1,
2025 QRhiTexture::UsedAsTransferSource);
2026 newtex->create();
2027 for(auto& [edge, pass] : self.m_p)
2028 if(pass.p.srb)
2029 score::gfx::replaceTexture(*pass.p.srb, sampler, newtex);
2030 texture = newtex;
2031
2032 if(oldtex && oldtex != &renderer.emptyTexture())
2033 {
2034 oldtex->deleteLater();
2035 }
2036
2037 return newtex;
2038 }
2039 else
2040 {
2041 for(auto& [edge, pass] : self.m_p)
2042 if(pass.p.srb)
2043 score::gfx::replaceTexture(*pass.p.srb, sampler, &renderer.emptyTexture());
2044
2045 return &renderer.emptyTexture();
2046 }
2047}
2048
2049template <avnd::cpu_texture Tex>
2050static void uploadOutputTexture(auto& self,
2051 score::gfx::RenderList& renderer, int k, Tex& cpu_tex,
2052 QRhiResourceUpdateBatch* res)
2053{
2054 if(cpu_tex.changed)
2055 {
2056 if(auto texture = updateTexture(self, renderer, k, cpu_tex))
2057 {
2058 if(!cpu_tex.bytes || cpu_tex.bytesize() <= 0)
2059 {
2060 cpu_tex.changed = false;
2061 return;
2062 }
2063
2064 QByteArray buf
2065 = QByteArray::fromRawData((const char*)cpu_tex.bytes, cpu_tex.bytesize());
2066 if constexpr(requires { Tex::RGB; })
2067 {
2068 // RGB -> RGBA
2069 // FIXME other conversions
2070 const QByteArray rgb = buf;
2071 QByteArray rgba;
2072 rgba.resize(cpu_tex.width * cpu_tex.height * 4);
2073 auto src = (const unsigned char*)rgb.constData();
2074 auto dst = (unsigned char*)rgba.data();
2075 for(int rgb_byte = 0, rgba_byte = 0, N = rgb.size(); rgb_byte < N;)
2076 {
2077 dst[rgba_byte + 0] = src[rgb_byte + 0];
2078 dst[rgba_byte + 1] = src[rgb_byte + 1];
2079 dst[rgba_byte + 2] = src[rgb_byte + 2];
2080 dst[rgba_byte + 3] = 255;
2081 rgb_byte += 3;
2082 rgba_byte += 4;
2083 }
2084 buf = rgba;
2085 }
2086
2087 // Published in the row order of a rendered texture (RowOrder.hpp): the
2088 // halp texture is top row first, so its rows are reversed off Vulkan.
2090 *renderer.state.rhi, std::move(buf), cpu_tex.height);
2091 {
2092 QRhiTextureSubresourceUploadDescription sd(buf);
2093 QRhiTextureUploadDescription desc{QRhiTextureUploadEntry{0, 0, sd}};
2094
2095 res->uploadTexture(texture, desc);
2096 }
2097
2098 cpu_tex.changed = false;
2099 }
2100 }
2101}
2102
2103// Copies a texture outlet into the render target of each outgoing edge. Every
2104// 2D texture an outlet publishes, uploaded from the CPU or drawn on the GPU, has
2105// the row order of a rendered texture (RowOrder.hpp), so this is a straight
2106// copy on every backend: GLSL needs no flip, while SPIR-V (top-first rows,
2107// flipped clipSpaceCorrMatrix) and HLSL/MSL (bottom-first rows, identity
2108// clipSpaceCorrMatrix) both need v flipped.
2109static const constexpr auto generic_texgen_vs = R"_(#version 450
2110layout(location = 0) in vec2 position;
2111layout(location = 1) in vec2 texcoord;
2112
2113layout(binding=3) uniform sampler2D y_tex;
2114layout(location = 0) out vec2 v_texcoord;
2115
2116layout(std140, binding = 0) uniform renderer_t {
2117 mat4 clipSpaceCorrMatrix;
2118 vec2 renderSize;
2119} renderer;
2120
2121out gl_PerVertex { vec4 gl_Position; };
2122
2123void main()
2124{
2125#if defined(QSHADER_SPIRV) || defined(QSHADER_HLSL) || defined(QSHADER_MSL)
2126 v_texcoord = vec2(texcoord.x, 1. - texcoord.y);
2127#else
2128 v_texcoord = texcoord;
2129#endif
2130 gl_Position = renderer.clipSpaceCorrMatrix * vec4(position.xy, 0.0, 1.);
2131}
2132)_";
2133
2134static const constexpr auto generic_texgen_fs = R"_(#version 450
2135layout(location = 0) in vec2 v_texcoord;
2136layout(location = 0) out vec4 fragColor;
2137
2138layout(std140, binding = 0) uniform renderer_t {
2139mat4 clipSpaceCorrMatrix;
2140vec2 renderSize;
2141} renderer;
2142
2143layout(binding=3) uniform sampler2D y_tex;
2144
2145void main ()
2146{
2147 fragColor = texture(y_tex, v_texcoord);
2148}
2149)_";
2150
2151template<typename T>
2152struct texture_outputs_storage;
2153
2154// If we have texture outs we need the whole rendering infrastructure
2155template<typename T>
2156 requires (avnd::texture_output_introspection<T>::size > 0)
2157struct texture_outputs_storage<T>
2158{
2159 static bool drawnOutputPremultiplied(auto& self) noexcept
2160 {
2161 bool premultiplied = false;
2162 const auto first = [&]<typename F, std::size_t N>(F&, avnd::predicate_index<N>) {
2163 if constexpr(N == 0)
2164 premultiplied = requires { F::premultiplied; };
2165 };
2166 if constexpr(avnd::cpu_texture_output_introspection<T>::size > 0)
2167 avnd::cpu_texture_output_introspection<T>::for_all_n(
2168 avnd::get_outputs<T>(*self.state), first);
2169 else
2170 avnd::gpu_texture_output_introspection<T>::for_all_n(
2171 avnd::get_outputs<T>(*self.state), first);
2172 return premultiplied;
2173 }
2174
2175 void init(auto& self, score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
2176 {
2177 const auto& mesh = renderer.defaultTriangle();
2178 self.defaultMeshInit(renderer, mesh, res);
2179 self.processUBOInit(renderer);
2180 // Not needed here as we do not have a GPU pass:
2181 // this->m_material.init(renderer, this->node.input, this->m_samplers);
2182
2183 std::tie(self.m_vertexS, self.m_fragmentS) = score::gfx::makeShaders(
2184 renderer.state, generic_texgen_vs, generic_texgen_fs);
2185
2186 avnd::cpu_texture_output_introspection<T>::for_all(
2187 avnd::get_outputs<T>(*self.state), [&](auto& t) {
2188 self.m_samplers.push_back(
2189 createOutputTexture(renderer, t.texture, QSize{t.texture.width, t.texture.height}));
2190 });
2191
2192 gpu_first = self.m_samplers.size();
2193 avnd::gpu_texture_output_introspection<T>::for_all(
2194 avnd::get_outputs<T>(*self.state), [&](auto&) {
2195 auto sampler = renderer.state.rhi->newSampler(
2196 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
2197 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
2198 sampler->create();
2199 self.m_samplers.push_back(score::gfx::Sampler{sampler, nullptr});
2200 });
2201
2202 self.m_outputPremultiplied = drawnOutputPremultiplied(self);
2203 self.defaultPassesInit(renderer, mesh);
2204
2205 // defaultPassesInit only covers the edges of output[0]: an edge leaving any
2206 // other texture outlet (e.g. Image Processor's Mask / Depth) gets its pass
2207 // here, bound to that outlet's own sampler (see samplersForOutputEdge).
2208 for(auto* port : self.node().output)
2209 {
2210 if(!port || port->type != score::gfx::Types::Image)
2211 continue;
2212 for(auto* edge : port->edges)
2213 if(!self.hasOutputPassForEdge(*edge))
2214 self.addOutputPass(renderer, *edge, res);
2215 }
2216 }
2217
2218 void runInitialPasses(auto& self,
2219 score::gfx::RenderList& renderer,
2220 QRhiResourceUpdateBatch*& res)
2221 {
2222 avnd::cpu_texture_output_introspection<T>::for_all_n(
2223 avnd::get_outputs<T>(*self.state), [&]<std::size_t N>(auto& t, avnd::predicate_index<N>) {
2224 uploadOutputTexture(self, renderer, N, t.texture, res);
2225 });
2226
2227 std::size_t k = gpu_first;
2228 avnd::gpu_texture_output_introspection<T>::for_all(
2229 avnd::get_outputs<T>(*self.state), [&](auto& t) {
2230 auto* tex = static_cast<QRhiTexture*>(t.texture.handle);
2231 if(tex
2232 && (tex->flags()
2233 & (QRhiTexture::CubeMap | QRhiTexture::ThreeDimensional
2234 | QRhiTexture::TextureArray)))
2235 tex = nullptr;
2236 auto& sampler = self.m_samplers[k];
2237 if(tex != sampler.texture)
2238 {
2239 sampler.texture = tex;
2240 for(auto& [edge, pass] : self.m_p)
2241 {
2242 if(!pass.p.srb || !edge)
2243 continue;
2244 // A pass bound to its edge's own sampler (samplersForOutputEdge) holds
2245 // it at binding 3; a pass holding every sampler has this one at 3 + k.
2246 const auto bound = self.samplersForOutputEdge(*edge);
2247 if(bound.size() == 1)
2248 {
2249 if(bound.data() == &sampler)
2250 score::gfx::replaceTexture(
2251 *pass.p.srb, 3, tex ? tex : &renderer.emptyTexture());
2252 }
2253 else
2254 {
2255 score::gfx::replaceTexture(
2256 *pass.p.srb, int(3 + k), tex ? tex : &renderer.emptyTexture());
2257 }
2258 }
2259 }
2260 k++;
2261 });
2262 }
2263
2264 void release(auto& self, score::gfx::RenderList& r)
2265 {
2266 for(std::size_t i = 0; i < self.m_samplers.size(); i++)
2267 {
2268 auto& texture = self.m_samplers[i].texture;
2269 if(i < gpu_first && texture != &r.emptyTexture())
2270 texture->deleteLater();
2271 texture = nullptr;
2272 }
2273 }
2274
2275 std::size_t gpu_first{};
2276
2277};
2278
2279template<typename T>
2280 requires (avnd::texture_output_introspection<T>::size == 0)
2281struct texture_outputs_storage<T>
2282{
2283 static void init(auto& self, score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
2284 {
2285 }
2286
2287 static void runInitialPasses(auto& self,
2288 score::gfx::RenderList& renderer,
2289 QRhiResourceUpdateBatch*& res)
2290 {
2291 }
2292
2293 static void release(auto& self, score::gfx::RenderList& r)
2294 {
2295 }
2296};
2297template<typename T>
2298struct geometry_outputs_storage;
2299
2300template<typename T>
2301 requires (avnd::geometry_output_introspection<T>::size > 0)
2302struct geometry_outputs_storage<T>
2303{
2304 ossia::geometry_spec specs[avnd::geometry_output_introspection<T>::size];
2305
2306 struct sent_transform
2307 {
2308 std::optional<ossia::transform3d> value;
2309 std::vector<std::pair<const score::gfx::Edge*, const score::gfx::NodeRenderer*>>
2310 receivers;
2311 };
2312 sent_transform transforms[avnd::geometry_output_introspection<T>::size];
2313
2314 template <avnd::geometry_port Field>
2315 void reload_mesh(Field& ctrl, ossia::geometry_spec& spc)
2316 {
2317 spc.meshes = std::make_shared<ossia::mesh_list>();
2318 auto& ossia_meshes = *spc.meshes;
2319 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
2320 {
2321 ossia_meshes.meshes.resize(1);
2322 load_geometry(ctrl, ossia_meshes.meshes[0]);
2323 }
2324 else if constexpr(
2325 avnd::static_geometry_type<decltype(Field::mesh)>
2326 || avnd::dynamic_geometry_type<decltype(Field::mesh)>)
2327 {
2328 ossia_meshes.meshes.resize(1);
2329 load_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
2330 }
2331 else
2332 {
2333 load_geometry(ctrl, ossia_meshes);
2334 }
2335 }
2336
2337 template <avnd::geometry_port Field, std::size_t N>
2338 void upload(
2339 score::gfx::RenderList& renderer, Field& ctrl, score::gfx::Edge& edge,
2340 avnd::predicate_index<N>)
2341 {
2342 auto edge_sink = edge.sink;
2343 if(auto pnode = edge_sink->node)
2344 {
2345 ossia::geometry_spec& spc = specs[N];
2346
2347 // 1. Reload mesh
2348 {
2349 if(ctrl.dirty_mesh)
2350 {
2351 reload_mesh(ctrl, spc);
2352 }
2353 else
2354 {
2355 if(spc.meshes)
2356 {
2357 auto& ossia_meshes = *spc.meshes;
2358
2359 bool any_need_reload = false;
2360 bool any_need_upload = false;
2361 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
2362 {
2363 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
2364 auto [need_reload, need_upload]
2365 = update_geometry(ctrl, ossia_meshes.meshes[0]);
2366 any_need_reload = need_reload;
2367 any_need_upload = need_upload;
2368 }
2369 else if constexpr(
2370 avnd::static_geometry_type<decltype(Field::mesh)>
2371 || avnd::dynamic_geometry_type<decltype(Field::mesh)>)
2372 {
2373 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
2374 auto [need_reload, need_upload]
2375 = update_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
2376 any_need_reload = need_reload;
2377 any_need_upload = need_upload;
2378 }
2379 else
2380 {
2381 auto [need_reload, need_upload] = update_geometry(ctrl, ossia_meshes);
2382 any_need_reload = need_reload;
2383 any_need_upload = need_upload;
2384 }
2385
2386 if(any_need_reload)
2387 {
2388 reload_mesh(ctrl, spc);
2389 }
2390 }
2391 }
2392 ctrl.dirty_mesh = false;
2393 }
2394
2395 // 2. Push to next node
2396 // FIXME this should be for the renderer of edge, not the node, since
2397 // geometries can have gpu buffers
2398 auto rendered_node = pnode->renderedNodes.find(&renderer);
2399 SCORE_ASSERT(rendered_node != pnode->renderedNodes.end());
2400
2401 auto it = std::find(
2402 edge_sink->node->input.begin(), edge_sink->node->input.end(), edge_sink);
2403 SCORE_ASSERT(it != edge_sink->node->input.end());
2404 int n = it - edge_sink->node->input.begin();
2405
2406 rendered_node->second->process(n, spc, edge.source);
2407
2408 // 3. Same for transform3d
2409
2410 if constexpr(requires { ctrl.transform; })
2411 {
2412 auto& sent = transforms[N];
2413 if(ctrl.dirty_transform)
2414 {
2415 sent.value.emplace();
2416 std::copy_n(ctrl.transform, std::ssize(ctrl.transform), sent.value->matrix);
2417 sent.receivers.clear();
2418 ctrl.dirty_transform = false;
2419 }
2420
2421 const std::pair<const score::gfx::Edge*, const score::gfx::NodeRenderer*>
2422 receiver{&edge, rendered_node->second};
2423 if(sent.value && !ossia::contains(sent.receivers, receiver))
2424 {
2425 sent.receivers.push_back(receiver);
2426 rendered_node->second->process(n, *sent.value);
2427 if(auto pnode = dynamic_cast<score::gfx::ProcessNode*>(edge_sink->node))
2428 pnode->process(n, *sent.value);
2429 }
2430 }
2431 }
2432 }
2433
2434 void upload(score::gfx::RenderList& renderer, auto& state, score::gfx::Edge& edge)
2435 {
2436 // FIXME we need something such as port_run_{pre,post}process for GPU nodes
2437 avnd::geometry_output_introspection<T>::for_all_n(
2438 avnd::get_outputs(state),
2439 [&](auto& field, auto pred) { this->upload(renderer, field, edge, pred); });
2440 }
2441
2442 // Lifecycle parity with the other *_outs storages. The geometry_spec
2443 // wrapper carries non-owning pointers + transform values, so release has
2444 // nothing to do; it exists so RHI handles added later have a hook.
2445 void release(score::gfx::RenderList&) noexcept { }
2446};
2447
2448
2449template<typename T>
2450 requires (avnd::geometry_output_introspection<T>::size == 0)
2451struct geometry_outputs_storage<T>
2452{
2453 static void upload(auto&&...)
2454 {
2455
2456 }
2457 static void release(auto&&...) noexcept { }
2458};
2459
2460// Scene output support (Crousti-side pending promotion to avendish).
2461// The `scene_port` concept and `scene_dirt_flags` live in SceneConcepts.hpp
2462// so the port-creation visitor in ProcessModelPortInit.hpp can reuse them.
2463
2464template <typename Field>
2465using is_scene_port_t = boost::mp11::mp_bool<scene_port<Field>>;
2466
2467template <typename T>
2468using scene_output_introspection =
2469 avnd::predicate_introspection<typename avnd::outputs_type<T>::type, is_scene_port_t>;
2470
2471template <typename T>
2472using scene_input_introspection =
2473 avnd::predicate_introspection<typename avnd::inputs_type<T>::type, is_scene_port_t>;
2474
2475// Scene input transport: NodeRenderer::process(port, scene_spec, source)
2476// keeps one scene per (port, source) and merges them all into `this->scene`.
2477// A node with a single scene input gets that merged scene. With several, each
2478// field receives only the scenes that arrived on its own port, merged across
2479// sources; the merge is memoized per port on the (state, version) set so an
2480// unchanged input keeps its scene_state identity.
2481template <typename T>
2482struct scene_inputs_storage;
2483
2484template <typename T>
2485 requires(scene_input_introspection<T>::size > 0)
2486struct scene_inputs_storage<T>
2487{
2488 struct port_cache
2489 {
2490 ossia::small_vector<std::pair<const ossia::scene_state*, int64_t>, 4> inputs;
2491 ossia::scene_spec merged;
2492 };
2493 port_cache m_cache[scene_input_introspection<T>::size];
2494
2495 static ossia::scene_spec
2496 sceneOnPort(const score::gfx::NodeRenderer& renderer, int port, port_cache& cache)
2497 {
2498 ossia::small_vector<std::pair<const ossia::scene_state*, int64_t>, 4> sig;
2499 ossia::small_vector<ossia::scene_spec, 4> scenes;
2500 renderer.forEachSceneOnPort(port, [&](const ossia::scene_spec& s) {
2501 sig.push_back({s.state.get(), s.state->version});
2502 scenes.push_back(s);
2503 });
2504
2505 if(scenes.empty())
2506 {
2507 cache = {};
2508 return {};
2509 }
2510 if(scenes.size() == 1)
2511 {
2512 cache = {};
2513 return scenes[0];
2514 }
2515 if(sig == cache.inputs && cache.merged.state)
2516 return cache.merged;
2517
2518 cache.inputs.assign(sig.begin(), sig.end());
2519 cache.merged = ossia::merge_scenes(
2520 std::span<const ossia::scene_spec>{scenes.data(), scenes.size()});
2521 return cache.merged;
2522 }
2523
2524 void readInputScenes(const score::gfx::NodeRenderer& renderer, auto& state)
2525 {
2526 if constexpr(scene_input_introspection<T>::size == 1)
2527 {
2528 scene_input_introspection<T>::for_all(
2529 avnd::get_inputs<T>(state), [&](auto& field) { field.scene = renderer.scene; });
2530 return;
2531 }
2532 scene_input_introspection<T>::for_all_n2(
2533 avnd::get_inputs<T>(state),
2534 [&]<typename F, std::size_t K, std::size_t N>(
2535 F& field, avnd::predicate_index<K>, avnd::field_index<N>) {
2536 field.scene = sceneOnPort(renderer, int(N), m_cache[K]);
2537 });
2538 }
2539
2540 void release(score::gfx::RenderList&)
2541 {
2542 for(auto& c : m_cache)
2543 c = {};
2544 }
2545};
2546
2547template <typename T>
2548 requires(scene_input_introspection<T>::size == 0)
2549struct scene_inputs_storage<T>
2550{
2551 static void readInputScenes(auto&&...) { }
2552 static void release(auto&&...) { }
2553};
2554
2555template <typename T>
2556struct scene_outputs_storage;
2557
2558template <typename T>
2559 requires(scene_output_introspection<T>::size > 0)
2560struct scene_outputs_storage<T>
2561{
2562 template <scene_port Field, std::size_t N>
2563 void upload(
2564 score::gfx::RenderList& renderer, Field& ctrl, score::gfx::Edge& edge,
2565 avnd::predicate_index<N>)
2566 {
2567 // Publish the scene every frame rather than only when `ctrl.dirty` is
2568 // set: a once-only push gets overwritten by any other producer on the
2569 // same downstream inlet. Consumers short-circuit on shared_ptr
2570 // identity + version, so publishing every frame costs only refcount
2571 // bumps.
2572 if(!ctrl.scene.state)
2573 return;
2574
2575 auto* edge_sink = edge.sink;
2576 if(!edge_sink || !edge_sink->node)
2577 return;
2578
2579 auto rendered_node = edge_sink->node->renderedNodes.find(&renderer);
2580 if(rendered_node == edge_sink->node->renderedNodes.end())
2581 return;
2582
2583 auto it = std::find(
2584 edge_sink->node->input.begin(), edge_sink->node->input.end(), edge_sink);
2585 if(it == edge_sink->node->input.end())
2586 return;
2587 int n = it - edge_sink->node->input.begin();
2588
2589 // NodeRenderer::process(port, scene_spec, source_key) handles additive
2590 // merging across multiple producers converging on the same sink port
2591 // (keyed on the source edge's producer Port pointer), extracts a legacy
2592 // geometry_spec for downstream consumers that only understand geometry,
2593 // and sets sceneChanged=true.
2594 rendered_node->second->process(n, ctrl.scene, edge.source);
2595
2596 if constexpr(requires { ctrl.dirty; })
2597 ctrl.dirty = 0;
2598 }
2599
2600 void upload(score::gfx::RenderList& renderer, auto& state, score::gfx::Edge& edge)
2601 {
2602 scene_output_introspection<T>::for_all_n(
2603 avnd::get_outputs(state),
2604 [&](auto& field, auto pred) { this->upload(renderer, field, edge, pred); });
2605 }
2606
2607 // Lifecycle parity with texture_outputs_storage / buffer_outputs_storage:
2608 // the storage owns no QRhi resources (the scene_spec is a value-semantics
2609 // struct plus a shared_ptr to scene_state, both managed by their own
2610 // destructors), so release has nothing to do. It keeps CpuFilterNode /
2611 // CpuAnalysisNode releaseState symmetric across all storages, and gives
2612 // RHI handles added later a hook.
2613 void release(score::gfx::RenderList&) noexcept { }
2614};
2615
2616template <typename T>
2617 requires(scene_output_introspection<T>::size == 0)
2618struct scene_outputs_storage<T>
2619{
2620 static void upload(auto&&...) { }
2621 static void release(auto&&...) noexcept { }
2622};
2623
2624}
2625
2626#endif
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:115
std::vector< Port * > input
Input ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:144
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:155
virtual void process(Message &&msg)
Process a message from the execution engine.
Definition score-plugin-gfx/Gfx/Graph/Node.cpp:25
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:150
Common base class for most single-pass, simple nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:329
Renderer for a given node.
Definition NodeRenderer.hpp:11
Base class for sink nodes (QWindow, spout, syphon, NDI output, ...)
Definition OutputNode.hpp:56
Common base class for nodes that map to score processes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:266
List of nodes to be rendered to an output.
Definition RenderList.hpp:36
static void noteBufferLive(const QRhiBuffer *buf) noexcept
Definition RenderList.cpp:1178
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing (2D)
Definition RenderList.hpp:329
TreeNode< DeviceExplorerNode > Node
Definition DeviceNode.hpp:74
void settle(::State::Address &a, const score::DocumentContext &ctx)
Definition ScriptableReference.cpp:121
Definition Controls.hpp:31
void bind_worker(Object &obj, std::weak_ptr< Object > obj_wp, Delivery deliver)
Definition WorkerBinding.hpp:31
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag, int multiViewCount)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:1707
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples, bool depth, bool samplableDepth)
Create a render target from a texture.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:86
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:1065
QByteArray bytesInPublishedRowOrder(const QRhi &rhi, QByteArray bytes, int rows)
Definition RowOrder.hpp:47
QRhiBuffer::UsageFlags compatibleBufferUsage(QRhi &rhi, QRhiBuffer::UsageFlags usage) noexcept
Drop a StorageBuffer usage the backend cannot actually honour.
Definition RenderState.hpp:241
Base toolkit upon which the software is built.
Definition Application.cpp:85
STL namespace.
Definition DocumentContext.hpp:18
Definition Mesh.hpp:18
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:236
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:60
Definition OutputNode.hpp:17
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:215
score::gfx::Node * node
Parent node of the port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:217
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:71
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:169
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:307