1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "renderextension.h"
#include <QtCore/qpointer.h>
#include <QtCore/qfile.h>
#include <rhi/qshader.h>
#include <rhi/qrhi.h>
#include <ssg/qssgrenderextensions.h>
#include <ssg/qssgrenderhelpers.h>
#include <ssg/qssgrendercontextcore.h>
#include <ssg/qssgrhicontext.h>
#include <ssg/qquick3dextensionhelpers.h>
static constexpr float triangle[] = {
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
};
class TextureProvider : public QSSGRenderExtension
{
public:
explicit TextureProvider(RenderExtension *ext);
bool prepareData(QSSGFrameData &data) override;
void prepareRender(QSSGFrameData &data) override;
void render(QSSGFrameData &data) override;
void resetForFrame() override;
RenderMode mode() const override { return QSSGRenderExtension::RenderMode::Standalone; }
RenderStage stage() const override { return QSSGRenderExtension::RenderStage::PreColor; }
private:
static QShader getShader(const QString &name)
{
QFile f(name);
return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
}
QPointer<RenderExtension> m_ext;
std::unique_ptr<QRhiTexture> texture;
std::unique_ptr<QRhiTextureRenderTarget> rt;
std::unique_ptr<QRhiRenderPassDescriptor> rpDesc;
std::unique_ptr<QRhiBuffer> vbuf;
std::unique_ptr<QRhiBuffer> ubuf;
std::unique_ptr<QRhiShaderResourceBindings> srb;
std::unique_ptr<QRhiGraphicsPipeline> ps;
float rotation = 0.0f;
};
TextureProvider::TextureProvider(RenderExtension *ext)
: m_ext(ext)
{
}
bool TextureProvider::prepareData(QSSGFrameData &data)
{
Q_UNUSED(data);
bool ret = true;
return ret;
}
void TextureProvider::prepareRender(QSSGFrameData &data)
{
const auto &ctxIfx = data.contextInterface();
const auto &rhiCtx = ctxIfx->rhiContext();
if (!rhiCtx)
return;
QRhi *rhi = rhiCtx->rhi();
if (!texture) {
texture.reset(rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget));
texture->create();
rt.reset(rhi->newTextureRenderTarget({ texture.get() }));
rpDesc.reset(rt->newCompatibleRenderPassDescriptor());
rt->setRenderPassDescriptor(rpDesc.get());
rt->create();
QSSGExtensionId extensionId = QQuick3DExtensionHelpers::getExtensionId(*m_ext);
Q_ASSERT(!QQuick3DExtensionHelpers::isNull(extensionId));
QSSGRenderExtensionHelpers::registerRenderResult(data, extensionId, texture.get());
}
if (!ps) {
vbuf.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(triangle)));
vbuf->create();
ubuf.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64));
ubuf->create();
srb.reset(rhi->newShaderResourceBindings());
srb->setBindings({
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, ubuf.get()),
});
srb->create();
ps.reset(rhi->newGraphicsPipeline());
ps->setShaderStages({
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/shaders/color.vert.qsb")) },
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/shaders/color.frag.qsb")) }
});
QRhiVertexInputLayout inputLayout;
inputLayout.setBindings({
{ 5 * sizeof(float) }
});
inputLayout.setAttributes({
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 },
{ 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
});
ps->setVertexInputLayout(inputLayout);
ps->setShaderResourceBindings(srb.get());
ps->setRenderPassDescriptor(rpDesc.get());
ps->create();
QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
QRhiResourceUpdateBatch *resourceUpdates = rhi->nextResourceUpdateBatch();
resourceUpdates->uploadStaticBuffer(vbuf.get(), triangle);
cb->resourceUpdate(resourceUpdates);
}
}
void TextureProvider::render(QSSGFrameData &data)
{
const auto &ctxIfx = data.contextInterface();
const auto &rhiCtx = ctxIfx->rhiContext();
if (!rhiCtx)
return;
QRhi *rhi = rhiCtx->rhi();
QRhiResourceUpdateBatch *resourceUpdates = rhi->nextResourceUpdateBatch();
const QSize outputSize = rt->pixelSize();
QMatrix4x4 viewProjection = rhi->clipSpaceCorrMatrix();
viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
viewProjection.translate(0, 0, -4);
QMatrix4x4 modelViewProjection = viewProjection;
modelViewProjection.rotate(rotation, 0, 1, 0);
resourceUpdates->updateDynamicBuffer(ubuf.get(), 0, 64, modelViewProjection.constData());
QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f);
cb->beginPass(rt.get(), clearColor, { 1.0f, 0}, resourceUpdates);
cb->setGraphicsPipeline(ps.get());
cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height()));
cb->setShaderResources();
const QRhiCommandBuffer::VertexInput vbufBinding(vbuf.get(), 0);
cb->setVertexInput(0, 1, &vbufBinding);
cb->draw(3);
cb->endPass();
}
void TextureProvider::resetForFrame()
{
}
RenderExtension::RenderExtension()
{
}
QSSGRenderGraphObject *RenderExtension::updateSpatialNode(QSSGRenderGraphObject *node)
{
TextureProvider *renderNode = static_cast<TextureProvider *>(node);
if (!renderNode)
renderNode = new TextureProvider(this);
return renderNode;
}
|