Debian Codegraph @

tests/manual/rhi/compressedtexture_bc1_subupload/compressedtexture_bc1_subupload.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5 #include "../shared/cube.h"
6 #include "../shared/dds_bc1.h"
7
8 struct {
9 QRhiBuffer *vbuf = nullptr;
10 bool vbufReady = false;
11 QRhiBuffer *ubuf = nullptr;
12 QRhiTexture *tex = nullptr;
13 QRhiSampler *sampler = nullptr;
14 QRhiShaderResourceBindings *srb = nullptr;
15 QRhiGraphicsPipeline *ps = nullptr;
16
17 float rotation = 0;
18
19 QByteArrayList compressedData;
20 QByteArrayList compressedData2;
21 } d;
22
23 void Window::customInit()
24 {
25 if (!m_r->isTextureFormatSupported(QRhiTexture::BC1))
26 qFatal("This backend does not support BC1");
27
28 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
29 d.vbuf->create();
30 d.vbufReady = false;
31
32 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
33 d.ubuf->create();
34
35 QSize imageSize;
36 d.compressedData = loadBC1(QLatin1String(":/qt256_bc1_9mips.dds"), &imageSize);
37 Q_ASSERT(imageSize == QSize(256, 256));
38
39 d.tex = m_r->newTexture(QRhiTexture::BC1, imageSize);
40 d.tex->create();
41
42 d.compressedData2 = loadBC1(QLatin1String(":/bwqt224_64_nomips.dds"), &imageSize);
43 Q_ASSERT(imageSize == QSize(224, 64));
44
45 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, // no mipmapping here
46 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
47 d.sampler->create();
48
49 d.srb = m_r->newShaderResourceBindings();
50 d.srb->setBindings({
51 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
52 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
53 });
54 d.srb->create();
55
56 d.ps = m_r->newGraphicsPipeline();
57
58 d.ps->setDepthTest(true);
59 d.ps->setDepthWrite(true);
60 d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
61
62 d.ps->setCullMode(QRhiGraphicsPipeline::Back);
63 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
64
65 const QShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
66 if (!vs.isValid())
67 qFatal("Failed to load shader pack (vertex)");
68 const QShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
69 if (!fs.isValid())
70 qFatal("Failed to load shader pack (fragment)");
71
72 d.ps->setShaderStages({
73 { QRhiShaderStage::Vertex, vs },
74 { QRhiShaderStage::Fragment, fs }
75 });
76
77 QRhiVertexInputLayout inputLayout;
78 inputLayout.setBindings({
79 { 3 * sizeof(float) },
80 { 2 * sizeof(float) }
81 });
82 inputLayout.setAttributes({
83 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
84 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
85 });
86
87 d.ps->setVertexInputLayout(inputLayout);
88 d.ps->setShaderResourceBindings(d.srb);
89 d.ps->setRenderPassDescriptor(m_rp);
90
91 d.ps->create();
92 }
93
94 void Window::customRelease()
95 {
96 delete d.ps;
97 d.ps = nullptr;
98
99 delete d.srb;
100 d.srb = nullptr;
101
102 delete d.ubuf;
103 d.ubuf = nullptr;
104
105 delete d.vbuf;
106 d.vbuf = nullptr;
107
108 delete d.sampler;
109 d.sampler = nullptr;
110
111 delete d.tex;
112 d.tex = nullptr;
113 }
114
115 void Window::customRender()
116 {
117 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
118 if (!d.vbufReady) {
119 d.vbufReady = true;
120 u->uploadStaticBuffer(d.vbuf, cube);
121 qint32 flip = 0;
122 u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
123 }
124 if (!d.compressedData.isEmpty()) {
125 {
126 QRhiTextureUploadDescription desc({ 0, 0, { d.compressedData[0].constData(), quint32(d.compressedData[0].size()) } });
127 u->uploadTexture(d.tex, desc);
128 d.compressedData.clear();
129 }
130
131 // now exercise uploading a smaller compressed image into the same texture
132 {
133 QRhiTextureSubresourceUploadDescription image(d.compressedData2[0].constData(), d.compressedData2[0].size());
134 // positions and sizes must be multiples of 4 here (for BC1)
135 image.setDestinationTopLeft(QPoint(16, 32));
136 // the image is smaller than the subresource size (224x64 vs
137 // 256x256) so the size must be specified manually
138 image.setSourceSize(QSize(224, 64));
139 QRhiTextureUploadDescription desc({ 0, 0, image });
140 u->uploadTexture(d.tex, desc);
141 d.compressedData2.clear();
142 }
143 }
144 d.rotation += 1.0f;
145 QMatrix4x4 mvp = m_proj;
146 mvp.scale(0.5f);
147 mvp.rotate(d.rotation, 0, 1, 0);
148 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
149
150 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
151 const QSize outputSizeInPixels = m_sc->currentPixelSize();
152
153 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
154
155 cb->setGraphicsPipeline(d.ps);
156 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
157 cb->setShaderResources();
158 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
159 { d.vbuf, 0 },
160 { d.vbuf, quint32(36 * 3 * sizeof(float)) }
161 };
162 cb->setVertexInput(0, 2, vbufBindings);
163 cb->draw(36);
164
165 cb->endPass();
166 }