QtBase  v6.3.1
triquadcube.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 // An example exercising more than a single feature. Enables profiling
52 // (resource logging to a file) and inserts debug markers and sets some
53 // object names. Can also be used to test MSAA swapchains, swapchain image
54 // readback, requesting an sRGB swapchain, and some texture features.
55 
56 #define EXAMPLEFW_PREINIT
57 #include "../shared/examplefw.h"
58 #include "trianglerenderer.h"
59 #include "quadrenderer.h"
60 #include "texturedcuberenderer.h"
61 #include "triangleoncuberenderer.h"
62 
63 #include <QFileInfo>
64 #include <QFile>
65 #include <QtGui/private/qrhiprofiler_p.h>
66 
67 #define PROFILE_TO_FILE
68 //#define SKIP_PRESENT
69 //#define USE_MSAA
70 //#define USE_SRGB_SWAPCHAIN
71 //#define READBACK_SWAPCHAIN
72 //#define NO_VSYNC
73 //#define USE_MIN_SWAPCHAIN_BUFFERS
74 //#define DECLARE_EXT_CONTENTS
75 
76 struct {
81  bool onScreenOnly = false;
82  bool triangleOnly = false;
84  int frameCount = 0;
86 } d;
87 
88 void preInit()
89 {
90 #ifdef PROFILE_TO_FILE
92  const QString profFn = QFileInfo(QLatin1String("rhiprof.txt")).absoluteFilePath();
93  qDebug("Writing profiling output to %s", qPrintable(profFn));
94  d.profOut.setFileName(profFn);
95  d.profOut.open(QIODevice::WriteOnly);
96 #endif
97 
98 #ifdef SKIP_PRESENT
100 #endif
101 
102 #ifdef USE_MSAA
103  sampleCount = 4; // enable 4x MSAA (except for the render-to-texture pass)
104 #endif
105 
106 #ifdef READBACK_SWAPCHAIN
108 #endif
109 
110 #ifdef USE_SRGB_SWAPCHAIN
112 #endif
113 
114 #ifdef NO_VSYNC
116 #endif
117 
118 #ifdef USE_MIN_SWAPCHAIN_BUFFERS
120 #endif
121 
122 #ifdef DECLARE_EXT_CONTENTS
123  beginFrameFlags |= QRhi::ExternalContentsInPass;
124 #endif
125 
126  // For OpenGL some of these are incorporated into the QSurfaceFormat by
127  // examplefw.h after returning from here as that is out of the RHI's control.
128 }
129 
130 void Window::customInit()
131 {
132 #ifdef PROFILE_TO_FILE
133  m_r->profiler()->setDevice(&d.profOut);
134 #endif
135 
136  d.triRenderer.setRhi(m_r);
137  d.triRenderer.setSampleCount(sampleCount);
138  d.triRenderer.initResources(m_rp);
139 
140  if (!d.triangleOnly) {
141  d.triRenderer.setTranslation(QVector3D(0, 0.5f, 0));
142 
143  d.quadRenderer.setRhi(m_r);
144  d.quadRenderer.setSampleCount(sampleCount);
145  d.quadRenderer.setPipeline(d.triRenderer.pipeline());
146  d.quadRenderer.initResources(m_rp);
147  d.quadRenderer.setTranslation(QVector3D(1.5f, -0.5f, 0));
148 
149  d.cubeRenderer.setRhi(m_r);
150  d.cubeRenderer.setSampleCount(sampleCount);
151  d.cubeRenderer.initResources(m_rp);
152  d.cubeRenderer.setTranslation(QVector3D(0, -0.5f, 0));
153  }
154 
155  if (!d.onScreenOnly) {
156  d.liveTexCubeRenderer.setRhi(m_r);
157  d.liveTexCubeRenderer.setSampleCount(sampleCount);
158  d.liveTexCubeRenderer.initResources(m_rp);
159  d.liveTexCubeRenderer.setTranslation(QVector3D(-2.0f, 0, 0));
160  }
161 
162  // Put the gpu mem allocator statistics to the profiling stream after doing
163  // all the init. (where applicable)
165 
166  // Check some features/limits.
167  qDebug("isFeatureSupported(MultisampleTexture): %d", m_r->isFeatureSupported(QRhi::MultisampleTexture));
168  qDebug("isFeatureSupported(MultisampleRenderBuffer): %d", m_r->isFeatureSupported(QRhi::MultisampleRenderBuffer));
169  qDebug("isFeatureSupported(DebugMarkers): %d", m_r->isFeatureSupported(QRhi::DebugMarkers));
170  qDebug("isFeatureSupported(Timestamps): %d", m_r->isFeatureSupported(QRhi::Timestamps));
171  qDebug("isFeatureSupported(Instancing): %d", m_r->isFeatureSupported(QRhi::Instancing));
172  qDebug("isFeatureSupported(CustomInstanceStepRate): %d", m_r->isFeatureSupported(QRhi::CustomInstanceStepRate));
173  qDebug("isFeatureSupported(PrimitiveRestart): %d", m_r->isFeatureSupported(QRhi::PrimitiveRestart));
174  qDebug("isFeatureSupported(NonDynamicUniformBuffers): %d", m_r->isFeatureSupported(QRhi::NonDynamicUniformBuffers));
175  qDebug("isFeatureSupported(NonFourAlignedEffectiveIndexBufferOffset): %d", m_r->isFeatureSupported(QRhi::NonFourAlignedEffectiveIndexBufferOffset));
176  qDebug("isFeatureSupported(NPOTTextureRepeat): %d", m_r->isFeatureSupported(QRhi::NPOTTextureRepeat));
177  qDebug("isFeatureSupported(RedOrAlpha8IsRed): %d", m_r->isFeatureSupported(QRhi::RedOrAlpha8IsRed));
178  qDebug("isFeatureSupported(ElementIndexUint): %d", m_r->isFeatureSupported(QRhi::ElementIndexUint));
179  qDebug("isFeatureSupported(Compute): %d", m_r->isFeatureSupported(QRhi::Compute));
180  qDebug("isFeatureSupported(WideLines): %d", m_r->isFeatureSupported(QRhi::WideLines));
181  qDebug("isFeatureSupported(VertexShaderPointSize): %d", m_r->isFeatureSupported(QRhi::VertexShaderPointSize));
182  qDebug("isFeatureSupported(BaseVertex): %d", m_r->isFeatureSupported(QRhi::BaseVertex));
183  qDebug("isFeatureSupported(BaseInstance): %d", m_r->isFeatureSupported(QRhi::BaseInstance));
184  qDebug("isFeatureSupported(PipelineCacheDataLoadSave): %d", m_r->isFeatureSupported(QRhi::PipelineCacheDataLoadSave));
185  qDebug("Min 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMin));
186  qDebug("Max 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMax));
187  qDebug("Max color attachment count: %d", m_r->resourceLimit(QRhi::MaxColorAttachments));
188  qDebug("MaxThreadGroupsPerDimension: %d", m_r->resourceLimit(QRhi::MaxThreadGroupsPerDimension));
189  qDebug("MaxThreadsPerThreadGroup: %d", m_r->resourceLimit(QRhi::MaxThreadsPerThreadGroup));
190  qDebug("MaxThreadGroupX: %d", m_r->resourceLimit(QRhi::MaxThreadGroupX));
191  qDebug("MaxThreadGroupY: %d", m_r->resourceLimit(QRhi::MaxThreadGroupY));
192  qDebug("MaxThreadGroupZ: %d", m_r->resourceLimit(QRhi::MaxThreadGroupZ));
193  qDebug("TextureArraySizeMax: %d", m_r->resourceLimit(QRhi::TextureArraySizeMax));
194  qDebug("MaxUniformBufferRange: %d", m_r->resourceLimit(QRhi::MaxUniformBufferRange));
195 }
196 
198 {
199  d.triRenderer.releaseResources();
200 
201  if (!d.triangleOnly) {
202  d.quadRenderer.releaseResources();
203  d.cubeRenderer.releaseResources();
204  }
205 
206  if (!d.onScreenOnly)
207  d.liveTexCubeRenderer.releaseResources();
208 }
209 
211 {
212  const QSize outputSize = m_sc->currentPixelSize();
213  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
214 
215  if (outputSize != d.lastOutputSize) {
216  d.triRenderer.resize(outputSize);
217  if (!d.triangleOnly) {
218  d.quadRenderer.resize(outputSize);
219  d.cubeRenderer.resize(outputSize);
220  }
221  if (!d.onScreenOnly)
222  d.liveTexCubeRenderer.resize(outputSize);
223  d.lastOutputSize = outputSize;
224  }
225 
226  if (!d.onScreenOnly) {
227  cb->debugMarkBegin("Offscreen triangle pass");
228  d.liveTexCubeRenderer.queueOffscreenPass(cb);
229  cb->debugMarkEnd();
230  }
231 
233  d.triRenderer.queueResourceUpdates(u);
234  if (!d.triangleOnly) {
235  d.quadRenderer.queueResourceUpdates(u);
236  d.cubeRenderer.queueResourceUpdates(u);
237  }
238  if (!d.onScreenOnly)
239  d.liveTexCubeRenderer.queueResourceUpdates(u);
240 
241  cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
242  cb->debugMarkBegin(QByteArrayLiteral("Triangle"));
243  d.triRenderer.queueDraw(cb, outputSize);
244  cb->debugMarkEnd();
245  if (!d.triangleOnly) {
246  cb->debugMarkMsg(QByteArrayLiteral("More stuff"));
247  cb->debugMarkBegin(QByteArrayLiteral("Quad"));
248  d.quadRenderer.queueDraw(cb, outputSize);
249  cb->debugMarkEnd();
250  cb->debugMarkBegin(QByteArrayLiteral("Cube"));
251  d.cubeRenderer.queueDraw(cb, outputSize);
252  cb->debugMarkEnd();
253  }
254  if (!d.onScreenOnly) {
255  cb->debugMarkMsg(QByteArrayLiteral("Even more stuff"));
256  cb->debugMarkBegin(QByteArrayLiteral("Cube with offscreen triangle"));
257  d.liveTexCubeRenderer.queueDraw(cb, outputSize);
258  cb->debugMarkEnd();
259  }
260 
261  QRhiResourceUpdateBatch *passEndUpdates = nullptr;
262 #ifdef READBACK_SWAPCHAIN
263  passEndUpdates = m_r->nextResourceUpdateBatch();
264  QRhiReadbackDescription rb; // no texture given -> backbuffer
265  QRhiReadbackResult *rbResult = new QRhiReadbackResult;
266  int frameNo = d.frameCount;
267  rbResult->completed = [this, rbResult, frameNo] {
268  {
271  const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData());
272  QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt);
273  QString fn = QString::asprintf("frame%d.png", frameNo);
275  qDebug("Saving into %s", qPrintable(fn));
276  if (m_r->isYUpInFramebuffer())
277  image.mirrored().save(fn);
278  else
279  image.save(fn);
280  }
281  delete rbResult;
282  };
283  passEndUpdates->readBackTexture(rb, rbResult);
284 #endif
285 
286  cb->endPass(passEndUpdates);
287 
288  d.frameCount += 1;
289 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
const char * constData() const noexcept
Definition: qbytearray.h:144
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:94
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:57
QString absoluteFilePath() const
Definition: qfileinfo.cpp:556
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
Format
Definition: qimage.h:77
@ Format_RGBA8888_Premultiplied
Definition: qimage.h:96
@ Format_ARGB32_Premultiplied
Definition: qimage.h:84
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
int resourceLimit(ResourceLimit limit) const
Definition: qrhi.cpp:6329
bool isYUpInFramebuffer() const
Definition: qrhi.cpp:6238
bool isFeatureSupported(QRhi::Feature feature) const
Definition: qrhi.cpp:6318
@ MaxThreadsPerThreadGroup
Definition: qrhi_p.h:1611
@ MaxThreadGroupZ
Definition: qrhi_p.h:1614
@ TextureSizeMin
Definition: qrhi_p.h:1605
@ MaxThreadGroupsPerDimension
Definition: qrhi_p.h:1610
@ TextureArraySizeMax
Definition: qrhi_p.h:1615
@ MaxColorAttachments
Definition: qrhi_p.h:1607
@ MaxThreadGroupY
Definition: qrhi_p.h:1613
@ MaxThreadGroupX
Definition: qrhi_p.h:1612
@ TextureSizeMax
Definition: qrhi_p.h:1606
@ MaxUniformBufferRange
Definition: qrhi_p.h:1616
QRhiProfiler * profiler()
Definition: qrhi.cpp:6377
@ SkipPresent
Definition: qrhi_p.h:1600
@ CustomInstanceStepRate
Definition: qrhi_p.h:1567
@ NonDynamicUniformBuffers
Definition: qrhi_p.h:1569
@ ElementIndexUint
Definition: qrhi_p.h:1573
@ MultisampleRenderBuffer
Definition: qrhi_p.h:1563
@ PipelineCacheDataLoadSave
Definition: qrhi_p.h:1587
@ BaseVertex
Definition: qrhi_p.h:1577
@ Compute
Definition: qrhi_p.h:1574
@ WideLines
Definition: qrhi_p.h:1575
@ BaseInstance
Definition: qrhi_p.h:1578
@ DebugMarkers
Definition: qrhi_p.h:1564
@ MultisampleTexture
Definition: qrhi_p.h:1562
@ NonFourAlignedEffectiveIndexBufferOffset
Definition: qrhi_p.h:1570
@ RedOrAlpha8IsRed
Definition: qrhi_p.h:1572
@ Timestamps
Definition: qrhi_p.h:1565
@ PrimitiveRestart
Definition: qrhi_p.h:1568
@ VertexShaderPointSize
Definition: qrhi_p.h:1576
@ NPOTTextureRepeat
Definition: qrhi_p.h:1571
@ Instancing
Definition: qrhi_p.h:1566
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
@ EnableProfiling
Definition: qrhi_p.h:1547
void setDevice(QIODevice *device)
void addVMemAllocatorStats()
void readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
Definition: qrhi.cpp:5519
@ UsedAsTransferSource
Definition: qrhi_p.h:1324
@ MinimalBufferCount
Definition: qrhi_p.h:1326
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
static QString static QString asprintf(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition: qstring.cpp:6759
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvectornd.h:205
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
QSize outputSize
QRhi::EndFrameFlags endFrameFlags
Definition: examplefw.h:129
QRhiSwapChain::Flags scFlags
Definition: examplefw.h:127
QRhi::Flags rhiFlags
Definition: examplefw.h:125
QRhi::BeginFrameFlags beginFrameFlags
Definition: examplefw.h:128
int sampleCount
Definition: examplefw.h:126
QRhiRenderBuffer * rb
Definition: image.cpp:51
#define QByteArrayLiteral(str)
Definition: qbytearray.h:80
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
Definition: qglobal.h:332
#define qDebug
[1]
Definition: qlogging.h:177
GLeglImageOES image
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
QByteArray data
Definition: qrhi_p.h:1471
QRhiTexture::Format format
Definition: qrhi_p.h:1469
std::function< void()> completed
Definition: qrhi_p.h:1468
TriangleRenderer triRenderer
Definition: triquadcube.cpp:77
QuadRenderer quadRenderer
Definition: triquadcube.cpp:78
bool onScreenOnly
Definition: triquadcube.cpp:81
struct @927 d
bool triangleOnly
Definition: triquadcube.cpp:82
int frameCount
Definition: triquadcube.cpp:84
void preInit()
Definition: triquadcube.cpp:88
TriangleOnCubeRenderer liveTexCubeRenderer
Definition: triquadcube.cpp:80
QFile profOut
Definition: triquadcube.cpp:85
QSize lastOutputSize
Definition: triquadcube.cpp:83
TexturedCubeRenderer cubeRenderer
Definition: triquadcube.cpp:79
int fn(int &i)