24 static constexpr const char* y_frag = R
"_(#version 450
25 layout(location = 0) in vec2 v_texcoord;
26 layout(location = 0) out vec4 fragColor;
27 layout(binding = 3) uniform sampler2D src_tex;
29 vec2 flip_y(vec2 tc) {
30 #if defined(QSHADER_MSL) || defined(QSHADER_HLSL)
33 return vec2(tc.x, 1.0 - tc.y);
37 vec3 rgb = texture(src_tex, flip_y(v_texcoord)).rgb;
38 vec3 yuv = convert_from_rgb(rgb);
39 fragColor = vec4(yuv.x, 0.0, 0.0, 1.0);
43 static constexpr const char* u_frag = R
"_(#version 450
44 layout(location = 0) in vec2 v_texcoord;
45 layout(location = 0) out vec4 fragColor;
46 layout(binding = 3) uniform sampler2D src_tex;
48 vec2 flip_y(vec2 tc) {
49 #if defined(QSHADER_MSL) || defined(QSHADER_HLSL)
52 return vec2(tc.x, 1.0 - tc.y);
56 vec3 rgb = texture(src_tex, flip_y(v_texcoord)).rgb;
57 vec3 yuv = convert_from_rgb(rgb);
58 fragColor = vec4(yuv.y, 0.0, 0.0, 1.0);
62 static constexpr const char* v_frag = R
"_(#version 450
63 layout(location = 0) in vec2 v_texcoord;
64 layout(location = 0) out vec4 fragColor;
65 layout(binding = 3) uniform sampler2D src_tex;
67 vec2 flip_y(vec2 tc) {
68 #if defined(QSHADER_MSL) || defined(QSHADER_HLSL)
71 return vec2(tc.x, 1.0 - tc.y);
75 vec3 rgb = texture(src_tex, flip_y(v_texcoord)).rgb;
76 vec3 yuv = convert_from_rgb(rgb);
77 fragColor = vec4(yuv.z, 0.0, 0.0, 1.0);
83 QRhiTexture* texture{};
84 QRhiTextureRenderTarget* rt{};
85 QRhiRenderPassDescriptor* rp{};
86 QRhiShaderResourceBindings* srb{};
87 QRhiGraphicsPipeline* pipeline{};
88 QRhiReadbackResult readback{};
106 QRhiSampler* m_sampler{};
111 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
112 PlaneResources& plane,
int w,
int h,
const char* fragTemplate,
113 const QString& colorConversion)
115 plane.texture = rhi.newTexture(
116 QRhiTexture::R8, QSize{w, h}, 1,
117 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
118 plane.texture->create();
120 plane.rt = rhi.newTextureRenderTarget({plane.texture});
121 plane.rp = plane.rt->newCompatibleRenderPassDescriptor();
122 plane.rt->setRenderPassDescriptor(plane.rp);
125 plane.srb = rhi.newShaderResourceBindings();
126 plane.srb->setBindings({
127 QRhiShaderResourceBinding::sampledTexture(
128 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler),
134 QString::fromLatin1(fragTemplate).arg(colorConversion));
135 plane.pipeline = rhi.newGraphicsPipeline();
136 plane.pipeline->setShaderStages({
137 {QRhiShaderStage::Vertex, vs},
138 {QRhiShaderStage::Fragment, fs},
140 plane.pipeline->setVertexInputLayout({});
141 plane.pipeline->setShaderResourceBindings(plane.srb);
142 plane.pipeline->setRenderPassDescriptor(plane.rp);
143 plane.pipeline->create();
146 void execPlane(QRhi& rhi, QRhiCommandBuffer& cb, PlaneResources& plane,
int w,
int h)
148 cb.beginPass(plane.rt, Qt::black, {1.0f, 0});
149 cb.setGraphicsPipeline(plane.pipeline);
150 cb.setShaderResources(plane.srb);
151 cb.setViewport(QRhiViewport(0, 0, w, h));
154 auto* batch = rhi.nextResourceUpdateBatch();
155 batch->readBackTexture(QRhiReadbackDescription{plane.texture}, &plane.readback);
160 QRhi& rhi,
const RenderState& state, QRhiTexture* inputRGBA,
int width,
161 int height,
const QString& colorConversion = colorMatrixOut())
override
166 m_sampler = rhi.newSampler(
167 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
168 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
171 initPlane(rhi, state, inputRGBA, m_planes[0], width, height, y_frag, colorConversion);
172 initPlane(rhi, state, inputRGBA, m_planes[1], width / 2, height / 2, u_frag, colorConversion);
173 initPlane(rhi, state, inputRGBA, m_planes[2], width / 2, height / 2, v_frag, colorConversion);
176 void exec(QRhi& rhi, QRhiCommandBuffer& cb)
override
178 execPlane(rhi, cb, m_planes[0], m_width, m_height);
179 execPlane(rhi, cb, m_planes[1], m_width / 2, m_height / 2);
180 execPlane(rhi, cb, m_planes[2], m_width / 2, m_height / 2);
185 const QRhiReadbackResult&
readback(
int plane)
const override
187 return m_planes[plane].readback;
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:395