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 namespace score::gfx
13 {
14 class Node;
15 class NodeModel;
16 struct Port;
17 struct Edge;
18 class RenderList;
22 struct Sampler
23 {
24  QRhiSampler* sampler{};
25  QRhiTexture* texture{};
26 };
27 
32 {
33  ossia::hash_map<RenderList*, Sampler> samplers;
34 
35  std::vector<float> data;
36  int channels{};
37  int fixedSize{0};
38  int rectUniformOffset{};
39  bool fft{};
40 };
41 
45 struct Port
46 {
49 
51  void* value{};
52 
54  Types type{};
55 
57  std::vector<Edge*> edges;
58 };
59 
63 struct Edge
64 {
65  Edge(Port* source, Port* sink)
66  : source{source}
67  , sink{sink}
68  {
69  source->edges.push_back(this);
70  sink->edges.push_back(this);
71  }
72 
73  ~Edge()
74  {
75  if(auto it = std::find(source->edges.begin(), source->edges.end(), this);
76  it != source->edges.end())
77  source->edges.erase(it);
78  if(auto it = std::find(sink->edges.begin(), sink->edges.end(), this);
79  it != sink->edges.end())
80  sink->edges.erase(it);
81  }
82 
83  Port* source{};
84  Port* sink{};
85 };
86 
90 struct Pipeline
91 {
92  QRhiGraphicsPipeline* pipeline{};
93  QRhiShaderResourceBindings* srb{};
94 
95  void release()
96  {
97  delete pipeline;
98  pipeline = nullptr;
99 
100  delete srb;
101  srb = nullptr;
102  }
103 };
104 
109 {
110  QRhiTexture* texture{};
111  QRhiRenderBuffer* colorRenderBuffer{};
112  QRhiRenderBuffer* depthRenderBuffer{};
113  QRhiRenderPassDescriptor* renderPass{};
114  QRhiRenderTarget* renderTarget{};
115 
116  operator bool() const noexcept { return texture != nullptr; }
117 
118  void release()
119  {
120  if(texture)
121  {
122  delete texture;
123  texture = nullptr;
124 
125  delete colorRenderBuffer;
126  colorRenderBuffer = nullptr;
127 
128  delete depthRenderBuffer;
129  depthRenderBuffer = nullptr;
130 
131  delete renderPass;
132  renderPass = nullptr;
133 
134  delete renderTarget;
135  renderTarget = nullptr;
136  }
137  }
138 };
139 
143 struct Image
144 {
145  QString path;
146  std::vector<QImage> frames;
147 };
148 
152 SCORE_PLUGIN_GFX_EXPORT
154 createRenderTarget(const RenderState& state, QRhiTexture* tex, int samples);
155 
161 SCORE_PLUGIN_GFX_EXPORT
163  const RenderState& state, QRhiTexture::Format fmt, QSize sz, int samples,
164  QRhiTexture::Flags = {});
165 
166 SCORE_PLUGIN_GFX_EXPORT
167 void replaceBuffer(QRhiShaderResourceBindings&, int binding, QRhiBuffer* newBuffer);
168 SCORE_PLUGIN_GFX_EXPORT
169 void replaceSampler(QRhiShaderResourceBindings&, int binding, QRhiSampler* newSampler);
170 SCORE_PLUGIN_GFX_EXPORT
171 void replaceTexture(QRhiShaderResourceBindings&, int binding, QRhiTexture* newTexture);
172 
173 SCORE_PLUGIN_GFX_EXPORT
174 void replaceBuffer(
175  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiBuffer* newBuffer);
176 SCORE_PLUGIN_GFX_EXPORT
177 void replaceSampler(
178  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiSampler* newSampler);
179 SCORE_PLUGIN_GFX_EXPORT
180 void replaceTexture(
181  std::vector<QRhiShaderResourceBinding>&, int binding, QRhiTexture* newTexture);
182 
186 SCORE_PLUGIN_GFX_EXPORT
187 void replaceSampler(
188  QRhiShaderResourceBindings&, QRhiSampler* oldSampler, QRhiSampler* newSampler);
189 
193 SCORE_PLUGIN_GFX_EXPORT
194 void replaceTexture(
195  QRhiShaderResourceBindings&, QRhiSampler* sampler, QRhiTexture* newTexture);
196 
200 SCORE_PLUGIN_GFX_EXPORT
201 void replaceTexture(
202  QRhiShaderResourceBindings& srb, QRhiTexture* old_tex, QRhiTexture* new_tex);
206 SCORE_PLUGIN_GFX_EXPORT
207 QRhiShaderResourceBindings* createDefaultBindings(
208  const RenderList& renderer, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
209  QRhiBuffer* materialUBO, const std::vector<Sampler>& samplers);
210 
214 SCORE_PLUGIN_GFX_EXPORT
215 Pipeline buildPipeline(
216  const RenderList& renderer, const Mesh& mesh, const QShader& vertexS,
217  const QShader& fragmentS, const TextureRenderTarget& rt, QRhiBuffer* processUBO,
218  QRhiBuffer* materialUBO, const std::vector<Sampler>& samplers);
219 
225 SCORE_PLUGIN_GFX_EXPORT
226 std::pair<QShader, QShader>
227 makeShaders(const RenderState& v, QString vert, QString frag);
228 
234 SCORE_PLUGIN_GFX_EXPORT
235 QShader makeCompute(const RenderState& v, QString compt);
236 
242 struct SCORE_PLUGIN_GFX_EXPORT DefaultShaderMaterial
243 {
244  void init(
245  RenderList& renderer, const std::vector<Port*>& input,
246  std::vector<Sampler>& samplers);
247 
248  QRhiBuffer* buffer{};
249  int size{};
250 };
251 
255 SCORE_PLUGIN_GFX_EXPORT
256 QSize resizeTextureSize(QSize img, int min, int max) noexcept;
257 
261 SCORE_PLUGIN_GFX_EXPORT
262 QImage resizeTexture(const QImage& img, int min, int max) noexcept;
263 
264 inline void copyMatrix(const QMatrix4x4& mat, float* ptr) noexcept
265 {
266  memcpy(ptr, mat.constData(), sizeof(float) * 16);
267 }
268 inline void copyMatrix(const QMatrix3x3& mat, float* ptr) noexcept
269 {
270  memcpy(ptr, mat.constData(), sizeof(float) * 9);
271 }
272 
276 SCORE_PLUGIN_GFX_EXPORT
277 QSizeF computeScale(score::gfx::ScaleMode mode, QSizeF viewport, QSizeF texture);
278 }
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: PreviewWidget.hpp:12
QRhiShaderResourceBindings * createDefaultBindings(const RenderList &renderer, const TextureRenderTarget &rt, QRhiBuffer *processUBO, QRhiBuffer *materialUBO, const std::vector< Sampler > &samplers)
Create bindings following the score conventions for shaders and materials.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:265
QShader makeCompute(const RenderState &v, QString compute)
Compile a compute shader.
Definition: score-plugin-gfx/Gfx/Graph/Utils.cpp:358
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:495
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:505
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:334
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:444
Data model for audio data being sent to the GPU.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:32
Utility to represent a shader material following score conventions.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:243
Connection between two score::gfx::Port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:64
Image data and metadata.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:144
Useful abstraction for storing a graphics pipeline and associated resource bindings.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:91
Port of a score::gfx::Node.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:46
void * value
Pointer to the corresponding data.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:51
score::gfx::Node * node
Parent node of the port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:48
Types type
Type of the value.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:54
std::vector< Edge * > edges
Edges connected to that port.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:57
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:23
Useful abstraction for storing all the data related to a render target.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:109