Loading...
Searching...
No Matches
NodeRenderer.hpp
1#pragma once
2#include <Gfx/Graph/Node.hpp>
3
4namespace score::gfx
5{
6
10class SCORE_PLUGIN_GFX_EXPORT NodeRenderer
11{
12public:
13 explicit NodeRenderer(const Node& node)
14 : node{node}
15 {
16 }
17 virtual ~NodeRenderer();
18
19 virtual TextureRenderTarget renderTargetForInput(const Port& input);
20 virtual BufferView bufferForInput(const Port& input);
21 virtual BufferView bufferForOutput(const Port& output);
22
27 virtual QRhiTexture* textureForOutput(const Port& output);
28
33 virtual void updateInputTexture(const Port& input, QRhiTexture* tex, QRhiTexture* depthTex = nullptr);
34
37 virtual void updateInputSamplerFilter(
38 const Port& input, const RenderTargetSpecs& spec);
39
42 virtual void
43 inputAboutToFinish(RenderList& renderer, const Port& p, QRhiResourceUpdateBatch*&);
44
45 virtual void init(RenderList& renderer, QRhiResourceUpdateBatch& res) = 0;
46 virtual void update(RenderList& renderer, QRhiResourceUpdateBatch& res, Edge* edge)
47 = 0;
48
49 virtual void runInitialPasses(
50 RenderList&, QRhiCommandBuffer& commands, QRhiResourceUpdateBatch*& res,
51 Edge& edge);
52
53 virtual void runRenderPass(RenderList&, QRhiCommandBuffer& commands, Edge& edge);
54
56 virtual QRhiGraphicsPipeline::CompareOp depthCompare() const noexcept
57 {
58 return QRhiGraphicsPipeline::Greater;
59 }
60
61 virtual void release(RenderList&) = 0;
62
79 virtual void initState(RenderList& renderer, QRhiResourceUpdateBatch& res);
80
83 virtual void releaseState(RenderList& renderer);
84
86 virtual void addOutputPass(
87 RenderList& renderer, Edge& edge, QRhiResourceUpdateBatch& res);
88
93 virtual void removeOutputPass(RenderList& renderer, Edge& edge) = 0;
94
97 virtual void
98 addInputEdge(RenderList& renderer, Edge& edge, QRhiResourceUpdateBatch& res);
99
101 virtual void removeInputEdge(RenderList& renderer, Edge& edge);
102
104 virtual bool hasOutputPassForEdge(Edge& edge) const;
105
114 virtual void seedInitialOutputs(RenderList& renderer);
115
118 void checkForChanges()
119 {
120 // Use |= to preserve flags set externally (e.g. by reconciliation
121 // or maybeRebuild). The flag is cleared by the renderer's update()
122 // after processing, preventing infinite re-uploads.
123 materialChanged |= node.hasMaterialChanged(materialChangedIndex);
124 renderTargetSpecsChanged |= node.hasRenderTargetChanged(renderTargetSpecsChangedIndex);
125 }
126
131 {
132 node.hasRenderTargetChanged(renderTargetSpecsChangedIndex);
133 renderTargetSpecsChanged = false;
134 }
135
136 void process(int32_t port, const ossia::geometry_spec& v);
137 void process(int32_t port, const ossia::scene_spec& v);
138 virtual void process(int32_t port, const ossia::transform3d& v);
139
146 void process(int32_t port, const ossia::geometry_spec& v, const void* source_key);
147 void process(int32_t port, const ossia::scene_spec& v, const void* source_key);
148
152 const ossia::geometry_spec* findGeometryByPort(int32_t port) const
153 {
154 for(const auto& [k, v] : m_portGeometries)
155 if(k.first == port)
156 return &v;
157 return nullptr;
158 }
159
166 template <typename F>
167 void forEachSceneOnPort(int32_t port, F&& fn) const
168 {
169 for(const auto& [k, v] : m_portScenes)
170 if(k.first == port && v.state)
171 fn(v);
172 }
173
174private:
177 void rebuildMergedScene();
178
179public:
180
181 const Node& node;
182
193 ossia::geometry_spec geometry;
194
200 using PortSourceKey = std::pair<int32_t, const void*>;
201 ossia::small_flat_map<PortSourceKey, ossia::geometry_spec, 4> m_portGeometries;
202
210 ossia::scene_spec scene;
211
213 ossia::small_flat_map<PortSourceKey, ossia::scene_spec, 4> m_portScenes;
214
223 using MergeCacheKey = std::pair<const ossia::scene_state*, int64_t>;
224 ossia::small_vector<MergeCacheKey, 4> m_mergeCacheInputs;
225 ossia::scene_spec m_mergeCacheOutput;
226
231 ossia::small_flat_map<
232 PortSourceKey, std::pair<ossia::geometry_spec, ossia::scene_spec>, 4>
234
235 int32_t nodeId{-1};
236 bool materialChanged{false};
237 bool geometryChanged{false};
238 bool sceneChanged{false};
239 bool renderTargetSpecsChanged{false};
240
243 bool m_initialized{false};
244
245private:
246 int64_t materialChangedIndex{-1};
247 int64_t renderTargetSpecsChangedIndex{-1};
248};
249
250struct Pass
251{
252 // User-declared ctors (including the implicit ones spelled out here)
253 // suppress -Wmissing-field-initializers on the many call sites that
254 // brace-init this struct with three arguments: the fallback plan is
255 // then default-constructed into an empty list, which is what
256 // non-fallback pipelines need. This costs aggregate initialization --
257 // call sites that want fallback_bindings assign it afterwards.
258 Pass() = default;
259 Pass(TextureRenderTarget rt, Pipeline pi, QRhiBuffer* ubo)
260 : renderTarget{std::move(rt)}, p{pi}, processUBO{ubo} {}
261 // Compat for addons that build a pass from a bare Pipeline; such passes
262 // fetch their render target through renderer.renderTargetForOutput(edge)
263 // at draw time.
264 Pass(Pipeline pi)
265 : p{pi} {}
266
267 TextureRenderTarget renderTarget;
268 Pipeline p;
269 QRhiBuffer* processUBO{};
270 // Bindings for "REQUIRED: false" VERTEX_INPUTS that had no matching
271 // upstream attribute when this pass's pipeline was built. Empty for
272 // pipelines where the shader is strict-matched (the common case).
273 // Consumed by the draw path: each slot's buffer is bound at its
274 // `binding_index` in the vertex-input array before the draw call.
275 // The buffers themselves are owned by VertexFallbackPool — the plan
276 // holds non-owning pointers.
277 FallbackBindingPlan fallback_bindings;
278
279 void release()
280 {
281 p.release();
282 if(processUBO)
283 {
284 processUBO->deleteLater();
285 processUBO = nullptr;
286 }
287 fallback_bindings.clear();
288 // renderTarget NOT released here — owned by RenderList
289 }
290};
291
292using PassMap = ossia::small_vector<std::pair<Edge*, Pass>, 2>;
293SCORE_PLUGIN_GFX_EXPORT
294void defaultPassesInit(
295 PassMap& passes, const std::vector<Edge*>& edges, RenderList& renderer,
296 const Mesh& mesh, const QShader& v, const QShader& f, QRhiBuffer* processUBO,
297 QRhiBuffer* matUBO, std::span<const Sampler> samplers,
298 std::span<QRhiShaderResourceBinding> additionalBindings = {});
299
300SCORE_PLUGIN_GFX_EXPORT
301void defaultRenderPass(
302 RenderList& renderer, const Mesh& mesh, const MeshBuffers& bufs,
303 QRhiCommandBuffer& cb, Edge& edge, PassMap& passes);
304
305SCORE_PLUGIN_GFX_EXPORT
306void quadRenderPass(
307 RenderList& renderer, const MeshBuffers& bufs, QRhiCommandBuffer& cb, Edge& edge,
308 PassMap& passes);
309
316class SCORE_PLUGIN_GFX_EXPORT GenericNodeRenderer : public score::gfx::NodeRenderer
317{
318public:
319 GenericNodeRenderer(const NodeModel& node) noexcept
320 : NodeRenderer{node}
321 {
322 }
323
324 virtual ~GenericNodeRenderer() { }
325
327 score::gfx::Port* imageOutlet() const noexcept;
328
329 ossia::small_vector<Sampler, 8> m_samplers;
330
331 QShader m_vertexS;
332 QShader m_fragmentS;
333
334 // Pipeline
335 PassMap m_p;
336
337 // Per-renderer pipeline cache, keyed by the rp-desc's serializedFormat().
338 // QRhi guarantees a pipeline can be used with any render target whose
339 // rp-desc isCompatible with the pipeline's, and serializedFormat() is
340 // documented as the in-memory comparison key for exactly that relation —
341 // identical blobs ⇔ isCompatible. Keying by pointer instead would be
342 // ABA-unsafe: a freshly allocated rp-desc can reuse the address of one
343 // just destroyed, silently serving a pipeline built for a dead layout.
344 //
345 // Ownership: Pass::p.pipeline is NON-OWNING — the actual QRhiGraphicsPipeline
346 // lives in this cache. Pass::p.srb is still per-edge and owned by the Pass.
347 // GenericNodeRenderer::removeOutputPass and releaseState take care of
348 // nulling Pass::p.pipeline before calling Pipeline::release() so it
349 // does not try to deleteLater() a pointer we still own here.
350 ossia::small_vector<
351 std::pair<QVector<quint32>, QRhiGraphicsPipeline*>, 2>
352 m_pipelineCache;
353
354 MeshBuffers m_meshbufs;
355
356 QRhiBuffer* m_processUBO{};
357
358 DefaultShaderMaterial m_material;
359
360 const score::gfx::Mesh* m_mesh{};
361
362 // Render loop
363 void
364 defaultMeshInit(RenderList& renderer, const Mesh& mesh, QRhiResourceUpdateBatch& res);
365 void processUBOInit(RenderList& renderer);
366 void defaultPassesInit(RenderList& renderer, const Mesh& mesh);
367 void defaultPassesInit(
368 RenderList& renderer, const Mesh& mesh, const QShader& v, const QShader& f,
369 std::span<QRhiShaderResourceBinding> additionalBindings = {});
370
371 void init(RenderList& renderer, QRhiResourceUpdateBatch& res) override;
372
373 void initState(RenderList& renderer, QRhiResourceUpdateBatch& res) override;
374 void releaseState(RenderList& renderer) override;
375 void addOutputPass(
376 RenderList& renderer, Edge& edge, QRhiResourceUpdateBatch& res) override;
377 void removeOutputPass(RenderList& renderer, Edge& edge) override;
378 bool hasOutputPassForEdge(Edge& edge) const override;
379
380 void defaultUBOUpdate(RenderList& renderer, QRhiResourceUpdateBatch& res);
381 void defaultMeshUpdate(RenderList& renderer, QRhiResourceUpdateBatch& res);
382 void update(RenderList& renderer, QRhiResourceUpdateBatch& res, Edge* edge) override;
383
384 void defaultRelease(RenderList&);
385 void release(RenderList&) override;
386
387 void defaultRenderPass(
388 RenderList&, const Mesh& mesh, QRhiCommandBuffer& commands, Edge& edge);
389
390 void defaultRenderPass(
391 RenderList&, const Mesh& mesh, QRhiCommandBuffer& commands, Edge& edge,
392 PassMap& passes);
393
394 void runRenderPass(RenderList&, QRhiCommandBuffer& commands, Edge& edge) override;
395
396 void updateInputTexture(const Port& input, QRhiTexture* tex, QRhiTexture* depthTex = nullptr) override;
397};
398
399}
Generic renderer.
Definition NodeRenderer.hpp:317
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:74
Common base class for most single-pass, simple nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:228
Renderer for a given node.
Definition NodeRenderer.hpp:11
virtual void removeOutputPass(RenderList &renderer, Edge &edge)=0
std::pair< const ossia::scene_state *, int64_t > MergeCacheKey
Definition NodeRenderer.hpp:223
ossia::small_flat_map< PortSourceKey, ossia::scene_spec, 4 > m_portScenes
Per-(port, source) scene storage. See m_portGeometries comment.
Definition NodeRenderer.hpp:213
ossia::scene_spec scene
The scene to use (when receiving scene_spec data).
Definition NodeRenderer.hpp:210
ossia::geometry_spec geometry
The geometry to use.
Definition NodeRenderer.hpp:193
std::pair< int32_t, const void * > PortSourceKey
Definition NodeRenderer.hpp:200
ossia::small_flat_map< PortSourceKey, std::pair< ossia::geometry_spec, ossia::scene_spec >, 4 > m_wrapCache
Definition NodeRenderer.hpp:233
void syncRenderTargetIndex()
Definition NodeRenderer.hpp:130
virtual QRhiGraphicsPipeline::CompareOp depthCompare() const noexcept
The depth compare this renderer's pipeline was built with.
Definition NodeRenderer.hpp:56
void forEachSceneOnPort(int32_t port, F &&fn) const
Definition NodeRenderer.hpp:167
const ossia::geometry_spec * findGeometryByPort(int32_t port) const
Definition NodeRenderer.hpp:152
List of nodes to be rendered to an output.
Definition RenderList.hpp:33
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
Definition Mesh.hpp:18
Utility to represent a shader material following score conventions.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:776
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:198
Definition VertexFallbackPlan.hpp:27
Definition Mesh.hpp:94
Data model for meshes.
Definition Mesh.hpp:179
Definition NodeRenderer.hpp:251
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:227
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:177
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:56
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:247