Loading...
Searching...
No Matches
score-plugin-gfx/Gfx/Graph/Utils.hpp
1#pragma once
2#include <Process/Dataflow/CableData.hpp>
3
4#include <Gfx/Graph/Mesh.hpp>
5#include <Gfx/Graph/RenderState.hpp>
6#include <Gfx/Graph/Scale.hpp>
7#include <Gfx/Graph/Uniforms.hpp>
8
9#include <ossia/detail/hash_map.hpp>
10#include <ossia/detail/small_flat_map.hpp>
11
12#include <score_plugin_gfx_export.h>
13
14#include <span>
15
16namespace score::gfx
17{
18class Node;
19class NodeModel;
20struct Port;
21struct Edge;
22class RenderList;
26struct Sampler
27{
28 QRhiSampler* sampler{};
29 QRhiTexture* texture{};
30};
31
36{
37 ossia::hash_map<RenderList*, Sampler> samplers;
38
39 std::vector<float> data;
40 int channels{};
41 int fixedSize{0};
42 enum Mode
43 {
44 Waveform,
45 FFT,
46 Histogram
47 } mode{};
48};
49
53struct Port
54{
57
59 void* value{};
60
62 Types type{};
63
65 Flag flags{};
66
68 std::vector<Edge*> edges;
69};
70
74struct Edge
75{
76 Edge(Port* source, Port* sink, Process::CableType t)
77 : source{source}
78 , sink{sink}
79 , type{t}
80 {
81 source->edges.push_back(this);
82 sink->edges.push_back(this);
83 }
84
85 ~Edge()
86 {
87 if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
88 it != source->edges.end())
89 source->edges.erase(it);
90 if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
91 it != sink->edges.end())
92 sink->edges.erase(it);
93 }
94
95 Port* source{};
96 Port* sink{};
97 Process::CableType type{};
98};
99
104{
105 QRhiGraphicsPipeline* pipeline{};
106 QRhiShaderResourceBindings* srb{};
107
108 void release()
109 {
110 delete pipeline;
111 pipeline = nullptr;
112
113 delete srb;
114 srb = nullptr;
115 }
116};
117
122{
123 QRhiTexture* texture{}; // Primary color attachment (location 0)
124 std::vector<QRhiTexture*> additionalColorTextures; // MRT: locations 1..N
125 QRhiRenderBuffer* colorRenderBuffer{};
126 QRhiRenderBuffer* depthRenderBuffer{};
127 QRhiTexture* depthTexture{}; // Sampleable depth (alternative to depthRenderBuffer)
128 QRhiTexture* msDepthTexture{}; // MSAA depth attachment when depthTexture is the resolve target
129 QRhiRenderPassDescriptor* renderPass{};
130 QRhiRenderTarget* renderTarget{};
131
132 operator bool() const noexcept { return texture != nullptr; }
133
134 int colorAttachmentCount() const noexcept
135 {
136 return texture ? 1 + (int)additionalColorTextures.size() : 0;
137 }
138
139 // Returns the actual MSAA sample count of this render target, or -1 if it
140 // cannot be determined from the stored fields (e.g. when only renderPass is
141 // set, as for placeholders that target a swap chain). Callers must treat
142 // -1 as "unknown — fall back to the renderlist's global sample count".
143 // This value is the authoritative input to QRhiGraphicsPipeline::setSampleCount()
144 // when known, since an RT may have been degraded (samplable-depth + MSAA
145 // without depth-resolve support).
146 int sampleCount() const noexcept
147 {
148 if(renderTarget)
149 return renderTarget->sampleCount();
150 if(colorRenderBuffer)
151 return colorRenderBuffer->sampleCount();
152 if(texture)
153 return texture->sampleCount();
154 return -1;
155 }
156
157 void release()
158 {
159 if(texture)
160 {
161 delete texture;
162 texture = nullptr;
163
164 for(auto* t : additionalColorTextures)
165 delete t;
166 additionalColorTextures.clear();
167
168 delete colorRenderBuffer;
169 colorRenderBuffer = nullptr;
170
171 delete depthRenderBuffer;
172 depthRenderBuffer = nullptr;
173
174 delete depthTexture;
175 depthTexture = nullptr;
176
177 delete msDepthTexture;
178 msDepthTexture = nullptr;
179
180 delete renderPass;
181 renderPass = nullptr;
182
183 delete renderTarget;
184 renderTarget = nullptr;
185 }
186 }
187};
188
192struct Image
193{
194 QString path;
195 std::vector<QImage> frames;
196};
197
201SCORE_PLUGIN_GFX_EXPORT
203createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples, bool depth, bool samplableDepth = false);
204
210SCORE_PLUGIN_GFX_EXPORT
212 const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples, bool depth,
213 bool samplableDepth = false, QRhiTexture::Flags flags = {});
214
221SCORE_PLUGIN_GFX_EXPORT
222TextureRenderTarget createRenderTarget(
223 const RenderState& state,
224 std::span<QRhiTexture* const> colorTextures,
225 QRhiTexture* depthTexture,
226 int samples);
227
228SCORE_PLUGIN_GFX_EXPORT
229void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
230SCORE_PLUGIN_GFX_EXPORT
231void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
232SCORE_PLUGIN_GFX_EXPORT
233void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
234
235SCORE_PLUGIN_GFX_EXPORT
236void replaceBuffer(
237 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
238SCORE_PLUGIN_GFX_EXPORT
239void replaceSampler(
240 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
241SCORE_PLUGIN_GFX_EXPORT
242void replaceTexture(
243 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
244
248SCORE_PLUGIN_GFX_EXPORT
249void replaceSampler(
250 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
251
255SCORE_PLUGIN_GFX_EXPORT
256void replaceTexture(
257 QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
258
262SCORE_PLUGIN_GFX_EXPORT
264 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler,
265 QRhiTexture* newTexture);
266
270SCORE_PLUGIN_GFX_EXPORT
271void replaceTexture(
272 QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
276SCORE_PLUGIN_GFX_EXPORT
277QRhiShaderResourceBindings* createDefaultBindings(
278 const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
279 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
280 std::span<QRhiShaderResourceBinding> additionalBindings = {});
281
290SCORE_PLUGIN_GFX_EXPORT
292 QRhiGraphicsPipeline& pip, const QShader& vertexShader,
293 const ossia::geometry& geom);
294
298SCORE_PLUGIN_GFX_EXPORT
299Pipeline buildPipeline(
300 const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
301 const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
302 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
303 std::span<QRhiShaderResourceBinding> additionalBindings = {});
304
310SCORE_PLUGIN_GFX_EXPORT
311std::pair<QShader, QShader>
312makeShaders(const RenderState& v, QString vert, QString frag);
313
319SCORE_PLUGIN_GFX_EXPORT
320QShader makeCompute(const RenderState& v, QString compt);
321
327struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
328{
329 void init(
330 RenderList& renderer, const std::vector<Port*>& input,
331 ossia::small_vector<Sampler, 8>& samplers);
332
333 QRhiBuffer* buffer{};
334 int size{};
335};
336
340SCORE_PLUGIN_GFX_EXPORT
341QSize resizeTextureSize(QSize img, int min, int max) noexcept;
342
346SCORE_PLUGIN_GFX_EXPORT
347QImage resizeTexture(const QImage& img, int min, int max) noexcept;
348
349inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
350{
351 memcpy(ptr, mat.constData(), sizeof(float) * 16);
352}
353inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
354{
355 memcpy(ptr, mat.constData(), sizeof(float) * 9);
356}
357
361SCORE_PLUGIN_GFX_EXPORT
362QSizeF computeScaleForMeshSizing(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
363
367SCORE_PLUGIN_GFX_EXPORT
369 score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
370
375 QRhiResourceUpdateBatch* ub
376 , QRhiBuffer* buf
377 , int offset
378 , int64_t bytesize
379 , const char* data
380 )
381{
382#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
383 ub->updateDynamicBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
384#else
385 ub->updateDynamicBuffer(buf, offset, bytesize, data);
386#endif
387}
388
390 QRhiResourceUpdateBatch* ub
391 , QRhiBuffer* buf
392 , int offset
393 , QByteArray b
394 )
395{
396#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
397 ub->updateDynamicBuffer(buf, offset, std::move(b));
398#else
399 ub->updateDynamicBuffer(buf, offset, b.size(), b.data());
400#endif
401}
402
407 QRhiResourceUpdateBatch* ub
408 , QRhiBuffer* buf
409 , int offset
410 , int64_t bytesize
411 , const char* data
412 )
413{
414#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
415 ub->uploadStaticBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
416#else
417 ub->uploadStaticBuffer(buf, offset, bytesize, data);
418#endif
419}
420
422 QRhiResourceUpdateBatch* ub
423 , QRhiBuffer* buf
424 , int offset
425 , QByteArray b
426 )
427{
428#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
429 ub->uploadStaticBuffer(buf, offset, std::move(b));
430#else
431 ub->uploadStaticBuffer(buf, offset, b.size(), b.data());
432#endif
433}
434
435SCORE_PLUGIN_GFX_EXPORT
436std::vector<Sampler> initInputSamplers(
437 const score::gfx::Node& node, RenderList& renderer, const std::vector<Port*>& ports);
438}
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:74
List of nodes to be rendered to an output.
Definition RenderList.hpp:19
TreeNode< DeviceExplorerNode > Node
Definition DeviceNode.hpp:74
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:12
QSizeF computeScaleForTexcoordSizing(ScaleMode mode, QSizeF renderSize, QSizeF textureSize)
Compute the scale to apply to a texture rendered to a quad the size of viewport.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:861
void updateDynamicBufferWithStoredData(QRhiResourceUpdateBatch *ub, QRhiBuffer *buf, int offset, int64_t bytesize, const char *data)
Schedule a Dynamic buffer update when we can guarantee the buffer outlives the frame.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:374
bool remapPipelineVertexInputs(QRhiGraphicsPipeline &pip, const QShader &vertexShader, const ossia::geometry &geom)
Remap a pipeline's vertex input layout using semantic matching.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:436
QRhiShaderResourceBindings * createDefaultBindings(const RenderList &renderer, const TextureRenderTarget &rt, QRhiBuffer *processUBO, QRhiBuffer *materialUBO, std::span< const Sampler > samplers, std::span< QRhiShaderResourceBinding > additionalBindings)
Create bindings following the score conventions for shaders and materials.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:570
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:674
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:11
void uploadStaticBufferWithStoredData(QRhiResourceUpdateBatch *ub, QRhiBuffer *buf, int offset, int64_t bytesize, const char *data)
Schedule a Static buffer update when we can guarantee the buffer outlives the frame.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:406
QImage resizeTexture(const QImage &img, int min, int max) noexcept
Resize a texture to fit within GPU limits.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:811
QSizeF computeScaleForMeshSizing(ScaleMode mode, QSizeF viewport, QSizeF texture)
Compute the scale to apply to a texture so that it fits in a GL viewport.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:821
void replaceSamplerAndTexture(QRhiShaderResourceBindings &srb, QRhiSampler *oldSampler, QRhiSampler *newSampler, QRhiTexture *newTexture)
Replace both sampler and texture in a SRC.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:364
ScaleMode
How to resize a texture to adapt it to a viewport.
Definition Scale.hpp: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:647
QSize resizeTextureSize(QSize sz, int min, int max) noexcept
Resize the size of a texture to fit within GPU limits.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:760
Data model for audio data being sent to the GPU.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:36
Utility to represent a shader material following score conventions.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:328
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:75
Image data and metadata.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:193
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:104
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:54
void * value
Pointer to the corresponding data.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:59
score::gfx::Node * node
Parent node of the port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:56
Types type
Type of the value.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:62
std::vector< Edge * > edges
Edges connected to that port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:68
Flag flags
Optional setting flags.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:65
Global state associated to a rendering context.
Definition RenderState.hpp:37
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:27
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:122