Skip to content

Commit

Permalink
[avnd] Support worker api in GPU nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Mar 30, 2024
1 parent cdcf659 commit d56ed4f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 77 deletions.
16 changes: 1 addition & 15 deletions src/plugins/score-plugin-avnd/Crousti/CpuAnalysisNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,7 @@ struct GfxRenderer<Node_T> final : score::gfx::OutputNodeRenderer
});
}

// Apply the controls
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
auto& mess = this->parent.last_message;

if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
}
}
});
parent.processControlIn(state, this->parent.last_message);

// Run the processor
state();
Expand All @@ -158,7 +145,6 @@ struct GfxRenderer<Node_T> final : score::gfx::OutputNodeRenderer
}
};


template <typename Node_T>
requires(
avnd::texture_input_introspection<Node_T>::size > 0
Expand Down
18 changes: 3 additions & 15 deletions src/plugins/score-plugin-avnd/Crousti/CpuFilterNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,7 @@ struct GfxRenderer<Node_T> final : score::gfx::GenericNodeRenderer
});
}

// Apply the controls
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
auto& mess = this->parent.last_message;

if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
}
}
});
parent.processControlIn(state, this->parent.last_message);

// Run the processor
state();
Expand All @@ -289,12 +276,13 @@ struct GfxRenderer<Node_T> final : score::gfx::GenericNodeRenderer
}
};


template <typename Node_T>
requires(avnd::texture_input_introspection<Node_T>::size > 0
&& avnd::texture_output_introspection<Node_T>::size > 0)
struct GfxNode<Node_T> final
: CustomGfxNodeBase
, GpuWorker
, GpuControlIns
, GpuControlOuts
{
oscr::ProcessModel<Node_T>& processModel;
Expand Down
18 changes: 3 additions & 15 deletions src/plugins/score-plugin-avnd/Crousti/CpuGeneratorNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,7 @@ struct GfxRenderer<Node_T> final : score::gfx::GenericNodeRenderer
}
m_last_time = parent.last_message.token.date;

// Apply the controls
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
auto& mess = this->parent.last_message;

if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
}
}
});
parent.processControlIn(state, this->parent.last_message);

// Run the processor
state();
Expand All @@ -197,12 +184,13 @@ struct GfxRenderer<Node_T> final : score::gfx::GenericNodeRenderer
}
};


template <typename Node_T>
requires(avnd::texture_input_introspection<Node_T>::size == 0
&& avnd::texture_output_introspection<Node_T>::size > 0)
struct GfxNode<Node_T> final
: CustomGfxNodeBase
, GpuWorker
, GpuControlIns
, GpuControlOuts
{
oscr::ProcessModel<Node_T>& processModel;
Expand Down
7 changes: 2 additions & 5 deletions src/plugins/score-plugin-avnd/Crousti/Executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ class Executor final
if constexpr(requires { ptr->impl.effect; })
if constexpr(std::is_same_v<std::decay_t<decltype(ptr->impl.effect)>, Node>)
connect_message_bus(element, ctx, ptr->impl.effect);
connect_worker(element, ctx, ptr);
connect_worker(ctx, ptr->impl);

node->finish_init();

Expand Down Expand Up @@ -767,13 +767,10 @@ class Executor final
}
}

void connect_worker(
ProcessModel<Node>& element, const ::Execution::Context& ctx,
std::shared_ptr<safe_node<Node>>& ptr)
void connect_worker(const ::Execution::Context& ctx, avnd::effect_container<Node>& eff)
{
if constexpr(avnd::has_worker<Node>)
{
avnd::effect_container<Node>& eff = ptr->impl;
// Initialize the thread pool beforehand
auto& tq = score::TaskPool::instance();
using worker_type = decltype(eff.effect.worker);
Expand Down
1 change: 0 additions & 1 deletion src/plugins/score-plugin-avnd/Crousti/GfxNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ template <typename Node_T>
struct GfxNode;
template <typename Node_T>
struct GfxRenderer;

}
#endif
15 changes: 1 addition & 14 deletions src/plugins/score-plugin-avnd/Crousti/GpuComputeNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,7 @@ struct GpuComputeRenderer final : ComputeRendererBaseType<Node_T>
// m_last_time = parent.last_message.token.date;

// Apply the controls
{
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
auto& mess = this->parent.last_message;
if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
}
}
});
}
parent.processControlIn(this->state, this->parent.last_message);

// Run the compute shader
{
Expand Down
13 changes: 1 addition & 12 deletions src/plugins/score-plugin-avnd/Crousti/GpuNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,7 @@ struct CustomGpuRenderer final : score::gfx::NodeRenderer
// Apply the controls
for(auto& state : states)
{
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
auto& mess = this->parent.last_message;
if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
}
}
});
this->parent.processControlIn(state, this->parent.last_message);
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/plugins/score-plugin-avnd/Crousti/GpuUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,55 @@ struct generate_shaders

namespace oscr
{
struct GpuWorker
{
template <typename Node>
static void initWorker(Node& state) noexcept
{
if constexpr(avnd::has_worker<Node>)
{
using worker_type = decltype(state.worker);

state.worker.request = [&state]<typename... Args>(Args&&... f) {
using type_of_result = decltype(worker_type::work(std::forward<Args>(f)...));
if constexpr(std::is_void_v<type_of_result>)
{
worker_type::work(std::forward<Args>(f)...);
}
else
{
// If the worker returns a std::function, it
// is to be invoked back in the processor DSP thread
auto res = worker_type::work(std::forward<Args>(f)...);
if(!res)
return;
res(state);
}
};
}
}
};
struct GpuControlIns
{
template <typename Node_T>
static void processControlIn(Node_T& state, const score::gfx::Message& mess) noexcept
{
// Apply the controls
avnd::parameter_input_introspection<Node_T>::for_all_n2(
avnd::get_inputs<Node_T>(state),
[&](avnd::parameter auto& t, auto pred_index, auto field_index) {
if(mess.input.size() > field_index)
{
if(auto val = ossia::get_if<ossia::value>(&mess.input[field_index]))
{
oscr::from_ossia_value(t, *val, t.value);
if_possible(t.update(state));
}
}
});
}
};

struct GpuControlOuts
{
std::weak_ptr<Execution::ExecutionCommandQueue> queue;
Expand Down Expand Up @@ -866,6 +915,8 @@ struct SCORE_PLUGIN_AVND_EXPORT CustomGfxOutputNodeBase : score::gfx::OutputNode
};
struct CustomGpuNodeBase
: score::gfx::Node
, GpuWorker
, GpuControlIns
, GpuControlOuts
{
CustomGpuNodeBase(
Expand All @@ -883,6 +934,8 @@ struct CustomGpuNodeBase

struct SCORE_PLUGIN_AVND_EXPORT CustomGpuOutputNodeBase
: score::gfx::OutputNode
, GpuWorker
, GpuControlIns
, GpuControlOuts
{
CustomGpuOutputNodeBase(
Expand Down Expand Up @@ -919,6 +972,10 @@ struct SCORE_PLUGIN_AVND_EXPORT CustomGpuOutputNodeBase
template <typename Node_T, typename Node>
void prepareNewState(Node_T& eff, const Node& parent)
{
if constexpr(avnd::has_worker<Node_T>)
{
parent.initWorker(eff);
}
if constexpr(avnd::has_processor_to_gui_bus<Node_T>)
{
auto& process = parent.processModel;
Expand Down

0 comments on commit d56ed4f

Please sign in to comment.