2#include <Gfx/Graph/encoders/GPUVideoEncoder.hpp>
28#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
31 static constexpr const char* pack_fn = R
"_(
32 // 10-bit value -> high 10 bits of a 16-bit UNORM word (low 6 bits zero).
33 float pack10hi(float v) {
34 return round(clamp(v, 0.0, 1.0) * 1023.0) * 64.0 / 65535.0;
39 static constexpr const char* y_frag = R
"_(#version 450
40 layout(location = 0) in vec2 v_texcoord;
41 layout(location = 0) out vec4 fragColor;
42 layout(binding = 3) uniform sampler2D src_tex;
45 vec2 flip_y(vec2 tc) {
46 // Only OpenGL. The rest of the engine puts its geometry through
47 // renderer.clipSpaceCorrMatrix, which negates Y on Vulkan; this pass draws
48 // a hardcoded triangle in raw NDC and does not, so the correction it needs
49 // is not the same one. Flipping on Vulkan as well handed libav, GStreamer,
50 // NDI and every other consumer an upside-down picture.
51 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
54 return vec2(tc.x, 1.0 - tc.y);
58 vec3 rgb = texture(src_tex, flip_y(v_texcoord)).rgb;
59 vec3 yuv = convert_from_rgb(rgb);
60 fragColor = vec4(pack10hi(yuv.x), 0.0, 0.0, 1.0);
67 static constexpr const char* uv_frag = R
"_(#version 450
68 layout(location = 0) in vec2 v_texcoord;
69 layout(location = 0) out vec4 fragColor;
70 layout(binding = 3) uniform sampler2D src_tex;
73 vec2 flip_y(vec2 tc) {
74 // Only OpenGL. The rest of the engine puts its geometry through
75 // renderer.clipSpaceCorrMatrix, which negates Y on Vulkan; this pass draws
76 // a hardcoded triangle in raw NDC and does not, so the correction it needs
77 // is not the same one. Flipping on Vulkan as well handed libav, GStreamer,
78 // NDI and every other consumer an upside-down picture.
79 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
82 return vec2(tc.x, 1.0 - tc.y);
85 vec3 yuv_at(int x, int y, ivec2 sz) {
86 return convert_from_rgb(
87 texelFetch(src_tex, ivec2(min(x, sz.x - 1), min(y, sz.y - 1)), 0).rgb);
90 ivec2 sz = textureSize(src_tex, 0);
91 // The chroma row through flip_y, as in the luma pass; each covers two
92 // source rows, as each chroma column covers two source columns.
93 int cy = int(floor(flip_y(v_texcoord).y * float(sz.y / 2)));
94 int cx = int(floor(v_texcoord.x * float(sz.x / 2)));
95 int x0 = cx * 2, y0 = cy * 2;
96 vec3 yuv = (yuv_at(x0, y0, sz) + yuv_at(x0 + 1, y0, sz)
97 + yuv_at(x0, y0 + 1, sz) + yuv_at(x0 + 1, y0 + 1, sz)) * 0.25;
98 fragColor = vec4(pack10hi(yuv.y), pack10hi(yuv.z), 0.0, 1.0);
103 QRhiTexture* m_yTexture{};
104 QRhiTextureRenderTarget* m_yRT{};
105 QRhiRenderPassDescriptor* m_yRP{};
106 QRhiShaderResourceBindings* m_ySRB{};
107 QRhiGraphicsPipeline* m_yPipeline{};
108 QRhiReadbackResult m_yReadback{};
111 QRhiTexture* m_uvTexture{};
112 QRhiTextureRenderTarget* m_uvRT{};
113 QRhiRenderPassDescriptor* m_uvRP{};
114 QRhiShaderResourceBindings* m_uvSRB{};
115 QRhiGraphicsPipeline* m_uvPipeline{};
116 QRhiReadbackResult m_uvReadback{};
118 QRhiSampler* m_sampler{};
123 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
124 QRhiTexture::Format fmt,
int w,
int h,
const char* frag,
125 const QString& colorConversion, QRhiTexture*& tex,
126 QRhiTextureRenderTarget*& rt, QRhiRenderPassDescriptor*& rp,
127 QRhiShaderResourceBindings*& srb, QRhiGraphicsPipeline*& pipeline)
129 tex = rhi.newTexture(
131 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
134 rt = rhi.newTextureRenderTarget({tex});
135 rp = rt->newCompatibleRenderPassDescriptor();
136 rt->setRenderPassDescriptor(rp);
139 srb = rhi.newShaderResourceBindings();
141 QRhiShaderResourceBinding::sampledTexture(
142 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler),
148 QString::fromLatin1(frag).arg(colorConversion).arg(pack_fn));
149 pipeline = rhi.newGraphicsPipeline();
150 pipeline->setShaderStages({
151 {QRhiShaderStage::Vertex, vs},
152 {QRhiShaderStage::Fragment, fs},
154 pipeline->setVertexInputLayout({});
155 pipeline->setShaderResourceBindings(srb);
156 pipeline->setRenderPassDescriptor(rp);
161 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
int width,
162 int height,
const QString& colorConversion)
override
167 m_sampler = rhi.newSampler(
168 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
169 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
173 rhi, state, inputRGBA, QRhiTexture::R16, width, height, y_frag,
174 colorConversion, m_yTexture, m_yRT, m_yRP, m_ySRB, m_yPipeline);
176 rhi, state, inputRGBA, QRhiTexture::RG16, width / 2, height / 2, uv_frag,
177 colorConversion, m_uvTexture, m_uvRT, m_uvRP, m_uvSRB, m_uvPipeline);
180 void exec(QRhi& rhi, QRhiCommandBuffer& cb)
override
182 cb.beginPass(m_yRT, Qt::black, {0.0f, 0});
183 cb.setGraphicsPipeline(m_yPipeline);
184 cb.setShaderResources(m_ySRB);
185 cb.setViewport(QRhiViewport(0, 0, m_width, m_height));
187 auto* yb = rhi.nextResourceUpdateBatch();
188 yb->readBackTexture(QRhiReadbackDescription{m_yTexture}, &m_yReadback);
191 cb.beginPass(m_uvRT, Qt::black, {0.0f, 0});
192 cb.setGraphicsPipeline(m_uvPipeline);
193 cb.setShaderResources(m_uvSRB);
194 cb.setViewport(QRhiViewport(0, 0, m_width / 2, m_height / 2));
196 auto* uvb = rhi.nextResourceUpdateBatch();
197 uvb->readBackTexture(QRhiReadbackDescription{m_uvTexture}, &m_uvReadback);
203 const QRhiReadbackResult&
readback(
int plane)
const override
205 return plane == 0 ? m_yReadback : m_uvReadback;
221 m_uvPipeline = m_yPipeline =
nullptr;
222 m_uvSRB = m_ySRB =
nullptr;
223 m_uvRP = m_yRP =
nullptr;
224 m_uvRT = m_yRT =
nullptr;
225 m_uvTexture = m_yTexture =
nullptr;
230struct P010Encoder : GPUVideoEncoder
232 static constexpr const char* common = R
"_(
233 int flip_y_int(int y, int h) {
234 // See GPUVideoEncoder::y_flip_glsl: only OpenGL. The rest of the engine
235 // negates Y through renderer.clipSpaceCorrMatrix on Vulkan; this pass
236 // indexes texels directly and does not, so it must not flip there.
237 #if defined(QSHADER_SPIRV) || defined(QSHADER_MSL) || defined(QSHADER_HLSL)
243 // 10-bit component (0=Y,1=U,2=V) shifted into the high 10 bits of 16 bits.
244 uint hi10(vec3 rgb, int c) {
245 vec3 yuv = clamp(convert_from_rgb(rgb), 0.0, 1.0);
246 float v = (c == 0) ? yuv.x : (c == 1 ? yuv.y : yuv.z);
247 return uint(v * 1023.0 + 0.5) << 6u;
249 vec4 pack2(uint a, uint b) {
250 return vec4(float(a & 0xFFu), float((a >> 8u) & 0xFFu),
251 float(b & 0xFFu), float((b >> 8u) & 0xFFu)) / 255.0;
256 static constexpr const char* y_frag = R
"_(#version 450
257 layout(location = 0) in vec2 v_texcoord;
258 layout(location = 0) out vec4 fragColor;
259 layout(binding = 3) uniform sampler2D src_tex;
263 ivec2 sz = textureSize(src_tex, 0);
264 ivec2 o = ivec2(gl_FragCoord.xy);
265 int sy = flip_y_int(o.y, sz.y);
267 uint a = hi10(texelFetch(src_tex, ivec2(min(x0, sz.x - 1), sy), 0).rgb, 0);
268 uint b = hi10(texelFetch(src_tex, ivec2(min(x0 + 1, sz.x - 1), sy), 0).rgb, 0);
269 fragColor = pack2(a, b);
275 static constexpr const char* uv_frag = R
"_(#version 450
276 layout(location = 0) in vec2 v_texcoord;
277 layout(location = 0) out vec4 fragColor;
278 layout(binding = 3) uniform sampler2D src_tex;
281 uint avg(int x, int y0, int y1, ivec2 sz, int c) {
282 int xa = min(x, sz.x - 1), xb = min(x + 1, sz.x - 1);
283 uint s = hi10(texelFetch(src_tex, ivec2(xa, y0), 0).rgb, c)
284 + hi10(texelFetch(src_tex, ivec2(xb, y0), 0).rgb, c)
285 + hi10(texelFetch(src_tex, ivec2(xa, y1), 0).rgb, c)
286 + hi10(texelFetch(src_tex, ivec2(xb, y1), 0).rgb, c);
287 return (s >> 2u) & 0xFFC0u; // keep 10 bits in the high position
290 ivec2 sz = textureSize(src_tex, 0);
291 ivec2 o = ivec2(gl_FragCoord.xy);
293 int y0 = flip_y_int(o.y * 2, sz.y);
294 int y1 = flip_y_int(o.y * 2 + 1, sz.y);
295 fragColor = pack2(avg(x0, y0, y1, sz, 1), avg(x0, y0, y1, sz, 2));
299 struct PlaneResources
301 QRhiTexture* texture{};
302 QRhiTextureRenderTarget* rt{};
303 QRhiRenderPassDescriptor* rp{};
304 QRhiShaderResourceBindings* srb{};
305 QRhiGraphicsPipeline* pipeline{};
306 QRhiReadbackResult readback{};
311 delete pipeline;
delete srb;
delete rp;
delete rt;
delete texture;
312 pipeline =
nullptr; srb =
nullptr; rp =
nullptr; rt =
nullptr; texture =
nullptr;
316 PlaneResources m_y, m_uv;
317 QRhiSampler* m_sampler{};
322 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
323 PlaneResources& p,
int packW,
int packH,
const QString& frag)
327 p.texture = rhi.newTexture(
328 QRhiTexture::RGBA8, QSize{packW, packH}, 1,
329 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
331 p.rt = rhi.newTextureRenderTarget({p.texture});
332 p.rp = p.rt->newCompatibleRenderPassDescriptor();
333 p.rt->setRenderPassDescriptor(p.rp);
335 p.srb = rhi.newShaderResourceBindings();
336 p.srb->setBindings({QRhiShaderResourceBinding::sampledTexture(
337 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler)});
340 p.pipeline = rhi.newGraphicsPipeline();
341 p.pipeline->setShaderStages({
342 {QRhiShaderStage::Vertex, vs}, {QRhiShaderStage::Fragment, fs}});
343 p.pipeline->setVertexInputLayout({});
344 p.pipeline->setShaderResourceBindings(p.srb);
345 p.pipeline->setRenderPassDescriptor(p.rp);
346 p.pipeline->create();
349 void execPlane(QRhi& rhi, QRhiCommandBuffer& cb, PlaneResources& p)
351 cb.beginPass(p.rt, Qt::black, {0.0f, 0});
352 cb.setGraphicsPipeline(p.pipeline);
353 cb.setShaderResources(p.srb);
354 cb.setViewport(QRhiViewport(0, 0, p.w, p.h));
356 auto* batch = rhi.nextResourceUpdateBatch();
357 batch->readBackTexture(QRhiReadbackDescription{p.texture}, &p.readback);
362 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
int width,
363 int height,
const QString& colorConversion)
override
367 m_sampler = rhi.newSampler(
368 QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
369 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
372 const QString cm = QString::fromLatin1(common);
373 const QString y = QString::fromLatin1(y_frag).arg(colorConversion, cm);
374 const QString uv = QString::fromLatin1(uv_frag).arg(colorConversion, cm);
375 initPlane(rhi, state, inputRGBA, m_y, width / 2, height, y);
376 initPlane(rhi, state, inputRGBA, m_uv, width / 2, height / 2, uv);
379 void exec(QRhi& rhi, QRhiCommandBuffer& cb)
override
381 execPlane(rhi, cb, m_y);
382 execPlane(rhi, cb, m_uv);
387 const QRhiReadbackResult&
readback(
int plane)
const override
389 return plane == 0 ? m_y.readback : m_uv.readback;
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:1523
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 -> p010le encoder (semi-planar 4:2:0, 10-bit).
Definition encoders/P010.hpp:30
void release() override
Release all GPU resources.
Definition encoders/P010.hpp:202
void init(QRhi &rhi, const RenderState &state, QRhiTexture *inputRGBA, int width, int height, const QString &colorConversion) override
Definition encoders/P010.hpp:154
int planeCount() const override
Number of readback planes (1 for UYVY, 2 for NV12, 3 for I420).
Definition encoders/P010.hpp:195
const QRhiReadbackResult & readback(int plane) const override
Get the readback result for a given plane. Valid after endOffscreenFrame.
Definition encoders/P010.hpp:197
void exec(QRhi &rhi, QRhiCommandBuffer &cb) override
Definition encoders/P010.hpp:174
Global state associated to a rendering context.
Definition RenderState.hpp:40