Debian Codegraph @

tests/manual/rhi/triquadcube/texturedcuberenderer.cpp

raw
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 "texturedcuberenderer.h"
52 #include <QFile>
53 #include <QtGui/private/qshader_p.h>
54
55 #include "../shared/cube.h"
56
57 const bool MIPMAP = true;
58 const bool AUTOGENMIPMAP = true;
59
60 static QShader getShader(const QString &name)
61 {
62 QFile f(name);
63 if (f.open(QIODevice::ReadOnly))
64 return QShader::fromSerialized(f.readAll());
65
66 return QShader();
67 }
68
69 void TexturedCubeRenderer::initResources(QRhiRenderPassDescriptor *rp)
70 {
71 m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
72 m_vbuf->setName(QByteArrayLiteral("Cube vbuf (textured)"));
73 m_vbuf->build();
74 m_vbufReady = false;
75
76 m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4);
77 m_ubuf->setName(QByteArrayLiteral("Cube ubuf (textured)"));
78 m_ubuf->build();
79
80 m_image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888);
81 QRhiTexture::Flags texFlags;
82 if (MIPMAP)
83 texFlags |= QRhiTexture::MipMapped;
84 if (AUTOGENMIPMAP)
85 texFlags |= QRhiTexture::UsedWithGenerateMips;
86 m_tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(m_image.width(), m_image.height()), 1, texFlags);
87 m_tex->setName(QByteArrayLiteral("Qt texture"));
88 m_tex->build();
89
90 m_sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, MIPMAP ? QRhiSampler::Linear : QRhiSampler::None,
91 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
92 m_sampler->build();
93
94 m_srb = m_r->newShaderResourceBindings();
95 m_srb->setBindings({
96 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf),
97 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, m_tex, m_sampler)
98 });
99 m_srb->build();
100
101 m_ps = m_r->newGraphicsPipeline();
102
103 // No blending but the texture has alpha which we do not want to write out.
104 // Be nice. (would not matter for an onscreen window but makes a difference
105 // when reading back and saving into image files f.ex.)
106 QRhiGraphicsPipeline::TargetBlend blend;
107 blend.colorWrite = QRhiGraphicsPipeline::R | QRhiGraphicsPipeline::G | QRhiGraphicsPipeline::B;
108 m_ps->setTargetBlends({ blend });
109
110 m_ps->setDepthTest(true);
111 m_ps->setDepthWrite(true);
112 m_ps->setDepthOp(QRhiGraphicsPipeline::Less);
113
114 m_ps->setCullMode(QRhiGraphicsPipeline::Back);
115 m_ps->setFrontFace(QRhiGraphicsPipeline::CCW);
116
117 m_ps->setSampleCount(m_sampleCount);
118
119 QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
120 Q_ASSERT(vs.isValid());
121 QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
122 Q_ASSERT(fs.isValid());
123 m_ps->setShaderStages({
124 { QRhiShaderStage::Vertex, vs },
125 { QRhiShaderStage::Fragment, fs }
126 });
127
128 QRhiVertexInputLayout inputLayout;
129 inputLayout.setBindings({
130 { 3 * sizeof(float) },
131 { 2 * sizeof(float) }
132 });
133 inputLayout.setAttributes({
134 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
135 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
136 });
137
138 m_ps->setVertexInputLayout(inputLayout);
139 m_ps->setShaderResourceBindings(m_srb);
140 m_ps->setRenderPassDescriptor(rp);
141
142 m_ps->build();
143 }
144
145 void TexturedCubeRenderer::resize(const QSize &pixelSize)
146 {
147 m_proj = m_r->clipSpaceCorrMatrix();
148 m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f);
149 m_proj.translate(0, 0, -4);
150 }
151
152 void TexturedCubeRenderer::releaseResources()
153 {
154 delete m_ps;
155 m_ps = nullptr;
156
157 delete m_srb;
158 m_srb = nullptr;
159
160 delete m_sampler;
161 m_sampler = nullptr;
162
163 delete m_tex;
164 m_tex = nullptr;
165
166 delete m_ubuf;
167 m_ubuf = nullptr;
168
169 delete m_vbuf;
170 m_vbuf = nullptr;
171 }
172
173 void TexturedCubeRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
174 {
175 if (!m_vbufReady) {
176 m_vbufReady = true;
177 resourceUpdates->uploadStaticBuffer(m_vbuf, cube);
178 qint32 flip = 0;
179 resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &flip);
180 }
181
182 if (!m_image.isNull()) {
183 if (MIPMAP) {
184 QVarLengthArray<QRhiTextureUploadEntry, 16> descEntries;
185 if (!AUTOGENMIPMAP) {
186 // the ghetto mipmap generator...
187 for (int i = 0, ie = m_r->mipLevelsForSize(m_image.size()); i != ie; ++i) {
188 QImage image = m_image.scaled(m_r->sizeForMipLevel(i, m_image.size()));
189 descEntries.append({ 0, i, image });
190 }
191 } else {
192 descEntries.append({ 0, 0, m_image });
193 }
194 QRhiTextureUploadDescription desc;
195 desc.setEntries(descEntries.cbegin(), descEntries.cend());
196 resourceUpdates->uploadTexture(m_tex, desc);
197 if (AUTOGENMIPMAP)
198 resourceUpdates->generateMips(m_tex);
199 } else {
200 resourceUpdates->uploadTexture(m_tex, m_image);
201 }
202 m_image = QImage();
203 }
204
205 m_rotation += 1.0f;
206 QMatrix4x4 mvp = m_proj;
207 mvp.translate(m_translation);
208 mvp.scale(0.5f);
209 mvp.rotate(m_rotation, 0, 1, 0);
210 resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData());
211 }
212
213 void TexturedCubeRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels)
214 {
215 cb->setGraphicsPipeline(m_ps);
216 cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
217 cb->setShaderResources();
218 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
219 { m_vbuf, 0 },
220 { m_vbuf, 36 * 3 * sizeof(float) }
221 };
222 cb->setVertexInput(0, 2, vbufBindings);
223 cb->draw(36);
224 }