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,
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 std::shared_ptr<Node_T> state;
57 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
60 ossia::time_value m_last_time{-1};
62 QRhiShaderResourceBindings* m_srb{};
63 QRhiComputePipeline* m_pipeline{};
65 bool m_createdPipeline{};
69 ossia::flat_map<int, QRhiBuffer*> createdUbos;
70 ossia::flat_map<int, QRhiTexture*> createdTexs;
72#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
73 std::vector<QRhiBufferReadbackResult*> bufReadbacks;
74 void addReadback(QRhiBufferReadbackResult* r) { bufReadbacks.push_back(r); }
77 std::vector<QRhiReadbackResult*> texReadbacks;
78 void addReadback(QRhiReadbackResult* r) { texReadbacks.push_back(r); }
80 const GpuComputeNode<Node_T>& node() const noexcept
82 return static_cast<const GpuComputeNode<Node_T>&
>(score::gfx::NodeRenderer::node);
85 GpuComputeRenderer(
const GpuComputeNode<Node_T>& p)
86 : ComputeRendererBaseType<Node_T>{p}
87 , state{std::make_shared<Node_T>()}
89 prepareNewState<Node_T>(state, p);
95 auto it = m_rts.find(&p);
96 SCORE_ASSERT(it != m_rts.end());
100 QRhiTexture* createInput(
103 auto& parent = node();
104 auto port = parent.input[k];
105 static constexpr auto flags
106 = QRhiTexture::RenderTarget | QRhiTexture::UsedWithLoadStore;
107 auto texture = renderer.
state.rhi->newTexture(fmt, size, 1, flags);
108 SCORE_ASSERT(texture->create());
114 template <
typename F>
117 static constexpr auto bindingStages = QRhiShaderResourceBinding::ComputeStage;
118 if constexpr(
requires { F::ubo; })
120 auto it = createdUbos.find(F::binding());
121 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
122 return QRhiShaderResourceBinding::uniformBuffer(
123 F::binding(), bindingStages, buffer);
125 else if constexpr(
requires { F::image2D; })
127 auto tex_it = createdTexs.find(F::binding());
129 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
132 requires { F::load; } &&
requires { F::store; })
133 return QRhiShaderResourceBinding::imageLoadStore(
134 F::binding(), bindingStages, tex, 0);
135 else if constexpr(
requires { F::readonly; })
136 return QRhiShaderResourceBinding::imageLoad(F::binding(), bindingStages, tex, 0);
137 else if constexpr(
requires { F::writeonly; })
138 return QRhiShaderResourceBinding::imageStore(
139 F::binding(), bindingStages, tex, 0);
141 static_assert(F::load || F::store);
143 else if constexpr(
requires { F::buffer; })
145 auto it = createdUbos.find(F::binding());
146 QRhiBuffer* buf = it != createdUbos.end() ? it->second :
nullptr;
149 requires { F::load; } &&
requires { F::store; })
150 return QRhiShaderResourceBinding::bufferLoadStore(
151 F::binding(), bindingStages, buf);
152 else if constexpr(
requires { F::load; })
153 return QRhiShaderResourceBinding::bufferLoad(F::binding(), bindingStages, buf);
154 else if constexpr(
requires { F::store; })
155 return QRhiShaderResourceBinding::bufferStore(F::binding(), bindingStages, buf);
157 static_assert(F::load || F::store);
161 static_assert(F::nope);
168 auto& rhi = *renderer.
state.rhi;
170 auto srb = rhi.newShaderResourceBindings();
173 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
175 using bindings_type =
decltype(Node_T::layout::bindings);
176 boost::pfr::for_each_field(
177 bindings_type{}, [&](
auto f) { bindings.push_back(initBinding(renderer, f)); });
179 srb->setBindings(bindings.begin(), bindings.end());
185 auto& parent = node();
186 auto& rhi = *renderer.
state.rhi;
187 auto compute = rhi.newComputePipeline();
189 compute->setShaderStage(QRhiShaderStage(QRhiShaderStage::Compute, cs));
199 template <std::
size_t Idx,
typename F>
200 requires avnd::image_port<F>
203 using bindings_type =
decltype(Node_T::layout::bindings);
204 using image_type = std::decay_t<
decltype(bindings_type{}.*F::image())>;
205 auto tex = createInput(
206 renderer, sampler_k++, gpp::qrhi::textureFormat<image_type>(),
207 renderer.
state.renderSize);
209 using sampler_type =
typename avnd::member_reflection<F::image()>::member_type;
210 createdTexs[sampler_type::binding()] = tex;
213 template <std::
size_t Idx,
typename F>
214 requires avnd::uniform_port<F>
217 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
220 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
223 auto ubo = renderer.
state.rhi->newBuffer(
224 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
227 createdUbos[ubo_type::binding()] = ubo;
232 auto& parent = node();
233 if constexpr(
requires { state->prepare(); })
235 parent.processControlIn(
236 *
this, *state, m_last_message, parent.last_message, parent.m_ctx);
241 avnd::input_introspection<Node_T>::for_all(
242 [
this, &renderer](
auto f) { init_input(renderer, f); });
244 m_srb = initBindings(renderer);
245 m_pipeline = createComputePipeline(renderer);
246 m_pipeline->setShaderResourceBindings(m_srb);
249 if constexpr(!
requires { &Node_T::update; })
251 SCORE_ASSERT(m_srb->create());
252 SCORE_ASSERT(m_pipeline->create());
253 m_createdPipeline =
true;
257 std::vector<QRhiShaderResourceBinding> tmp;
263 avnd::gpu_uniform_introspection<Node_T>::for_all(
264 avnd::get_inputs<Node_T>(*state), [&]<avnd::uniform_port F>(
const F& t) {
266 typename avnd::member_reflection<F::uniform()>::member_type;
267 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
269 auto ubo = this->createdUbos.at(ubo_type::binding());
271#define MSVC_BUGGY_STATIC_CONSTEXPR
273#define MSVC_BUGGY_STATIC_CONSTEXPR static constexpr
275 MSVC_BUGGY_STATIC_CONSTEXPR
int offset = gpp::std140_offset<F::uniform()>();
276 MSVC_BUGGY_STATIC_CONSTEXPR
int size =
sizeof(uniform_type::value);
277 res.updateDynamicBuffer(ubo, offset, size, &t.value);
284 if constexpr(
requires { &Node_T::update; })
286 bool srb_touched{
false};
287 tmp.assign(m_srb->cbeginBindings(), m_srb->cendBindings());
288 for(
auto& promise : state->update())
290 using ret_type =
decltype(promise.feedback_value);
291 gpp::qrhi::handle_update<GpuComputeRenderer, ret_type> handler{
292 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
293 promise.feedback_value = visit(handler, promise.current_command);
298 if(m_createdPipeline)
300 m_srb->setBindings(tmp.begin(), tmp.end());
327 if(!m_createdPipeline)
329 SCORE_ASSERT(m_srb->create());
330 SCORE_ASSERT(m_pipeline->create());
331 m_createdPipeline =
true;
339 m_createdPipeline =
false;
342 if constexpr(
requires { &Node_T::release; })
344 for(
auto& promise : state->release())
346 gpp::qrhi::handle_release handler{*r.
state.rhi};
347 visit(handler, promise.current_command);
353 for(
auto& [
id, tex] : this->createdTexs)
355 this->createdTexs.clear();
358 for(
auto& [
id, ubo] : this->createdUbos)
360 this->createdUbos.clear();
372 m_srb->deleteLater();
374 m_pipeline->deleteLater();
376 m_pipeline =
nullptr;
378 m_createdPipeline =
false;
386 QRhiResourceUpdateBatch*& res)
388 auto& parent = node();
396 parent.processControlIn(
397 *
this, *this->state, m_last_message, parent.last_message, parent.m_ctx);
401 SCORE_ASSERT(this->m_pipeline);
402 SCORE_ASSERT(this->m_pipeline->shaderResourceBindings());
403 for(
auto& promise : this->state->dispatch())
405 using ret_type =
decltype(promise.feedback_value);
406 gpp::qrhi::handle_dispatch<GpuComputeRenderer, ret_type> handler{
407 *
this, *renderer.
state.rhi, cb, res, *this->m_pipeline};
408 promise.feedback_value = visit(handler, promise.current_command);
413#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
414 for(
auto rb : this->bufReadbacks)
416 this->bufReadbacks.clear();
418 for(
auto rb : this->texReadbacks)
420 this->texReadbacks.clear();
423 parent.processControlOut(*this->state);
426 void runInitialPasses(
430 runCompute(renderer, cb, res);
440template <
typename Node_T>
444 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
bool requiresDepth() const noexcept
Whether this list of rendering actions requires depth testing at all.
Definition RenderList.hpp:137
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:417
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
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:50
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