Debian Codegraph @

tests/manual/rhi/cubemap_scissor/cubemap_scissor.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 // This is a test for scissoring. Based on the cubemap test (because there the
5 // rendering covers the entire viewport which is what we need here). The
6 // scissor rectangle moves first up, then down, then from the center to the
7 // left and then to right. The important part is to ensure that the behavior
8 // identical between all backends, especially when the rectangle is partly or
9 // fully off window.
10
11 #include "../shared/examplefw.h"
12 #include "../shared/cube.h"
13
14 struct {
15 QList<QRhiResource *> releasePool;
16 QRhiBuffer *vbuf = nullptr;
17 QRhiBuffer *ubuf = nullptr;
18 QRhiTexture *tex = nullptr;
19 QRhiSampler *sampler = nullptr;
20 QRhiShaderResourceBindings *srb = nullptr;
21 QRhiGraphicsPipeline *ps = nullptr;
22 QRhiResourceUpdateBatch *initialUpdates = nullptr;
23
24 QPoint scissorBottomLeft;
25 QSize scissorSize;
26 int scissorAnimState = 0;
27 QSize outputSize;
28 } d;
29
30 void Window::customInit()
31 {
32 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
33 d.vbuf->create();
34 d.releasePool << d.vbuf;
35
36 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
37 d.ubuf->create();
38 d.releasePool << d.ubuf;
39
40 const QSize cubeMapSize(512, 512);
41 d.tex = m_r->newTexture(QRhiTexture::RGBA8, cubeMapSize, 1, QRhiTexture::CubeMap);
42 d.releasePool << d.tex;
43 d.tex->create();
44
45 d.initialUpdates = m_r->nextResourceUpdateBatch();
46 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
47
48 QImage img = QImage(":/c.png").flipped().convertToFormat(QImage::Format_RGBA8888);
49 // just use the same image for all faces for now
50 QRhiTextureSubresourceUploadDescription subresDesc(img);
51 QRhiTextureUploadDescription desc({
52 { 0, 0, subresDesc }, // +X
53 { 1, 0, subresDesc }, // -X
54 { 2, 0, subresDesc }, // +Y
55 { 3, 0, subresDesc }, // -Y
56 { 4, 0, subresDesc }, // +Z
57 { 5, 0, subresDesc } // -Z
58 });
59 d.initialUpdates->uploadTexture(d.tex, desc);
60
61 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
62 QRhiSampler::Repeat, QRhiSampler::Repeat);
63 d.releasePool << d.sampler;
64 d.sampler->create();
65
66 d.srb = m_r->newShaderResourceBindings();
67 d.releasePool << d.srb;
68 d.srb->setBindings({
69 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
70 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
71 });
72 d.srb->create();
73
74 d.ps = m_r->newGraphicsPipeline();
75 d.releasePool << d.ps;
76
77 d.ps->setFlags(QRhiGraphicsPipeline::UsesScissor);
78
79 d.ps->setDepthTest(true);
80 d.ps->setDepthWrite(true);
81 d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
82
83 d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back
84 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data
85
86 QShader vs = getShader(QLatin1String(":/cubemap.vert.qsb"));
87 Q_ASSERT(vs.isValid());
88 QShader fs = getShader(QLatin1String(":/cubemap.frag.qsb"));
89 Q_ASSERT(fs.isValid());
90 d.ps->setShaderStages({
91 { QRhiShaderStage::Vertex, vs },
92 { QRhiShaderStage::Fragment, fs }
93 });
94
95 QRhiVertexInputLayout inputLayout;
96 inputLayout.setBindings({
97 { 3 * sizeof(float) }
98 });
99 inputLayout.setAttributes({
100 { 0, 0, QRhiVertexInputAttribute::Float3, 0 }
101 });
102
103 d.ps->setVertexInputLayout(inputLayout);
104 d.ps->setShaderResourceBindings(d.srb);
105 d.ps->setRenderPassDescriptor(m_rp);
106
107 d.ps->create();
108
109 d.scissorAnimState = 0;
110 }
111
112 void Window::customRelease()
113 {
114 qDeleteAll(d.releasePool);
115 d.releasePool.clear();
116 }
117
118 static void advanceScissor()
119 {
120 switch (d.scissorAnimState) {
121 case 1: // up
122 d.scissorBottomLeft.setX(d.outputSize.width() / 4);
123 d.scissorBottomLeft.ry() += 1;
124 if (d.scissorBottomLeft.y() > d.outputSize.height() + 100)
125 d.scissorAnimState = 2;
126 break;
127 case 2: // down
128 d.scissorBottomLeft.ry() -= 1;
129 if (d.scissorBottomLeft.y() < -d.scissorSize.height() - 100)
130 d.scissorAnimState = 3;
131 break;
132 case 3: // left
133 d.scissorBottomLeft.setY(d.outputSize.height() / 4);
134 d.scissorBottomLeft.rx() += 1;
135 if (d.scissorBottomLeft.x() > d.outputSize.width() + 100)
136 d.scissorAnimState = 4;
137 break;
138 case 4: // right
139 d.scissorBottomLeft.rx() -= 1;
140 if (d.scissorBottomLeft.x() < -d.scissorSize.width() - 100)
141 d.scissorAnimState = 1;
142 break;
143 }
144
145 qDebug() << "scissor bottom-left" << d.scissorBottomLeft << "size" << d.scissorSize;
146 }
147
148 void Window::customRender()
149 {
150 const QSize outputSizeInPixels = m_sc->currentPixelSize();
151 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
152 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
153
154 if (d.initialUpdates) {
155 u->merge(d.initialUpdates);
156 d.initialUpdates->release();
157 d.initialUpdates = nullptr;
158 }
159
160 d.outputSize = outputSizeInPixels;
161 if (d.scissorAnimState == 0) {
162 d.scissorBottomLeft = QPoint(outputSizeInPixels.width() / 4, 0);
163 d.scissorSize = QSize(outputSizeInPixels.width() / 2, outputSizeInPixels.height() / 2);
164 d.scissorAnimState = 1;
165 }
166
167 QMatrix4x4 mvp = m_r->clipSpaceCorrMatrix();
168 mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f);
169 // cube vertices go from -1..1
170 mvp.scale(10);
171 static float rx = 0;
172 mvp.rotate(rx, 1, 0, 0);
173 rx += 0.5f;
174 // no translation
175 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
176
177 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
178 cb->setGraphicsPipeline(d.ps);
179 cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
180
181 // Apply a scissor rectangle that moves around on the screen, also
182 // exercising the out of screen (negative x or y) case.
183 cb->setScissor(QRhiScissor(d.scissorBottomLeft.x(), d.scissorBottomLeft.y(),
184 d.scissorSize.width(), d.scissorSize.height()));
185
186 cb->setShaderResources();
187
188 const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
189 cb->setVertexInput(0, 1, &vbufBinding);
190 cb->draw(36);
191 cb->endPass();
192
193 advanceScissor();
194 }