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 <ossia/detail/algorithms.hpp>
10#include <Gfx/Graph/Uniforms.hpp>
17template <
typename Node_T>
18struct CustomGpuRenderer final
20 , GpuRendererFiles<Node_T>
22 using texture_inputs = avnd::texture_input_introspection<Node_T>;
23 using texture_outputs = avnd::texture_output_introspection<Node_T>;
24 std::vector<std::shared_ptr<Node_T>> states;
25 GpuMessageState m_last_message{};
26 ossia::small_flat_map<const score::gfx::Port*, score::gfx::TextureRenderTarget, 2>
29 ossia::time_value m_last_time{-1};
31 score::gfx::PassMap m_p;
39 ossia::small_vector<bool, 2> m_passCreated;
43 QRhiShaderResourceBindings* m_srb{};
45 ossia::flat_map<int, QRhiBuffer*> createdUbos;
46 ossia::flat_map<int, QRhiSampler*> createdSamplers;
47 ossia::flat_map<int, QRhiTexture*> createdTexs;
48 ossia::flat_map<int, score::gfx::RenderTargetSpecs> samplerSpecs;
49 ossia::flat_map<const score::gfx::Port*, int> inputSamplerBindings;
51 const CustomGpuNodeBase& node() const noexcept
53 return static_cast<const CustomGpuNodeBase&
>(score::gfx::NodeRenderer::node);
56 CustomGpuRenderer(
const CustomGpuNodeBase& p)
64 auto it = m_rts.find(&p);
70 QRhiTexture* createInput(
73 auto& parent = node();
74 auto port = parent.input[k];
75 QRhiTexture::Flags flags = QRhiTexture::RenderTarget;
76 if(spec.mipmap_mode != QRhiSampler::None)
77 flags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips;
78 auto texture = renderer.state.rhi->newTexture(spec.format, spec.size, 1, flags);
79 SCORE_ASSERT(texture->create());
81 renderer.state, texture, renderer.samples(), renderer.requiresDepth(*port));
88 static constexpr auto bindingStages = QRhiShaderResourceBinding::VertexStage
89 | QRhiShaderResourceBinding::FragmentStage;
90 if constexpr(
requires { F::ubo; })
92 auto it = createdUbos.find(F::binding());
93 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
94 return QRhiShaderResourceBinding::uniformBuffer(
95 F::binding(), bindingStages, buffer);
97 else if constexpr(
requires { F::sampler2D; })
99 auto tex_it = createdTexs.find(F::binding());
101 = tex_it != createdTexs.end() ? tex_it->second : &renderer.emptyTexture();
104 spec.address_u = QRhiSampler::ClampToEdge;
105 spec.address_v = QRhiSampler::ClampToEdge;
106 spec.address_w = QRhiSampler::ClampToEdge;
107 if(
auto spec_it = samplerSpecs.find(F::binding()); spec_it != samplerSpecs.end())
108 spec = spec_it->second;
110 QRhiSampler* sampler = renderer.state.rhi->newSampler(
111 spec.mag_filter, spec.min_filter, spec.mipmap_mode, spec.address_u,
112 spec.address_v, spec.address_w);
114 createdSamplers[F::binding()] = sampler;
116 return QRhiShaderResourceBinding::sampledTexture(
117 F::binding(), bindingStages, tex, sampler);
121 static_assert(F::nope);
128 auto& rhi = *renderer.state.rhi;
130 auto srb = rhi.newShaderResourceBindings();
133 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
135 if constexpr(
requires {
decltype(Node_T::layout::bindings){}; })
137 using bindings_type =
decltype(Node_T::layout::bindings);
138 avnd::pfr::for_each_field(bindings_type{}, [&](
auto f) {
139 bindings.push_back(initBinding(renderer, f));
142 else if constexpr(
requires {
sizeof(
typename Node_T::layout::bindings); })
144 using bindings_type =
typename Node_T::layout::bindings;
145 avnd::pfr::for_each_field(bindings_type{}, [&](
auto f) {
146 bindings.push_back(initBinding(renderer, f));
150 srb->setBindings(bindings.begin(), bindings.end());
154 QRhiGraphicsPipeline* createRenderPipeline(
157 auto& parent = node();
158 auto& rhi = *renderer.state.rhi;
159 auto& mesh = renderer.defaultTriangle();
160 auto ps = rhi.newGraphicsPipeline();
161 ps->setName(
"createRenderPipeline");
163 QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
164 premulAlphaBlend.enable =
true;
165 premulAlphaBlend.srcColor = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
166 premulAlphaBlend.dstColor = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
167 premulAlphaBlend.srcAlpha = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
168 premulAlphaBlend.dstAlpha = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
169 ps->setTargetBlends({premulAlphaBlend});
177 const int rtS = rt.sampleCount();
178 ps->setSampleCount(rtS > 0 ? rtS : renderer.samples());
181 mesh.preparePipeline(*ps);
185 ps->setShaderStages({{QRhiShaderStage::Vertex, v}, {QRhiShaderStage::Fragment, f}});
187 SCORE_ASSERT(rt.renderPass);
188 ps->setRenderPassDescriptor(rt.renderPass);
198 template <std::
size_t Idx,
typename F>
199 requires avnd::sampler_port<F>
202 const auto spec = renderer.resolveInputRenderTargetSpecs(node(), Idx);
203 auto tex = createInput(renderer, Idx, spec);
205 using sampler_type =
typename avnd::member_reflection<F::sampler()>::member_type;
206 createdTexs[sampler_type::binding()] = tex;
207 samplerSpecs[sampler_type::binding()] = spec;
208 inputSamplerBindings[node().input[Idx]] = sampler_type::binding();
214 auto binding_it = inputSamplerBindings.find(&input);
215 if(binding_it == inputSamplerBindings.end())
217 auto sampler_it = createdSamplers.find(binding_it->second);
218 if(sampler_it == createdSamplers.end() || !sampler_it->second)
220 auto* sampler = sampler_it->second;
221 const auto tex_it = createdTexs.find(binding_it->second);
222 const bool mipmapped = tex_it != createdTexs.end() && tex_it->second
223 && tex_it->second->flags().testFlag(QRhiTexture::MipMapped);
224 const auto mip = mipmapped ? spec.mipmap_mode : QRhiSampler::None;
225 if(sampler->magFilter() == spec.mag_filter && sampler->minFilter() == spec.min_filter
226 && sampler->mipmapMode() == mip && sampler->addressU() == spec.address_u
227 && sampler->addressV() == spec.address_v && sampler->addressW() == spec.address_w)
230 sampler->setMagFilter(spec.mag_filter);
231 sampler->setMinFilter(spec.min_filter);
232 sampler->setMipmapMode(mip);
233 sampler->setAddressU(spec.address_u);
234 sampler->setAddressV(spec.address_v);
235 sampler->setAddressW(spec.address_w);
239 template <std::
size_t Idx,
typename F>
240 requires avnd::uniform_port<F>
243 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
246 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
249 auto ubo = renderer.state.rhi->newBuffer(
250 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
251 ubo->setName(oscr::getUtf8Name<F>());
254 createdUbos[ubo_type::binding()] = ubo;
267 if(m_meshBuffer.buffers.empty())
269 auto& mesh = renderer.defaultTriangle();
270 m_meshBuffer = renderer.initMeshBuffer(mesh, res);
274 avnd::input_introspection<Node_T>::for_all(
275 [
this, &renderer](
auto f) { init_input(renderer, f); });
278 m_srb = initBindings(renderer);
280 m_initialized =
true;
285 QRhiResourceUpdateBatch& res)
override
287 auto& parent = node();
288 auto rt = renderer.renderTargetForOutput(edge);
291 states.push_back(std::make_shared<Node_T>());
292 prepareNewState(states.back(), parent);
298 if constexpr(
requires { states.back()->prepare(); })
300 parent.processControlIn(
301 *
this, *states.back(), m_last_message, parent.last_message, parent.m_ctx);
302 states.back()->prepare();
305 auto ps = createRenderPipeline(renderer, rt);
306 ps->setShaderResourceBindings(m_srb);
309 m_passCreated.push_back(
false);
315 if constexpr(!
requires { &Node_T::update; })
317 SCORE_ASSERT(m_srb->create());
318 SCORE_ASSERT(ps->create());
319 m_passCreated.back() =
true;
326 return ossia::find_if(m_p, [&](
const auto& p) {
return p.first == &edge; })
338 = ossia::find_if(m_p, [&](
const auto& p) {
return p.first == &edge; });
341 const auto idx = std::distance(m_p.begin(), it);
342 it->second.p.srb =
nullptr;
343 it->second.release();
345 if((std::size_t)idx < states.size())
346 states.erase(states.begin() + idx);
347 if((std::size_t)idx < m_passCreated.size())
348 m_passCreated.erase(m_passCreated.begin() + idx);
356 m_passCreated.clear();
359 if constexpr(
requires { &Node_T::release; })
361 for(
auto& state : states)
363 for(
auto& promise : state->release())
366 visit(handler, promise.current_command);
372 this->releaseFiles();
378 for(
auto& [
id, tex] : this->createdTexs)
380 const bool ownedByRenderTarget = ossia::any_of(
381 m_rts, [tex](
const auto& rt) {
return rt.second.texture == tex; });
382 if(!ownedByRenderTarget)
385 this->createdTexs.clear();
388 for(
auto& [
id, sampl] : this->createdSamplers)
389 sampl->deleteLater();
390 this->createdSamplers.clear();
391 this->samplerSpecs.clear();
392 this->inputSamplerBindings.clear();
395 for(
auto& [
id, ubo] : this->createdUbos)
397 this->createdUbos.clear();
400 for(
auto [port, rt] : m_rts)
408 for(
auto& pass : m_p)
410 pass.second.p.srb =
nullptr;
411 pass.second.release();
415 m_srb->deleteLater();
420 m_initialized =
false;
425 initState(renderer, res);
427 auto& parent = node();
429 addOutputPass(renderer, *edge, res);
432 std::vector<QRhiShaderResourceBinding> tmp;
438 if(states.size() > 0)
440 auto& state = *states[0];
442 avnd::gpu_uniform_introspection<Node_T>::for_all(
443 avnd::get_inputs<Node_T>(state), [&]<avnd::uniform_port F>(
const F& t) {
446 typename avnd::member_reflection<F::uniform()>::member_type;
447 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
449 auto ubo = this->createdUbos.at(ubo_type::binding());
451#define MSVC_BUGGY_STATIC_CONSTEXPR
453#define MSVC_BUGGY_STATIC_CONSTEXPR static constexpr
455 static constexpr int offset = gpp::std140_offset<F::uniform()>();
456 static constexpr int size =
sizeof(uniform_type::value);
457 res.updateDynamicBuffer(ubo, offset, size, &t.value);
461 if constexpr(
requires { &Node_T::update; })
466 SCORE_ASSERT(states.size() == m_p.size());
467 SCORE_ASSERT(states.size() == m_passCreated.size());
469 for(
int k = 0; k < states.size(); k++)
471 auto& state = *states[k];
472 auto& pass = m_p[k].second;
479 const bool created = m_passCreated[k];
481 bool srb_touched{
false};
482 tmp.assign(pass.p.srb->cbeginBindings(), pass.p.srb->cendBindings());
483 for(
auto& promise : state.update())
485 using ret_type =
decltype(promise.feedback_value);
487 *
this, *renderer.state.rhi, res, tmp, srb_touched};
488 promise.feedback_value = visit(handler, promise.current_command);
494 pass.p.srb->destroy();
496 pass.p.srb->setBindings(tmp.begin(), tmp.end());
497 if(created && !pass.p.srb->create())
498 qWarning(
"GpuNode: SRB recreation failed");
503 SCORE_ASSERT(pass.p.srb->create());
504 SCORE_ASSERT(pass.p.pipeline->create());
505 m_passCreated[k] =
true;
514 void runInitialPasses(
518 auto& parent = node();
520 if(parent.last_message.message.token.date == m_last_time)
524 m_last_time = parent.last_message.message.token.date;
527 for(
auto& state : states)
529 parent.processControlIn(
530 *
this, *state, m_last_message, parent.last_message, parent.m_ctx);
532 parent.drainWorker(renderer.dateFromStepClock);
539 auto& parent = node();
540 auto& mesh = renderer.defaultTriangle();
541 score::gfx::defaultRenderPass(renderer, mesh, m_meshBuffer, commands, edge, m_p);
544 if(!this->states.empty())
545 parent.processControlOut(*this->states[0]);
546 for(
auto& state : states)
547 parent.clearControlIn(*state);
551template <
typename Node_T>
552struct CustomGpuNode final
554 , GpuNodeElements<Node_T>
557 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
559 : CustomGpuNodeBase{
std::move(q),
std::move(ctls), ctx}
563 initGfxPorts<Node_T>(
this, this->input, this->output);
565 using layout =
typename Node_T::layout;
566 static constexpr auto lay = layout{};
569 if constexpr(
requires { &Node_T::vertex; })
571 vertex = QString::fromStdString(gen.vertex_shader(lay) + Node_T{}.vertex().data());
575 vertex = gpp::qrhi::DefaultPipeline::vertex();
579 = QString::fromStdString(gen.fragment_shader(lay) + Node_T{}.fragment().data());
585 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:33
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:274
Definition Controls.hpp:30
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag, int multiViewCount)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:1523
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples, bool depth, bool samplableDepth)
Create a render target from a texture.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:86
void updateInputSamplerFilter(std::span< Sampler > samplers, const score::gfx::Node &node, const Port &input, const RenderTargetSpecs &spec)
Give the sampler initInputSamplers() created for input, one of node's input ports,...
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:2029
Definition GppShaders.hpp:10
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:236
Definition NodeRenderer.hpp:268
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:278
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:215
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:61
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:307