Loading...
Searching...
No Matches
score-plugin-gfx/Gfx/Graph/Utils.hpp
1#pragma once
2
3#include <Gfx/Graph/Mesh.hpp>
4#include <Gfx/Graph/RenderState.hpp>
5#include <Gfx/Graph/Scale.hpp>
6#include <Gfx/Graph/Uniforms.hpp>
7
8#include <ossia/detail/hash_map.hpp>
9#include <ossia/detail/small_flat_map.hpp>
10
11#include <score_plugin_gfx_export.h>
12
13#include <span>
14
15namespace score::gfx
16{
17class Node;
18class NodeModel;
19struct Port;
20struct Edge;
21class RenderList;
25struct Sampler
26{
27 QRhiSampler* sampler{};
28 QRhiTexture* texture{};
29};
30
35{
36 ossia::hash_map<RenderList*, Sampler> samplers;
37
38 std::vector<float> data;
39 int channels{};
40 int fixedSize{0};
41 enum Mode
42 {
43 Waveform,
44 FFT,
45 Histogram
46 } mode{};
47};
48
52struct Port
53{
56
58 void* value{};
59
61 Types type{};
62
64 Flag flags{};
65
67 std::vector<Edge*> edges;
68};
69
73struct Edge
74{
75 Edge(Port* source, Port* sink)
76 : source{source}
77 , sink{sink}
78 {
79 source->edges.push_back(this);
80 sink->edges.push_back(this);
81 }
82
83 ~Edge()
84 {
85 if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
86 it != source->edges.end())
87 source->edges.erase(it);
88 if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
89 it != sink->edges.end())
90 sink->edges.erase(it);
91 }
92
93 Port* source{};
94 Port* sink{};
95};
96
101{
102 QRhiGraphicsPipeline* pipeline{};
103 QRhiShaderResourceBindings* srb{};
104
105 void release()
106 {
107 delete pipeline;
108 pipeline = nullptr;
109
110 delete srb;
111 srb = nullptr;
112 }
113};
114
119{
120 QRhiTexture* texture{};
121 QRhiRenderBuffer* colorRenderBuffer{};
122 QRhiRenderBuffer* depthRenderBuffer{};
123 QRhiRenderPassDescriptor* renderPass{};
124 QRhiRenderTarget* renderTarget{};
125
126 operator bool() const noexcept { return texture != nullptr; }
127
128 void release()
129 {
130 if(texture)
131 {
132 delete texture;
133 texture = nullptr;
134
135 delete colorRenderBuffer;
136 colorRenderBuffer = nullptr;
137
138 delete depthRenderBuffer;
139 depthRenderBuffer = nullptr;
140
141 delete renderPass;
142 renderPass = nullptr;
143
144 delete renderTarget;
145 renderTarget = nullptr;
146 }
147 }
148};
149
153struct Image
154{
155 QString path;
156 std::vector<QImage> frames;
157};
158
162SCORE_PLUGIN_GFX_EXPORT
164createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples, bool depth);
165
171SCORE_PLUGIN_GFX_EXPORT
173 const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples, bool depth,
174 QRhiTexture::Flags = {});
175
176SCORE_PLUGIN_GFX_EXPORT
177void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
178SCORE_PLUGIN_GFX_EXPORT
179void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
180SCORE_PLUGIN_GFX_EXPORT
181void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
182
183SCORE_PLUGIN_GFX_EXPORT
184void replaceBuffer(
185 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
186SCORE_PLUGIN_GFX_EXPORT
187void replaceSampler(
188 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
189SCORE_PLUGIN_GFX_EXPORT
190void replaceTexture(
191 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
192
196SCORE_PLUGIN_GFX_EXPORT
197void replaceSampler(
198 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
199
203SCORE_PLUGIN_GFX_EXPORT
204void replaceTexture(
205 QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
206
210SCORE_PLUGIN_GFX_EXPORT
212 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler,
213 QRhiTexture* newTexture);
214
218SCORE_PLUGIN_GFX_EXPORT
219void replaceTexture(
220 QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
224SCORE_PLUGIN_GFX_EXPORT
225QRhiShaderResourceBindings* createDefaultBindings(
226 const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
227 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
228 std::span<QRhiShaderResourceBinding> additionalBindings = {});
229
233SCORE_PLUGIN_GFX_EXPORT
234Pipeline buildPipeline(
235 const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
236 const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
237 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
238 std::span<QRhiShaderResourceBinding> additionalBindings = {});
239
245SCORE_PLUGIN_GFX_EXPORT
246std::pair<QShader, QShader>
247makeShaders(const RenderState& v, QString vert, QString frag);
248
254SCORE_PLUGIN_GFX_EXPORT
255QShader makeCompute(const RenderState& v, QString compt);
256
262struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
263{
264 void init(
265 RenderList& renderer, const std::vector<Port*>& input,
266 ossia::small_vector<Sampler, 8>& samplers);
267
268 QRhiBuffer* buffer{};
269 int size{};
270};
271
275SCORE_PLUGIN_GFX_EXPORT
276QSize resizeTextureSize(QSize img, int min, int max) noexcept;
277
281SCORE_PLUGIN_GFX_EXPORT
282QImage resizeTexture(const QImage& img, int min, int max) noexcept;
283
284inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
285{
286 memcpy(ptr, mat.constData(), sizeof(float) * 16);
287}
288inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
289{
290 memcpy(ptr, mat.constData(), sizeof(float) * 9);
291}
292
296SCORE_PLUGIN_GFX_EXPORT
297QSizeF computeScaleForMeshSizing(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
298
302SCORE_PLUGIN_GFX_EXPORT
304 score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
305
310 QRhiResourceUpdateBatch* ub
311 , QRhiBuffer* buf
312 , int offset
313 , int64_t bytesize
314 , const char* data
315 )
316{
317#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
318 ub->updateDynamicBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
319#else
320 ub->updateDynamicBuffer(buf, offset, bytesize, data);
321#endif
322}
323
325 QRhiResourceUpdateBatch* ub
326 , QRhiBuffer* buf
327 , int offset
328 , QByteArray b
329 )
330{
331#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
332 ub->updateDynamicBuffer(buf, offset, std::move(b));
333#else
334 ub->updateDynamicBuffer(buf, offset, b.size(), b.data());
335#endif
336}
337
342 QRhiResourceUpdateBatch* ub
343 , QRhiBuffer* buf
344 , int offset
345 , int64_t bytesize
346 , const char* data
347 )
348{
349#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
350 ub->uploadStaticBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
351#else
352 ub->uploadStaticBuffer(buf, offset, bytesize, data);
353#endif
354}
355
357 QRhiResourceUpdateBatch* ub
358 , QRhiBuffer* buf
359 , int offset
360 , QByteArray b
361 )
362{
363#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
364 ub->uploadStaticBuffer(buf, offset, std::move(b));
365#else
366 ub->uploadStaticBuffer(buf, offset, b.size(), b.data());
367#endif
368}
369
370SCORE_PLUGIN_GFX_EXPORT
371std::vector<Sampler> initInputSamplers(
372 const score::gfx::Node& node, RenderList& renderer, const std::vector<Port*>& ports,
373 ossia::small_flat_map<const Port*, TextureRenderTarget, 2>& m_rts);
374}
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:608
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:309
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:317
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:421
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:341
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:558
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:568
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:200
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:394
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples, bool depth)
Create a render target from a texture.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:10
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:507
Data model for audio data being sent to the GPU.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:35
Utility to represent a shader material following score conventions.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:263
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:74
Image data and metadata.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:154
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:101
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:53
void * value
Pointer to the corresponding data.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:58
score::gfx::Node * node
Parent node of the port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:55
Types type
Type of the value.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:61
std::vector< Edge * > edges
Edges connected to that port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:67
Flag flags
Optional setting flags.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:64
Global state associated to a rendering context.
Definition RenderState.hpp:35
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:26
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:119