Loading...
Searching...
No Matches
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
9namespace 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#if defined(QSHADER_HLSL) || defined(QSHADER_MSL)
33 gl_Position.y = - gl_Position.y;
34#endif
35 }
36 )_";
37
38 static const constexpr auto filter = R"_(#version 450
39 layout(location = 0) in vec2 v_texcoord;
40 layout(location = 0) out vec4 fragColor;
41
42 layout(std140, binding = 0) uniform renderer_t {
43 mat4 clipSpaceCorrMatrix;
44 vec2 renderSize;
45 } renderer;
46
47 layout(binding=3) uniform sampler2D y_tex;
48
49
50 void main ()
51 {
52 fragColor = texture(y_tex, v_texcoord);
53 }
54 )_";
55
56 struct ubo
57 {
58 int currentImageIndex{};
59 float pad;
60 float position[2];
61 } ubo;
62
64 {
65 using GenericNodeRenderer::GenericNodeRenderer;
66
67 ~Rendered() { }
68
69 QRhiTexture* texture{};
70 void init(RenderList& renderer, QRhiResourceUpdateBatch& res) override
71 {
72 const auto& mesh = renderer.defaultTriangle();
73 defaultMeshInit(renderer, mesh, res);
74 processUBOInit(renderer);
75 m_material.init(renderer, node.input, m_samplers);
76 std::tie(m_vertexS, m_fragmentS)
77 = score::gfx::makeShaders(renderer.state, vertex, filter);
78
79 auto& n = static_cast<const TexgenNode&>(this->node);
80 auto& rhi = *renderer.state.rhi;
81 {
82 texture
83 = rhi.newTexture(QRhiTexture::RGBA8, n.image.size(), 1, QRhiTexture::Flag{});
84
85 texture->create();
86 }
87
88 {
89 auto sampler = rhi.newSampler(
90 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
91 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
92
93 sampler->create();
94 m_samplers.push_back({sampler, texture});
95 }
96 defaultPassesInit(renderer, mesh);
97 }
98
99 void update(
100 RenderList& renderer, QRhiResourceUpdateBatch& res,
101 score::gfx::Edge* edge) override
102 {
103 defaultUBOUpdate(renderer, res);
104 auto& n = static_cast<const TexgenNode&>(this->node);
105 if(func_t f = n.function.load())
106 {
107 f(const_cast<uchar*>(n.image.bits()), n.image.width(), n.image.height(), t++);
108 res.uploadTexture(texture, n.image);
109 }
110 }
111
112 void release(RenderList& r) override
113 {
114 texture->deleteLater();
115 texture = nullptr;
116
117 defaultRelease(r);
118 }
119
120 int t = 0;
121 };
122
123 QImage image;
124 TexgenNode()
125 {
126 image = QImage{QSize(640, 480), QImage::Format_ARGB32_Premultiplied};
127 output.push_back(new Port{this, {}, Types::Image, {}});
128 }
129 virtual ~TexgenNode()
130 {
131 m_materialData.release();
132 }
133
134 using func_t = void (*)(unsigned char* rgb, int width, int height, int t);
135 std::atomic<func_t> function{};
136
138 {
139 return new Rendered{*this};
140 }
141};
142
143
144}
Generic renderer.
Definition NodeRenderer.hpp:84
std::vector< Port * > input
Input ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:104
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:110
Common base class for most single-pass, simple nodes.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:228
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:305
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:89
Graphics rendering pipeline for ossia score.
Definition Filter/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:390
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:66
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:48
Definition TexgenNode.hpp:64
Definition TexgenNode.hpp:57
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:137
Definition Uniforms.hpp:5