3#include <avnd/introspection/gfx.hpp>
5#include <Process/ExecutionContext.hpp>
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>
19#include <score/tools/ThreadPool.hpp>
21#include <ossia/detail/small_flat_map.hpp>
23#include <ossia-qt/invoke.hpp>
25#include <QCoreApplication>
27#include <QtGui/private/qrhi_p.h>
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>
39#include <score_plugin_avnd_export.h>
46 void initWorker(
this auto& self, std::shared_ptr<T>& state)
noexcept
48 if constexpr(avnd::has_worker<T>)
50 auto ptr = QPointer{&self};
51 auto& tq = score::TaskPool::instance();
52 using worker_type =
decltype(state->worker);
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>)
60 worker_type::work(std::forward<
decltype(ff)>(ff)...);
66 auto res = worker_type::work(std::forward<
decltype(ff)>(ff)...);
71 QCoreApplication::instance(),
72 [res = std::move(res), wk_state, ptr]()
mutable {
74 if(
auto state = wk_state.lock())
84template <
typename GpuNodeRenderer,
typename Node>
93 bool can_process_message(std::size_t N)
95 if(mess.input.size() <= N)
98 if(prev_mess.input.size() == mess.input.size())
100 auto& prev = prev_mess.input[N];
101 auto& next = mess.input[N];
102 if(prev.index() == 1 && next.index() == 1)
104 if(ossia::get<ossia::value>(prev) == ossia::get<ossia::value>(next))
113 void operator()(avnd::parameter
auto& t,
auto field_index)
115 if(!can_process_message(field_index))
118 if(
auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
120 oscr::from_ossia_value(t, *val, t.value);
121 if_possible(t.update(state));
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)
130 using node_type = std::remove_cvref_t<
decltype(gpu.node())>;
131 using file_ports = avnd::raw_file_input_introspection<Node>;
133 if(!can_process_message(field_index))
136 auto val = ossia::get_if<ossia::value>(&mess.input[field_index]);
140 static constexpr bool has_text =
requires {
decltype(Field::file)::text; };
141 static constexpr bool has_mmap =
requires {
decltype(Field::file)::mmap; };
144 if(
auto hdl = loadRawfile(*val, ctx, has_text, has_mmap))
146 static constexpr auto N = file_ports::field_index_to_index(NField);
147 if constexpr(avnd::port_can_process<Field>)
151 auto func = executePortPreprocess<Field>(*hdl);
152 const_cast<node_type&
>(gpu.node())
154 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
160 const_cast<node_type&
>(gpu.node())
162 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
168 template <avnd::buffer_port Field, std::
size_t NField>
169 void operator()(Field& t, avnd::field_index<NField> field_index)
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]);
176 node.process(NField, *val);
179 template <avnd::texture_port Field, std::
size_t NField>
180 void operator()(Field& t, avnd::field_index<NField> field_index)
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]);
187 node.process(NField, *val);
190 template <avnd::geometry_port Field, std::
size_t NField>
191 void operator()(Field& t, avnd::field_index<NField> field_index)
193 using node_type = std::remove_cvref_t<
decltype(gpu.node())>;
194 auto& node =
const_cast<node_type&
>(gpu.node());
199 void operator()(
auto& t,
auto field_index) =
delete;
204 template <
typename Self,
typename Node_T>
205 static void processControlIn(
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;
219 std::weak_ptr<Execution::ExecutionCommandQueue> queue;
220 Gfx::exec_controls control_outs;
224 template <
typename Node_T>
225 void processControlOut(Node_T& state)
const noexcept
227 if(!this->control_outs.empty())
229 auto q = this->queue.lock();
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;
249struct SCORE_PLUGIN_AVND_EXPORT GpuNodeElements
251 [[no_unique_address]] oscr::soundfile_storage<T> soundfiles;
253 [[no_unique_address]] oscr::midifile_storage<T> midifiles;
255#if defined(OSCR_HAS_MMAP_FILE_STORAGE)
256 [[no_unique_address]] oscr::raw_file_storage<T> rawfiles;
259 template <std::
size_t N, std::
size_t NField>
261 auto& state,
const std::shared_ptr<oscr::raw_file_data>& hdl,
262 avnd::predicate_index<N>, avnd::field_index<NField>)
265 state, hdl, avnd::predicate_index<N>{}, avnd::field_index<NField>{});
272 :
score::gfx::NodeModel{}
276 virtual ~CustomGfxNodeBase();
284 virtual ~CustomGfxOutputNodeBase();
289struct CustomGpuNodeBase
296 std::weak_ptr<Execution::ExecutionCommandQueue>&& q, Gfx::exec_controls&& ctls,
298 : GpuControlOuts{
std::move(q),
std::move(ctls)}
303 virtual ~CustomGpuNodeBase() =
default;
306 QString vertex, fragment, compute;
311struct SCORE_PLUGIN_AVND_EXPORT CustomGpuOutputNodeBase
317 CustomGpuOutputNodeBase(
318 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls&& ctls,
320 virtual ~CustomGpuOutputNodeBase();
323 std::weak_ptr<score::gfx::RenderList> m_renderer{};
324 std::shared_ptr<score::gfx::RenderState> m_renderState{};
325 std::function<void()> m_update;
327 QString vertex, fragment, compute;
332 void setRenderer(std::shared_ptr<score::gfx::RenderList>)
override;
335 void startRendering()
override;
336 void render()
override;
337 void stopRendering()
override;
338 bool canRender()
const override;
339 void onRendererChange()
override;
343 std::function<
void()> onUpdate, std::function<
void()> onResize)
override;
345 void destroyOutput()
override;
346 std::shared_ptr<score::gfx::RenderState> renderState()
const override;
348 Configuration configuration() const noexcept override;
351template <typename Node_T, typename Node>
352void prepareNewState(
std::shared_ptr<Node_T>& eff, const Node& parent)
354 if constexpr(avnd::has_worker<Node_T>)
356 parent.initWorker(eff);
358 if constexpr(avnd::has_processor_to_gui_bus<Node_T>)
360 auto& process = parent.processModel;
361 eff->send_message = [ptr = QPointer{&process}](
auto&& b)
mutable {
365 if(ptr && ptr->to_ui)
366 MessageBusSender{ptr->to_ui}(std::move(b));
373 avnd::init_controls(*eff);
375 if constexpr(avnd::can_prepare<Node_T>)
377 if constexpr(avnd::function_reflection<&Node_T::prepare>::count == 1)
379 using prepare_type = avnd::first_argument<&Node_T::prepare>;
381 if_possible(t.instance = parent.instance);
391struct port_to_type_enum
393 template <std::
size_t I, avnd::buffer_port F>
394 constexpr auto operator()(avnd::field_reflection<I, F> p)
396 return score::gfx::Types::Buffer;
399 template <std::
size_t I, avnd::cpu_texture_port F>
400 constexpr auto operator()(avnd::field_reflection<I, F> p)
402 using texture_type = std::remove_cvref_t<
decltype(F::texture)>;
403 return avnd::cpu_fixed_format_texture<texture_type> ? score::gfx::Types::Image
404 : score::gfx::Types::Buffer;
407 template <std::
size_t I, avnd::sampler_port F>
408 constexpr auto operator()(avnd::field_reflection<I, F> p)
410 return score::gfx::Types::Image;
412 template <std::
size_t I, avnd::image_port F>
413 constexpr auto operator()(avnd::field_reflection<I, F> p)
415 return score::gfx::Types::Image;
417 template <std::
size_t I, avnd::attachment_port F>
418 constexpr auto operator()(avnd::field_reflection<I, F> p)
420 return score::gfx::Types::Image;
423 template <std::
size_t I, avnd::geometry_port F>
424 constexpr auto operator()(avnd::field_reflection<I, F> p)
426 return score::gfx::Types::Geometry;
428 template <std::
size_t I, avnd::mono_audio_port F>
429 constexpr auto operator()(avnd::field_reflection<I, F> p)
431 return score::gfx::Types::Audio;
433 template <std::
size_t I, avnd::poly_audio_port F>
434 constexpr auto operator()(avnd::field_reflection<I, F> p)
436 return score::gfx::Types::Audio;
438 template <std::
size_t I, avnd::
int_parameter F>
439 constexpr auto operator()(avnd::field_reflection<I, F> p)
441 return score::gfx::Types::Int;
443 template <std::
size_t I, avnd::enum_parameter F>
444 constexpr auto operator()(avnd::field_reflection<I, F> p)
446 return score::gfx::Types::Int;
448 template <std::
size_t I, avnd::
float_parameter F>
449 constexpr auto operator()(avnd::field_reflection<I, F> p)
451 return score::gfx::Types::Float;
453 template <std::
size_t I, avnd::parameter F>
454 constexpr auto operator()(avnd::field_reflection<I, F> p)
456 using value_type = std::remove_cvref_t<
decltype(F::value)>;
458 if constexpr(std::is_aggregate_v<value_type>)
460 constexpr int sz = boost::pfr::tuple_size_v<value_type>;
461 if constexpr(sz == 2)
463 return score::gfx::Types::Vec2;
465 else if constexpr(sz == 3)
467 return score::gfx::Types::Vec3;
469 else if constexpr(sz == 4)
471 return score::gfx::Types::Vec4;
474 return score::gfx::Types::Empty;
476 template <std::
size_t I,
typename F>
477 constexpr auto operator()(avnd::field_reflection<I, F> p)
479 return score::gfx::Types::Empty;
483template <
typename Node_T>
484inline void initGfxPorts(
auto* self,
auto& input,
auto& output)
486 avnd::input_introspection<Node_T>::for_all(
487 [self, &input]<
typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
488 static constexpr auto type = port_to_type_enum{}(f);
491 avnd::output_introspection<Node_T>::for_all(
493 &output]<
typename Field, std::size_t I>(avnd::field_reflection<I, Field> f) {
494 static constexpr auto type = port_to_type_enum{}(f);
502 const auto& inputs = parent.
input;
506 for(
auto& edge : p->edges)
508 auto src_node = edge->source->node;
512 return src_renderer->bufferForOutput(*edge->source);
521static void readbackInputBuffer(
523 , QRhiResourceUpdateBatch& res
525 , QRhiBufferReadbackResult& readback
531 if(
auto buf = getInputBuffer(renderer, parent, port_index))
534 res.readBackBuffer(buf.handle, buf.byte_offset, buf.byte_size, &readback);
538static void recreateOutputBuffer(
542 const auto bytesize = avnd::get_bytesize(cpu_buf);
547 buf.handle = renderer.
state.rhi->newBuffer(
548 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
550 buf.handle->setName(
"GpuUtils::recreateOutputBuffer");
552 buf.byte_size = bytesize;
554 buf.handle->create();
558 cpu_buf.changed =
false;
562 else if(buf.handle->size() != bytesize)
564 buf.handle->destroy();
565 buf.handle->setSize(bytesize);
566 buf.handle->create();
567 buf.byte_size = bytesize;
571static void uploadOutputBuffer(
577 const auto bytesize = avnd::get_bytesize(cpu_buf);
578 recreateOutputBuffer(renderer, cpu_buf, res, rhi_buf);
580 &res, rhi_buf.handle, 0, cpu_buf.byte_size,
581 (
const char*)avnd::get_bytes(cpu_buf));
582 cpu_buf.changed =
false;
586static void uploadOutputBuffer(
590 rhi_buf.handle =
reinterpret_cast<QRhiBuffer*
>(gpu_buf.handle);
591 rhi_buf.byte_size = gpu_buf.byte_size;
592 rhi_buf.byte_offset = gpu_buf.byte_offset;
596struct geometry_inputs_storage;
598struct mesh_input_storage
600 std::vector<QRhiBufferReadbackResult> readbacks;
601 std::vector<QRhiBuffer*> buffers;
603struct geometry_input_storage
605 ossia::geometry_spec spec;
606 std::vector<mesh_input_storage> meshes;
610 requires(avnd::geometry_input_introspection<T>::size > 0)
611struct geometry_inputs_storage<T>
614 static_assert(avnd::geometry_input_introspection<T>::size == 1);
616 geometry_input_storage inputs[avnd::geometry_input_introspection<T>::size];
617 ossia::small_vector<QRhiBuffer*, 4> allocated;
619 void readInputGeometries(
626 avnd::geometry_input_introspection<T>::for_all_n(
627 avnd::get_inputs<T>(state),
628 [&]<
typename Field, std::size_t N>(Field& t, avnd::predicate_index<N> np) {
629 this->inputs[N].spec = spec;
630 this->inputs[N].meshes.resize(1);
633 auto& meshes = this->inputs[N].meshes[0];
635 oscr::meshes_from_ossia(
637 [&](
auto& write_buf,
int buffer_index,
void* data, int64_t bytesize) {
639 SCORE_ASSERT(buffer_index >= 0);
640 if(buffer_index < meshes.readbacks.size())
642 QRhiBuffer* handle = meshes.buffers[buffer_index];
643 write_buf.handle = handle;
644 write_buf.byte_size = handle->size();
646 }, [&](
auto& write_buf,
int buffer_index,
void* handle) {
648 SCORE_ASSERT(buffer_index >= 0);
649 if(buffer_index < meshes.readbacks.size())
652 auto& readback = meshes.readbacks[buffer_index].data;
653 write_buf.raw_data =
reinterpret_cast<unsigned char*
>(readback.data());
654 write_buf.byte_size = readback.size();
660 void inputAboutToFinish(
662 const ossia::geometry_spec& spec,
auto& state,
auto& parent)
664 avnd::geometry_input_introspection<T>::for_all_n2(
665 avnd::get_inputs<T>(state),
666 [&]<
typename Field, std::size_t N, std::size_t NField>(
667 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
668 this->inputs[N].spec = spec;
669 this->inputs[N].meshes.resize(1);
672 auto& meshes = this->inputs[N].meshes[0];
673 oscr::meshes_from_ossia(
675 [&](
auto& write_buf,
int buffer_index,
void* data, int64_t bytesize) {
677 if(meshes.buffers.size() <= buffer_index)
679 meshes.buffers.resize(buffer_index + 1);
680 meshes.readbacks.resize(buffer_index + 1);
682 auto buf = renderer.state.rhi->newBuffer(
683 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
685 buf->setName(oscr::getUtf8Name<T>() +
"::" + oscr::getUtf8Name(t));
687 allocated.push_back(buf);
688 meshes.buffers[buffer_index] = buf;
691 res->uploadStaticBuffer(meshes.buffers[buffer_index], 0, bytesize, data);
692 }, [&](
auto& write_buf,
int buffer_index,
void* handle) {
694 if(meshes.readbacks.size() <= buffer_index)
696 meshes.buffers.resize(buffer_index + 1);
697 meshes.readbacks.resize(buffer_index + 1);
700 meshes.readbacks[buffer_index] = {};
701 if(
auto buf =
static_cast<QRhiBuffer*
>(handle))
703 meshes.buffers[buffer_index] = buf;
704 res->readBackBuffer(buf, 0, buf->size(), &meshes.readbacks[buffer_index]);
708 meshes.buffers[buffer_index] = {};
709 meshes.readbacks[buffer_index] = {};
717 for(
auto& buf : allocated)
718 renderer.releaseBuffer(buf);
724 requires(avnd::geometry_input_introspection<T>::size == 0)
725struct geometry_inputs_storage<T>
727 static void readInputBuffers(
auto&&...) { }
729 static void inputAboutToFinish(
auto&&...) { }
733struct buffer_inputs_storage;
736 requires (avnd::buffer_input_introspection<T>::size > 0)
737struct buffer_inputs_storage<T>
740 QRhiBufferReadbackResult
741 m_readbacks[avnd::cpu_buffer_input_introspection<T>::size + 1];
744 void readInputBuffers(
747 if constexpr(avnd::cpu_buffer_input_introspection<T>::size > 0)
752 avnd::cpu_buffer_input_introspection<T>::for_all_n(
753 avnd::get_inputs<T>(state),
754 [&]<
typename Field, std::size_t N>
755 (Field& t, avnd::predicate_index<N> np)
757 auto& readback = m_readbacks[N].data;
758 t.buffer.raw_data =
reinterpret_cast<unsigned char*
>(readback.data());
759 t.buffer.byte_size = readback.size();
760 t.buffer.byte_offset = 0;
761 t.buffer.changed =
true;
765 if constexpr(avnd::gpu_buffer_input_introspection<T>::size > 0)
770 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
771 avnd::get_inputs<T>(state),
772 [&]<
typename Field, std::size_t N, std::size_t NField>(
773 Field& t, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
776 buf = getInputBuffer(renderer, parent, nf);
779 t.buffer.handle = buf.handle;
780 t.buffer.byte_size = buf.byte_size;
781 t.buffer.byte_offset = buf.byte_offset;
787 void inputAboutToFinish(
789 QRhiResourceUpdateBatch*& res,
793 avnd::cpu_buffer_input_introspection<T>::for_all_n2(
794 avnd::get_inputs<T>(state),
795 [&]<
typename Field, std::size_t N, std::size_t NField>
796 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
797 readbackInputBuffer(renderer, *res, parent, m_readbacks[N], nf);
799 avnd::gpu_buffer_input_introspection<T>::for_all_n2(
800 avnd::get_inputs<T>(state),
801 [&]<
typename Field, std::size_t N, std::size_t NField>
802 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
803 m_gpubufs[N] = getInputBuffer(renderer, parent, nf);
809 requires (avnd::buffer_input_introspection<T>::size == 0)
810struct buffer_inputs_storage<T>
812 static void readInputBuffers(
auto&&...)
817 static void inputAboutToFinish(
auto&&...)
829struct buffer_outputs_storage;
832 requires (avnd::buffer_output_introspection<T>::size > 0)
833struct buffer_outputs_storage<T>
835 std::pair<const score::gfx::Port*, MaybeOwnedBuffer>
836 m_buffers[avnd::buffer_output_introspection<T>::size];
838 QRhiResourceUpdateBatch* currentResourceUpdateBatch{};
840 template <
typename Field, std::
size_t N, std::
size_t NField>
841 requires avnd::cpu_buffer<std::decay_t<
decltype(Field::buffer)>>
844 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
846 auto& [gfx_port, buf] = m_buffers[N];
847 gfx_port = parent.
output[nf];
848 buf.handle = renderer.
state.rhi->newBuffer(
849 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer, 1);
850 buf.handle->setName(oscr::getUtf8Name<T>() +
"::" + oscr::getUtf8Name(port));
855 buf.handle->create();
858 = [
this, &renderer, &port](
const char* data, int64_t offset, int64_t bytesize) {
860 SCORE_ASSERT(currentResourceUpdateBatch);
861 auto& [gfx_port, buf] = m_buffers[N];
867 buf.handle = renderer.
state.rhi->newBuffer(
868 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
870 buf.handle->setName(oscr::getUtf8Name<T>() +
"::" + oscr::getUtf8Name(port));
872 buf.byte_size = bytesize;
875 buf.handle->create();
879 buf.handle = renderer.
state.rhi->newBuffer(
880 QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer,
882 buf.handle->setName(oscr::getUtf8Name<T>() +
"::" + oscr::getUtf8Name(port));
887 buf.handle->create();
891 else if(buf.handle->size() != bytesize)
893 buf.handle->destroy();
894 buf.handle->setSize(bytesize);
895 buf.handle->create();
896 buf.byte_size = bytesize;
900 currentResourceUpdateBatch, buf.handle, offset, bytesize, data);
904 template <
typename Field, std::
size_t N, std::
size_t NField>
905 requires avnd::gpu_buffer<std::decay_t<
decltype(Field::buffer)>>
908 avnd::predicate_index<N> np, avnd::field_index<NField> nf)
910 auto& [gfx_port, buf] = m_buffers[N];
911 gfx_port = parent.
output[nf];
912 buf.handle =
reinterpret_cast<QRhiBuffer*
>(port.buffer.handle);
913 buf.byte_size = port.buffer.byte_size;
914 buf.byte_offset = port.buffer.byte_offset;
921 avnd::buffer_output_introspection<T>::for_all_n2(
922 avnd::get_outputs<T>(state), [&]<
typename Field, std::size_t N, std::size_t NField>
923 (Field& port, avnd::predicate_index<N> np, avnd::field_index<NField> nf) {
924 SCORE_ASSERT(parent.
output.size() > nf);
925 SCORE_ASSERT(parent.
output[nf]->type == score::gfx::Types::Buffer);
926 using buffer_type = std::decay_t<
decltype(port.buffer)>;
928 if constexpr(avnd::cpu_raw_buffer<buffer_type> &&
requires {
929 port.buffer.upload(
nullptr, 0, 0);
932 createOutput(renderer, parent, port, np, nf);
934 else if constexpr(avnd::gpu_buffer<buffer_type>)
936 createOutput(renderer, parent, port, np, nf);
941 static_assert(std::is_same_v<T, void>,
"unsupported");
946 void prepareUpload(QRhiResourceUpdateBatch& res)
948 currentResourceUpdateBatch = &res;
953 avnd::buffer_output_introspection<T>::for_all_n(
954 avnd::get_outputs<T>(state), [&]<std::size_t N>(
auto& t, avnd::predicate_index<N> idx) {
955 auto& [port, buf] = m_buffers[N];
956 uploadOutputBuffer(renderer, t.buffer, res, buf);
963 for(
auto& [p, buf] : m_buffers)
966 renderer.releaseBuffer(buf.handle);
967 buf.handle =
nullptr;
974 requires (avnd::buffer_output_introspection<T>::size == 0)
975struct buffer_outputs_storage<T>
977 static void init(
auto&&...)
982 static void prepareUpload(
auto&&...)
986 static void upload(
auto&&...)
990 static void release(
auto&&...)
996template <
typename Tex>
1000 auto& rhi = *renderer.
state.rhi;
1002 if(size.width() > 0 && size.height() > 0)
1004 texture = rhi.newTexture(
1005 gpp::qrhi::textureFormat(texture_spec), size, 1, QRhiTexture::Flag{});
1010 auto sampler = rhi.newSampler(
1011 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1012 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
1020struct texture_inputs_storage;
1023 requires (avnd::texture_input_introspection<T>::size > 0)
1024struct texture_inputs_storage<T>
1026 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
1029 QRhiReadbackResult m_readbacks[avnd::texture_input_introspection<T>::size];
1031 template <
typename Tex>
1036 static constexpr auto flags
1037 = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
1038 auto texture = renderer.
state.rhi->newTexture(
1039 gpp::qrhi::textureFormat(texture_spec), spec.size, 1, flags);
1040 SCORE_ASSERT(texture->create());
1048 avnd::cpu_texture_input_introspection<T>::for_all_n(
1049 avnd::get_inputs<T>(*self.state),
1050 [&]<
typename F, std::size_t K>(F& t, avnd::predicate_index<K>) {
1052 auto& parent = self.node();
1053 auto spec = parent.resolveRenderTargetSpecs(K, renderer);
1054 if constexpr(requires {
1059 spec.size.rwidth() = t.request_width;
1060 spec.size.rheight() = t.request_height;
1063 createInput(renderer, parent.
input[K], t.texture, spec);
1065 if constexpr(avnd::cpu_fixed_format_texture<
decltype(t.texture)>)
1067 t.texture.width = spec.size.width();
1068 t.texture.height = spec.size.height();
1073 void runInitialPasses(
auto& self, QRhi& rhi)
1079 avnd::cpu_texture_input_introspection<T>::for_all_n(
1080 avnd::get_inputs<T>(*self.state), [&]<std::size_t K>(
auto& t, avnd::predicate_index<K>) {
1081 oscr::loadInputTexture(rhi, m_readbacks, t.texture, K);
1089 for(
auto [port, rt] : m_rts)
1094 void inputAboutToFinish(
auto& parent,
const score::gfx::Port& p, QRhiResourceUpdateBatch*& res)
1096 const auto& inputs = parent.
input;
1097 auto index_of_port = ossia::find(inputs, &p) - inputs.begin();
1099 auto tex = m_rts[&p].texture;
1100 auto& readback = m_readbacks[index_of_port];
1102 res->readBackTexture(QRhiReadbackDescription{tex}, &readback);
1108 requires (avnd::texture_input_introspection<T>::size == 0)
1109struct texture_inputs_storage<T>
1111 static void init(
auto&&...) { }
1112 static void runInitialPasses(
auto&&...) { }
1113 static void release(
auto&&...) { }
1114 static void inputAboutToFinish(
auto&&...) { }
1119template <avnd::cpu_texture Tex>
1120static QRhiTexture* updateTexture(
auto& self,
score::gfx::RenderList& renderer,
int k,
const Tex& cpu_tex)
1122 auto& [sampler, texture] = self.m_samplers[k];
1125 auto sz = texture->pixelSize();
1126 if(cpu_tex.width == sz.width() && cpu_tex.height == sz.height())
1131 if(cpu_tex.width > 0 && cpu_tex.height > 0)
1133 QRhiTexture* oldtex = texture;
1134 QRhiTexture* newtex = renderer.
state.rhi->newTexture(
1135 gpp::qrhi::textureFormat(cpu_tex), QSize{cpu_tex.width, cpu_tex.height}, 1,
1136 QRhiTexture::Flag{});
1138 for(
auto& [edge, pass] : self.m_p)
1140 score::gfx::replaceTexture(*pass.srb, sampler, newtex);
1145 oldtex->deleteLater();
1152 for(
auto& [edge, pass] : self.m_p)
1154 score::gfx::replaceTexture(*pass.srb, sampler, &renderer.emptyTexture());
1160template <avnd::cpu_texture Tex>
1161static void uploadOutputTexture(
auto& self,
1163 QRhiResourceUpdateBatch* res)
1167 if(
auto texture = updateTexture(self, renderer, k, cpu_tex))
1170 = QByteArray::fromRawData((
const char*)cpu_tex.bytes, cpu_tex.bytesize());
1171 if constexpr(
requires { Tex::RGB; })
1175 const QByteArray rgb = buf;
1177 rgba.resize(cpu_tex.width * cpu_tex.height * 4);
1178 auto src = (
const unsigned char*)rgb.constData();
1179 auto dst = (
unsigned char*)rgba.data();
1180 for(
int rgb_byte = 0, rgba_byte = 0, N = rgb.size(); rgb_byte < N;)
1182 dst[rgba_byte + 0] = src[rgb_byte + 0];
1183 dst[rgba_byte + 1] = src[rgb_byte + 1];
1184 dst[rgba_byte + 2] = src[rgb_byte + 2];
1185 dst[rgba_byte + 3] = 255;
1194 QRhiTextureSubresourceUploadDescription sd(buf);
1195 QRhiTextureUploadDescription desc{QRhiTextureUploadEntry{0, 0, sd}};
1197 res->uploadTexture(texture, desc);
1200 cpu_tex.changed =
false;
1205static const constexpr auto generic_texgen_vs = R
"_(#version 450
1206layout(location = 0) in vec2 position;
1207layout(location = 1) in vec2 texcoord;
1209layout(binding=3) uniform sampler2D y_tex;
1210layout(location = 0) out vec2 v_texcoord;
1212layout(std140, binding = 0) uniform renderer_t {
1213 mat4 clipSpaceCorrMatrix;
1217out gl_PerVertex { vec4 gl_Position; };
1221#if defined(QSHADER_SPIRV) || defined(QSHADER_GLSL)
1222 v_texcoord = vec2(texcoord.x, 1. - texcoord.y);
1224 v_texcoord = texcoord;
1226 gl_Position = renderer.clipSpaceCorrMatrix * vec4(position.xy, 0.0, 1.);
1230static const constexpr auto generic_texgen_fs = R
"_(#version 450
1231layout(location = 0) in vec2 v_texcoord;
1232layout(location = 0) out vec4 fragColor;
1234layout(std140, binding = 0) uniform renderer_t {
1235mat4 clipSpaceCorrMatrix;
1239layout(binding=3) uniform sampler2D y_tex;
1243 fragColor = texture(y_tex, v_texcoord);
1248struct texture_outputs_storage;
1252 requires (avnd::texture_output_introspection<T>::size > 0)
1253struct texture_outputs_storage<T>
1258 self.defaultMeshInit(renderer, mesh, res);
1259 self.processUBOInit(renderer);
1263 std::tie(self.m_vertexS, self.m_fragmentS)
1266 avnd::cpu_texture_output_introspection<T>::for_all(
1267 avnd::get_outputs<T>(*self.state), [&](
auto& t) {
1268 self.m_samplers.push_back(
1269 createOutputTexture(renderer, t.texture, QSize{t.texture.width, t.texture.height}));
1272 self.defaultPassesInit(renderer, mesh);
1275 void runInitialPasses(
auto& self,
1277 QRhiResourceUpdateBatch*& res)
1279 avnd::cpu_texture_output_introspection<T>::for_all_n(
1280 avnd::get_outputs<T>(*self.state), [&]<std::size_t N>(
auto& t, avnd::predicate_index<N>) {
1281 uploadOutputTexture(self, renderer, N, t.texture, res);
1288 for(
auto& [sampl, texture] : self.m_samplers)
1291 texture->deleteLater();
1299 requires (avnd::texture_output_introspection<T>::size == 0)
1300struct texture_outputs_storage<T>
1306 static void runInitialPasses(
auto& self,
1308 QRhiResourceUpdateBatch*& res)
1317struct geometry_outputs_storage;
1320 requires (avnd::geometry_output_introspection<T>::size > 0)
1321struct geometry_outputs_storage<T>
1323 ossia::geometry_spec specs[avnd::geometry_output_introspection<T>::size];
1325 template <avnd::geometry_port Field>
1326 void reload_mesh(Field& ctrl, ossia::geometry_spec& spc)
1328 spc.meshes = std::make_shared<ossia::mesh_list>();
1329 auto& ossia_meshes = *spc.meshes;
1330 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
1332 ossia_meshes.meshes.resize(1);
1333 load_geometry(ctrl, ossia_meshes.meshes[0]);
1336 avnd::static_geometry_type<
decltype(Field::mesh)>
1337 || avnd::dynamic_geometry_type<
decltype(Field::mesh)>)
1339 ossia_meshes.meshes.resize(1);
1340 load_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
1344 load_geometry(ctrl, ossia_meshes);
1348 template <avnd::geometry_port Field, std::
size_t N>
1351 avnd::predicate_index<N>)
1353 auto edge_sink = edge.sink;
1354 if(
auto pnode = edge_sink->node)
1356 ossia::geometry_spec& spc = specs[N];
1362 reload_mesh(ctrl, spc);
1368 auto& ossia_meshes = *spc.meshes;
1370 bool any_need_reload =
false;
1371 bool any_need_upload =
false;
1372 if constexpr(avnd::static_geometry_type<Field> || avnd::dynamic_geometry_type<Field>)
1374 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
1375 auto [need_reload, need_upload]
1376 = update_geometry(ctrl, ossia_meshes.meshes[0]);
1377 any_need_reload = need_reload;
1378 any_need_upload = need_upload;
1381 avnd::static_geometry_type<
decltype(Field::mesh)>
1382 || avnd::dynamic_geometry_type<
decltype(Field::mesh)>)
1384 SCORE_ASSERT(ossia_meshes.meshes.size() == 1);
1385 auto [need_reload, need_upload]
1386 = update_geometry(ctrl.mesh, ossia_meshes.meshes[0]);
1387 any_need_reload = need_reload;
1388 any_need_upload = need_upload;
1392 auto [need_reload, need_upload] = update_geometry(ctrl, ossia_meshes);
1393 any_need_reload = need_reload;
1394 any_need_upload = need_upload;
1399 reload_mesh(ctrl, spc);
1403 ctrl.dirty_mesh =
false;
1409 auto rendered_node = pnode->renderedNodes.find(&renderer);
1410 SCORE_ASSERT(rendered_node != pnode->renderedNodes.end());
1412 auto it = std::find(
1413 edge_sink->node->input.begin(), edge_sink->node->input.end(), edge_sink);
1414 SCORE_ASSERT(it != edge_sink->node->input.end());
1415 int n = it - edge_sink->node->input.begin();
1417 rendered_node->second->process(n, spc);
1421 if constexpr(
requires { ctrl.transform; })
1423 if(ctrl.dirty_transform)
1425 ossia::transform3d transform;
1426 std::copy_n(ctrl.transform, std::ssize(ctrl.transform), transform.matrix);
1427 ctrl.dirty_transform =
false;
1430 pnode->process(n, transform);
1439 avnd::geometry_output_introspection<T>::for_all_n(
1440 avnd::get_outputs(state),
1441 [&](
auto& field,
auto pred) { this->upload(renderer, field, edge, pred); });
1447 requires (avnd::geometry_output_introspection<T>::size == 0)
1448struct geometry_outputs_storage<T>
1450 static void upload(
auto&&...)
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:75
std::vector< Port * > input
Input ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:104
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:110
Common base class for most single-pass, simple nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:204
Renderer for a given node.
Definition NodeRenderer.hpp:11
Base class for sink nodes (QWindow, spout, syphon, NDI output, ...)
Definition OutputNode.hpp:24
Common base class for nodes that map to score processes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:177
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
GraphicsApi
Available graphics APIs to use.
Definition RenderState.hpp:20
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:97
Definition DocumentContext.hpp:18
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:71
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:50
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:53
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:57
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:26