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 
10 #include <score_plugin_gfx_export.h>
11 
12 #include <span>
13 
14 namespace score::gfx
15 {
16 class Node;
17 class NodeModel;
18 struct Port;
19 struct Edge;
20 class RenderList;
24 struct Sampler
25 {
26  QRhiSampler* sampler{};
27  QRhiTexture* texture{};
28 };
29 
34 {
35  ossia::hash_map<RenderList*, Sampler> samplers;
36 
37  std::vector<float> data;
38  int channels{};
39  int fixedSize{0};
40  int rectUniformOffset{};
41  bool fft{};
42 };
43 
47 struct Port
48 {
51 
53  void* value{};
54 
56  Types type{};
57 
59  std::vector<Edge*> edges;
60 };
61 
65 struct Edge
66 {
67  Edge(Port* source, Port* sink)
68  : source{source}
69  , sink{sink}
70  {
71  source->edges.push_back(this);
72  sink->edges.push_back(this);
73  }
74 
75  ~Edge()
76  {
77  if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
78  it != source->edges.end())
79  source->edges.erase(it);
80  if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
81  it != sink->edges.end())
82  sink->edges.erase(it);
83  }
84 
85  Port* source{};
86  Port* sink{};
87 };
88 
92 struct Pipeline
93 {
94  QRhiGraphicsPipeline* pipeline{};
95  QRhiShaderResourceBindings* srb{};
96 
97  void release()
98  {
99  delete pipeline;
100  pipeline = nullptr;
101 
102  delete srb;
103  srb = nullptr;
104  }
105 };
106 
111 {
112  QRhiTexture* texture{};
113  QRhiRenderBuffer* colorRenderBuffer{};
114  QRhiRenderBuffer* depthRenderBuffer{};
115  QRhiRenderPassDescriptor* renderPass{};
116  QRhiRenderTarget* renderTarget{};
117 
118  operator bool() const noexcept { return texture != nullptr; }
119 
120  void release()
121  {
122  if(texture)
123  {
124  delete texture;
125  texture = nullptr;
126 
127  delete colorRenderBuffer;
128  colorRenderBuffer = nullptr;
129 
130  delete depthRenderBuffer;
131  depthRenderBuffer = nullptr;
132 
133  delete renderPass;
134  renderPass = nullptr;
135 
136  delete renderTarget;
137  renderTarget = nullptr;
138  }
139  }
140 };
141 
145 struct Image
146 {
147  QString path;
148  std::vector<QImage> frames;
149 };
150 
154 SCORE_PLUGIN_GFX_EXPORT
156 createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples);
157 
163 SCORE_PLUGIN_GFX_EXPORT
165  const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples,
166  QRhiTexture::Flags = {});
167 
168 SCORE_PLUGIN_GFX_EXPORT
169 void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
170 SCORE_PLUGIN_GFX_EXPORT
171 void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
172 SCORE_PLUGIN_GFX_EXPORT
173 void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
174 
175 SCORE_PLUGIN_GFX_EXPORT
176 void replaceBuffer(
177  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
178 SCORE_PLUGIN_GFX_EXPORT
179 void replaceSampler(
180  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
181 SCORE_PLUGIN_GFX_EXPORT
182 void replaceTexture(
183  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
184 
188 SCORE_PLUGIN_GFX_EXPORT
189 void replaceSampler(
190  QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
191 
195 SCORE_PLUGIN_GFX_EXPORT
196 void replaceTexture(
197  QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
198 
202 SCORE_PLUGIN_GFX_EXPORT
203 void replaceTexture(
204  QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
208 SCORE_PLUGIN_GFX_EXPORT
209 QRhiShaderResourceBindings* createDefaultBindings(
210  const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
211  QRhiBuffer* materialUBO, const std::vector<Sampler>& samplers,
212  std::span<QRhiShaderResourceBinding> additionalBindings = {});
213 
217 SCORE_PLUGIN_GFX_EXPORT
218 Pipeline buildPipeline(
219  const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
220  const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
221  QRhiBuffer* materialUBO, const std::vector<Sampler>& samplers,
222  std::span<QRhiShaderResourceBinding> additionalBindings = {});
223 
229 SCORE_PLUGIN_GFX_EXPORT
230 std::pair<QShader, QShader>
231 makeShaders(const RenderState& v, QString vert, QString frag);
232 
238 SCORE_PLUGIN_GFX_EXPORT
239 QShader makeCompute(const RenderState& v, QString compt);
240 
246 struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
247 {
248  void init(
249  RenderList& renderer, const std::vector<Port*>& input,
250  std::vector<Sampler>& samplers);
251 
252  QRhiBuffer* buffer{};
253  int size{};
254 };
255 
259 SCORE_PLUGIN_GFX_EXPORT
260 QSize resizeTextureSize(QSize img, int min, int max) noexcept;
261 
265 SCORE_PLUGIN_GFX_EXPORT
266 QImage resizeTexture(const QImage& img, int min, int max) noexcept;
267 
268 inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
269 {
270  memcpy(ptr, mat.constData(), sizeof(float) * 16);
271 }
272 inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
273 {
274  memcpy(ptr, mat.constData(), sizeof(float) * 9);
275 }
276 
280 SCORE_PLUGIN_GFX_EXPORT
281 QSizeF computeScale(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
282 }
Root data model for visual nodes.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:60
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
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:369
QRhiShaderResourceBindings * createDefaultBindings(const RenderList &renderer, const TextureRenderTarget &rt, QRhiBuffer *processUBO, QRhiBuffer *materialUBO, const std::vector< Sampler > &samplers, std::span< QRhiShaderResourceBinding > additionalBindings)
Create bindings following the score conventions for shaders and materials.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:265
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:506
TextureRenderTarget createRenderTarget(const RenderState &state, QRhiTexture *tex, int samples)
Create a render target from a texture.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:10
ScaleMode
How to resize a texture to adapt it to a viewport.
Definition: Scale.hpp:10
QSizeF computeScale(ScaleMode mode, QSizeF viewport, QSizeF texture)
Comput the scale to apply to a texture so that it fits in a GL viewport.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:516
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:342
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:455
Data model for audio data being sent to the GPU.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:34
Utility to represent a shader material following score conventions.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:247
Connection between two score::gfx::Port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:66
Image data and metadata.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:146
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:93
Port of a score::gfx::Node.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:48
void * value
Pointer to the corresponding data.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:53
score::gfx::Node * node
Parent node of the port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:50
Types type
Type of the value.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:56
std::vector< Edge * > edges
Edges connected to that port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:59
Global state associated to a rendering context.
Definition: RenderState.hpp:31
Stores a sampler and the texture currently associated with it.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:25
Useful abstraction for storing all the data related to a render target.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:111