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;
23
24
25
29struct Sampler
30{
31 QRhiSampler* sampler{};
32 QRhiTexture* texture{};
33};
34
39{
40 ossia::hash_map<RenderList*, Sampler> samplers;
41
42 std::vector<float> data;
43 int channels{};
44 int fixedSize{0};
45 enum Mode
46 {
47 Waveform,
48 FFT,
49 Histogram
50 } mode{};
51};
52
56struct Port
57{
60
62 void* value{};
63
65 Types type{};
66
68 Flag flags{};
69
71 std::vector<Edge*> edges;
72};
73
77struct Edge
78{
79 Edge(Port* source, Port* sink, Process::CableType t)
80 : source{source}
81 , sink{sink}
82 , type{t}
83 {
84 source->edges.push_back(this);
85 sink->edges.push_back(this);
86 }
87
88 ~Edge()
89 {
90 if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
91 it != source->edges.end())
92 source->edges.erase(it);
93 if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
94 it != sink->edges.end())
95 sink->edges.erase(it);
96 }
97
98 Port* source{};
99 Port* sink{};
100 Process::CableType type{};
101};
102
107{
108 QRhiGraphicsPipeline* pipeline{};
109 QRhiShaderResourceBindings* srb{};
110
111 void release()
112 {
113 delete pipeline;
114 pipeline = nullptr;
115
116 delete srb;
117 srb = nullptr;
118 }
119};
120
125{
126 QRhiTexture* texture{}; // Primary color attachment (location 0)
127 std::vector<QRhiTexture*> additionalColorTextures; // MRT: locations 1..N
128 QRhiRenderBuffer* colorRenderBuffer{};
129 QRhiRenderBuffer* depthRenderBuffer{};
130 QRhiTexture* depthTexture{}; // Sampleable depth (alternative to depthRenderBuffer)
131 QRhiTexture* msDepthTexture{}; // MSAA depth attachment when depthTexture is the resolve target
132 QRhiRenderPassDescriptor* renderPass{};
133 QRhiRenderTarget* renderTarget{};
134
135 operator bool() const noexcept { return texture != nullptr; }
136
137 int colorAttachmentCount() const noexcept
138 {
139 return texture ? 1 + (int)additionalColorTextures.size() : 0;
140 }
141
142 // Returns the actual MSAA sample count of this render target, or -1 if it
143 // cannot be determined from the stored fields (e.g. when only renderPass is
144 // set, as for placeholders that target a swap chain). Callers must treat
145 // -1 as "unknown — fall back to the renderlist's global sample count".
146 // This value is the authoritative input to QRhiGraphicsPipeline::setSampleCount()
147 // when known, since an RT may have been degraded (samplable-depth + MSAA
148 // without depth-resolve support).
149 int sampleCount() const noexcept
150 {
151 if(renderTarget)
152 return renderTarget->sampleCount();
153 if(colorRenderBuffer)
154 return colorRenderBuffer->sampleCount();
155 if(texture)
156 return texture->sampleCount();
157 return -1;
158 }
159
160 void release()
161 {
162 if(texture)
163 {
164 delete texture;
165 texture = nullptr;
166
167 for(auto* t : additionalColorTextures)
168 delete t;
169 additionalColorTextures.clear();
170
171 delete colorRenderBuffer;
172 colorRenderBuffer = nullptr;
173
174 delete depthRenderBuffer;
175 depthRenderBuffer = nullptr;
176
177 delete depthTexture;
178 depthTexture = nullptr;
179
180 delete msDepthTexture;
181 msDepthTexture = nullptr;
182
183 delete renderPass;
184 renderPass = nullptr;
185
186 delete renderTarget;
187 renderTarget = nullptr;
188 }
189 }
190};
191
195struct Image
196{
197 QString path;
198 std::vector<QImage> frames;
199};
200
204SCORE_PLUGIN_GFX_EXPORT
206createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples, bool depth, bool samplableDepth = false);
207
213SCORE_PLUGIN_GFX_EXPORT
215 const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples, bool depth,
216 bool samplableDepth = false, QRhiTexture::Flags flags = {});
217
224SCORE_PLUGIN_GFX_EXPORT
225TextureRenderTarget createRenderTarget(
226 const RenderState& state,
227 std::span<QRhiTexture* const> colorTextures,
228 QRhiTexture* depthTexture,
229 int samples);
230
231SCORE_PLUGIN_GFX_EXPORT
232void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
233SCORE_PLUGIN_GFX_EXPORT
234void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
235SCORE_PLUGIN_GFX_EXPORT
236void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
237
238SCORE_PLUGIN_GFX_EXPORT
239void replaceBuffer(
240 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
241SCORE_PLUGIN_GFX_EXPORT
242void replaceSampler(
243 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
244SCORE_PLUGIN_GFX_EXPORT
245void replaceTexture(
246 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
247
251SCORE_PLUGIN_GFX_EXPORT
252void replaceSampler(
253 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
254
258SCORE_PLUGIN_GFX_EXPORT
259void replaceTexture(
260 QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
261
265SCORE_PLUGIN_GFX_EXPORT
267 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler,
268 QRhiTexture* newTexture);
269
273SCORE_PLUGIN_GFX_EXPORT
274void replaceTexture(
275 QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
279SCORE_PLUGIN_GFX_EXPORT
280QRhiShaderResourceBindings* createDefaultBindings(
281 const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
282 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
283 std::span<QRhiShaderResourceBinding> additionalBindings = {});
284
293SCORE_PLUGIN_GFX_EXPORT
295 QRhiGraphicsPipeline& pip, const QShader& vertexShader,
296 const ossia::geometry& geom);
297
301SCORE_PLUGIN_GFX_EXPORT
302Pipeline buildPipeline(
303 const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
304 const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
305 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
306 std::span<QRhiShaderResourceBinding> additionalBindings = {});
307
313SCORE_PLUGIN_GFX_EXPORT
314std::pair<QShader, QShader>
315makeShaders(const RenderState& v, QString vert, QString frag);
316
322SCORE_PLUGIN_GFX_EXPORT
323QShader makeCompute(const RenderState& v, QString compt);
324
330struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
331{
332 void init(
333 RenderList& renderer, const std::vector<Port*>& input,
334 ossia::small_vector<Sampler, 8>& samplers);
335
336 QRhiBuffer* buffer{};
337 int size{};
338};
339
343SCORE_PLUGIN_GFX_EXPORT
344QSize resizeTextureSize(QSize img, int min, int max) noexcept;
345
349SCORE_PLUGIN_GFX_EXPORT
350QImage resizeTexture(const QImage& img, int min, int max) noexcept;
351
352inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
353{
354 memcpy(ptr, mat.constData(), sizeof(float) * 16);
355}
356inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
357{
358 memcpy(ptr, mat.constData(), sizeof(float) * 9);
359}
360
364SCORE_PLUGIN_GFX_EXPORT
365QSizeF computeScaleForMeshSizing(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
366
370SCORE_PLUGIN_GFX_EXPORT
372 score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
373
378 QRhiResourceUpdateBatch* ub
379 , QRhiBuffer* buf
380 , int offset
381 , int64_t bytesize
382 , const char* data
383 )
384{
385#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
386 ub->updateDynamicBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
387#else
388 ub->updateDynamicBuffer(buf, offset, bytesize, data);
389#endif
390}
391
393 QRhiResourceUpdateBatch* ub
394 , QRhiBuffer* buf
395 , int offset
396 , QByteArray b
397 )
398{
399#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
400 ub->updateDynamicBuffer(buf, offset, std::move(b));
401#else
402 ub->updateDynamicBuffer(buf, offset, b.size(), b.data());
403#endif
404}
405
410 QRhiResourceUpdateBatch* ub
411 , QRhiBuffer* buf
412 , int offset
413 , int64_t bytesize
414 , const char* data
415 )
416{
417#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
418 ub->uploadStaticBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
419#else
420 ub->uploadStaticBuffer(buf, offset, bytesize, data);
421#endif
422}
423
425 QRhiResourceUpdateBatch* ub
426 , QRhiBuffer* buf
427 , int offset
428 , QByteArray b
429 )
430{
431#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
432 ub->uploadStaticBuffer(buf, offset, std::move(b));
433#else
434 ub->uploadStaticBuffer(buf, offset, b.size(), b.data());
435#endif
436}
437
438SCORE_PLUGIN_GFX_EXPORT
439std::vector<Sampler> initInputSamplers(
440 const score::gfx::Node& node, RenderList& renderer, const std::vector<Port*>& ports);
441}
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:866
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:377
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:441
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:575
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:679
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:16
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:409
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:816
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:826
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:369
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:652
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:765
Data model for audio data being sent to the GPU.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:39
Utility to represent a shader material following score conventions.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:331
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:78
Image data and metadata.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:196
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:107
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:57
void * value
Pointer to the corresponding data.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:62
score::gfx::Node * node
Parent node of the port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:59
Types type
Type of the value.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:65
std::vector< Edge * > edges
Edges connected to that port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:71
Flag flags
Optional setting flags.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:68
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:30
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:125