Debian Codegraph @

tests/manual/rhi/triquadcube/quadrenderer.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 "quadrenderer.h"
52 #include <QFile>
53 #include <QtGui/private/qshader_p.h>
54
55 // Renders a quad using indexed drawing. No QRhiGraphicsPipeline is created, it
56 // expects to reuse the one created by TriangleRenderer. A separate
57 // QRhiShaderResourceBindings is still needed, this will override the one the
58 // QRhiGraphicsPipeline references.
59
60 static float vertexData[] =
61 { // Y up (note m_proj), CCW
62 -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
63 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
64 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
65 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
66 };
67
68 static quint16 indexData[] =
69 {
70 0, 1, 2, 0, 2, 3
71 };
72
73 void QuadRenderer::initResources(QRhiRenderPassDescriptor *)
74 {
75 m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
76 m_vbuf->build();
77 m_vbufReady = false;
78
79 m_ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
80 m_ibuf->build();
81
82 m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
83 m_ubuf->build();
84
85 m_srb = m_r->newShaderResourceBindings();
86 m_srb->setBindings({
87 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf)
88 });
89 m_srb->build();
90 }
91
92 void QuadRenderer::setPipeline(QRhiGraphicsPipeline *ps)
93 {
94 m_ps = ps;
95 }
96
97 void QuadRenderer::resize(const QSize &pixelSize)
98 {
99 m_proj = m_r->clipSpaceCorrMatrix();
100 m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f);
101 m_proj.translate(0, 0, -4);
102 }
103
104 void QuadRenderer::releaseResources()
105 {
106 delete m_srb;
107 m_srb = nullptr;
108
109 delete m_ubuf;
110 m_ubuf = nullptr;
111
112 delete m_ibuf;
113 m_ibuf = nullptr;
114
115 delete m_vbuf;
116 m_vbuf = nullptr;
117 }
118
119 void QuadRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
120 {
121 if (!m_vbufReady) {
122 m_vbufReady = true;
123 resourceUpdates->uploadStaticBuffer(m_vbuf, vertexData);
124 resourceUpdates->uploadStaticBuffer(m_ibuf, indexData);
125 }
126
127 m_rotation += 1.0f;
128 QMatrix4x4 mvp = m_proj;
129 mvp.translate(m_translation);
130 mvp.rotate(m_rotation, 0, 1, 0);
131 resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData());
132
133 if (!m_opacityReady) {
134 m_opacityReady = true;
135 const float opacity = 1.0f;
136 resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &opacity);
137 }
138 }
139
140 void QuadRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &/*outputSizeInPixels*/)
141 {
142 cb->setGraphicsPipeline(m_ps);
143 //cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
144 cb->setShaderResources(m_srb);
145 const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf, 0);
146 cb->setVertexInput(0, 1, &vbufBinding, m_ibuf, 0, QRhiCommandBuffer::IndexUInt16);
147 cb->drawIndexed(6);
148 }