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// The uploaded buffer's row 0 is its top, and it must come out at the top on
33// every backend. Vulkan was left out of this correction, so a JIT Texgen -- and
34// anything else painting a texture from C++ -- came out upside down there,
35// through every sink: the window, the readback outputs and the GPU encoders
36// alike.
37#if !(defined(QSHADER_HLSL) || defined(QSHADER_MSL))
38 gl_Position.y = - gl_Position.y;
39#endif
40 }
41 )_";
42
43 static const constexpr auto filter = R"_(#version 450
44 layout(location = 0) in vec2 v_texcoord;
45 layout(location = 0) out vec4 fragColor;
46
47 layout(std140, binding = 0) uniform renderer_t {
48 mat4 clipSpaceCorrMatrix;
49 vec2 renderSize;
50 } renderer;
51
52 layout(binding=3) uniform sampler2D y_tex;
53
54
55 void main ()
56 {
57 fragColor = texture(y_tex, v_texcoord);
58 }
59 )_";
60
61 struct ubo
62 {
63 int currentImageIndex{};
64 float pad;
65 float position[2];
66 } ubo;
67
69 {
70 using GenericNodeRenderer::GenericNodeRenderer;
71
72 ~Rendered() { }
73
74 QRhiTexture* texture{};
75 void initState(RenderList& renderer, QRhiResourceUpdateBatch& res) override
76 {
77 m_mesh = &renderer.defaultTriangle();
78 defaultMeshInit(renderer, *m_mesh, res);
79 processUBOInit(renderer);
80 m_material.init(renderer, node.input, m_samplers);
81 std::tie(m_vertexS, m_fragmentS)
82 = score::gfx::makeShaders(renderer.state, vertex, filter);
83
84 auto& rhi = *renderer.state.rhi;
85 {
86 vec.resize(640 * 480 * 4);
87 texture = rhi.newTexture(
88 QRhiTexture::RGBA8, QSize(640, 480), 1, QRhiTexture::Flag{});
89
90 texture->create();
91 }
92
93 {
94 auto sampler = rhi.newSampler(
95 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
96 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
97
98 sampler->create();
99 m_samplers.push_back({sampler, texture});
100 }
101
102 m_initialized = true;
103 }
104
105 void update(
106 RenderList& renderer, QRhiResourceUpdateBatch& res,
107 score::gfx::Edge* edge) override
108 {
109 if(!edge)
110 return;
111
112 auto& n = const_cast<TexgenNode&>(static_cast<const TexgenNode&>(this->node));
113 defaultUBOUpdate(renderer, res);
114 auto sz = renderer.renderSize(edge);
115 auto bytes = sz.width() * sz.height() * 4;
116 if(bytes != vec.size())
117 {
118 vec.resize(bytes, boost::container::default_init);
119
120 QRhiTexture* oldtex = texture;
121 QRhiTexture* newtex = renderer.state.rhi->newTexture(
122 QRhiTexture::RGBA8, sz, 1, QRhiTexture::Flag{});
123 newtex->create();
124 for(auto& [edge, pass] : this->m_p)
125 if(pass.p.srb)
126 score::gfx::replaceTexture(*pass.p.srb, m_samplers[0].sampler, newtex);
127 texture = newtex;
128
129 if(oldtex && oldtex != &renderer.emptyTexture())
130 {
131 oldtex->deleteLater();
132 }
133 }
134
135 if(func_t f = n.function.load())
136 {
137 f(vec.data(), sz.width(), sz.height(), t++);
138
139 QRhiTextureSubresourceUploadDescription subdesc{
140 QByteArray::fromRawData((const char*)vec.data(), vec.size())};
141 QRhiTextureUploadEntry entry{0, 0, subdesc};
142 QRhiTextureUploadDescription desc{entry};
143
144 res.uploadTexture(texture, desc);
145 }
146 }
147
148 void releaseState(RenderList& r) override
149 {
150 if(texture)
151 {
152 texture->deleteLater();
153 texture = nullptr;
154 }
155
157 }
158
159 int t = 0;
160 boost::container::vector<unsigned char> vec;
161 };
162
163 TexgenNode() { output.push_back(new Port{this, {}, Types::Image, {}}); }
164 virtual ~TexgenNode()
165 {
166 m_materialData.release();
167 }
168
169 using func_t = void (*)(unsigned char* rgb, int width, int height, int t);
170 std::atomic<func_t> function{};
171
173 {
174 return new Rendered{*this};
175 }
176};
177
178
179}
Generic renderer.
Definition NodeRenderer.hpp:311
void releaseState(RenderList &renderer) override
Definition NodeRenderer.cpp:333
std::vector< Port * > input
Input ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:103
ossia::small_pod_vector< Port *, 1 > output
Output ports of that node.
Definition score-plugin-gfx/Gfx/Graph/Node.hpp:109
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
bool m_initialized
Definition NodeRenderer.hpp:237
List of nodes to be rendered to an output.
Definition RenderList.hpp:30
const score::gfx::Mesh & defaultTriangle() const noexcept
A triangle mesh correct for this API.
Definition RenderList.cpp:984
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:169
QRhiTexture & emptyTexture() const noexcept
Texture to use when a texture is missing (2D)
Definition RenderList.hpp:192
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag, int multiViewCount)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:1238
Base toolkit upon which the software is built.
Definition Application.cpp:117
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:103
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:82
Definition TexgenNode.hpp:69
void initState(RenderList &renderer, QRhiResourceUpdateBatch &res) override
Definition TexgenNode.hpp:75
void releaseState(RenderList &r) override
Definition TexgenNode.hpp:148
Definition TexgenNode.hpp:62
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:172