Debian Codegraph @

tests/manual/rhi/instancing/instancing.cpp

raw
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 #include "../shared/examplefw.h"
52 #include "../shared/cube.h"
53 #include <QRandomGenerator>
54
55 // Instanced draw example. When running with OpenGL, at least 3.3 or ES 3.0 is
56 // needed.
57
58 const int INSTANCE_COUNT = 1024;
59
60 struct {
61 QVector<QRhiResource *> releasePool;
62
63 QRhiBuffer *vbuf = nullptr;
64 QRhiBuffer *instBuf = nullptr;
65 QRhiBuffer *ubuf = nullptr;
66 QRhiShaderResourceBindings *srb = nullptr;
67 QRhiGraphicsPipeline *ps = nullptr;
68
69 QRhiResourceUpdateBatch *initialUpdates = nullptr;
70 QMatrix4x4 winProj;
71 } d;
72
73 void Window::customInit()
74 {
75 if (!m_r->isFeatureSupported(QRhi::Instancing))
76 qFatal("Instanced drawing is not supported");
77
78 d.initialUpdates = m_r->nextResourceUpdateBatch();
79
80 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
81 d.vbuf->build();
82 d.releasePool << d.vbuf;
83
84 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
85
86 // translation + color (vec3 + vec3), interleaved, for each instance
87 d.instBuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, INSTANCE_COUNT * 6 * sizeof(float));
88 d.instBuf->build();
89 d.releasePool << d.instBuf;
90
91 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 12);
92 d.ubuf->build();
93 d.releasePool << d.ubuf;
94
95 d.srb = m_r->newShaderResourceBindings();
96 d.releasePool << d.srb;
97 d.srb->setBindings({
98 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, d.ubuf)
99 });
100 d.srb->build();
101
102 d.ps = m_r->newGraphicsPipeline();
103 d.releasePool << d.ps;
104 d.ps->setShaderStages({
105 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/inst.vert.qsb")) },
106 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/inst.frag.qsb")) }
107 });
108 QRhiVertexInputLayout inputLayout;
109 inputLayout.setBindings({
110 { 3 * sizeof(float) }, // cube vertices
111 { 6 * sizeof(float), QRhiVertexInputBinding::PerInstance } // per-instance translation and color
112 });
113 inputLayout.setAttributes({
114 { 0, 0, QRhiVertexInputAttribute::Float3, 0 }, // position
115 { 1, 1, QRhiVertexInputAttribute::Float3, 0 }, // instTranslate
116 { 1, 2, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) } // instColor
117 });
118 d.ps->setVertexInputLayout(inputLayout);
119 d.ps->setShaderResourceBindings(d.srb);
120 d.ps->setRenderPassDescriptor(m_rp);
121 d.ps->build();
122
123 QByteArray instData;
124 instData.resize(INSTANCE_COUNT * 6 * sizeof(float));
125 float *p = reinterpret_cast<float *>(instData.data());
126 QRandomGenerator *rgen = QRandomGenerator::global();
127 for (int i = 0; i < INSTANCE_COUNT; ++i) {
128 // translation
129 *p++ = rgen->bounded(8000) / 100.0f - 40.0f;
130 *p++ = rgen->bounded(8000) / 100.0f - 40.0f;
131 *p++ = 0.0f;
132 // color
133 *p++ = i / float(INSTANCE_COUNT);
134 *p++ = 0.0f;
135 *p++ = 0.0f;
136 }
137 d.initialUpdates->uploadStaticBuffer(d.instBuf, instData.constData());
138 }
139
140 void Window::customRelease()
141 {
142 qDeleteAll(d.releasePool);
143 d.releasePool.clear();
144 }
145
146 void Window::customRender()
147 {
148 const QSize outputSizeInPixels = m_sc->currentPixelSize();
149 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
150 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
151 if (d.initialUpdates) {
152 u->merge(d.initialUpdates);
153 d.initialUpdates->release();
154 d.initialUpdates = nullptr;
155 }
156
157 if (d.winProj != m_proj) {
158 d.winProj = m_proj;
159 QMatrix4x4 mvp = m_proj;
160 mvp.scale(0.05f);
161 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
162 }
163
164 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
165 cb->setGraphicsPipeline(d.ps);
166 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
167 cb->setShaderResources();
168 const QRhiCommandBuffer::VertexInput vbufBinding[] = {
169 { d.vbuf, 0 },
170 { d.instBuf, 0 }
171 };
172 cb->setVertexInput(0, 2, vbufBinding);
173 cb->draw(36, INSTANCE_COUNT);
174 cb->endPass();
175 }