QtBase  v6.3.1
compressedtexture_bc1.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 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 "../shared/cube.h"
53 #include "../shared/dds_bc1.h"
54 
55 struct {
56  QRhiBuffer *vbuf = nullptr;
57  bool vbufReady = false;
58  QRhiBuffer *ubuf = nullptr;
59  QRhiTexture *tex = nullptr;
60  QRhiSampler *sampler = nullptr;
63 
64  float rotation = 0;
65 
67 } d;
68 
70 {
72  qFatal("This backend does not support BC1");
73 
75  d.vbuf->create();
76  d.vbufReady = false;
77 
79  d.ubuf->create();
80 
82  d.compressedData = loadBC1(QLatin1String(":/qt256_bc1_9mips.dds"), &imageSize);
83  qDebug() << d.compressedData.count() << imageSize << m_r->mipLevelsForSize(imageSize);
84 
86  d.tex->create();
87 
90  d.sampler->create();
91 
93  d.srb->setBindings({
96  });
97  d.srb->create();
98 
99  d.ps = m_r->newGraphicsPipeline();
100 
101  d.ps->setDepthTest(true);
102  d.ps->setDepthWrite(true);
103  d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
104 
105  d.ps->setCullMode(QRhiGraphicsPipeline::Back);
106  d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
107 
108  const QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
109  if (!vs.isValid())
110  qFatal("Failed to load shader pack (vertex)");
111  const QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
112  if (!fs.isValid())
113  qFatal("Failed to load shader pack (fragment)");
114 
115  d.ps->setShaderStages({
116  { QRhiShaderStage::Vertex, vs },
118  });
119 
120  QRhiVertexInputLayout inputLayout;
121  inputLayout.setBindings({
122  { 3 * sizeof(float) },
123  { 2 * sizeof(float) }
124  });
125  inputLayout.setAttributes({
128  });
129 
130  d.ps->setVertexInputLayout(inputLayout);
131  d.ps->setShaderResourceBindings(d.srb);
132  d.ps->setRenderPassDescriptor(m_rp);
133 
134  d.ps->create();
135 }
136 
138 {
139  delete d.ps;
140  d.ps = nullptr;
141 
142  delete d.srb;
143  d.srb = nullptr;
144 
145  delete d.ubuf;
146  d.ubuf = nullptr;
147 
148  delete d.vbuf;
149  d.vbuf = nullptr;
150 
151  delete d.sampler;
152  d.sampler = nullptr;
153 
154  delete d.tex;
155  d.tex = nullptr;
156 }
157 
159 {
161  if (!d.vbufReady) {
162  d.vbufReady = true;
163  u->uploadStaticBuffer(d.vbuf, cube);
164  qint32 flip = 0;
165  u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
166  }
167  if (!d.compressedData.isEmpty()) {
169  for (int i = 0; i < d.compressedData.count(); ++i) {
170  QRhiTextureSubresourceUploadDescription image(d.compressedData[i].constData(), d.compressedData[i].size());
171  descEntries.append({ 0, i, image });
172  }
174  desc.setEntries(descEntries.cbegin(), descEntries.cend());
175  u->uploadTexture(d.tex, desc);
176  d.compressedData.clear();
177  }
178  d.rotation += 1.0f;
179  QMatrix4x4 mvp = m_proj;
180  mvp.scale(0.5f);
181  mvp.rotate(d.rotation, 0, 1, 0);
182  u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
183 
184  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
185  const QSize outputSizeInPixels = m_sc->currentPixelSize();
186 
187  cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
188 
189  cb->setGraphicsPipeline(d.ps);
190  cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
191  cb->setShaderResources();
192  const QRhiCommandBuffer::VertexInput vbufBindings[] = {
193  { d.vbuf, 0 },
194  { d.vbuf, quint32(36 * 3 * sizeof(float)) }
195  };
196  cb->setVertexInput(0, 2, vbufBindings);
197  cb->draw(36);
198 
199  cb->endPass();
200 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QByteArrayList class provides a list of byte arrays.
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 rotate(float angle, const QVector3D &vector)
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
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags={}) const
Definition: qrhi.cpp:6310
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
int mipLevelsForSize(const QSize &size) const
Definition: qrhi.cpp:6216
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition: qrhi.cpp:6609
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
@ MipMapped
Definition: qrhi_p.h:768
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Definition: qrhi_p.h:257
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Definition: qrhi_p.h:268
bool isValid() const
Definition: qshader.cpp:266
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
const_iterator cbegin() const noexcept
const_iterator cend() const noexcept
void append(const T &t)
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
float rotation
QRhiBuffer * ubuf
QRhiTexture * tex
QByteArrayList compressedData
bool vbufReady
QRhiShaderResourceBindings * srb
QRhiBuffer * vbuf
QRhiGraphicsPipeline * ps
struct @903 d
QShader getShader(const QString &name)
Definition: examplefw.h:86
Definition: image.cpp:51
unsigned int quint32
Definition: qglobal.h:288
int qint32
Definition: qglobal.h:287
@ desc
#define qDebug
[1]
Definition: qlogging.h:177
#define qFatal
Definition: qlogging.h:181
GLuint sampler
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei imageSize
GLeglImageOES image
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)