Debian Codegraph @

tests/manual/rhi/texturearray/texturearray.cpp

raw
1 // Copyright (C) 2021 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5 #include <QElapsedTimer>
6
7 // Creates a texture array object with size 4, uploads a different
8 // image to each, and cycles through them on-screen.
9
10 static const int ARRAY_SIZE = 4;
11 static const int UBUF_SIZE = 72;
12
13 static float vertexData[] =
14 { // Y up, CCW
15 -0.5f, 0.5f, 0.0f, 0.0f,
16 -0.5f, -0.5f, 0.0f, 1.0f,
17 0.5f, -0.5f, 1.0f, 1.0f,
18 0.5f, 0.5f, 1.0f, 0.0f
19 };
20
21 static quint16 indexData[] =
22 {
23 0, 1, 2, 0, 2, 3
24 };
25
26 struct {
27 QList<QRhiResource *> releasePool;
28 QRhiTexture *texArr = nullptr;
29 QRhiSampler *sampler = nullptr;
30 QRhiShaderResourceBindings *srb = nullptr;
31 QRhiBuffer *vbuf = nullptr;
32 QRhiBuffer *ibuf = nullptr;
33 QRhiBuffer *ubuf = nullptr;
34 QRhiGraphicsPipeline *ps = nullptr;
35 QRhiResourceUpdateBatch *initialUpdates = nullptr;
36 QMatrix4x4 winProj;
37 QElapsedTimer t;
38 int arrayIndex = 0;
39 } d;
40
41 void Window::customInit()
42 {
43 if (!m_r->isFeatureSupported(QRhi::TextureArrays))
44 qFatal("Texture array objects are not supported by this backend");
45
46 d.texArr = m_r->newTextureArray(QRhiTexture::RGBA8, ARRAY_SIZE, QSize(512, 512), 1,
47 // mipmaps will be generated, to exercise that too
48 QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips);
49 d.releasePool << d.texArr;
50 d.texArr->create();
51
52 d.initialUpdates = m_r->nextResourceUpdateBatch();
53
54 QImage img(512, 512, QImage::Format_RGBA8888);
55 img.fill(Qt::red);
56 d.initialUpdates->uploadTexture(d.texArr, QRhiTextureUploadDescription(QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(img))));
57 img.fill(Qt::green);
58 d.initialUpdates->uploadTexture(d.texArr, QRhiTextureUploadDescription(QRhiTextureUploadEntry(1, 0, QRhiTextureSubresourceUploadDescription(img))));
59 img.fill(Qt::blue);
60 d.initialUpdates->uploadTexture(d.texArr, QRhiTextureUploadDescription(QRhiTextureUploadEntry(2, 0, QRhiTextureSubresourceUploadDescription(img))));
61 img.fill(Qt::yellow);
62 d.initialUpdates->uploadTexture(d.texArr, QRhiTextureUploadDescription(QRhiTextureUploadEntry(3, 0, QRhiTextureSubresourceUploadDescription(img))));
63
64 d.initialUpdates->generateMips(d.texArr);
65
66 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear,
67 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
68 d.releasePool << d.sampler;
69 d.sampler->create();
70
71 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SIZE);
72 d.releasePool << d.ubuf;
73 d.ubuf->create();
74
75 d.srb = m_r->newShaderResourceBindings();
76 d.releasePool << d.srb;
77 d.srb->setBindings({
78 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, 0, UBUF_SIZE),
79 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.texArr, d.sampler)
80 });
81 d.srb->create();
82
83 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
84 d.releasePool << d.vbuf;
85 d.vbuf->create();
86
87 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
88 d.releasePool << d.ibuf;
89 d.ibuf->create();
90
91 d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(vertexData), vertexData);
92 d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData);
93
94 d.ps = m_r->newGraphicsPipeline();
95 d.releasePool << d.ps;
96 d.ps->setShaderStages({
97 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture_arr.vert.qsb")) },
98 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture_arr.frag.qsb")) }
99 });
100 QRhiVertexInputLayout inputLayout;
101 inputLayout.setBindings({ { 4 * sizeof(float) } });
102 inputLayout.setAttributes({
103 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
104 { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
105 });
106 d.ps->setVertexInputLayout(inputLayout);
107 d.ps->setShaderResourceBindings(d.srb);
108 d.ps->setRenderPassDescriptor(m_rp);
109 d.ps->create();
110
111 d.t.start();
112 }
113
114 void Window::customRelease()
115 {
116 qDeleteAll(d.releasePool);
117 d.releasePool.clear();
118 }
119
120 void Window::customRender()
121 {
122 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
123 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
124 if (d.initialUpdates) {
125 u->merge(d.initialUpdates);
126 d.initialUpdates->release();
127 d.initialUpdates = nullptr;
128 }
129
130 if (d.winProj != m_proj) {
131 d.winProj = m_proj;
132 QMatrix4x4 mvp = m_proj;
133 mvp.scale(2);
134 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
135 const qint32 flip = 0;
136 u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
137 u->updateDynamicBuffer(d.ubuf, 68, 4, &d.arrayIndex);
138 }
139
140 if (d.t.elapsed() > 2000) {
141 d.t.restart();
142 d.arrayIndex = (d.arrayIndex + 1) % ARRAY_SIZE;
143 u->updateDynamicBuffer(d.ubuf, 68, 4, &d.arrayIndex);
144 }
145
146 const QSize outputSizeInPixels = m_sc->currentPixelSize();
147 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
148 cb->setGraphicsPipeline(d.ps);
149 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
150 cb->setShaderResources();
151 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
152 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
153 cb->drawIndexed(6);
154 cb->endPass();
155 }