2#include <Gfx/Graph/encoders/GPUVideoEncoder.hpp>
38#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
45 static constexpr const char* y_frag = R
"_(#version 450
46 layout(location = 0) in vec2 v_texcoord;
47 layout(location = 0) out vec4 fragColor;
48 layout(binding = 3) uniform sampler2D src_tex;
50 vec2 flip_y(vec2 tc) {
51 // Only OpenGL. The rest of the engine puts its geometry through
52 // renderer.clipSpaceCorrMatrix, which negates Y on Vulkan; this pass draws
53 // a hardcoded triangle in raw NDC and does not, so the correction it needs
54 // is not the same one. Flipping on Vulkan as well handed libav, GStreamer,
55 // NDI and every other consumer an upside-down picture.
56 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
59 return vec2(tc.x, 1.0 - tc.y);
63 vec3 rgb = texture(src_tex, flip_y(v_texcoord)).rgb;
64 vec3 yuv = convert_from_rgb(rgb);
65 fragColor = vec4(clamp(yuv.x, 0.0, 1.0) * (65280.0 / 65535.0), 0.0, 0.0, 1.0);
72 static constexpr const char* uv_frag = R
"_(#version 450
73 layout(location = 0) in vec2 v_texcoord;
74 layout(location = 0) out vec4 fragColor;
75 layout(binding = 3) uniform sampler2D src_tex;
77 vec2 flip_y(vec2 tc) {
78 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
81 return vec2(tc.x, 1.0 - tc.y);
85 ivec2 sz = textureSize(src_tex, 0);
86 // The source row through flip_y, as in the luma pass: this target has
87 // the source's height, so each of its rows is one source row.
88 int sy = min(int(floor(flip_y(v_texcoord).y * float(sz.y))), sz.y - 1);
89 // Two source pixels per chroma site.
90 int x0 = int(floor(v_texcoord.x * float(sz.x / 2))) * 2;
91 vec3 yuv0 = convert_from_rgb(
92 texelFetch(src_tex, ivec2(min(x0, sz.x - 1), sy), 0).rgb);
93 vec3 yuv1 = convert_from_rgb(
94 texelFetch(src_tex, ivec2(min(x0 + 1, sz.x - 1), sy), 0).rgb);
95 vec2 uv = clamp((yuv0.yz + yuv1.yz) * 0.5, 0.0, 1.0);
96 fragColor = vec4(uv * (65280.0 / 65535.0), 0.0, 1.0);
100 QRhiTexture* m_yTexture{};
101 QRhiTextureRenderTarget* m_yRT{};
102 QRhiRenderPassDescriptor* m_yRP{};
103 QRhiShaderResourceBindings* m_ySRB{};
104 QRhiGraphicsPipeline* m_yPipeline{};
105 QRhiReadbackResult m_yReadback{};
107 QRhiTexture* m_uvTexture{};
108 QRhiTextureRenderTarget* m_uvRT{};
109 QRhiRenderPassDescriptor* m_uvRP{};
110 QRhiShaderResourceBindings* m_uvSRB{};
111 QRhiGraphicsPipeline* m_uvPipeline{};
112 QRhiReadbackResult m_uvReadback{};
114 QRhiSampler* m_sampler{};
117 bool m_readbackEnabled{
true};
120 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
121 QRhiTexture::Format fmt,
int w,
int h,
const char* frag,
122 const QString& colorConversion, QRhiTexture*& tex,
123 QRhiTextureRenderTarget*& rt, QRhiRenderPassDescriptor*& rp,
124 QRhiShaderResourceBindings*& srb, QRhiGraphicsPipeline*& pipeline)
126 tex = rhi.newTexture(
128 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
131 rt = rhi.newTextureRenderTarget({tex});
132 rp = rt->newCompatibleRenderPassDescriptor();
133 rt->setRenderPassDescriptor(rp);
136 srb = rhi.newShaderResourceBindings();
138 QRhiShaderResourceBinding::sampledTexture(
139 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler),
145 QString::fromLatin1(frag).arg(colorConversion));
146 pipeline = rhi.newGraphicsPipeline();
147 pipeline->setShaderStages({
148 {QRhiShaderStage::Vertex, vs},
149 {QRhiShaderStage::Fragment, fs},
151 pipeline->setVertexInputLayout({});
152 pipeline->setShaderResourceBindings(srb);
153 pipeline->setRenderPassDescriptor(rp);
158 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
int width,
159 int height,
const QString& colorConversion)
override
164 m_sampler = rhi.newSampler(
165 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
166 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
170 rhi, state, inputRGBA, QRhiTexture::R16, width, height, y_frag,
171 colorConversion, m_yTexture, m_yRT, m_yRP, m_ySRB, m_yPipeline);
173 rhi, state, inputRGBA, QRhiTexture::RG16, width / 2, height, uv_frag,
174 colorConversion, m_uvTexture, m_uvRT, m_uvRP, m_uvSRB, m_uvPipeline);
177 void exec(QRhi& rhi, QRhiCommandBuffer& cb)
override
179 cb.beginPass(m_yRT, Qt::black, {0.0f, 0});
180 cb.setGraphicsPipeline(m_yPipeline);
181 cb.setShaderResources(m_ySRB);
182 cb.setViewport(QRhiViewport(0, 0, m_width, m_height));
184 if(m_readbackEnabled)
186 auto* yb = rhi.nextResourceUpdateBatch();
187 yb->readBackTexture(QRhiReadbackDescription{m_yTexture}, &m_yReadback);
195 cb.beginPass(m_uvRT, Qt::black, {0.0f, 0});
196 cb.setGraphicsPipeline(m_uvPipeline);
197 cb.setShaderResources(m_uvSRB);
198 cb.setViewport(QRhiViewport(0, 0, m_width / 2, m_height));
200 if(m_readbackEnabled)
202 auto* uvb = rhi.nextResourceUpdateBatch();
203 uvb->readBackTexture(QRhiReadbackDescription{m_uvTexture}, &m_uvReadback);
214 const QRhiReadbackResult&
readback(
int plane)
const override
216 return plane == 0 ? m_yReadback : m_uvReadback;
223 return plane == 0 ? m_yTexture : m_uvTexture;
241 m_uvPipeline = m_yPipeline =
nullptr;
242 m_uvSRB = m_ySRB =
nullptr;
243 m_uvRP = m_yRP =
nullptr;
244 m_uvRT = m_yRT =
nullptr;
245 m_uvTexture = m_yTexture =
nullptr;
250struct P216Encoder : GPUVideoEncoder
252 static constexpr const char* common = R
"_(
253 int flip_y_int(int y, int h) {
254 // See GPUVideoEncoder::y_flip_glsl: only OpenGL. The rest of the engine
255 // negates Y through renderer.clipSpaceCorrMatrix on Vulkan; this pass
256 // indexes texels directly and does not, so it must not flip there.
257 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
263 // Component (0=Y,1=U,2=V) as a 16-bit word: the 8-bit code shifted left
264 // by 8, so limited-range white is 235 << 8 == 60160.
265 uint s16(vec3 rgb, int c) {
266 vec3 yuv = clamp(convert_from_rgb(rgb), 0.0, 1.0);
267 float v = (c == 0) ? yuv.x : (c == 1 ? yuv.y : yuv.z);
268 return uint(v * 65280.0 + 0.5);
270 vec4 pack2(uint a, uint b) {
271 return vec4(float(a & 0xFFu), float((a >> 8u) & 0xFFu),
272 float(b & 0xFFu), float((b >> 8u) & 0xFFu)) / 255.0;
277 static constexpr const char* y_frag = R
"_(#version 450
278 layout(location = 0) in vec2 v_texcoord;
279 layout(location = 0) out vec4 fragColor;
280 layout(binding = 3) uniform sampler2D src_tex;
284 ivec2 sz = textureSize(src_tex, 0);
285 ivec2 o = ivec2(gl_FragCoord.xy);
286 int sy = flip_y_int(o.y, sz.y);
288 uint a = s16(texelFetch(src_tex, ivec2(min(x0, sz.x - 1), sy), 0).rgb, 0);
289 uint b = s16(texelFetch(src_tex, ivec2(min(x0 + 1, sz.x - 1), sy), 0).rgb, 0);
290 fragColor = pack2(a, b);
296 static constexpr const char* uv_frag = R
"_(#version 450
297 layout(location = 0) in vec2 v_texcoord;
298 layout(location = 0) out vec4 fragColor;
299 layout(binding = 3) uniform sampler2D src_tex;
302 uint avg2(int x, int y, ivec2 sz, int c) {
303 int xa = min(x, sz.x - 1), xb = min(x + 1, sz.x - 1);
304 uint s = s16(texelFetch(src_tex, ivec2(xa, y), 0).rgb, c)
305 + s16(texelFetch(src_tex, ivec2(xb, y), 0).rgb, c);
309 ivec2 sz = textureSize(src_tex, 0);
310 ivec2 o = ivec2(gl_FragCoord.xy);
312 int sy = flip_y_int(o.y, sz.y);
313 fragColor = pack2(avg2(x0, sy, sz, 1), avg2(x0, sy, sz, 2));
317 struct PlaneResources
319 QRhiTexture* texture{};
320 QRhiTextureRenderTarget* rt{};
321 QRhiRenderPassDescriptor* rp{};
322 QRhiShaderResourceBindings* srb{};
323 QRhiGraphicsPipeline* pipeline{};
324 QRhiReadbackResult readback{};
329 delete pipeline;
delete srb;
delete rp;
delete rt;
delete texture;
330 pipeline =
nullptr; srb =
nullptr; rp =
nullptr; rt =
nullptr; texture =
nullptr;
334 PlaneResources m_y, m_uv;
335 QRhiSampler* m_sampler{};
338 bool m_readbackEnabled{
true};
341 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
342 PlaneResources& p,
int packW,
int packH,
const QString& frag)
346 p.texture = rhi.newTexture(
347 QRhiTexture::RGBA8, QSize{packW, packH}, 1,
348 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
350 p.rt = rhi.newTextureRenderTarget({p.texture});
351 p.rp = p.rt->newCompatibleRenderPassDescriptor();
352 p.rt->setRenderPassDescriptor(p.rp);
354 p.srb = rhi.newShaderResourceBindings();
355 p.srb->setBindings({QRhiShaderResourceBinding::sampledTexture(
356 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler)});
359 p.pipeline = rhi.newGraphicsPipeline();
360 p.pipeline->setShaderStages({
361 {QRhiShaderStage::Vertex, vs}, {QRhiShaderStage::Fragment, fs}});
362 p.pipeline->setVertexInputLayout({});
363 p.pipeline->setShaderResourceBindings(p.srb);
364 p.pipeline->setRenderPassDescriptor(p.rp);
365 p.pipeline->create();
368 void execPlane(QRhi& rhi, QRhiCommandBuffer& cb, PlaneResources& p)
370 cb.beginPass(p.rt, Qt::black, {0.0f, 0});
371 cb.setGraphicsPipeline(p.pipeline);
372 cb.setShaderResources(p.srb);
373 cb.setViewport(QRhiViewport(0, 0, p.w, p.h));
375 if(m_readbackEnabled)
377 auto* batch = rhi.nextResourceUpdateBatch();
378 batch->readBackTexture(QRhiReadbackDescription{p.texture}, &p.readback);
388 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
int width,
389 int height,
const QString& colorConversion)
override
393 m_sampler = rhi.newSampler(
394 QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
395 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
400 const QString y = QString::fromLatin1(y_frag)
401 .arg(colorConversion)
402 .arg(QString::fromLatin1(common));
403 const QString uv = QString::fromLatin1(uv_frag)
404 .arg(colorConversion)
405 .arg(QString::fromLatin1(common));
406 initPlane(rhi, state, inputRGBA, m_y, width / 2, height, y);
407 initPlane(rhi, state, inputRGBA, m_uv, width / 2, height, uv);
410 void exec(QRhi& rhi, QRhiCommandBuffer& cb)
override
412 execPlane(rhi, cb, m_y);
413 execPlane(rhi, cb, m_uv);
418 const QRhiReadbackResult&
readback(
int plane)
const override
420 return plane == 0 ? m_y.readback : m_uv.readback;
425 return plane == 0 ? m_y.texture : m_uv.texture;
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:1707
Base class for GPU-side video format conversion (RGBA to YUV).
Definition GPUVideoEncoder.hpp:30
static constexpr const char * vertex_shader
Definition GPUVideoEncoder.hpp:74
GPU RGBA -> p216 encoder (semi-planar 4:2:2, 16-bit).
Definition P216.hpp:40
const QRhiReadbackResult & readback(int plane) const override
Get the readback result for a given plane. Valid after endOffscreenFrame.
Definition P216.hpp:208
void exec(QRhi &rhi, QRhiCommandBuffer &cb) override
Definition P216.hpp:171
void setReadbackEnabled(bool e) noexcept override
Definition P216.hpp:220
QRhiTexture * planeTexture(int plane) const noexcept
Definition P216.hpp:215
void init(QRhi &rhi, const RenderState &state, QRhiTexture *inputRGBA, int width, int height, const QString &colorConversion) override
Definition P216.hpp:151
void release() override
Release all GPU resources.
Definition P216.hpp:222
int planeCount() const override
Number of readback planes (1 for UYVY, 2 for NV12, 3 for I420).
Definition P216.hpp:206
Global state associated to a rendering context.
Definition RenderState.hpp:40