TexgenNode.hpp
1 #pragma once
2 
3 #include <Gfx/Graph/Node.hpp>
4 #include <Gfx/Graph/NodeRenderer.hpp>
5 #include <Gfx/Graph/RenderList.hpp>
6 #include <Gfx/Graph/RenderState.hpp>
7 #include <Gfx/Graph/Uniforms.hpp>
8 
9 namespace score::gfx
10 {
11 
13 {
14  static const constexpr auto vertex = R"_(#version 450
15  layout(location = 0) in vec2 position;
16  layout(location = 1) in vec2 texcoord;
17 
18  layout(binding = 3) uniform sampler2D y_tex;
19  layout(location = 0) out vec2 v_texcoord;
20 
21  layout(std140, binding = 0) uniform renderer_t {
22  mat4 clipSpaceCorrMatrix;
23  vec2 renderSize;
24  } renderer;
25 
26  out gl_PerVertex { vec4 gl_Position; };
27 
28  void main()
29  {
30  v_texcoord = texcoord;
31  gl_Position = renderer.clipSpaceCorrMatrix * vec4(position.xy, 0.0, 1.);
32  }
33  )_";
34 
35  static const constexpr auto filter = R"_(#version 450
36  layout(location = 0) in vec2 v_texcoord;
37  layout(location = 0) out vec4 fragColor;
38 
39  layout(std140, binding = 0) uniform renderer_t {
40  mat4 clipSpaceCorrMatrix;
41  vec2 renderSize;
42  } renderer;
43 
44  layout(binding=3) uniform sampler2D y_tex;
45 
46 
47  void main ()
48  {
49  fragColor = texture(y_tex, v_texcoord);
50  }
51  )_";
52 
53  struct ubo
54  {
55  int currentImageIndex{};
56  float pad;
57  float position[2];
58  } ubo;
59 
61  {
62  using GenericNodeRenderer::GenericNodeRenderer;
63 
64  ~Rendered() { }
65 
66  QRhiTexture* texture{};
67  void init(RenderList& renderer, QRhiResourceUpdateBatch& res) override
68  {
69  const auto& mesh = renderer.defaultTriangle();
70  defaultMeshInit(renderer, mesh, res);
71  processUBOInit(renderer);
72  m_material.init(renderer, node.input, m_samplers);
73  std::tie(m_vertexS, m_fragmentS)
74  = score::gfx::makeShaders(renderer.state, vertex, filter);
75 
76  auto& n = static_cast<const TexgenNode&>(this->node);
77  auto& rhi = *renderer.state.rhi;
78  {
79  texture
80  = rhi.newTexture(QRhiTexture::RGBA8, n.image.size(), 1, QRhiTexture::Flag{});
81 
82  texture->create();
83  }
84 
85  {
86  auto sampler = rhi.newSampler(
87  QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
88  QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
89 
90  sampler->create();
91  m_samplers.push_back({sampler, texture});
92  }
93  defaultPassesInit(renderer, mesh);
94  }
95 
96  void update(RenderList& renderer, QRhiResourceUpdateBatch& res) override
97  {
98  defaultUBOUpdate(renderer, res);
99  auto& n = static_cast<const TexgenNode&>(this->node);
100  if(func_t f = n.function.load())
101  {
102  f(const_cast<uchar*>(n.image.bits()), n.image.width(), n.image.height(), t++);
103  res.uploadTexture(texture, n.image);
104  }
105  }
106 
107  void release(RenderList& r) override
108  {
109  texture->deleteLater();
110  texture = nullptr;
111 
112  defaultRelease(r);
113  }
114 
115  int t = 0;
116  };
117 
118  QImage image;
119  TexgenNode()
120  {
121  image = QImage{QSize(640, 480), QImage::Format_ARGB32_Premultiplied};
122  output.push_back(new Port{this, {}, Types::Image, {}});
123  }
124  virtual ~TexgenNode()
125  {
126  m_materialData.release();
127  }
128 
129  using func_t = void (*)(unsigned char* rgb, int width, int height, int t);
130  std::atomic<func_t> function{};
131 
133  {
134  return new Rendered{*this};
135  }
136 };
137 
138 
139 }
Generic renderer.
Definition: NodeRenderer.hpp:59
std::vector< Port * > input
Input ports of that node.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:89
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:95
Common base class for most single-pass, simple nodes.
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:180
Renderer for a given node.
Definition: NodeRenderer.hpp:11
List of nodes to be rendered to an output.
Definition: RenderList.hpp:19
const score::gfx::Mesh & defaultTriangle() const noexcept
A triangle mesh correct for this API.
Definition: RenderList.cpp:247
RenderState & state
RenderState corresponding to this RenderList.
Definition: RenderList.hpp:89
Graphics rendering pipeline for ossia score.
Definition: PreviewWidget.hpp:12
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
Port of a score::gfx::Node.
Definition: score-plugin-gfx/Gfx/Graph/Utils.hpp:46
Definition: TexgenNode.hpp:61
Definition: TexgenNode.hpp:54
Definition: TexgenNode.hpp:13
score::gfx::NodeRenderer * createRenderer(RenderList &r) const noexcept override
Create a renderer in a given context for this node.
Definition: TexgenNode.hpp:132
Definition: Uniforms.hpp:5