QtBase  v6.3.1
trianglerenderer.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 "trianglerenderer.h"
52 #include <QFile>
53 #include <QtGui/private/qshader_p.h>
54 
55 //#define VBUF_IS_DYNAMIC
56 
57 static float vertexData[] = { // Y up (note m_proj), CCW
58  0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
59  -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
60  0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f
61 };
62 
63 static QShader getShader(const QString &name)
64 {
65  QFile f(name);
66  if (f.open(QIODevice::ReadOnly))
67  return QShader::fromSerialized(f.readAll());
68 
69  return QShader();
70 }
71 
73 {
74 #ifdef VBUF_IS_DYNAMIC
75  m_vbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, sizeof(vertexData));
76 #else
77  m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
78 #endif
79  m_vbuf->setName(QByteArrayLiteral("Triangle vbuf"));
80  m_vbuf->create();
81  m_vbufReady = false;
82 
84  m_ubuf->setName(QByteArrayLiteral("Triangle ubuf"));
85  m_ubuf->create();
86 
87  m_srb = m_r->newShaderResourceBindings();
88  m_srb->setBindings({
90  });
91  m_srb->create();
92 
93  m_ps = m_r->newGraphicsPipeline();
94 
95  QRhiGraphicsPipeline::TargetBlend premulAlphaBlend; // convenient defaults...
96  premulAlphaBlend.enable = true;
98  for (int i = 0; i < m_colorAttCount; ++i)
99  rtblends << premulAlphaBlend;
100 
101  m_ps->setTargetBlends(rtblends.cbegin(), rtblends.cend());
102  m_ps->setSampleCount(m_sampleCount);
103 
104  if (m_depthWrite) { // TriangleOnCube may want to exercise this
105  m_ps->setDepthTest(true);
107  m_ps->setDepthWrite(true);
108  }
109 
110  QShader vs = getShader(QLatin1String(":/color.vert.qsb"));
111  Q_ASSERT(vs.isValid());
112  QShader fs = getShader(QLatin1String(":/color.frag.qsb"));
113  Q_ASSERT(fs.isValid());
114  m_ps->setShaderStages({
115  { QRhiShaderStage::Vertex, vs },
117  });
118 
119  QRhiVertexInputLayout inputLayout;
120  inputLayout.setBindings({
121  { 7 * sizeof(float) }
122  });
123  inputLayout.setAttributes({
125  { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
126  });
127 
128  m_ps->setVertexInputLayout(inputLayout);
129  m_ps->setShaderResourceBindings(m_srb);
130  m_ps->setRenderPassDescriptor(rp);
131 
132  m_ps->create();
133 }
134 
135 void TriangleRenderer::resize(const QSize &pixelSize)
136 {
137  m_proj = m_r->clipSpaceCorrMatrix();
138  m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f);
139  m_proj.translate(0, 0, -4);
140 }
141 
143 {
144  delete m_ps;
145  m_ps = nullptr;
146 
147  delete m_srb;
148  m_srb = nullptr;
149 
150  delete m_ubuf;
151  m_ubuf = nullptr;
152 
153  delete m_vbuf;
154  m_vbuf = nullptr;
155 }
156 
158 {
159 #if 0
160  static int messWithBufferTrigger = 0;
161  // recreate the underlying VkBuffer every second frame
162  // to exercise setShaderResources' built-in smartness
163  if (!(messWithBufferTrigger & 1)) {
164  m_ubuf->release();
165  m_ubuf->create();
166  }
167  ++messWithBufferTrigger;
168 #endif
169 
170  if (!m_vbufReady) {
171  m_vbufReady = true;
172 #ifdef VBUF_IS_DYNAMIC
173  resourceUpdates->updateDynamicBuffer(m_vbuf, 0, m_vbuf->size(), vertexData);
174 #else
175  resourceUpdates->uploadStaticBuffer(m_vbuf, vertexData);
176 #endif
177  }
178 
179  m_rotation += 1.0f;
180  QMatrix4x4 mvp = m_proj;
181  mvp.translate(m_translation);
182  mvp.scale(m_scale);
183  mvp.rotate(m_rotation, 0, 1, 0);
184  resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData());
185 
186  m_opacity += m_opacityDir * 0.005f;
187  if (m_opacity < 0.0f || m_opacity > 1.0f) {
188  m_opacityDir *= -1;
189  m_opacity = qBound(0.0f, m_opacity, 1.0f);
190  }
191  resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &m_opacity);
192 }
193 
194 void TriangleRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels)
195 {
196  cb->setGraphicsPipeline(m_ps);
197  cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
198  cb->setShaderResources();
199  const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf, 0);
200  cb->setVertexInput(0, 1, &vbufBinding);
201  cb->draw(3);
202 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:94
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
void translate(const QVector3D &vector)
Definition: qmatrix4x4.cpp:965
@ Immutable
Definition: qrhi_p.h:717
@ Dynamic
Definition: qrhi_p.h:719
int size() const
Definition: qrhi_p.h:743
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
virtual bool create()=0
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
void setTargetBlends(std::initializer_list< TargetBlend > list)
Definition: qrhi_p.h:1221
void setDepthWrite(bool enable)
Definition: qrhi_p.h:1235
void setShaderResourceBindings(QRhiShaderResourceBindings *srb)
Definition: qrhi_p.h:1281
void setDepthOp(CompareOp op)
Definition: qrhi_p.h:1238
void setVertexInputLayout(const QRhiVertexInputLayout &layout)
Definition: qrhi_p.h:1278
void setShaderStages(std::initializer_list< QRhiShaderStage > list)
Definition: qrhi_p.h:1267
void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
Definition: qrhi_p.h:1284
void setSampleCount(int s)
Definition: qrhi_p.h:1256
virtual bool create()=0
void setDepthTest(bool enable)
Definition: qrhi_p.h:1232
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
QRhiGraphicsPipeline * newGraphicsPipeline()
Definition: qrhi.cpp:6520
void setName(const QByteArray &name)
Definition: qrhi.cpp:2068
void uploadStaticBuffer(QRhiBuffer *buf, int offset, int size, const void *data)
Definition: qrhi.cpp:5346
void updateDynamicBuffer(QRhiBuffer *buf, int offset, int size, const void *data)
Definition: qrhi.cpp:5326
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition: qrhi.cpp:3268
void setBindings(std::initializer_list< QRhiShaderResourceBinding > list)
Definition: qrhi_p.h:1052
virtual bool create()=0
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Definition: qrhi_p.h:257
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Definition: qrhi_p.h:268
static QShader fromSerialized(const QByteArray &data)
Definition: qshader.cpp:418
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
The QString class provides a Unicode character string.
Definition: qstring.h:388
const_iterator cbegin() const noexcept
const_iterator cend() const noexcept
void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels)
void releaseResources() override
void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
void initResources() override
void resize(const QSize &pixelSize)
QShader getShader(const QString &name)
Definition: examplefw.h:86
#define QByteArrayLiteral(str)
Definition: qbytearray.h:80
GLfloat GLfloat f
GLuint name
#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)