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_SPIRV) || 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& rhi = *renderer.state.rhi;
80 {
81 vec.resize(640 * 480 * 4);
82 texture = rhi.newTexture(
83 QRhiTexture::RGBA8, QSize(640, 480), 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 if(!edge)
104 return;
105
106 auto& n = const_cast<TexgenNode&>(static_cast<const TexgenNode&>(this->node));
107 defaultUBOUpdate(renderer, res);
108 auto sz = renderer.renderSize(edge);
109 auto bytes = sz.width() * sz.height() * 4;
110 if(bytes != vec.size())
111 {
112 vec.resize(bytes, boost::container::default_init);
113
114 QRhiTexture* oldtex = texture;
115 QRhiTexture* newtex = renderer.state.rhi->newTexture(
116 QRhiTexture::RGBA8, sz, 1, QRhiTexture::Flag{});
117 newtex->create();
118 for(auto& [edge, pass] : this->m_p)
119 if(pass.srb)
120 score::gfx::replaceTexture(*pass.srb, m_samplers[0].sampler, newtex);
121 texture = newtex;
122
123 if(oldtex && oldtex != &renderer.emptyTexture())
124 {
125 oldtex->deleteLater();
126 }
127 }
128
129 if(func_t f = n.function.load())
130 {
131 f(vec.data(), sz.width(), sz.height(), t++);
132
133 QRhiTextureSubresourceUploadDescription subdesc{
134 QByteArray::fromRawData((const char*)vec.data(), vec.size())};
135 QRhiTextureUploadEntry entry{0, 0, subdesc};
136 QRhiTextureUploadDescription desc{entry};
137
138 res.uploadTexture(texture, desc);
139 }
140 }
141
142 void release(RenderList& r) override
143 {
144 texture->deleteLater();
145 texture = nullptr;
146
147 defaultRelease(r);
148 }
149
150 int t = 0;
151 boost::container::vector<unsigned char> vec;
152 };
153
154 TexgenNode() { output.push_back(new Port{this, {}, Types::Image, {}}); }
155 virtual ~TexgenNode()
156 {
157 m_materialData.release();
158 }
159
160 using func_t = void (*)(unsigned char* rgb, int width, int height, int t);
161 std::atomic<func_t> function{};
162
164 {
165 return new Rendered{*this};
166 }
167};
168
169
170}
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:230
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
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing.
Definition RenderList.hpp:112
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:393
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:70
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:52
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:163