QtBase  v6.3.1
texturearray.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "../shared/examplefw.h"
52 #include <QElapsedTimer>
53 
54 // Creates a texture array object with size 4, uploads a different
55 // image to each, and cycles through them on-screen.
56 
57 static const int ARRAY_SIZE = 4;
58 static const int UBUF_SIZE = 72;
59 
60 static float vertexData[] =
61 { // Y up, CCW
62  -0.5f, 0.5f, 0.0f, 0.0f,
63  -0.5f, -0.5f, 0.0f, 1.0f,
64  0.5f, -0.5f, 1.0f, 1.0f,
65  0.5f, 0.5f, 1.0f, 0.0f
66 };
67 
68 static quint16 indexData[] =
69 {
70  0, 1, 2, 0, 2, 3
71 };
72 
73 struct {
75  QRhiTexture *texArr = nullptr;
76  QRhiSampler *sampler = nullptr;
78  QRhiBuffer *vbuf = nullptr;
79  QRhiBuffer *ibuf = nullptr;
80  QRhiBuffer *ubuf = nullptr;
85  int arrayIndex = 0;
86 } d;
87 
88 void Window::customInit()
89 {
91  qFatal("Texture array objects are not supported by this backend");
92 
93  d.texArr = m_r->newTextureArray(QRhiTexture::RGBA8, ARRAY_SIZE, QSize(512, 512));
94  d.releasePool << d.texArr;
95  d.texArr->create();
96 
97  d.initialUpdates = m_r->nextResourceUpdateBatch();
98 
100  img.fill(Qt::red);
102  img.fill(Qt::green);
104  img.fill(Qt::blue);
106  img.fill(Qt::yellow);
108 
111  d.releasePool << d.sampler;
112  d.sampler->create();
113 
115  d.releasePool << d.ubuf;
116  d.ubuf->create();
117 
119  d.releasePool << d.srb;
120  d.srb->setBindings({
123  });
124  d.srb->create();
125 
126  d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
127  d.releasePool << d.vbuf;
128  d.vbuf->create();
129 
130  d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
131  d.releasePool << d.ibuf;
132  d.ibuf->create();
133 
134  d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(vertexData), vertexData);
135  d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData);
136 
137  d.ps = m_r->newGraphicsPipeline();
138  d.releasePool << d.ps;
139  d.ps->setShaderStages({
140  { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture_arr.vert.qsb")) },
141  { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture_arr.frag.qsb")) }
142  });
143  QRhiVertexInputLayout inputLayout;
144  inputLayout.setBindings({ { 4 * sizeof(float) } });
145  inputLayout.setAttributes({
147  { 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) }
148  });
149  d.ps->setVertexInputLayout(inputLayout);
150  d.ps->setShaderResourceBindings(d.srb);
151  d.ps->setRenderPassDescriptor(m_rp);
152  d.ps->create();
153 
154  d.t.start();
155 }
156 
158 {
159  qDeleteAll(d.releasePool);
160  d.releasePool.clear();
161 }
162 
164 {
165  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
167  if (d.initialUpdates) {
168  u->merge(d.initialUpdates);
169  d.initialUpdates->release();
170  d.initialUpdates = nullptr;
171  }
172 
173  if (d.winProj != m_proj) {
174  d.winProj = m_proj;
175  QMatrix4x4 mvp = m_proj;
176  mvp.scale(2);
177  u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
178  const qint32 flip = 0;
179  u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
180  u->updateDynamicBuffer(d.ubuf, 68, 4, &d.arrayIndex);
181  }
182 
183  if (d.t.elapsed() > 2000) {
184  d.t.restart();
185  d.arrayIndex = (d.arrayIndex + 1) % ARRAY_SIZE;
186  u->updateDynamicBuffer(d.ubuf, 68, 4, &d.arrayIndex);
187  }
188 
189  const QSize outputSizeInPixels = m_sc->currentPixelSize();
190  cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
191  cb->setGraphicsPipeline(d.ps);
192  cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
193  cb->setShaderResources();
194  QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
195  cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
196  cb->drawIndexed(6);
197  cb->endPass();
198 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:49
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
@ Format_RGBA8888
Definition: qimage.h:95
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:61
void scale(const QVector3D &vector)
Definition: qmatrix4x4.cpp:803
const float * constData() const
Definition: qmatrix4x4.h:183
@ Immutable
Definition: qrhi_p.h:717
@ Dynamic
Definition: qrhi_p.h:719
@ IndexBuffer
Definition: qrhi_p.h:724
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
QRhiTexture * newTextureArray(QRhiTexture::Format format, int arraySize, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition: qrhi.cpp:6662
bool isFeatureSupported(QRhi::Feature feature) const
Definition: qrhi.cpp:6318
QRhiShaderResourceBindings * newShaderResourceBindings()
Definition: qrhi.cpp:6543
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, int size)
Definition: qrhi.cpp:6562
QRhiSampler * newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter, QRhiSampler::Filter mipmapMode, QRhiSampler::AddressMode addressU, QRhiSampler::AddressMode addressV, QRhiSampler::AddressMode addressW=QRhiSampler::Repeat)
Definition: qrhi.cpp:6679
QRhiGraphicsPipeline * newGraphicsPipeline()
Definition: qrhi.cpp:6520
@ TextureArrays
Definition: qrhi_p.h:1592
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
@ ClampToEdge
Definition: qrhi_p.h:884
static QRhiShaderResourceBinding sampledTexture(int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
Definition: qrhi.cpp:3374
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition: qrhi.cpp:3268
Definition: qrhi_p.h:565
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Definition: qrhi_p.h:257
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Definition: qrhi_p.h:268
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr int width() const noexcept
Definition: qsize.h:157
std::unique_ptr< QRhiSwapChain > m_sc
Definition: window.h:89
std::unique_ptr< QRhiRenderPassDescriptor > m_rp
Definition: window.h:91
virtual void customRender()
QColor m_clearColor
Definition: examplefw.h:176
QMatrix4x4 m_proj
Definition: window.h:94
virtual void customInit()
QRhi * m_r
Definition: examplefw.h:161
qDeleteAll(list.begin(), list.end())
QShader getShader(const QString &name)
Definition: examplefw.h:86
@ blue
Definition: qnamespace.h:68
@ yellow
Definition: qnamespace.h:71
@ green
Definition: qnamespace.h:67
@ red
Definition: qnamespace.h:66
unsigned int quint32
Definition: qglobal.h:288
unsigned short quint16
Definition: qglobal.h:286
int qint32
Definition: qglobal.h:287
#define qFatal
Definition: qlogging.h:181
GLuint sampler
GLint void * img
Definition: qopenglext.h:233
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
QRhiBuffer * ibuf
struct @925 d
QRhiBuffer * ubuf
QList< QRhiResource * > releasePool
QRhiTexture * texArr
QElapsedTimer t
QRhiShaderResourceBindings * srb
QRhiBuffer * vbuf
QMatrix4x4 winProj
QRhiGraphicsPipeline * ps
int arrayIndex
QRhiResourceUpdateBatch * initialUpdates