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>
18 template <
typename Node_T>
19 using ComputeNodeBaseType = std::conditional_t<
20 avnd::gpu_image_output_introspection<Node_T>::size != 0, CustomGpuNodeBase,
21 CustomGpuOutputNodeBase>;
22 template <
typename Node_T>
23 using ComputeRendererBaseType = std::conditional_t<
27 template <
typename Node_T>
28 struct GpuComputeNode final : ComputeNodeBaseType<Node_T>
31 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
int id,
33 : ComputeNodeBaseType<Node_T>{std::move(q), std::move(ctls), ctx}
37 using texture_inputs = avnd::gpu_image_input_introspection<Node_T>;
38 using texture_outputs = avnd::gpu_image_output_introspection<Node_T>;
40 for(std::size_t i = 0; i < texture_inputs::size; i++)
42 this->input.push_back(
45 for(std::size_t i = 0; i < texture_outputs::size; i++)
47 this->output.push_back(
51 using layout =
typename Node_T::layout;
52 static constexpr
auto lay = layout{};
54 gpp::qrhi::generate_shaders gen;
56 = QString::fromStdString(gen.compute_shader(lay) + Node_T{}.compute().data());
63 template <
typename Node_T>
64 struct GpuComputeRenderer final : ComputeRendererBaseType<Node_T>
66 using texture_inputs = avnd::gpu_image_input_introspection<Node_T>;
67 using texture_outputs = avnd::gpu_image_output_introspection<Node_T>;
68 const GpuComputeNode<Node_T>& parent;
71 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
74 ossia::time_value m_last_time{-1};
76 QRhiShaderResourceBindings* m_srb{};
77 QRhiComputePipeline* m_pipeline{};
79 bool m_createdPipeline{};
83 ossia::flat_map<int, QRhiBuffer*> createdUbos;
84 ossia::flat_map<int, QRhiTexture*> createdTexs;
86 #if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
87 std::vector<QRhiBufferReadbackResult*> bufReadbacks;
88 void addReadback(QRhiBufferReadbackResult* r) { bufReadbacks.push_back(r); }
91 std::vector<QRhiReadbackResult*> texReadbacks;
92 void addReadback(QRhiReadbackResult* r) { texReadbacks.push_back(r); }
93 GpuComputeRenderer(
const GpuComputeNode<Node_T>& p)
94 : ComputeRendererBaseType<Node_T>{}
97 prepareNewState(state, parent);
103 auto it = m_rts.find(&p);
104 SCORE_ASSERT(it != m_rts.end());
108 QRhiTexture* createInput(
111 auto port = parent.input[k];
112 static constexpr
auto flags
113 = QRhiTexture::RenderTarget | QRhiTexture::UsedWithLoadStore;
114 auto texture = renderer.
state.rhi->newTexture(fmt, size, 1, flags);
115 SCORE_ASSERT(texture->create());
121 template <
typename F>
124 static constexpr
auto bindingStages = QRhiShaderResourceBinding::ComputeStage;
125 if constexpr(requires { F::ubo; })
127 auto it = createdUbos.find(F::binding());
128 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
129 return QRhiShaderResourceBinding::uniformBuffer(
130 F::binding(), bindingStages, buffer);
132 else if constexpr(requires { F::image2D; })
134 auto tex_it = createdTexs.find(F::binding());
136 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
139 requires { F::load; } && requires { F::store; })
140 return QRhiShaderResourceBinding::imageLoadStore(
141 F::binding(), bindingStages, tex, 0);
142 else if constexpr(requires { F::readonly; })
143 return QRhiShaderResourceBinding::imageLoad(F::binding(), bindingStages, tex, 0);
144 else if constexpr(requires { F::writeonly; })
145 return QRhiShaderResourceBinding::imageStore(
146 F::binding(), bindingStages, tex, 0);
148 static_assert(F::load || F::store);
150 else if constexpr(requires { F::buffer; })
152 auto it = createdUbos.find(F::binding());
153 QRhiBuffer* buf = it != createdUbos.end() ? it->second :
nullptr;
156 requires { F::load; } && requires { F::store; })
157 return QRhiShaderResourceBinding::bufferLoadStore(
158 F::binding(), bindingStages, buf);
159 else if constexpr(requires { F::load; })
160 return QRhiShaderResourceBinding::bufferLoad(F::binding(), bindingStages, buf);
161 else if constexpr(requires { F::store; })
162 return QRhiShaderResourceBinding::bufferStore(F::binding(), bindingStages, buf);
164 static_assert(F::load || F::store);
168 static_assert(F::nope);
175 auto& rhi = *renderer.
state.rhi;
177 auto srb = rhi.newShaderResourceBindings();
180 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
182 using bindings_type = decltype(Node_T::layout::bindings);
183 boost::pfr::for_each_field(
184 bindings_type{}, [&](
auto f) { bindings.push_back(initBinding(renderer, f)); });
186 srb->setBindings(bindings.begin(), bindings.end());
192 auto& rhi = *renderer.
state.rhi;
193 auto compute = rhi.newComputePipeline();
195 compute->setShaderStage(QRhiShaderStage(QRhiShaderStage::Compute, cs));
205 template <std::
size_t Idx,
typename F>
206 requires avnd::image_port<F>
209 using bindings_type = decltype(Node_T::layout::bindings);
210 using image_type = std::decay_t<decltype(bindings_type{}.*F::image())>;
211 auto tex = createInput(
212 renderer, sampler_k++, gpp::qrhi::textureFormat<image_type>(),
213 renderer.
state.renderSize);
215 using sampler_type =
typename avnd::member_reflection<F::image()>::member_type;
216 createdTexs[sampler_type::binding()] = tex;
219 template <std::
size_t Idx,
typename F>
220 requires avnd::uniform_port<F>
223 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
226 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
229 auto ubo = renderer.
state.rhi->newBuffer(
230 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
233 createdUbos[ubo_type::binding()] = ubo;
238 if constexpr(requires { state.prepare(); })
240 parent.processControlIn(
241 *
this, state, m_last_message, this->parent.last_message, this->parent.m_ctx);
246 avnd::input_introspection<Node_T>::for_all(
247 [
this, &renderer](
auto f) { init_input(renderer, f); });
249 m_srb = initBindings(renderer);
250 m_pipeline = createComputePipeline(renderer);
251 m_pipeline->setShaderResourceBindings(m_srb);
254 if constexpr(!requires { &Node_T::update; })
256 SCORE_ASSERT(m_srb->create());
257 SCORE_ASSERT(m_pipeline->create());
258 m_createdPipeline =
true;
262 std::vector<QRhiShaderResourceBinding> tmp;
266 avnd::gpu_uniform_introspection<Node_T>::for_all(
267 avnd::get_inputs<Node_T>(state), [&]<avnd::uniform_port F>(
const F& t) {
269 typename avnd::member_reflection<F::uniform()>::member_type;
270 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
272 auto ubo = this->createdUbos.at(ubo_type::binding());
274 static constexpr
int offset = gpp::std140_offset<F::uniform()>();
275 static constexpr
int size =
sizeof(uniform_type::value);
276 res.updateDynamicBuffer(ubo, offset, size, &t.value);
283 if constexpr(requires { &Node_T::update; })
285 bool srb_touched{
false};
286 tmp.assign(m_srb->cbeginBindings(), m_srb->cendBindings());
287 for(
auto& promise : state.update())
289 using ret_type = decltype(promise.feedback_value);
290 gpp::qrhi::handle_update<GpuComputeRenderer, ret_type> handler{
291 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
292 promise.feedback_value = visit(handler, promise.current_command);
297 if(m_createdPipeline)
299 m_srb->setBindings(tmp.begin(), tmp.end());
326 if(!m_createdPipeline)
328 SCORE_ASSERT(m_srb->create());
329 SCORE_ASSERT(m_pipeline->create());
330 m_createdPipeline =
true;
338 m_createdPipeline =
false;
341 if constexpr(requires { &Node_T::release; })
343 for(
auto& promise : state.release())
345 gpp::qrhi::handle_release handler{*r.
state.rhi};
346 visit(handler, promise.current_command);
352 for(
auto& [
id, tex] : this->createdTexs)
354 this->createdTexs.clear();
357 for(
auto& [
id, ubo] : this->createdUbos)
359 this->createdUbos.clear();
369 m_srb->deleteLater();
371 m_pipeline->deleteLater();
373 m_pipeline =
nullptr;
375 m_createdPipeline =
false;
383 QRhiResourceUpdateBatch*& res)
392 parent.processControlIn(
393 *
this, this->state, m_last_message, this->parent.last_message,
398 SCORE_ASSERT(this->m_pipeline);
399 SCORE_ASSERT(this->m_pipeline->shaderResourceBindings());
400 for(
auto& promise : this->state.dispatch())
402 using ret_type = decltype(promise.feedback_value);
403 gpp::qrhi::handle_dispatch<GpuComputeRenderer, ret_type> handler{
404 *
this, *renderer.
state.rhi, cb, res, *this->m_pipeline};
405 promise.feedback_value = visit(handler, promise.current_command);
410 #if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
411 for(
auto rb : this->bufReadbacks)
413 this->bufReadbacks.clear();
415 for(
auto rb : this->texReadbacks)
417 this->texReadbacks.clear();
420 parent.processControlOut(this->state);
423 void runInitialPasses(
427 runCompute(renderer, cb, res);
437 template <
typename Node_T>
441 return new GpuComputeRenderer<Node_T>{*
this};
Renderer for a given node.
Definition: NodeRenderer.hpp:11
Definition: OutputNode.hpp:11
List of nodes to be rendered to an output.
Definition: RenderList.hpp:19
RenderState & state
RenderState corresponding to this RenderList.
Definition: RenderList.hpp:89
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing.
Definition: RenderList.hpp:111
Definition: Factories.hpp:19
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:369
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples)
Create a render target from a texture.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:10
Definition: DocumentContext.hpp:18
Connection between two score::gfx::Port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:66
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:51
Port of a score::gfx::Node.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:48
Useful abstraction for storing all the data related to a render target.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:111