QtBase  v6.3.1
cubemap.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 
54 struct {
56  QRhiBuffer *vbuf = nullptr;
57  QRhiBuffer *ubuf = nullptr;
58  QRhiTexture *tex = nullptr;
59  QRhiSampler *sampler = nullptr;
63 } d;
64 
65 void Window::customInit()
66 {
68  d.vbuf->create();
69  d.releasePool << d.vbuf;
70 
72  d.ubuf->create();
73  d.releasePool << d.ubuf;
74 
75  const QSize cubeMapSize(512, 512);
77  | QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips); // exercise mipmap generation as well
78  d.releasePool << d.tex;
79  d.tex->create();
80 
81  d.initialUpdates = m_r->nextResourceUpdateBatch();
82  d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
83 
85  // just use the same image for all faces for now
88  { 0, 0, subresDesc }, // +X
89  { 1, 0, subresDesc }, // -X
90  { 2, 0, subresDesc }, // +Y
91  { 3, 0, subresDesc }, // -Y
92  { 4, 0, subresDesc }, // +Z
93  { 5, 0, subresDesc } // -Z
94  });
95  d.initialUpdates->uploadTexture(d.tex, desc);
96 
97  d.initialUpdates->generateMips(d.tex);
98 
101  d.releasePool << d.sampler;
102  d.sampler->create();
103 
105  d.releasePool << d.srb;
106  d.srb->setBindings({
109  });
110  d.srb->create();
111 
112  d.ps = m_r->newGraphicsPipeline();
113  d.releasePool << d.ps;
114 
115  d.ps->setDepthTest(true);
116  d.ps->setDepthWrite(true);
117  d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
118 
119  d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back
120  d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data
121 
122  QShader vs = getShader(QLatin1String(":/cubemap.vert.qsb"));
123  Q_ASSERT(vs.isValid());
124  QShader fs = getShader(QLatin1String(":/cubemap.frag.qsb"));
125  Q_ASSERT(fs.isValid());
126  d.ps->setShaderStages({
127  { QRhiShaderStage::Vertex, vs },
129  });
130 
131  QRhiVertexInputLayout inputLayout;
132  inputLayout.setBindings({
133  { 3 * sizeof(float) }
134  });
135  inputLayout.setAttributes({
137  });
138 
139  d.ps->setVertexInputLayout(inputLayout);
140  d.ps->setShaderResourceBindings(d.srb);
141  d.ps->setRenderPassDescriptor(m_rp);
142 
143  d.ps->create();
144 }
145 
147 {
148  qDeleteAll(d.releasePool);
149  d.releasePool.clear();
150 }
151 
153 {
154  const QSize outputSizeInPixels = m_sc->currentPixelSize();
155  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
157 
158  if (d.initialUpdates) {
159  u->merge(d.initialUpdates);
160  d.initialUpdates->release();
161  d.initialUpdates = nullptr;
162  }
163 
165  mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f);
166  // cube vertices go from -1..1
167  mvp.scale(10);
168  static float rx = 0;
169  mvp.rotate(rx, 1, 0, 0);
170  rx += 0.5f;
171  // no translation
172  u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
173 
174  cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
175  cb->setGraphicsPipeline(d.ps);
176  cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
177  cb->setShaderResources();
178  const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
179  cb->setVertexInput(0, 1, &vbufBinding);
180  cb->draw(36);
181  cb->endPass();
182 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
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
QImage mirrored(bool horizontally=false, bool vertically=true) const &
Definition: qimage.h:254
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition: qimage.h:160
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
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
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
QMatrix4x4 clipSpaceCorrMatrix() const
Definition: qrhi.cpp:6299
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
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition: qrhi.cpp:6609
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
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
@ UsedWithGenerateMips
Definition: qrhi_p.h:771
@ 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
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
virtual void customInit()
QRhi * m_r
Definition: examplefw.h:161
struct @907 d
QRhiBuffer * ubuf
Definition: cubemap.cpp:57
QList< QRhiResource * > releasePool
Definition: cubemap.cpp:55
QRhiTexture * tex
Definition: cubemap.cpp:58
QRhiShaderResourceBindings * srb
Definition: cubemap.cpp:60
QRhiBuffer * vbuf
Definition: cubemap.cpp:56
QRhiGraphicsPipeline * ps
Definition: cubemap.cpp:61
QRhiResourceUpdateBatch * initialUpdates
Definition: cubemap.cpp:62
float rx
qDeleteAll(list.begin(), list.end())
QShader getShader(const QString &name)
Definition: examplefw.h:86
@ desc
GLuint sampler
GLint void * img
Definition: qopenglext.h:233
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)