Loading...
Searching...
No Matches
BGRA.hpp
1#pragma once
2#include <Gfx/Graph/encoders/GPUVideoEncoder.hpp>
3
4namespace score::gfx
5{
6
20{
21 static constexpr const char* frag = R"_(#version 450
22 layout(location = 0) in vec2 v_texcoord;
23 layout(location = 0) out vec4 fragColor;
24 layout(binding = 3) uniform sampler2D src_tex;
25
26 vec2 flip_y(vec2 tc) {
27 #if defined(QSHADER_MSL) || defined(QSHADER_HLSL)
28 return tc;
29 #else
30 return vec2(tc.x, 1.0 - tc.y);
31 #endif
32 }
33
34 void main() {
35 vec4 c = texture(src_tex, flip_y(v_texcoord));
36 fragColor = vec4(c.b, c.g, c.r, c.a);
37 }
38 )_";
39
40 QRhiTexture* m_outTexture{};
41 QRhiTextureRenderTarget* m_renderTarget{};
42 QRhiRenderPassDescriptor* m_rpDesc{};
43 QRhiSampler* m_sampler{};
44 QRhiShaderResourceBindings* m_srb{};
45 QRhiGraphicsPipeline* m_pipeline{};
46 QRhiReadbackResult m_readback{};
47 int m_width{};
48 int m_height{};
49
50 void init(
51 QRhi& rhi, const RenderState& state, QRhiTexture* inputRGBA, int width,
52 int height, const QString& /*colorConversion*/ = colorMatrixOut()) override
53 {
54 m_width = width;
55 m_height = height;
56
57 m_outTexture = rhi.newTexture(
58 QRhiTexture::RGBA8, QSize{width, height}, 1,
59 QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
60 m_outTexture->create();
61
62 m_renderTarget = rhi.newTextureRenderTarget({m_outTexture});
63 m_rpDesc = m_renderTarget->newCompatibleRenderPassDescriptor();
64 m_renderTarget->setRenderPassDescriptor(m_rpDesc);
65 m_renderTarget->create();
66
67 m_sampler = rhi.newSampler(
68 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
69 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
70 m_sampler->create();
71
72 m_srb = rhi.newShaderResourceBindings();
73 m_srb->setBindings({
74 QRhiShaderResourceBinding::sampledTexture(
75 3, QRhiShaderResourceBinding::FragmentStage, inputRGBA, m_sampler),
76 });
77 m_srb->create();
78
79 auto [vertS, fragS] = makeShaders(
80 state, QString::fromLatin1(vertex_shader), QString::fromLatin1(frag));
81
82 m_pipeline = rhi.newGraphicsPipeline();
83 m_pipeline->setShaderStages({
84 {QRhiShaderStage::Vertex, vertS},
85 {QRhiShaderStage::Fragment, fragS},
86 });
87 m_pipeline->setVertexInputLayout({});
88 m_pipeline->setShaderResourceBindings(m_srb);
89 m_pipeline->setRenderPassDescriptor(m_rpDesc);
90 m_pipeline->create();
91 }
92
93 void exec(QRhi& rhi, QRhiCommandBuffer& cb) override
94 {
95 cb.beginPass(m_renderTarget, Qt::black, {1.0f, 0});
96 cb.setGraphicsPipeline(m_pipeline);
97 cb.setShaderResources(m_srb);
98 cb.setViewport(QRhiViewport(0, 0, m_width, m_height));
99 cb.draw(3);
100
101 auto* readbackBatch = rhi.nextResourceUpdateBatch();
102 readbackBatch->readBackTexture(QRhiReadbackDescription{m_outTexture}, &m_readback);
103 cb.endPass(readbackBatch);
104 }
106 int planeCount() const override { return 1; }
108 const QRhiReadbackResult& readback(int) const override { return m_readback; }
109
110 void release() override
111 {
112 delete m_pipeline;
113 m_pipeline = nullptr;
114 delete m_srb;
115 m_srb = nullptr;
116 delete m_sampler;
117 m_sampler = nullptr;
118 delete m_rpDesc;
119 m_rpDesc = nullptr;
120 delete m_renderTarget;
121 m_renderTarget = nullptr;
122 delete m_outTexture;
123 m_outTexture = nullptr;
124 }
125};
126
127} // namespace score::gfx
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:647
Cross-backend RGBA -> BGRA byte-order encoder.
Definition BGRA.hpp:20
const QRhiReadbackResult & readback(int) const override
Get the readback result for a given plane. Valid after endOffscreenFrame.
Definition BGRA.hpp:105
int planeCount() const override
Number of readback planes (1 for UYVY, 2 for NV12, 3 for I420).
Definition BGRA.hpp:103
void exec(QRhi &rhi, QRhiCommandBuffer &cb) override
Definition BGRA.hpp:90
void release() override
Release all GPU resources.
Definition BGRA.hpp:107
Base class for GPU-side video format conversion (RGBA to YUV).
Definition GPUVideoEncoder.hpp:30
static constexpr const char * vertex_shader
Definition GPUVideoEncoder.hpp:63