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>
11 #include <gpp/ports.hpp>
16 template <
typename Node_T>
19 using texture_inputs = avnd::texture_input_introspection<Node_T>;
20 using texture_outputs = avnd::texture_output_introspection<Node_T>;
21 const CustomGpuNodeBase& parent;
22 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 CustomGpuRenderer(
const CustomGpuNodeBase& p)
49 auto it = m_rts.find(&p);
50 SCORE_ASSERT(it != m_rts.end());
56 auto port = parent.input[k];
57 constexpr
auto flags = QRhiTexture::RenderTarget;
58 auto texture = renderer.
state.rhi->newTexture(QRhiTexture::RGBA8, size, 1, flags);
59 SCORE_ASSERT(texture->create());
68 constexpr
auto bindingStages = QRhiShaderResourceBinding::VertexStage
69 | QRhiShaderResourceBinding::FragmentStage;
70 if constexpr(requires { F::ubo; })
72 auto it = createdUbos.find(F::binding());
73 QRhiBuffer* buffer = it != createdUbos.end() ? it->second :
nullptr;
74 return QRhiShaderResourceBinding::uniformBuffer(
75 F::binding(), bindingStages, buffer);
77 else if constexpr(requires { F::sampler2D; })
79 auto tex_it = createdTexs.find(F::binding());
81 = tex_it != createdTexs.end() ? tex_it->second : &renderer.
emptyTexture();
84 QRhiSampler* sampler = renderer.
state.rhi->newSampler(
85 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
86 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
88 createdSamplers[F::binding()] = sampler;
90 return QRhiShaderResourceBinding::sampledTexture(
91 F::binding(), bindingStages, tex, sampler);
95 static_assert(F::nope);
102 auto& rhi = *renderer.
state.rhi;
104 auto srb = rhi.newShaderResourceBindings();
107 QVarLengthArray<QRhiShaderResourceBinding, 8> bindings;
109 if constexpr(requires { decltype(Node_T::layout::bindings){}; })
111 using bindings_type = decltype(Node_T::layout::bindings);
112 boost::pfr::for_each_field(bindings_type{}, [&](
auto f) {
113 bindings.push_back(initBinding(renderer, f));
116 else if constexpr(requires {
sizeof(
typename Node_T::layout::bindings); })
118 using bindings_type =
typename Node_T::layout::bindings;
119 boost::pfr::for_each_field(bindings_type{}, [&](
auto f) {
120 bindings.push_back(initBinding(renderer, f));
124 srb->setBindings(bindings.begin(), bindings.end());
128 QRhiGraphicsPipeline* createRenderPipeline(
131 auto& rhi = *renderer.
state.rhi;
133 auto ps = rhi.newGraphicsPipeline();
134 ps->setName(
"createRenderPipeline");
136 QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
137 premulAlphaBlend.enable =
true;
138 premulAlphaBlend.srcColor = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
139 premulAlphaBlend.dstColor = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
140 premulAlphaBlend.srcAlpha = QRhiGraphicsPipeline::BlendFactor::SrcAlpha;
141 premulAlphaBlend.dstAlpha = QRhiGraphicsPipeline::BlendFactor::OneMinusSrcAlpha;
142 ps->setTargetBlends({premulAlphaBlend});
144 ps->setSampleCount(1);
146 mesh.preparePipeline(*ps);
150 ps->setShaderStages({{QRhiShaderStage::Vertex, v}, {QRhiShaderStage::Fragment, f}});
152 SCORE_ASSERT(rt.renderPass);
153 ps->setRenderPassDescriptor(rt.renderPass);
163 template <std::
size_t Idx,
typename F>
164 requires avnd::sampler_port<F>
167 auto tex = createInput(renderer, sampler_k++, renderer.
state.renderSize);
169 using sampler_type =
typename avnd::member_reflection<F::sampler()>::member_type;
170 createdTexs[sampler_type::binding()] = tex;
173 template <std::
size_t Idx,
typename F>
174 requires avnd::uniform_port<F>
177 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
180 if(createdUbos.find(ubo_type::binding()) != createdUbos.end())
183 auto ubo = renderer.
state.rhi->newBuffer(
184 QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, gpp::std140_size<ubo_type>());
187 createdUbos[ubo_type::binding()] = ubo;
192 if constexpr(requires { states[0].init(); })
194 for(
auto& state : states)
202 m_meshBuffer = mbuffer;
203 m_idxBuffer = ibuffer;
207 avnd::input_introspection<Node_T>::for_all(
208 [
this, &renderer](
auto f) { init_input(renderer, f); });
213 auto srb = initBindings(renderer);
221 states.push_back({});
222 prepareNewState(states.back(), parent);
224 auto ps = createRenderPipeline(renderer, rt);
225 ps->setShaderResourceBindings(srb);
230 if constexpr(!requires { &Node_T::update; })
232 SCORE_ASSERT(srb->create());
233 SCORE_ASSERT(ps->create());
234 m_createdPipeline =
true;
240 std::vector<QRhiShaderResourceBinding> tmp;
244 if(states.size() > 0)
246 auto& state = states[0];
248 avnd::gpu_uniform_introspection<Node_T>::for_all(
249 avnd::get_inputs<Node_T>(state), [&]<avnd::uniform_port F>(
const F& t) {
252 typename avnd::member_reflection<F::uniform()>::member_type;
253 using ubo_type =
typename avnd::member_reflection<F::uniform()>::class_type;
255 auto ubo = this->createdUbos.at(ubo_type::binding());
257 constexpr
int offset = gpp::std140_offset<F::uniform()>();
258 constexpr
int size =
sizeof(uniform_type::value);
259 res.updateDynamicBuffer(ubo, offset, size, &t.value);
263 if constexpr(requires { &Node_T::update; })
268 SCORE_ASSERT(states.size() == m_p.size());
270 for(
int k = 0; k < states.size(); k++)
272 auto& state = states[k];
273 auto& pass = m_p[k].second;
275 bool srb_touched{
false};
276 tmp.assign(pass.srb->cbeginBindings(), pass.srb->cendBindings());
277 for(
auto& promise : state.update())
279 using ret_type = decltype(promise.feedback_value);
280 gpp::qrhi::handle_update<CustomGpuRenderer, ret_type> handler{
281 *
this, *renderer.
state.rhi, res, tmp, srb_touched};
282 promise.feedback_value = visit(handler, promise.current_command);
287 if(m_createdPipeline)
290 pass.srb->setBindings(tmp.begin(), tmp.end());
293 if(!m_createdPipeline)
295 SCORE_ASSERT(pass.srb->create());
296 SCORE_ASSERT(pass.pipeline->create());
299 m_createdPipeline =
true;
306 m_createdPipeline =
false;
309 if constexpr(requires { &Node_T::release; })
311 for(
auto& state : states)
313 for(
auto& promise : state.release())
315 gpp::qrhi::handle_release handler{*r.
state.rhi};
316 visit(handler, promise.current_command);
323 m_meshBuffer =
nullptr;
326 for(
auto& [
id, tex] : this->createdTexs)
328 this->createdTexs.clear();
331 for(
auto& [
id, sampl] : this->createdSamplers)
332 sampl->deleteLater();
333 this->createdSamplers.clear();
336 for(
auto& [
id, ubo] : this->createdUbos)
338 this->createdUbos.clear();
342 for(
auto [port, rt] : m_rts)
347 for(
auto& pass : m_p)
348 pass.second.release();
351 m_meshBuffer =
nullptr;
352 m_createdPipeline =
false;
357 void runInitialPasses(
362 if(parent.last_message.token.date == m_last_time)
366 m_last_time = parent.last_message.token.date;
369 for(
auto& state : states)
371 this->parent.processControlIn(state, this->parent.last_message);
380 score::gfx::defaultRenderPass(
381 renderer, mesh, {m_meshBuffer, m_idxBuffer}, commands, edge, m_p);
384 if(!this->states.empty())
385 parent.processControlOut(this->states[0]);
390 template <
typename Node_T>
391 struct CustomGpuNode final : CustomGpuNodeBase
394 std::weak_ptr<Execution::ExecutionCommandQueue> q, Gfx::exec_controls ctls,
int id)
395 : CustomGpuNodeBase{std::move(q), std::move(ctls)}
399 using texture_inputs = avnd::gpu_sampler_introspection<Node_T>;
400 using texture_outputs = avnd::gpu_attachment_introspection<Node_T>;
402 for(std::size_t i = 0; i < texture_inputs::size; i++)
404 input.push_back(
new score::gfx::Port{
this, {}, score::gfx::Types::Image, {}});
406 for(std::size_t i = 0; i < texture_outputs::size; i++)
408 output.push_back(
new score::gfx::Port{
this, {}, score::gfx::Types::Image, {}});
411 using layout =
typename Node_T::layout;
412 static constexpr
auto lay = layout{};
414 gpp::qrhi::generate_shaders gen;
415 if constexpr(requires { &Node_T::vertex; })
417 vertex = QString::fromStdString(gen.vertex_shader(lay) + Node_T{}.vertex().data());
421 vertex = gpp::qrhi::DefaultPipeline::vertex();
425 = QString::fromStdString(gen.fragment_shader(lay) + Node_T{}.fragment().data());
431 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:247
RenderState & state
RenderState corresponding to this RenderList.
Definition: RenderList.hpp:89
TextureRenderTarget renderTargetForOutput(const Edge &edge) noexcept
Obtain the texture corresponding to an output port.
Definition: RenderList.cpp:164
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing.
Definition: RenderList.hpp:111
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:334
Connection between two score::gfx::Port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:64
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:91
Port of a score::gfx::Node.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:46
Useful abstraction for storing all the data related to a render target.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:109