Debian Codegraph @

tests/manual/rhi/texuploads/texuploads.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5 #include "../shared/cube.h"
6 #include <QPainter>
7
8 struct {
9 QRhiBuffer *vbuf = nullptr;
10 QRhiBuffer *ubuf = nullptr;
11 QRhiTexture *tex = nullptr;
12 QRhiSampler *sampler = nullptr;
13 QRhiShaderResourceBindings *srb = nullptr;
14 QRhiGraphicsPipeline *ps = nullptr;
15 QList<QRhiResource *> releasePool;
16
17 float rotation = 0;
18 QRhiResourceUpdateBatch *initialUpdates = nullptr;
19 int frameCount = 0;
20 QImage customImage;
21 QRhiTexture *newTex = nullptr;
22 QRhiTexture *importedTex = nullptr;
23 int testStage = 0;
24
25 QRhiShaderResourceBinding bindings[2];
26 } d;
27
28 void Window::customInit()
29 {
30 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
31 d.releasePool << d.vbuf;
32 d.vbuf->create();
33
34 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
35 d.releasePool << d.ubuf;
36 d.ubuf->create();
37
38 QImage baseImage(QLatin1String(":/qt256.png"));
39 d.tex = m_r->newTexture(QRhiTexture::RGBA8, baseImage.size(), 1, QRhiTexture::UsedAsTransferSource);
40 d.releasePool << d.tex;
41 d.tex->create();
42
43 // As an alternative to what some of the other examples do, prepare an
44 // update batch right here instead of relying on vbufReady and similar flags.
45 d.initialUpdates = m_r->nextResourceUpdateBatch();
46 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
47 qint32 flip = 0;
48 d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
49 d.initialUpdates->uploadTexture(d.tex, baseImage);
50
51 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
52 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
53 d.releasePool << d.sampler;
54 d.sampler->create();
55
56 d.srb = m_r->newShaderResourceBindings();
57 d.releasePool << d.srb;
58
59 d.bindings[0] = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf);
60 d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler);
61 d.srb->setBindings(d.bindings, d.bindings + 2);
62 d.srb->create();
63
64 d.ps = m_r->newGraphicsPipeline();
65 d.releasePool << d.ps;
66
67 d.ps->setDepthTest(true);
68 d.ps->setDepthWrite(true);
69 d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
70
71 d.ps->setCullMode(QRhiGraphicsPipeline::Back);
72 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
73
74 const QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
75 if (!vs.isValid())
76 qFatal("Failed to load shader pack (vertex)");
77 const QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
78 if (!fs.isValid())
79 qFatal("Failed to load shader pack (fragment)");
80
81 d.ps->setShaderStages({
82 { QRhiShaderStage::Vertex, vs },
83 { QRhiShaderStage::Fragment, fs }
84 });
85
86 QRhiVertexInputLayout inputLayout;
87 inputLayout.setBindings({
88 { 3 * sizeof(float) },
89 { 2 * sizeof(float) }
90 });
91 inputLayout.setAttributes({
92 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
93 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
94 });
95
96 d.ps->setVertexInputLayout(inputLayout);
97 d.ps->setShaderResourceBindings(d.srb);
98 d.ps->setRenderPassDescriptor(m_rp);
99
100 d.ps->create();
101
102 d.customImage = QImage(128, 64, QImage::Format_RGBA8888);
103 d.customImage.fill(Qt::red);
104 QPainter painter(&d.customImage);
105 // the text may look different on different platforms, so no guarantee the
106 // output on the screen will be identical everywhere
107 painter.drawText(5, 25, "Hello world");
108 painter.end();
109 }
110
111 void Window::customRelease()
112 {
113 qDeleteAll(d.releasePool);
114 d.releasePool.clear();
115 }
116
117 void Window::customRender()
118 {
119 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
120
121 // take the initial set of updates, if this is the first frame
122 if (d.initialUpdates) {
123 u->merge(d.initialUpdates);
124 d.initialUpdates->release();
125 d.initialUpdates = nullptr;
126 }
127
128 d.rotation += 1.0f;
129 QMatrix4x4 mvp = m_proj;
130 mvp.scale(0.5f);
131 mvp.rotate(d.rotation, 0, 1, 0);
132 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
133
134 if (d.frameCount > 0 && (d.frameCount % 100) == 0) {
135 d.testStage += 1;
136 qDebug("testStage = %d", d.testStage);
137
138 // Partially change the texture.
139 if (d.testStage == 1) {
140 QRhiTextureSubresourceUploadDescription mipDesc(d.customImage);
141 // The image here is smaller than the original. Use a non-zero position
142 // to make it more interesting.
143 mipDesc.setDestinationTopLeft(QPoint(100, 20));
144 QRhiTextureUploadDescription desc({ 0, 0, mipDesc });
145 u->uploadTexture(d.tex, desc);
146 }
147
148 // Exercise image copying.
149 if (d.testStage == 2) {
150 const QSize sz = d.tex->pixelSize();
151 d.newTex = m_r->newTexture(QRhiTexture::RGBA8, sz);
152 d.releasePool << d.newTex;
153 d.newTex->create();
154
155 QImage empty(sz.width(), sz.height(), QImage::Format_RGBA8888);
156 empty.fill(Qt::blue);
157 u->uploadTexture(d.newTex, empty);
158
159 // Copy the left-half of tex to the right-half of newTex, while
160 // leaving the left-half of newTex blue. Keep a 20 pixel gap at
161 // the top.
162 QRhiTextureCopyDescription desc;
163 desc.setSourceTopLeft(QPoint(0, 20));
164 desc.setPixelSize(QSize(sz.width() / 2, sz.height() - 20));
165 desc.setDestinationTopLeft(QPoint(sz.width() / 2, 20));
166 u->copyTexture(d.newTex, d.tex, desc);
167
168 // Now replace d.tex with d.newTex as the shader resource.
169 d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.newTex, d.sampler);
170 d.srb->setBindings(d.bindings, d.bindings + 2);
171 // "rebuild", whatever that means for a given backend. This srb is
172 // already live as the ps in the setGraphicsPipeline references it,
173 // but that's fine. Changes will be picked up automatically.
174 d.srb->create();
175 }
176
177 // Exercise simple, full texture copy.
178 if (d.testStage == 4)
179 u->copyTexture(d.newTex, d.tex);
180
181 // Now again upload customImage but this time only a part of it.
182 if (d.testStage == 5) {
183 QRhiTextureSubresourceUploadDescription mipDesc(d.customImage);
184 mipDesc.setDestinationTopLeft(QPoint(10, 120));
185 mipDesc.setSourceSize(QSize(50, 40));
186 mipDesc.setSourceTopLeft(QPoint(20, 10));
187 QRhiTextureUploadDescription desc({ 0, 0, mipDesc });
188 u->uploadTexture(d.newTex, desc);
189 }
190
191 // Exercise texture object export/import.
192 if (d.testStage == 6) {
193 const QRhiTexture::NativeTexture nativeTexture = d.tex->nativeTexture();
194 if (nativeTexture.object) {
195 #if QT_CONFIG(metal)
196 if (graphicsApi == Metal) {
197 qDebug() << "Metal texture: " << nativeTexture.object;
198 // Now could cast to id<MTLTexture> and do something with
199 // it, keeping in mind that copy operations are only done
200 // in beginPass, while rendering into a texture may only
201 // have proper results in current_frame + 2, or after a
202 // finish(). The QRhiTexture still owns the native object.
203 }
204 #endif
205 // omit for other backends, the idea is the same
206
207 d.importedTex = m_r->newTexture(QRhiTexture::RGBA8, d.tex->pixelSize());
208 d.releasePool << d.importedTex;
209 if (!d.importedTex->createFrom(nativeTexture))
210 qWarning("Texture import failed");
211
212 // now d.tex and d.importedTex use the same MTLTexture
213 // underneath (owned by d.tex)
214
215 // switch to showing d.importedTex
216 d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.importedTex, d.sampler);
217 d.srb->setBindings(d.bindings, d.bindings + 2);
218 d.srb->create();
219 } else {
220 qWarning("Accessing native texture object is not supported");
221 }
222 }
223
224 // Exercise uploading uncompressed data without a QImage.
225 if (d.testStage == 7) {
226 d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.newTex, d.sampler);
227 d.srb->setBindings(d.bindings, d.bindings + 2);
228 d.srb->create();
229
230 const QSize sz(221, 139);
231 QByteArray data;
232 data.resize(sz.width() * sz.height() * 4);
233 for (int y = 0; y < sz.height(); ++y) {
234 uchar *p = reinterpret_cast<uchar *>(data.data()) + y * sz.width() * 4;
235 for (int x = 0; x < sz.width(); ++x) {
236 *p++ = 0; *p++ = 0; *p++ = y * (255 / sz.height());
237 *p++ = 255;
238 }
239 }
240 QRhiTextureSubresourceUploadDescription mipDesc(data.constData(), data.size());
241 mipDesc.setSourceSize(sz);
242 mipDesc.setDestinationTopLeft(QPoint(5, 25));
243 QRhiTextureUploadDescription desc({ 0, 0, mipDesc });
244 u->uploadTexture(d.newTex, desc);
245 }
246 }
247
248 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
249 const QSize outputSizeInPixels = m_sc->currentPixelSize();
250
251 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
252
253 cb->setGraphicsPipeline(d.ps);
254 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
255 cb->setShaderResources();
256 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
257 { d.vbuf, 0 },
258 { d.vbuf, quint32(36 * 3 * sizeof(float)) }
259 };
260 cb->setVertexInput(0, 2, vbufBindings);
261 cb->draw(36);
262
263 cb->endPass();
264
265 d.frameCount += 1;
266 }