4#include <Process/ExecutionContext.hpp>
6#include <Crousti/Concepts.hpp>
7#include <Crousti/GpuUtils.hpp>
8#include <Crousti/Metadatas.hpp>
9#include <Gfx/GfxExecNode.hpp>
10#include <Gfx/Graph/NodeRenderer.hpp>
11#include <Gfx/Graph/RenderList.hpp>
12#include <Gfx/Graph/Uniforms.hpp>
14#include <avnd/binding/ossia/metadatas.hpp>
20template <
typename Node_T>
21using ComputeNodeBaseType = std::conditional_t<
22 avnd::gpu_image_output_introspection<Node_T>::size != 0, CustomGpuNodeBase,
23 CustomGpuOutputNodeBase>;
24template <
typename Node_T>
25using ComputeRendererBaseType = std::conditional_t<
29template <
typename Node_T>
30struct GpuComputeNode final : ComputeNodeBaseType<Node_T>
33 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
35 : ComputeNodeBaseType<Node_T>{std::move(q), std::move(ctls), ctx}
39 initGfxPorts<Node_T>(
this, this->input, this->output);
40 using layout =
typename Node_T::layout;
41 static constexpr auto lay = layout{};
45 = QString::fromStdString(gen.compute_shader(lay) + Node_T{}.compute().data());
52template <
typename Node_T>
53struct GpuComputeRenderer final : ComputeRendererBaseType<Node_T>
55 using texture_inputs = avnd::gpu_image_input_introspection<Node_T>;
56 using texture_outputs = avnd::gpu_image_output_introspection<Node_T>;
57 std::shared_ptr<Node_T> state;
58 GpuMessageState m_last_message{};
59 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
62 ossia::time_value m_last_time{-1};
64 QRhiShaderResourceBindings* m_srb{};
65 QRhiComputePipeline* m_pipeline{};
67 bool m_createdPipeline{};
71 ossia::flat_map<int, QRhiBuffer*> createdUbos;
72 ossia::flat_map<int, QRhiTexture*> createdTexs;
74#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
75 std::vector<QRhiBufferReadbackResult*> bufReadbacks;
76 void addReadback(QRhiBufferReadbackResult* r) { bufReadbacks.push_back(r); }
79 std::vector<QRhiReadbackResult*> texReadbacks;
80 void addReadback(QRhiReadbackResult* r) { texReadbacks.push_back(r); }
82 const GpuComputeNode<Node_T>& node() const noexcept
84 return static_cast<const GpuComputeNode<Node_T>&
>(score::gfx::NodeRenderer::node);
87 GpuComputeRenderer(
const GpuComputeNode<Node_T>& p)
88 : ComputeRendererBaseType<Node_T>{p}
89 , state{std::make_shared<Node_T>()}
91 prepareNewState<Node_T>(state, p);
97 auto it = m_rts.find(&p);
103 QRhiTexture* createInput(
106 auto& parent = node();
107 auto port = parent.input[k];
108 static constexpr auto flags
109 = QRhiTexture::RenderTarget | QRhiTexture::UsedWithLoadStore;
110 auto texture = renderer.state.rhi->newTexture(fmt, size, 1, flags);
111 SCORE_ASSERT(texture->create());
113 renderer.state, texture, renderer.samples(), renderer.requiresDepth(*port));
117 template <
typename F>
120 static constexpr auto bindingStages = QRhiShaderResourceBinding::ComputeStage;
121 if constexpr(
requires { F::ubo; })
123 auto it = createdUbos.find(F::binding());
124 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
125 return QRhiShaderResourceBinding::uniformBuffer(
126 F::binding(), bindingStages, buffer);
128 else if constexpr(
requires { F::image2D; })
130 auto tex_it = createdTexs.find(F::binding());
132 = tex_it != createdTexs.end() ? tex_it->second : &renderer.emptyTexture();
135 requires { F::load; } &&
requires { F::store; })
136 return QRhiShaderResourceBinding::imageLoadStore(
137 F::binding(), bindingStages, tex, 0);
138 else if constexpr(
requires { F::readonly; })
139 return QRhiShaderResourceBinding::imageLoad(F::binding(), bindingStages, tex, 0);
140 else if constexpr(
requires { F::writeonly; })
141 return QRhiShaderResourceBinding::imageStore(
142 F::binding(), bindingStages, tex, 0);
144 static_assert(F::load || F::store);
146 else if constexpr(
requires { F::buffer; })
148 auto it = createdUbos.find(F::binding());
149 QRhiBuffer* buf = it != createdUbos.end() ? it->second :
nullptr;
152 requires { F::load; } &&
requires { F::store; })
153 return QRhiShaderResourceBinding::bufferLoadStore(
154 F::binding(), bindingStages, buf);
155 else if constexpr(
requires { F::load; })
156 return QRhiShaderResourceBinding::bufferLoad(F::binding(), bindingStages, buf);
157 else if constexpr(
requires { F::store; })
158 return QRhiShaderResourceBinding::bufferStore(F::binding(), bindingStages, buf);
160 static_assert(F::load || F::store);
164 static_assert(F::nope);
171 auto& rhi = *renderer.state.rhi;
173 auto srb = rhi.newShaderResourceBindings();
176 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
178 using bindings_type =
decltype(Node_T::layout::bindings);
179 avnd::pfr::for_each_field(
180 bindings_type{}, [&](
auto f) { bindings.push_back(initBinding(renderer, f)); });
182 srb->setBindings(bindings.begin(), bindings.end());
188 auto& parent = node();
189 auto& rhi = *renderer.state.rhi;
190 auto compute = rhi.newComputePipeline();
192 compute->setShaderStage(QRhiShaderStage(QRhiShaderStage::Compute, cs));
202 template <std::
size_t Idx,
typename F>
203 requires avnd::image_port<F>
206 using bindings_type =
decltype(Node_T::layout::bindings);
207 using image_type = std::decay_t<
decltype(bindings_type{}.*F::image())>;
208 auto tex = createInput(
209 renderer, Idx, gpp::qrhi::textureFormat<image_type>(),
210 renderer.resolveInputRenderTargetSpecs(node(), Idx).size);
212 using sampler_type =
typename avnd::member_reflection<F::image()>::member_type;
213 createdTexs[sampler_type::binding()] = tex;
216 template <std::
size_t Idx,
typename F>
217 requires avnd::uniform_port<F>
220 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
223 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
226 auto ubo = renderer.state.rhi->newBuffer(
227 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
228 ubo->setName(oscr::getUtf8Name<F>());
231 createdUbos[ubo_type::binding()] = ubo;
256 auto& parent = node();
257 if constexpr(
requires { state->prepare(); })
259 parent.processControlIn(
260 *
this, *state, m_last_message, parent.last_message, parent.m_ctx);
265 avnd::input_introspection<Node_T>::for_all(
266 [
this, &renderer](
auto f) { init_input(renderer, f); });
268 m_srb = initBindings(renderer);
269 m_pipeline = createComputePipeline(renderer);
270 m_pipeline->setShaderResourceBindings(m_srb);
273 if constexpr(!
requires { &Node_T::update; })
275 SCORE_ASSERT(m_srb->create());
276 SCORE_ASSERT(m_pipeline->create());
277 m_createdPipeline =
true;
280 m_initialized =
true;
285 initState(renderer, res);
288 std::vector<QRhiShaderResourceBinding> tmp;
294 avnd::gpu_uniform_introspection<Node_T>::for_all(
295 avnd::get_inputs<Node_T>(*state), [&]<avnd::uniform_port F>(
const F& t) {
297 typename avnd::member_reflection<F::uniform()>::member_type;
298 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
300 auto ubo = this->createdUbos.at(ubo_type::binding());
302#define MSVC_BUGGY_STATIC_CONSTEXPR
304#define MSVC_BUGGY_STATIC_CONSTEXPR static constexpr
306 MSVC_BUGGY_STATIC_CONSTEXPR
int offset = gpp::std140_offset<F::uniform()>();
307 MSVC_BUGGY_STATIC_CONSTEXPR
int size =
sizeof(uniform_type::value);
308 res.updateDynamicBuffer(ubo, offset, size, &t.value);
315 if constexpr(
requires { &Node_T::update; })
317 bool srb_touched{
false};
318 tmp.assign(m_srb->cbeginBindings(), m_srb->cendBindings());
319 for(
auto& promise : state->update())
321 using ret_type =
decltype(promise.feedback_value);
323 *
this, *renderer.state.rhi, res, tmp, srb_touched};
324 promise.feedback_value = visit(handler, promise.current_command);
329 if(m_createdPipeline)
331 m_srb->setBindings(tmp.begin(), tmp.end());
332 if(m_createdPipeline && !m_srb->create())
333 qWarning(
"GpuComputeNode: SRB recreation failed");
360 if(!m_createdPipeline)
362 SCORE_ASSERT(m_srb->create());
363 SCORE_ASSERT(m_pipeline->create());
364 m_createdPipeline =
true;
375 m_createdPipeline =
false;
378 if constexpr(
requires { &Node_T::release; })
380 for(
auto& promise : state->release())
383 visit(handler, promise.current_command);
391 this->createdTexs.clear();
394 for(
auto& [
id, ubo] : this->createdUbos)
396 this->createdUbos.clear();
408 m_srb->deleteLater();
410 m_pipeline->deleteLater();
412 m_pipeline =
nullptr;
414 m_createdPipeline =
false;
418 m_initialized =
false;
428 QRhiResourceUpdateBatch*& res)
430 auto& parent = node();
438 parent.processControlIn(
439 *
this, *this->state, m_last_message, parent.last_message, parent.m_ctx);
440 parent.drainWorker(renderer.dateFromStepClock);
444 SCORE_ASSERT(this->m_pipeline);
445 SCORE_ASSERT(this->m_pipeline->shaderResourceBindings());
446 for(
auto& promise : this->state->dispatch())
448 using ret_type =
decltype(promise.feedback_value);
450 *
this, *renderer.state.rhi, cb, res, *this->m_pipeline};
451 promise.feedback_value = visit(handler, promise.current_command);
456#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
457 for(
auto rb : this->bufReadbacks)
459 this->bufReadbacks.clear();
461 for(
auto rb : this->texReadbacks)
463 this->texReadbacks.clear();
466 parent.processControlOut(*this->state, renderer, parent);
467 parent.clearControlIn(*this->state);
470 void runInitialPasses(
474 runCompute(renderer, cb, res);
484template <
typename Node_T>
488 return new GpuComputeRenderer<Node_T>{*
this};
Renderer for a given node.
Definition NodeRenderer.hpp:11
Definition OutputNode.hpp:38
List of nodes to be rendered to an output.
Definition RenderList.hpp:36
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:299
Definition Controls.hpp:30
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:1806
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
Definition GppShaders.hpp:10
Definition GppCoroutines.hpp:129
Definition GppCoroutines.hpp:92
Definition GppCoroutines.hpp:253
Definition DocumentContext.hpp:18
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:236
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:215
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:307