Debian Codegraph @

tests/manual/rhi/cubemap_scissor/cubemap_scissor.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 // This is a test for scissoring. Based on the cubemap test (because there the
52 // rendering covers the entire viewport which is what we need here). The
53 // scissor rectangle moves first up, then down, then from the center to the
54 // left and then to right. The important part is to ensure that the behavior
55 // identical between all backends, especially when the rectangle is partly or
56 // fully off window.
57
58 #include "../shared/examplefw.h"
59 #include "../shared/cube.h"
60
61 struct {
62 QVector<QRhiResource *> releasePool;
63 QRhiBuffer *vbuf = nullptr;
64 QRhiBuffer *ubuf = nullptr;
65 QRhiTexture *tex = nullptr;
66 QRhiSampler *sampler = nullptr;
67 QRhiShaderResourceBindings *srb = nullptr;
68 QRhiGraphicsPipeline *ps = nullptr;
69 QRhiResourceUpdateBatch *initialUpdates = nullptr;
70
71 QPoint scissorBottomLeft;
72 QSize scissorSize;
73 int scissorAnimState = 0;
74 QSize outputSize;
75 } d;
76
77 void Window::customInit()
78 {
79 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
80 d.vbuf->build();
81 d.releasePool << d.vbuf;
82
83 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
84 d.ubuf->build();
85 d.releasePool << d.ubuf;
86
87 const QSize cubeMapSize(512, 512);
88 d.tex = m_r->newTexture(QRhiTexture::RGBA8, cubeMapSize, 1, QRhiTexture::CubeMap);
89 d.releasePool << d.tex;
90 d.tex->build();
91
92 d.initialUpdates = m_r->nextResourceUpdateBatch();
93 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
94
95 QImage img = QImage(":/c.png").mirrored().convertToFormat(QImage::Format_RGBA8888);
96 // just use the same image for all faces for now
97 QRhiTextureSubresourceUploadDescription subresDesc(img);
98 QRhiTextureUploadDescription desc({
99 { 0, 0, subresDesc }, // +X
100 { 1, 0, subresDesc }, // -X
101 { 2, 0, subresDesc }, // +Y
102 { 3, 0, subresDesc }, // -Y
103 { 4, 0, subresDesc }, // +Z
104 { 5, 0, subresDesc } // -Z
105 });
106 d.initialUpdates->uploadTexture(d.tex, desc);
107
108 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
109 QRhiSampler::Repeat, QRhiSampler::Repeat);
110 d.releasePool << d.sampler;
111 d.sampler->build();
112
113 d.srb = m_r->newShaderResourceBindings();
114 d.releasePool << d.srb;
115 d.srb->setBindings({
116 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
117 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
118 });
119 d.srb->build();
120
121 d.ps = m_r->newGraphicsPipeline();
122 d.releasePool << d.ps;
123
124 d.ps->setFlags(QRhiGraphicsPipeline::UsesScissor);
125
126 d.ps->setDepthTest(true);
127 d.ps->setDepthWrite(true);
128 d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
129
130 d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back
131 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data
132
133 QShader vs = getShader(QLatin1String(":/cubemap.vert.qsb"));
134 Q_ASSERT(vs.isValid());
135 QShader fs = getShader(QLatin1String(":/cubemap.frag.qsb"));
136 Q_ASSERT(fs.isValid());
137 d.ps->setShaderStages({
138 { QRhiShaderStage::Vertex, vs },
139 { QRhiShaderStage::Fragment, fs }
140 });
141
142 QRhiVertexInputLayout inputLayout;
143 inputLayout.setBindings({
144 { 3 * sizeof(float) }
145 });
146 inputLayout.setAttributes({
147 { 0, 0, QRhiVertexInputAttribute::Float3, 0 }
148 });
149
150 d.ps->setVertexInputLayout(inputLayout);
151 d.ps->setShaderResourceBindings(d.srb);
152 d.ps->setRenderPassDescriptor(m_rp);
153
154 d.ps->build();
155
156 d.scissorAnimState = 0;
157 }
158
159 void Window::customRelease()
160 {
161 qDeleteAll(d.releasePool);
162 d.releasePool.clear();
163 }
164
165 static void advanceScissor()
166 {
167 switch (d.scissorAnimState) {
168 case 1: // up
169 d.scissorBottomLeft.setX(d.outputSize.width() / 4);
170 d.scissorBottomLeft.ry() += 1;
171 if (d.scissorBottomLeft.y() > d.outputSize.height() + 100)
172 d.scissorAnimState = 2;
173 break;
174 case 2: // down
175 d.scissorBottomLeft.ry() -= 1;
176 if (d.scissorBottomLeft.y() < -d.scissorSize.height() - 100)
177 d.scissorAnimState = 3;
178 break;
179 case 3: // left
180 d.scissorBottomLeft.setY(d.outputSize.height() / 4);
181 d.scissorBottomLeft.rx() += 1;
182 if (d.scissorBottomLeft.x() > d.outputSize.width() + 100)
183 d.scissorAnimState = 4;
184 break;
185 case 4: // right
186 d.scissorBottomLeft.rx() -= 1;
187 if (d.scissorBottomLeft.x() < -d.scissorSize.width() - 100)
188 d.scissorAnimState = 1;
189 break;
190 }
191
192 qDebug() << "scissor bottom-left" << d.scissorBottomLeft << "size" << d.scissorSize;
193 }
194
195 void Window::customRender()
196 {
197 const QSize outputSizeInPixels = m_sc->currentPixelSize();
198 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
199 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
200
201 if (d.initialUpdates) {
202 u->merge(d.initialUpdates);
203 d.initialUpdates->release();
204 d.initialUpdates = nullptr;
205 }
206
207 d.outputSize = outputSizeInPixels;
208 if (d.scissorAnimState == 0) {
209 d.scissorBottomLeft = QPoint(outputSizeInPixels.width() / 4, 0);
210 d.scissorSize = QSize(outputSizeInPixels.width() / 2, outputSizeInPixels.height() / 2);
211 d.scissorAnimState = 1;
212 }
213
214 QMatrix4x4 mvp = m_r->clipSpaceCorrMatrix();
215 mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f);
216 // cube vertices go from -1..1
217 mvp.scale(10);
218 static float rx = 0;
219 mvp.rotate(rx, 1, 0, 0);
220 rx += 0.5f;
221 // no translation
222 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
223
224 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
225 cb->setGraphicsPipeline(d.ps);
226 cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
227
228 // Apply a scissor rectangle that moves around on the screen, also
229 // exercising the out of screen (negative x or y) case.
230 cb->setScissor(QRhiScissor(d.scissorBottomLeft.x(), d.scissorBottomLeft.y(),
231 d.scissorSize.width(), d.scissorSize.height()));
232
233 cb->setShaderResources();
234
235 const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
236 cb->setVertexInput(0, 1, &vbufBinding);
237 cb->draw(36);
238 cb->endPass();
239
240 advanceScissor();
241 }