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;
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);
98 SCORE_ASSERT(it != m_rts.end());
102 QRhiTexture* createInput(
105 auto& parent = node();
106 auto port = parent.input[k];
107 static constexpr auto flags
108 = QRhiTexture::RenderTarget | QRhiTexture::UsedWithLoadStore;
109 auto texture = renderer.
state.rhi->newTexture(fmt, size, 1, flags);
110 SCORE_ASSERT(texture->create());
116 template <
typename F>
119 static constexpr auto bindingStages = QRhiShaderResourceBinding::ComputeStage;
120 if constexpr(
requires { F::ubo; })
122 auto it = createdUbos.find(F::binding());
123 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
124 return QRhiShaderResourceBinding::uniformBuffer(
125 F::binding(), bindingStages, buffer);
127 else if constexpr(
requires { F::image2D; })
129 auto tex_it = createdTexs.find(F::binding());
131 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
134 requires { F::load; } &&
requires { F::store; })
135 return QRhiShaderResourceBinding::imageLoadStore(
136 F::binding(), bindingStages, tex, 0);
137 else if constexpr(
requires { F::readonly; })
138 return QRhiShaderResourceBinding::imageLoad(F::binding(), bindingStages, tex, 0);
139 else if constexpr(
requires { F::writeonly; })
140 return QRhiShaderResourceBinding::imageStore(
141 F::binding(), bindingStages, tex, 0);
143 static_assert(F::load || F::store);
145 else if constexpr(
requires { F::buffer; })
147 auto it = createdUbos.find(F::binding());
148 QRhiBuffer* buf = it != createdUbos.end() ? it->second :
nullptr;
151 requires { F::load; } &&
requires { F::store; })
152 return QRhiShaderResourceBinding::bufferLoadStore(
153 F::binding(), bindingStages, buf);
154 else if constexpr(
requires { F::load; })
155 return QRhiShaderResourceBinding::bufferLoad(F::binding(), bindingStages, buf);
156 else if constexpr(
requires { F::store; })
157 return QRhiShaderResourceBinding::bufferStore(F::binding(), bindingStages, buf);
159 static_assert(F::load || F::store);
163 static_assert(F::nope);
170 auto& rhi = *renderer.
state.rhi;
172 auto srb = rhi.newShaderResourceBindings();
175 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
177 using bindings_type =
decltype(Node_T::layout::bindings);
178 boost::pfr::for_each_field(
179 bindings_type{}, [&](
auto f) { bindings.push_back(initBinding(renderer, f)); });
181 srb->setBindings(bindings.begin(), bindings.end());
187 auto& parent = node();
188 auto& rhi = *renderer.
state.rhi;
189 auto compute = rhi.newComputePipeline();
191 compute->setShaderStage(QRhiShaderStage(QRhiShaderStage::Compute, cs));
201 template <std::
size_t Idx,
typename F>
202 requires avnd::image_port<F>
205 using bindings_type =
decltype(Node_T::layout::bindings);
206 using image_type = std::decay_t<
decltype(bindings_type{}.*F::image())>;
207 auto tex = createInput(
208 renderer, sampler_k++, gpp::qrhi::textureFormat<image_type>(),
209 renderer.
state.renderSize);
211 using sampler_type =
typename avnd::member_reflection<F::image()>::member_type;
212 createdTexs[sampler_type::binding()] = tex;
215 template <std::
size_t Idx,
typename F>
216 requires avnd::uniform_port<F>
219 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
222 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
225 auto ubo = renderer.
state.rhi->newBuffer(
226 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
227 ubo->setName(oscr::getUtf8Name<F>());
230 createdUbos[ubo_type::binding()] = ubo;
235 auto& parent = node();
236 if constexpr(
requires { state->prepare(); })
238 parent.processControlIn(
239 *
this, *state, m_last_message, parent.last_message, parent.m_ctx);
244 avnd::input_introspection<Node_T>::for_all(
245 [
this, &renderer](
auto f) { init_input(renderer, f); });
247 m_srb = initBindings(renderer);
248 m_pipeline = createComputePipeline(renderer);
249 m_pipeline->setShaderResourceBindings(m_srb);
252 if constexpr(!
requires { &Node_T::update; })
254 SCORE_ASSERT(m_srb->create());
255 SCORE_ASSERT(m_pipeline->create());
256 m_createdPipeline =
true;
260 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#define MSVC_BUGGY_STATIC_CONSTEXPR
276#define MSVC_BUGGY_STATIC_CONSTEXPR static constexpr
278 MSVC_BUGGY_STATIC_CONSTEXPR
int offset = gpp::std140_offset<F::uniform()>();
279 MSVC_BUGGY_STATIC_CONSTEXPR
int size =
sizeof(uniform_type::value);
280 res.updateDynamicBuffer(ubo, offset, size, &t.value);
287 if constexpr(
requires { &Node_T::update; })
289 bool srb_touched{
false};
290 tmp.assign(m_srb->cbeginBindings(), m_srb->cendBindings());
291 for(
auto& promise : state->update())
293 using ret_type =
decltype(promise.feedback_value);
295 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
296 promise.feedback_value = visit(handler, promise.current_command);
301 if(m_createdPipeline)
303 m_srb->setBindings(tmp.begin(), tmp.end());
330 if(!m_createdPipeline)
332 SCORE_ASSERT(m_srb->create());
333 SCORE_ASSERT(m_pipeline->create());
334 m_createdPipeline =
true;
342 m_createdPipeline =
false;
345 if constexpr(
requires { &Node_T::release; })
347 for(
auto& promise : state->release())
350 visit(handler, promise.current_command);
358 this->createdTexs.clear();
361 for(
auto& [
id, ubo] : this->createdUbos)
363 this->createdUbos.clear();
375 m_srb->deleteLater();
377 m_pipeline->deleteLater();
379 m_pipeline =
nullptr;
381 m_createdPipeline =
false;
389 QRhiResourceUpdateBatch*& res)
391 auto& parent = node();
399 parent.processControlIn(
400 *
this, *this->state, m_last_message, parent.last_message, parent.m_ctx);
404 SCORE_ASSERT(this->m_pipeline);
405 SCORE_ASSERT(this->m_pipeline->shaderResourceBindings());
406 for(
auto& promise : this->state->dispatch())
408 using ret_type =
decltype(promise.feedback_value);
410 *
this, *renderer.
state.rhi, cb, res, *this->m_pipeline};
411 promise.feedback_value = visit(handler, promise.current_command);
416#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
417 for(
auto rb : this->bufReadbacks)
419 this->bufReadbacks.clear();
421 for(
auto rb : this->texReadbacks)
423 this->texReadbacks.clear();
426 parent.processControlOut(*this->state);
429 void runInitialPasses(
433 runCompute(renderer, cb, res);
443template <
typename Node_T>
447 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(score::gfx::Port &p) const noexcept
Whether this list of rendering actions requires depth testing at all.
Definition RenderList.cpp:344
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:94
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing.
Definition RenderList.hpp:117
Definition Factories.hpp:19
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:421
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 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:71
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:50
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:53
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:116