QtBase  v6.3.1
noninstanced.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 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 #define EXAMPLEFW_PREINIT
52 #include "../shared/examplefw.h"
53 #include "../shared/cube.h"
54 #include <QRandomGenerator>
55 
56 // A variation on the instancing test. No instancing here, just
57 // individual uniform buffer updates and draw calls.
58 
59 const int INSTANCE_COUNT = 25000;
60 
61 struct {
63 
68 
70 
71  struct {
72  float x, y, z;
73  float r, g, b;
75  float rot = 0.0f;
76 } d;
77 
78 void preInit()
79 {
80  debugLayer = false;
81 }
82 
83 void Window::customInit()
84 {
85  d.initialUpdates = m_r->nextResourceUpdateBatch();
86 
88  d.vbuf->create();
89  d.releasePool << d.vbuf;
90 
91  d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
92 
93  for (int i = 0; i < INSTANCE_COUNT; ++i) {
95  d.ubuf[i]->create();
96  d.releasePool << d.ubuf[i];
97 
98  d.srb[i] = m_r->newShaderResourceBindings();
99  d.releasePool << d.srb[i];
100  d.srb[i]->setBindings({
102  });
103  d.srb[i]->create();
104  }
105 
106  d.ps = m_r->newGraphicsPipeline();
107  d.releasePool << d.ps;
108  d.ps->setShaderStages({
109  { QRhiShaderStage::Vertex, getShader(QLatin1String(":/material.vert.qsb")) },
110  { QRhiShaderStage::Fragment, getShader(QLatin1String(":/material.frag.qsb")) }
111  });
112  QRhiVertexInputLayout inputLayout;
113  inputLayout.setBindings({
114  { 3 * sizeof(float) },
115  });
116  inputLayout.setAttributes({
118  });
119  d.ps->setVertexInputLayout(inputLayout);
120  d.ps->setShaderResourceBindings(d.srb[0]);
121  d.ps->setRenderPassDescriptor(m_rp);
122  d.ps->create();
123 
125  for (int i = 0; i < INSTANCE_COUNT; ++i) {
126  d.instData[i].x = rgen->bounded(8000) / 100.0f - 40.0f;
127  d.instData[i].y = rgen->bounded(8000) / 100.0f - 40.0f;
128  d.instData[i].z = rgen->bounded(100) / -4.0f;
129  d.instData[i].r = i / float(INSTANCE_COUNT);
130  d.instData[i].g = 0.0f;
131  d.instData[i].b = 0.0f;
132  }
133 }
134 
136 {
137  qDeleteAll(d.releasePool);
138  d.releasePool.clear();
139 }
140 
142 {
143  const QSize outputSizeInPixels = m_sc->currentPixelSize();
144  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
145  QRhiResourceUpdateBatch *u = nullptr;
146  if (d.initialUpdates) {
147  u = d.initialUpdates;
148  d.initialUpdates = nullptr;
149  }
150 
151  for (int i = 0; i < INSTANCE_COUNT; ++i) {
152  char *p = d.ubuf[i]->beginFullDynamicBufferUpdateForCurrentFrame();
153  QMatrix4x4 mvp = m_proj;
154  mvp.rotate(d.rot, 0, 1, 0);
155  mvp.scale(0.05f);
156  memcpy(p, mvp.constData(), 64);
157 
158  // float *v = reinterpret_cast<float *>(p + 64);
159  // v[0] = d.instData[i].x;
160  // v[1] = d.instData[i].y;
161  // v[2] = d.instData[i].z;
162  memcpy(p + 64, &d.instData[i].x, 4);
163  memcpy(p + 68, &d.instData[i].y, 4);
164  memcpy(p + 72, &d.instData[i].z, 4);
165 
166  // v = reinterpret_cast<float *>(p + 80);
167  // v[0] = d.instData[i].r;
168  // v[1] = d.instData[i].g;
169  // v[2] = d.instData[i].b;
170  memcpy(p + 80, &d.instData[i].r, 4);
171  memcpy(p + 84, &d.instData[i].g, 4);
172  memcpy(p + 88, &d.instData[i].b, 4);
173 
174  d.ubuf[i]->endFullDynamicBufferUpdateForCurrentFrame();
175  }
176 
177  cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u, QRhiCommandBuffer::DoNotTrackResourcesForCompute);
178  for (int i = 0; i < INSTANCE_COUNT; ++i) {
179  cb->setGraphicsPipeline(d.ps);
180  if (i == 0)
181  cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
182  cb->setShaderResources(d.srb[i]);
183  const QRhiCommandBuffer::VertexInput vbufBinding[] = {
184  { d.vbuf, 0 },
185  };
186  cb->setVertexInput(0, 1, vbufBinding);
187  cb->draw(36);
188  }
189  cb->endPass();
190 
191  d.rot += 0.1f;
192 }
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 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
The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Gener...
Definition: qrandom.h:57
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
Definition: qrandom.h:311
double bounded(double highest)
Definition: qrandom.h:108
@ 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
@ DoNotTrackResourcesForCompute
Definition: qrhi_p.h:1406
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
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition: qrhi.cpp:3268
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())
bool debugLayer
Definition: examplefw.h:132
QShader getShader(const QString &name)
Definition: examplefw.h:86
struct @919 d
float r
QRhiBuffer * ubuf[INSTANCE_COUNT]
QList< QRhiResource * > releasePool
float rot
float b
float g
float y
const int INSTANCE_COUNT
QRhiBuffer * vbuf
void preInit()
float x
[1]
QRhiGraphicsPipeline * ps
struct @919::@920 instData[INSTANCE_COUNT]
QRhiResourceUpdateBatch * initialUpdates
float z
QRhiShaderResourceBindings * srb[INSTANCE_COUNT]
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)