Debian Codegraph @

tests/manual/rhi/compressedtexture_bc1_subupload/compressedtexture_bc1_subupload.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 QByteArrayList compressedData2;
68 } d;
69
70 void Window::customInit()
71 {
72 if (!m_r->isTextureFormatSupported(QRhiTexture::BC1))
73 qFatal("This backend does not support BC1");
74
75 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
76 d.vbuf->build();
77 d.vbufReady = false;
78
79 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
80 d.ubuf->build();
81
82 QSize imageSize;
83 d.compressedData = loadBC1(QLatin1String(":/qt256_bc1_9mips.dds"), &imageSize);
84 Q_ASSERT(imageSize == QSize(256, 256));
85
86 d.tex = m_r->newTexture(QRhiTexture::BC1, imageSize);
87 d.tex->build();
88
89 d.compressedData2 = loadBC1(QLatin1String(":/bwqt224_64_nomips.dds"), &imageSize);
90 Q_ASSERT(imageSize == QSize(224, 64));
91
92 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, // no mipmapping here
93 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
94 d.sampler->build();
95
96 d.srb = m_r->newShaderResourceBindings();
97 d.srb->setBindings({
98 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
99 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
100 });
101 d.srb->build();
102
103 d.ps = m_r->newGraphicsPipeline();
104
105 d.ps->setDepthTest(true);
106 d.ps->setDepthWrite(true);
107 d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
108
109 d.ps->setCullMode(QRhiGraphicsPipeline::Back);
110 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
111
112 const QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
113 if (!vs.isValid())
114 qFatal("Failed to load shader pack (vertex)");
115 const QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
116 if (!fs.isValid())
117 qFatal("Failed to load shader pack (fragment)");
118
119 d.ps->setShaderStages({
120 { QRhiShaderStage::Vertex, vs },
121 { QRhiShaderStage::Fragment, fs }
122 });
123
124 QRhiVertexInputLayout inputLayout;
125 inputLayout.setBindings({
126 { 3 * sizeof(float) },
127 { 2 * sizeof(float) }
128 });
129 inputLayout.setAttributes({
130 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
131 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
132 });
133
134 d.ps->setVertexInputLayout(inputLayout);
135 d.ps->setShaderResourceBindings(d.srb);
136 d.ps->setRenderPassDescriptor(m_rp);
137
138 d.ps->build();
139 }
140
141 void Window::customRelease()
142 {
143 delete d.ps;
144 d.ps = nullptr;
145
146 delete d.srb;
147 d.srb = nullptr;
148
149 delete d.ubuf;
150 d.ubuf = nullptr;
151
152 delete d.vbuf;
153 d.vbuf = nullptr;
154
155 delete d.sampler;
156 d.sampler = nullptr;
157
158 delete d.tex;
159 d.tex = nullptr;
160 }
161
162 void Window::customRender()
163 {
164 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
165 if (!d.vbufReady) {
166 d.vbufReady = true;
167 u->uploadStaticBuffer(d.vbuf, cube);
168 qint32 flip = 0;
169 u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
170 }
171 if (!d.compressedData.isEmpty()) {
172 {
173 QRhiTextureUploadDescription desc({ 0, 0, { d.compressedData[0].constData(), d.compressedData[0].size() } });
174 u->uploadTexture(d.tex, desc);
175 d.compressedData.clear();
176 }
177
178 // now exercise uploading a smaller compressed image into the same texture
179 {
180 QRhiTextureSubresourceUploadDescription image(d.compressedData2[0].constData(), d.compressedData2[0].size());
181 // positions and sizes must be multiples of 4 here (for BC1)
182 image.setDestinationTopLeft(QPoint(16, 32));
183 // the image is smaller than the subresource size (224x64 vs
184 // 256x256) so the size must be specified manually
185 image.setSourceSize(QSize(224, 64));
186 QRhiTextureUploadDescription desc({ 0, 0, image });
187 u->uploadTexture(d.tex, desc);
188 d.compressedData2.clear();
189 }
190 }
191 d.rotation += 1.0f;
192 QMatrix4x4 mvp = m_proj;
193 mvp.scale(0.5f);
194 mvp.rotate(d.rotation, 0, 1, 0);
195 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
196
197 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
198 const QSize outputSizeInPixels = m_sc->currentPixelSize();
199
200 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
201
202 cb->setGraphicsPipeline(d.ps);
203 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
204 cb->setShaderResources();
205 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
206 { d.vbuf, 0 },
207 { d.vbuf, 36 * 3 * sizeof(float) }
208 };
209 cb->setVertexInput(0, 2, vbufBindings);
210 cb->draw(36);
211
212 cb->endPass();
213 }