Debian Codegraph @

tests/manual/rhi/compressedtexture_bc1/compressedtexture_bc1.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 "../shared/examplefw.h"
52 #include "../shared/cube.h"
53 #include "../shared/dds_bc1.h"
54
55 struct {
56 QRhiBuffer *vbuf = nullptr;
57 bool vbufReady = false;
58 QRhiBuffer *ubuf = nullptr;
59 QRhiTexture *tex = nullptr;
60 QRhiSampler *sampler = nullptr;
61 QRhiShaderResourceBindings *srb = nullptr;
62 QRhiGraphicsPipeline *ps = nullptr;
63
64 float rotation = 0;
65
66 QByteArrayList compressedData;
67 } d;
68
69 void Window::customInit()
70 {
71 if (!m_r->isTextureFormatSupported(QRhiTexture::BC1))
72 qFatal("This backend does not support BC1");
73
74 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
75 d.vbuf->build();
76 d.vbufReady = false;
77
78 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
79 d.ubuf->build();
80
81 QSize imageSize;
82 d.compressedData = loadBC1(QLatin1String(":/qt256_bc1_9mips.dds"), &imageSize);
83 qDebug() << d.compressedData.count() << imageSize << m_r->mipLevelsForSize(imageSize);
84
85 d.tex = m_r->newTexture(QRhiTexture::BC1, imageSize, 1, QRhiTexture::MipMapped);
86 d.tex->build();
87
88 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear,
89 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
90 d.sampler->build();
91
92 d.srb = m_r->newShaderResourceBindings();
93 d.srb->setBindings({
94 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
95 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
96 });
97 d.srb->build();
98
99 d.ps = m_r->newGraphicsPipeline();
100
101 d.ps->setDepthTest(true);
102 d.ps->setDepthWrite(true);
103 d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
104
105 d.ps->setCullMode(QRhiGraphicsPipeline::Back);
106 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
107
108 const QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
109 if (!vs.isValid())
110 qFatal("Failed to load shader pack (vertex)");
111 const QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
112 if (!fs.isValid())
113 qFatal("Failed to load shader pack (fragment)");
114
115 d.ps->setShaderStages({
116 { QRhiShaderStage::Vertex, vs },
117 { QRhiShaderStage::Fragment, fs }
118 });
119
120 QRhiVertexInputLayout inputLayout;
121 inputLayout.setBindings({
122 { 3 * sizeof(float) },
123 { 2 * sizeof(float) }
124 });
125 inputLayout.setAttributes({
126 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
127 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
128 });
129
130 d.ps->setVertexInputLayout(inputLayout);
131 d.ps->setShaderResourceBindings(d.srb);
132 d.ps->setRenderPassDescriptor(m_rp);
133
134 d.ps->build();
135 }
136
137 void Window::customRelease()
138 {
139 delete d.ps;
140 d.ps = nullptr;
141
142 delete d.srb;
143 d.srb = nullptr;
144
145 delete d.ubuf;
146 d.ubuf = nullptr;
147
148 delete d.vbuf;
149 d.vbuf = nullptr;
150
151 delete d.sampler;
152 d.sampler = nullptr;
153
154 delete d.tex;
155 d.tex = nullptr;
156 }
157
158 void Window::customRender()
159 {
160 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
161 if (!d.vbufReady) {
162 d.vbufReady = true;
163 u->uploadStaticBuffer(d.vbuf, cube);
164 qint32 flip = 0;
165 u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
166 }
167 if (!d.compressedData.isEmpty()) {
168 QVarLengthArray<QRhiTextureUploadEntry, 16> descEntries;
169 for (int i = 0; i < d.compressedData.count(); ++i) {
170 QRhiTextureSubresourceUploadDescription image(d.compressedData[i].constData(), d.compressedData[i].size());
171 descEntries.append({ 0, i, image });
172 }
173 QRhiTextureUploadDescription desc;
174 desc.setEntries(descEntries.cbegin(), descEntries.cend());
175 u->uploadTexture(d.tex, desc);
176 d.compressedData.clear();
177 }
178 d.rotation += 1.0f;
179 QMatrix4x4 mvp = m_proj;
180 mvp.scale(0.5f);
181 mvp.rotate(d.rotation, 0, 1, 0);
182 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
183
184 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
185 const QSize outputSizeInPixels = m_sc->currentPixelSize();
186
187 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
188
189 cb->setGraphicsPipeline(d.ps);
190 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
191 cb->setShaderResources();
192 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
193 { d.vbuf, 0 },
194 { d.vbuf, 36 * 3 * sizeof(float) }
195 };
196 cb->setVertexInput(0, 2, vbufBindings);
197 cb->draw(36);
198
199 cb->endPass();
200 }