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>
18template <
typename Node_T>
19using ComputeNodeBaseType = std::conditional_t<
20 avnd::gpu_image_output_introspection<Node_T>::size != 0, CustomGpuNodeBase,
21 CustomGpuOutputNodeBase>;
22template <
typename Node_T>
23using ComputeRendererBaseType = std::conditional_t<
27template <
typename Node_T>
28struct 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 initGfxPorts<Node_T>(
this, this->input, this->output);
38 using layout =
typename Node_T::layout;
39 static constexpr auto lay = layout{};
41 gpp::qrhi::generate_shaders gen;
43 = QString::fromStdString(gen.compute_shader(lay) + Node_T{}.compute().data());
50template <
typename Node_T>
51struct GpuComputeRenderer final : ComputeRendererBaseType<Node_T>
53 using texture_inputs = avnd::gpu_image_input_introspection<Node_T>;
54 using texture_outputs = avnd::gpu_image_output_introspection<Node_T>;
55 const GpuComputeNode<Node_T>& parent;
58 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
61 ossia::time_value m_last_time{-1};
63 QRhiShaderResourceBindings* m_srb{};
64 QRhiComputePipeline* m_pipeline{};
66 bool m_createdPipeline{};
70 ossia::flat_map<int, QRhiBuffer*> createdUbos;
71 ossia::flat_map<int, QRhiTexture*> createdTexs;
73#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
74 std::vector<QRhiBufferReadbackResult*> bufReadbacks;
75 void addReadback(QRhiBufferReadbackResult* r) { bufReadbacks.push_back(r); }
78 std::vector<QRhiReadbackResult*> texReadbacks;
79 void addReadback(QRhiReadbackResult* r) { texReadbacks.push_back(r); }
80 GpuComputeRenderer(
const GpuComputeNode<Node_T>& p)
81 : ComputeRendererBaseType<Node_T>{}
84 prepareNewState(state, parent);
90 auto it = m_rts.find(&p);
91 SCORE_ASSERT(it != m_rts.end());
95 QRhiTexture* createInput(
98 auto port = parent.input[k];
99 static constexpr auto flags
100 = QRhiTexture::RenderTarget | QRhiTexture::UsedWithLoadStore;
101 auto texture = renderer.
state.rhi->newTexture(fmt, size, 1, flags);
102 SCORE_ASSERT(texture->create());
108 template <
typename F>
111 static constexpr auto bindingStages = QRhiShaderResourceBinding::ComputeStage;
112 if constexpr(
requires { F::ubo; })
114 auto it = createdUbos.find(F::binding());
115 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
116 return QRhiShaderResourceBinding::uniformBuffer(
117 F::binding(), bindingStages, buffer);
119 else if constexpr(
requires { F::image2D; })
121 auto tex_it = createdTexs.find(F::binding());
123 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
126 requires { F::load; } &&
requires { F::store; })
127 return QRhiShaderResourceBinding::imageLoadStore(
128 F::binding(), bindingStages, tex, 0);
129 else if constexpr(
requires { F::readonly; })
130 return QRhiShaderResourceBinding::imageLoad(F::binding(), bindingStages, tex, 0);
131 else if constexpr(
requires { F::writeonly; })
132 return QRhiShaderResourceBinding::imageStore(
133 F::binding(), bindingStages, tex, 0);
135 static_assert(F::load || F::store);
137 else if constexpr(
requires { F::buffer; })
139 auto it = createdUbos.find(F::binding());
140 QRhiBuffer* buf = it != createdUbos.end() ? it->second :
nullptr;
143 requires { F::load; } &&
requires { F::store; })
144 return QRhiShaderResourceBinding::bufferLoadStore(
145 F::binding(), bindingStages, buf);
146 else if constexpr(
requires { F::load; })
147 return QRhiShaderResourceBinding::bufferLoad(F::binding(), bindingStages, buf);
148 else if constexpr(
requires { F::store; })
149 return QRhiShaderResourceBinding::bufferStore(F::binding(), bindingStages, buf);
151 static_assert(F::load || F::store);
155 static_assert(F::nope);
162 auto& rhi = *renderer.
state.rhi;
164 auto srb = rhi.newShaderResourceBindings();
167 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
169 using bindings_type =
decltype(Node_T::layout::bindings);
170 boost::pfr::for_each_field(
171 bindings_type{}, [&](
auto f) { bindings.push_back(initBinding(renderer, f)); });
173 srb->setBindings(bindings.begin(), bindings.end());
179 auto& rhi = *renderer.
state.rhi;
180 auto compute = rhi.newComputePipeline();
182 compute->setShaderStage(QRhiShaderStage(QRhiShaderStage::Compute, cs));
192 template <std::
size_t Idx,
typename F>
193 requires avnd::image_port<F>
196 using bindings_type =
decltype(Node_T::layout::bindings);
197 using image_type = std::decay_t<
decltype(bindings_type{}.*F::image())>;
198 auto tex = createInput(
199 renderer, sampler_k++, gpp::qrhi::textureFormat<image_type>(),
200 renderer.
state.renderSize);
202 using sampler_type =
typename avnd::member_reflection<F::image()>::member_type;
203 createdTexs[sampler_type::binding()] = tex;
206 template <std::
size_t Idx,
typename F>
207 requires avnd::uniform_port<F>
210 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
213 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
216 auto ubo = renderer.
state.rhi->newBuffer(
217 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
220 createdUbos[ubo_type::binding()] = ubo;
225 if constexpr(
requires { state.prepare(); })
227 parent.processControlIn(
228 *
this, state, m_last_message, this->parent.last_message, this->parent.m_ctx);
233 avnd::input_introspection<Node_T>::for_all(
234 [
this, &renderer](
auto f) { init_input(renderer, f); });
236 m_srb = initBindings(renderer);
237 m_pipeline = createComputePipeline(renderer);
238 m_pipeline->setShaderResourceBindings(m_srb);
241 if constexpr(!
requires { &Node_T::update; })
243 SCORE_ASSERT(m_srb->create());
244 SCORE_ASSERT(m_pipeline->create());
245 m_createdPipeline =
true;
249 std::vector<QRhiShaderResourceBinding> tmp;
253 avnd::gpu_uniform_introspection<Node_T>::for_all(
254 avnd::get_inputs<Node_T>(state), [&]<avnd::uniform_port F>(
const F& t) {
256 typename avnd::member_reflection<F::uniform()>::member_type;
257 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
259 auto ubo = this->createdUbos.at(ubo_type::binding());
261 static constexpr int offset = gpp::std140_offset<F::uniform()>();
262 static constexpr int size =
sizeof(uniform_type::value);
263 res.updateDynamicBuffer(ubo, offset, size, &t.value);
270 if constexpr(
requires { &Node_T::update; })
272 bool srb_touched{
false};
273 tmp.assign(m_srb->cbeginBindings(), m_srb->cendBindings());
274 for(
auto& promise : state.update())
276 using ret_type =
decltype(promise.feedback_value);
277 gpp::qrhi::handle_update<GpuComputeRenderer, ret_type> handler{
278 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
279 promise.feedback_value = visit(handler, promise.current_command);
284 if(m_createdPipeline)
286 m_srb->setBindings(tmp.begin(), tmp.end());
313 if(!m_createdPipeline)
315 SCORE_ASSERT(m_srb->create());
316 SCORE_ASSERT(m_pipeline->create());
317 m_createdPipeline =
true;
325 m_createdPipeline =
false;
328 if constexpr(
requires { &Node_T::release; })
330 for(
auto& promise : state.release())
332 gpp::qrhi::handle_release handler{*r.
state.rhi};
333 visit(handler, promise.current_command);
339 for(
auto& [
id, tex] : this->createdTexs)
341 this->createdTexs.clear();
344 for(
auto& [
id, ubo] : this->createdUbos)
346 this->createdUbos.clear();
356 m_srb->deleteLater();
358 m_pipeline->deleteLater();
360 m_pipeline =
nullptr;
362 m_createdPipeline =
false;
370 QRhiResourceUpdateBatch*& res)
379 parent.processControlIn(
380 *
this, this->state, m_last_message, this->parent.last_message,
385 SCORE_ASSERT(this->m_pipeline);
386 SCORE_ASSERT(this->m_pipeline->shaderResourceBindings());
387 for(
auto& promise : this->state.dispatch())
389 using ret_type =
decltype(promise.feedback_value);
390 gpp::qrhi::handle_dispatch<GpuComputeRenderer, ret_type> handler{
391 *
this, *renderer.
state.rhi, cb, res, *this->m_pipeline};
392 promise.feedback_value = visit(handler, promise.current_command);
397#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
398 for(
auto rb : this->bufReadbacks)
400 this->bufReadbacks.clear();
402 for(
auto rb : this->texReadbacks)
404 this->texReadbacks.clear();
407 parent.processControlOut(this->state);
410 void runInitialPasses(
414 runCompute(renderer, cb, res);
424template <
typename Node_T>
428 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:112
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