Debian Codegraph @

tests/manual/rhi/hellominimalcrossgfxtriangle/hellowindow.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 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 "hellowindow.h"
52 #include <QFile>
53 #include <QtGui/private/qshader_p.h>
54
55 static float vertexData[] = {
56 // Y up (note clipSpaceCorrMatrix in m_proj), CCW
57 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
58 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
59 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
60 };
61
62 HelloWindow::HelloWindow(QRhi::Implementation graphicsApi)
63 : Window(graphicsApi)
64 {
65 }
66
67 QShader HelloWindow::getShader(const QString &name)
68 {
69 QFile f(name);
70 if (f.open(QIODevice::ReadOnly))
71 return QShader::fromSerialized(f.readAll());
72
73 return QShader();
74 }
75
76 void HelloWindow::customInit()
77 {
78 m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)));
79 m_vbuf->build();
80 m_vbufReady = false;
81
82 m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68));
83 m_ubuf->build();
84
85 m_srb.reset(m_rhi->newShaderResourceBindings());
86 m_srb->setBindings({
87 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
88 m_ubuf.get())
89 });
90 m_srb->build();
91
92 m_ps.reset(m_rhi->newGraphicsPipeline());
93
94 QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
95 premulAlphaBlend.enable = true;
96 m_ps->setTargetBlends({ premulAlphaBlend });
97
98 const QShader vs = getShader(QLatin1String(":/color.vert.qsb"));
99 if (!vs.isValid())
100 qFatal("Failed to load shader pack (vertex)");
101 const QShader fs = getShader(QLatin1String(":/color.frag.qsb"));
102 if (!fs.isValid())
103 qFatal("Failed to load shader pack (fragment)");
104
105 m_ps->setShaderStages({
106 { QRhiShaderStage::Vertex, vs },
107 { QRhiShaderStage::Fragment, fs }
108 });
109
110 QRhiVertexInputLayout inputLayout;
111 inputLayout.setBindings({
112 { 5 * sizeof(float) }
113 });
114 inputLayout.setAttributes({
115 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
116 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
117 });
118
119 m_ps->setVertexInputLayout(inputLayout);
120 m_ps->setShaderResourceBindings(m_srb.get());
121 m_ps->setRenderPassDescriptor(m_rp.get());
122
123 m_ps->build();
124 }
125
126 // called once per frame
127 void HelloWindow::customRender()
128 {
129 QRhiResourceUpdateBatch *u = m_rhi->nextResourceUpdateBatch();
130 if (!m_vbufReady) {
131 m_vbufReady = true;
132 u->uploadStaticBuffer(m_vbuf.get(), vertexData);
133 }
134 m_rotation += 1.0f;
135 QMatrix4x4 mvp = m_proj;
136 mvp.rotate(m_rotation, 0, 1, 0);
137 u->updateDynamicBuffer(m_ubuf.get(), 0, 64, mvp.constData());
138 m_opacity += m_opacityDir * 0.005f;
139 if (m_opacity < 0.0f || m_opacity > 1.0f) {
140 m_opacityDir *= -1;
141 m_opacity = qBound(0.0f, m_opacity, 1.0f);
142 }
143 u->updateDynamicBuffer(m_ubuf.get(), 64, 4, &m_opacity);
144
145 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
146 const QSize outputSizeInPixels = m_sc->currentPixelSize();
147
148 cb->beginPass(m_sc->currentFrameRenderTarget(), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f), { 1.0f, 0 }, u);
149
150 cb->setGraphicsPipeline(m_ps.get());
151 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
152 cb->setShaderResources();
153
154 const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0);
155 cb->setVertexInput(0, 1, &vbufBinding);
156 cb->draw(3);
157
158 cb->endPass();
159 }