Debian Codegraph @

tests/manual/rhi/cubemap/cubemap.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
54 struct {
55 QVector<QRhiResource *> releasePool;
56 QRhiBuffer *vbuf = nullptr;
57 QRhiBuffer *ubuf = nullptr;
58 QRhiTexture *tex = nullptr;
59 QRhiSampler *sampler = nullptr;
60 QRhiShaderResourceBindings *srb = nullptr;
61 QRhiGraphicsPipeline *ps = nullptr;
62 QRhiResourceUpdateBatch *initialUpdates = nullptr;
63 } d;
64
65 void Window::customInit()
66 {
67 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
68 d.vbuf->build();
69 d.releasePool << d.vbuf;
70
71 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
72 d.ubuf->build();
73 d.releasePool << d.ubuf;
74
75 const QSize cubeMapSize(512, 512);
76 d.tex = m_r->newTexture(QRhiTexture::RGBA8, cubeMapSize, 1, QRhiTexture::CubeMap);
77 d.releasePool << d.tex;
78 d.tex->build();
79
80 d.initialUpdates = m_r->nextResourceUpdateBatch();
81 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
82
83 QImage img = QImage(":/c.png").mirrored().convertToFormat(QImage::Format_RGBA8888);
84 // just use the same image for all faces for now
85 QRhiTextureSubresourceUploadDescription subresDesc(img);
86 QRhiTextureUploadDescription desc({
87 { 0, 0, subresDesc }, // +X
88 { 1, 0, subresDesc }, // -X
89 { 2, 0, subresDesc }, // +Y
90 { 3, 0, subresDesc }, // -Y
91 { 4, 0, subresDesc }, // +Z
92 { 5, 0, subresDesc } // -Z
93 });
94 d.initialUpdates->uploadTexture(d.tex, desc);
95
96 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
97 QRhiSampler::Repeat, QRhiSampler::Repeat);
98 d.releasePool << d.sampler;
99 d.sampler->build();
100
101 d.srb = m_r->newShaderResourceBindings();
102 d.releasePool << d.srb;
103 d.srb->setBindings({
104 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
105 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
106 });
107 d.srb->build();
108
109 d.ps = m_r->newGraphicsPipeline();
110 d.releasePool << d.ps;
111
112 d.ps->setDepthTest(true);
113 d.ps->setDepthWrite(true);
114 d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
115
116 d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back
117 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data
118
119 QShader vs = getShader(QLatin1String(":/cubemap.vert.qsb"));
120 Q_ASSERT(vs.isValid());
121 QShader fs = getShader(QLatin1String(":/cubemap.frag.qsb"));
122 Q_ASSERT(fs.isValid());
123 d.ps->setShaderStages({
124 { QRhiShaderStage::Vertex, vs },
125 { QRhiShaderStage::Fragment, fs }
126 });
127
128 QRhiVertexInputLayout inputLayout;
129 inputLayout.setBindings({
130 { 3 * sizeof(float) }
131 });
132 inputLayout.setAttributes({
133 { 0, 0, QRhiVertexInputAttribute::Float3, 0 }
134 });
135
136 d.ps->setVertexInputLayout(inputLayout);
137 d.ps->setShaderResourceBindings(d.srb);
138 d.ps->setRenderPassDescriptor(m_rp);
139
140 d.ps->build();
141 }
142
143 void Window::customRelease()
144 {
145 qDeleteAll(d.releasePool);
146 d.releasePool.clear();
147 }
148
149 void Window::customRender()
150 {
151 const QSize outputSizeInPixels = m_sc->currentPixelSize();
152 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
153 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
154
155 if (d.initialUpdates) {
156 u->merge(d.initialUpdates);
157 d.initialUpdates->release();
158 d.initialUpdates = nullptr;
159 }
160
161 QMatrix4x4 mvp = m_r->clipSpaceCorrMatrix();
162 mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f);
163 // cube vertices go from -1..1
164 mvp.scale(10);
165 static float rx = 0;
166 mvp.rotate(rx, 1, 0, 0);
167 rx += 0.5f;
168 // no translation
169 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
170
171 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
172 cb->setGraphicsPipeline(d.ps);
173 cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
174 cb->setShaderResources();
175 const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
176 cb->setVertexInput(0, 1, &vbufBinding);
177 cb->draw(36);
178 cb->endPass();
179 }