4#include <Crousti/Concepts.hpp>
5#include <Crousti/GpuUtils.hpp>
6#include <Crousti/Metadatas.hpp>
7#include <Gfx/Graph/NodeRenderer.hpp>
8#include <Gfx/Graph/RenderList.hpp>
9#include <Gfx/Graph/Uniforms.hpp>
16template <
typename Node_T>
19 using texture_inputs = avnd::texture_input_introspection<Node_T>;
20 using texture_outputs = avnd::texture_output_introspection<Node_T>;
21 std::vector<Node_T> states;
23 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
26 ossia::time_value m_last_time{-1};
28 score::gfx::PassMap m_p;
30 QRhiBuffer* m_meshBuffer{};
31 QRhiBuffer* m_idxBuffer{};
33 bool m_createdPipeline{};
36 ossia::flat_map<int, QRhiBuffer*> createdUbos;
37 ossia::flat_map<int, QRhiSampler*> createdSamplers;
38 ossia::flat_map<int, QRhiTexture*> createdTexs;
40 const CustomGpuNodeBase& node() const noexcept
42 return static_cast<const CustomGpuNodeBase&
>(score::gfx::NodeRenderer::node);
45 CustomGpuRenderer(
const CustomGpuNodeBase& p)
53 auto it = m_rts.find(&p);
54 SCORE_ASSERT(it != m_rts.end());
60 auto& parent = node();
61 auto port = parent.input[k];
62 static constexpr auto flags = QRhiTexture::RenderTarget;
63 auto texture = renderer.
state.rhi->newTexture(QRhiTexture::RGBA8, size, 1, flags);
64 SCORE_ASSERT(texture->create());
73 static constexpr auto bindingStages = QRhiShaderResourceBinding::VertexStage
74 | QRhiShaderResourceBinding::FragmentStage;
75 if constexpr(
requires { F::ubo; })
77 auto it = createdUbos.find(F::binding());
78 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
79 return QRhiShaderResourceBinding::uniformBuffer(
80 F::binding(), bindingStages, buffer);
82 else if constexpr(
requires { F::sampler2D; })
84 auto tex_it = createdTexs.find(F::binding());
86 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
89 QRhiSampler* sampler = renderer.
state.rhi->newSampler(
90 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
91 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
93 createdSamplers[F::binding()] = sampler;
95 return QRhiShaderResourceBinding::sampledTexture(
96 F::binding(), bindingStages, tex, sampler);
100 static_assert(F::nope);
107 auto& rhi = *renderer.
state.rhi;
109 auto srb = rhi.newShaderResourceBindings();
112 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
114 if constexpr(
requires {
decltype(Node_T::layout::bindings){}; })
116 using bindings_type =
decltype(Node_T::layout::bindings);
117 boost::pfr::for_each_field(bindings_type{}, [&](
auto f) {
118 bindings.push_back(initBinding(renderer, f));
121 else if constexpr(
requires {
sizeof(
typename Node_T::layout::bindings); })
123 using bindings_type =
typename Node_T::layout::bindings;
124 boost::pfr::for_each_field(bindings_type{}, [&](
auto f) {
125 bindings.push_back(initBinding(renderer, f));
129 srb->setBindings(bindings.begin(), bindings.end());
133 QRhiGraphicsPipeline* createRenderPipeline(
136 auto& parent = node();
137 auto& rhi = *renderer.
state.rhi;
139 auto ps = rhi.newGraphicsPipeline();
140 ps->setName(
"createRenderPipeline");
142 QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
143 premulAlphaBlend.enable =
true;
144 premulAlphaBlend.srcColor = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
145 premulAlphaBlend.dstColor = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
146 premulAlphaBlend.srcAlpha = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
147 premulAlphaBlend.dstAlpha = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
148 ps->setTargetBlends({premulAlphaBlend});
150 ps->setSampleCount(1);
152 mesh.preparePipeline(*ps);
156 ps->setShaderStages({{QRhiShaderStage::Vertex, v}, {QRhiShaderStage::Fragment, f}});
158 SCORE_ASSERT(rt.renderPass);
159 ps->setRenderPassDescriptor(rt.renderPass);
169 template <std::
size_t Idx,
typename F>
170 requires avnd::sampler_port<F>
173 auto tex = createInput(renderer, sampler_k++, renderer.
state.renderSize);
175 using sampler_type =
typename avnd::member_reflection<F::sampler()>::member_type;
176 createdTexs[sampler_type::binding()] = tex;
179 template <std::
size_t Idx,
typename F>
180 requires avnd::uniform_port<F>
183 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
186 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
189 auto ubo = renderer.
state.rhi->newBuffer(
190 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
193 createdUbos[ubo_type::binding()] = ubo;
198 auto& parent = node();
199 if constexpr(
requires { states[0].prepare(); })
201 for(
auto& state : states)
203 parent.processControlIn(
204 *
this, state, m_last_message, parent.last_message, parent.m_ctx);
213 m_meshBuffer = mbuffer;
214 m_idxBuffer = ibuffer;
218 avnd::input_introspection<Node_T>::for_all(
219 [
this, &renderer](
auto f) { init_input(renderer, f); });
224 auto srb = initBindings(renderer);
232 states.push_back({});
233 prepareNewState(states.back(), parent);
235 auto ps = createRenderPipeline(renderer, rt);
236 ps->setShaderResourceBindings(srb);
241 if constexpr(!
requires { &Node_T::update; })
243 SCORE_ASSERT(srb->create());
244 SCORE_ASSERT(ps->create());
245 m_createdPipeline =
true;
251 std::vector<QRhiShaderResourceBinding> tmp;
257 if(states.size() > 0)
259 auto& state = states[0];
261 avnd::gpu_uniform_introspection<Node_T>::for_all(
262 avnd::get_inputs<Node_T>(state), [&]<avnd::uniform_port F>(
const F& t) {
265 typename avnd::member_reflection<F::uniform()>::member_type;
266 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
268 auto ubo = this->createdUbos.at(ubo_type::binding());
270 static constexpr int offset = gpp::std140_offset<F::uniform()>();
271 static constexpr int size =
sizeof(uniform_type::value);
272 res.updateDynamicBuffer(ubo, offset, size, &t.value);
276 if constexpr(
requires { &Node_T::update; })
281 SCORE_ASSERT(states.size() == m_p.size());
283 for(
int k = 0; k < states.size(); k++)
285 auto& state = states[k];
286 auto& pass = m_p[k].second;
288 bool srb_touched{
false};
289 tmp.assign(pass.srb->cbeginBindings(), pass.srb->cendBindings());
290 for(
auto& promise : state.update())
292 using ret_type =
decltype(promise.feedback_value);
293 gpp::qrhi::handle_update<CustomGpuRenderer, ret_type> handler{
294 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
295 promise.feedback_value = visit(handler, promise.current_command);
300 if(m_createdPipeline)
303 pass.srb->setBindings(tmp.begin(), tmp.end());
306 if(!m_createdPipeline)
308 SCORE_ASSERT(pass.srb->create());
309 SCORE_ASSERT(pass.pipeline->create());
312 m_createdPipeline =
true;
319 m_createdPipeline =
false;
322 if constexpr(
requires { &Node_T::release; })
324 for(
auto& state : states)
326 for(
auto& promise : state.release())
328 gpp::qrhi::handle_release handler{*r.
state.rhi};
329 visit(handler, promise.current_command);
336 m_meshBuffer =
nullptr;
339 for(
auto& [
id, tex] : this->createdTexs)
341 this->createdTexs.clear();
344 for(
auto& [
id, sampl] : this->createdSamplers)
345 sampl->deleteLater();
346 this->createdSamplers.clear();
349 for(
auto& [
id, ubo] : this->createdUbos)
351 this->createdUbos.clear();
355 for(
auto [port, rt] : m_rts)
360 for(
auto& pass : m_p)
361 pass.second.release();
364 m_meshBuffer =
nullptr;
365 m_createdPipeline =
false;
370 void runInitialPasses(
374 auto& parent = node();
376 if(parent.last_message.token.date == m_last_time)
380 m_last_time = parent.last_message.token.date;
383 for(
auto& state : states)
385 parent.processControlIn(
386 *
this, state, m_last_message, parent.last_message, parent.m_ctx);
394 auto& parent = node();
396 score::gfx::defaultRenderPass(
397 renderer, mesh, {m_meshBuffer, m_idxBuffer}, commands, edge, m_p);
400 if(!this->states.empty())
401 parent.processControlOut(this->states[0]);
405template <
typename Node_T>
406struct CustomGpuNode final
408 , GpuNodeElements<Node_T>
411 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
int id,
413 : CustomGpuNodeBase{
std::move(q),
std::move(ctls), ctx}
417 initGfxPorts<Node_T>(
this, this->input, this->output);
419 using layout =
typename Node_T::layout;
420 static constexpr auto lay = layout{};
422 gpp::qrhi::generate_shaders gen;
423 if constexpr(
requires { &Node_T::vertex; })
425 vertex = QString::fromStdString(gen.vertex_shader(lay) + Node_T{}.vertex().data());
429 vertex = gpp::qrhi::DefaultPipeline::vertex();
433 = QString::fromStdString(gen.fragment_shader(lay) + Node_T{}.fragment().data());
439 return new CustomGpuRenderer<Node_T>{*
this};
Renderer for a given node.
Definition NodeRenderer.hpp:11
List of nodes to be rendered to an output.
Definition RenderList.hpp:19
MeshBuffers initMeshBuffer(const Mesh &mesh, QRhiResourceUpdateBatch &res)
Create buffers for a mesh and mark them for upload.
Definition RenderList.cpp:39
const score::gfx::Mesh & defaultTriangle() const noexcept
A triangle mesh correct for this API.
Definition RenderList.cpp:297
TextureRenderTarget renderTargetForOutput(const Edge &edge) const noexcept
Obtain the texture corresponding to an output port.
Definition RenderList.cpp:170
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
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
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:372
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
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:93
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