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 std::vector<Edge*> edges;
65};
66
70struct Edge
71{
72 Edge(Port* source, Port* sink)
73 : source{source}
74 , sink{sink}
75 {
76 source->edges.push_back(this);
77 sink->edges.push_back(this);
78 }
79
80 ~Edge()
81 {
82 if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
83 it != source->edges.end())
84 source->edges.erase(it);
85 if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
86 it != sink->edges.end())
87 sink->edges.erase(it);
88 }
89
90 Port* source{};
91 Port* sink{};
92};
93
98{
99 QRhiGraphicsPipeline* pipeline{};
100 QRhiShaderResourceBindings* srb{};
101
102 void release()
103 {
104 delete pipeline;
105 pipeline = nullptr;
106
107 delete srb;
108 srb = nullptr;
109 }
110};
111
116{
117 QRhiTexture* texture{};
118 QRhiRenderBuffer* colorRenderBuffer{};
119 QRhiRenderBuffer* depthRenderBuffer{};
120 QRhiRenderPassDescriptor* renderPass{};
121 QRhiRenderTarget* renderTarget{};
122
123 operator bool() const noexcept { return texture != nullptr; }
124
125 void release()
126 {
127 if(texture)
128 {
129 delete texture;
130 texture = nullptr;
131
132 delete colorRenderBuffer;
133 colorRenderBuffer = nullptr;
134
135 delete depthRenderBuffer;
136 depthRenderBuffer = nullptr;
137
138 delete renderPass;
139 renderPass = nullptr;
140
141 delete renderTarget;
142 renderTarget = nullptr;
143 }
144 }
145};
146
150struct Image
151{
152 QString path;
153 std::vector<QImage> frames;
154};
155
159SCORE_PLUGIN_GFX_EXPORT
161createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples, bool depth);
162
168SCORE_PLUGIN_GFX_EXPORT
170 const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples, bool depth,
171 QRhiTexture::Flags = {});
172
173SCORE_PLUGIN_GFX_EXPORT
174void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
175SCORE_PLUGIN_GFX_EXPORT
176void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
177SCORE_PLUGIN_GFX_EXPORT
178void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
179
180SCORE_PLUGIN_GFX_EXPORT
181void replaceBuffer(
182 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
183SCORE_PLUGIN_GFX_EXPORT
184void replaceSampler(
185 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
186SCORE_PLUGIN_GFX_EXPORT
187void replaceTexture(
188 std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
189
193SCORE_PLUGIN_GFX_EXPORT
194void replaceSampler(
195 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
196
200SCORE_PLUGIN_GFX_EXPORT
201void replaceTexture(
202 QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
203
207SCORE_PLUGIN_GFX_EXPORT
209 QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler,
210 QRhiTexture* newTexture);
211
215SCORE_PLUGIN_GFX_EXPORT
216void replaceTexture(
217 QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
221SCORE_PLUGIN_GFX_EXPORT
222QRhiShaderResourceBindings* createDefaultBindings(
223 const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
224 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
225 std::span<QRhiShaderResourceBinding> additionalBindings = {});
226
230SCORE_PLUGIN_GFX_EXPORT
231Pipeline buildPipeline(
232 const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
233 const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
234 QRhiBuffer* materialUBO, std::span<const Sampler> samplers,
235 std::span<QRhiShaderResourceBinding> additionalBindings = {});
236
242SCORE_PLUGIN_GFX_EXPORT
243std::pair<QShader, QShader>
244makeShaders(const RenderState& v, QString vert, QString frag);
245
251SCORE_PLUGIN_GFX_EXPORT
252QShader makeCompute(const RenderState& v, QString compt);
253
259struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
260{
261 void init(
262 RenderList& renderer, const std::vector<Port*>& input,
263 ossia::small_vector<Sampler, 8>& samplers);
264
265 QRhiBuffer* buffer{};
266 int size{};
267};
268
272SCORE_PLUGIN_GFX_EXPORT
273QSize resizeTextureSize(QSize img, int min, int max) noexcept;
274
278SCORE_PLUGIN_GFX_EXPORT
279QImage resizeTexture(const QImage& img, int min, int max) noexcept;
280
281inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
282{
283 memcpy(ptr, mat.constData(), sizeof(float) * 16);
284}
285inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
286{
287 memcpy(ptr, mat.constData(), sizeof(float) * 9);
288}
289
293SCORE_PLUGIN_GFX_EXPORT
294QSizeF computeScaleForMeshSizing(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
295
299SCORE_PLUGIN_GFX_EXPORT
301 score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
302
307 QRhiResourceUpdateBatch* ub
308 , QRhiBuffer* buf
309 , int offset
310 , int64_t bytesize
311 , const char* data
312 )
313{
314#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
315 ub->updateDynamicBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
316#else
317 ub->updateDynamicBuffer(buf, offset, bytesize, data);
318#endif
319}
320
322 QRhiResourceUpdateBatch* ub
323 , QRhiBuffer* buf
324 , int offset
325 , QByteArray b
326 )
327{
328#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
329 ub->updateDynamicBuffer(buf, offset, std::move(b));
330#else
331 ub->updateDynamicBuffer(buf, offset, b.size(), b.data());
332#endif
333}
334
339 QRhiResourceUpdateBatch* ub
340 , QRhiBuffer* buf
341 , int offset
342 , int64_t bytesize
343 , const char* data
344 )
345{
346#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
347 ub->uploadStaticBuffer(buf, offset, QByteArray::fromRawData(data, bytesize));
348#else
349 ub->uploadStaticBuffer(buf, offset, bytesize, data);
350#endif
351}
352
354 QRhiResourceUpdateBatch* ub
355 , QRhiBuffer* buf
356 , int offset
357 , QByteArray b
358 )
359{
360#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
361 ub->uploadStaticBuffer(buf, offset, std::move(b));
362#else
363 ub->uploadStaticBuffer(buf, offset, b.size(), b.data());
364#endif
365}
366
367SCORE_PLUGIN_GFX_EXPORT
368std::vector<Sampler> initInputSamplers(
369 const score::gfx::Node& node, RenderList& renderer, const std::vector<Port*>& ports,
370 ossia::small_flat_map<const Port*, TextureRenderTarget, 2>& m_rts);
371}
Root data model for visual nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:75
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:306
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:338
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:260
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:71
Image data and metadata.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:151
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:98
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: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:116