QtBase  v6.3.1
quadrenderer.cpp
Go to the documentation of this file.
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 
74 {
75  m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
76  m_vbuf->create();
77  m_vbufReady = false;
78 
79  m_ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData));
80  m_ibuf->create();
81 
83  m_ubuf->create();
84 
85  m_srb = m_r->newShaderResourceBindings();
86  m_srb->setBindings({
88  });
89  m_srb->create();
90 }
91 
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 
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 
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 }
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:61
void rotate(float angle, const QVector3D &vector)
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
const float * constData() const
Definition: qmatrix4x4.h:183
void translate(const QVector3D &vector)
Definition: qmatrix4x4.cpp:965
@ Immutable
Definition: qrhi_p.h:717
@ Dynamic
Definition: qrhi_p.h:719
@ IndexBuffer
Definition: qrhi_p.h:724
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
virtual bool create()=0
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
QMatrix4x4 clipSpaceCorrMatrix() const
Definition: qrhi.cpp:6299
QRhiShaderResourceBindings * newShaderResourceBindings()
Definition: qrhi.cpp:6543
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, int size)
Definition: qrhi.cpp:6562
void uploadStaticBuffer(QRhiBuffer *buf, int offset, int size, const void *data)
Definition: qrhi.cpp:5346
void updateDynamicBuffer(QRhiBuffer *buf, int offset, int size, const void *data)
Definition: qrhi.cpp:5326
static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf)
Definition: qrhi.cpp:3268
void setBindings(std::initializer_list< QRhiShaderResourceBinding > list)
Definition: qrhi_p.h:1052
virtual bool create()=0
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr int width() const noexcept
Definition: qsize.h:157
void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
void releaseResources()
void initResources(QRhiRenderPassDescriptor *rp)
void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels)
void resize(const QSize &pixelSize)
void setPipeline(QRhiGraphicsPipeline *ps)
QRhiGraphicsPipeline * ps
unsigned short quint16
Definition: qglobal.h:286
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)