| 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 "trianglerenderer.h" |
| 52 |
#include <QFile> |
| 53 |
#include <QtGui/private/qshader_p.h> |
| 54 |
|
| 55 |
//#define VBUF_IS_DYNAMIC |
| 56 |
|
| 57 |
static float vertexData[] = { // Y up (note m_proj), CCW |
| 58 |
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 59 |
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
| 60 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f |
| 61 |
}; |
| 62 |
|
| 63 |
static QShader getShader(const QString &name) |
| 64 |
{ |
| 65 |
QFile f(name); |
| 66 |
if (f.open(QIODevice::ReadOnly)) |
| 67 |
return QShader::fromSerialized(f.readAll()); |
| 68 |
|
| 69 |
return QShader(); |
| 70 |
} |
| 71 |
|
| 72 |
void TriangleRenderer::initResources(QRhiRenderPassDescriptor *rp) |
| 73 |
{ |
| 74 |
#ifdef VBUF_IS_DYNAMIC |
| 75 |
m_vbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, sizeof(vertexData)); |
| 76 |
#else |
| 77 |
m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); |
| 78 |
#endif |
| 79 |
m_vbuf->setName(QByteArrayLiteral("Triangle vbuf")); |
| 80 |
m_vbuf->build(); |
| 81 |
m_vbufReady = false; |
| 82 |
|
| 83 |
m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 84 |
m_ubuf->setName(QByteArrayLiteral("Triangle ubuf")); |
| 85 |
m_ubuf->build(); |
| 86 |
|
| 87 |
m_srb = m_r->newShaderResourceBindings(); |
| 88 |
m_srb->setBindings({ |
| 89 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf) |
| 90 |
}); |
| 91 |
m_srb->build(); |
| 92 |
|
| 93 |
m_ps = m_r->newGraphicsPipeline(); |
| 94 |
|
| 95 |
QRhiGraphicsPipeline::TargetBlend premulAlphaBlend; // convenient defaults... |
| 96 |
premulAlphaBlend.enable = true; |
| 97 |
QVarLengthArray<QRhiGraphicsPipeline::TargetBlend, 4> rtblends; |
| 98 |
for (int i = 0; i < m_colorAttCount; ++i) |
| 99 |
rtblends << premulAlphaBlend; |
| 100 |
|
| 101 |
m_ps->setTargetBlends(rtblends.cbegin(), rtblends.cend()); |
| 102 |
m_ps->setSampleCount(m_sampleCount); |
| 103 |
|
| 104 |
if (m_depthWrite) { // TriangleOnCube may want to exercise this |
| 105 |
m_ps->setDepthTest(true); |
| 106 |
m_ps->setDepthOp(QRhiGraphicsPipeline::Always); |
| 107 |
m_ps->setDepthWrite(true); |
| 108 |
} |
| 109 |
|
| 110 |
QShader vs = getShader(QLatin1String(":/color.vert.qsb")); |
| 111 |
Q_ASSERT(vs.isValid()); |
| 112 |
QShader fs = getShader(QLatin1String(":/color.frag.qsb")); |
| 113 |
Q_ASSERT(fs.isValid()); |
| 114 |
m_ps->setShaderStages({ |
| 115 |
{ QRhiShaderStage::Vertex, vs }, |
| 116 |
{ QRhiShaderStage::Fragment, fs } |
| 117 |
}); |
| 118 |
|
| 119 |
QRhiVertexInputLayout inputLayout; |
| 120 |
inputLayout.setBindings({ |
| 121 |
{ 7 * sizeof(float) } |
| 122 |
}); |
| 123 |
inputLayout.setAttributes({ |
| 124 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 125 |
{ 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) } |
| 126 |
}); |
| 127 |
|
| 128 |
m_ps->setVertexInputLayout(inputLayout); |
| 129 |
m_ps->setShaderResourceBindings(m_srb); |
| 130 |
m_ps->setRenderPassDescriptor(rp); |
| 131 |
|
| 132 |
m_ps->build(); |
| 133 |
} |
| 134 |
|
| 135 |
void TriangleRenderer::resize(const QSize &pixelSize) |
| 136 |
{ |
| 137 |
m_proj = m_r->clipSpaceCorrMatrix(); |
| 138 |
m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f); |
| 139 |
m_proj.translate(0, 0, -4); |
| 140 |
} |
| 141 |
|
| 142 |
void TriangleRenderer::releaseResources() |
| 143 |
{ |
| 144 |
delete m_ps; |
| 145 |
m_ps = nullptr; |
| 146 |
|
| 147 |
delete m_srb; |
| 148 |
m_srb = nullptr; |
| 149 |
|
| 150 |
delete m_ubuf; |
| 151 |
m_ubuf = nullptr; |
| 152 |
|
| 153 |
delete m_vbuf; |
| 154 |
m_vbuf = nullptr; |
| 155 |
} |
| 156 |
|
| 157 |
void TriangleRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) |
| 158 |
{ |
| 159 |
#if 0 |
| 160 |
static int messWithBufferTrigger = 0; |
| 161 |
// recreate the underlying VkBuffer every second frame |
| 162 |
// to exercise setShaderResources' built-in smartness |
| 163 |
if (!(messWithBufferTrigger & 1)) { |
| 164 |
m_ubuf->release(); |
| 165 |
m_ubuf->build(); |
| 166 |
} |
| 167 |
++messWithBufferTrigger; |
| 168 |
#endif |
| 169 |
|
| 170 |
if (!m_vbufReady) { |
| 171 |
m_vbufReady = true; |
| 172 |
#ifdef VBUF_IS_DYNAMIC |
| 173 |
resourceUpdates->updateDynamicBuffer(m_vbuf, 0, m_vbuf->size(), vertexData); |
| 174 |
#else |
| 175 |
resourceUpdates->uploadStaticBuffer(m_vbuf, vertexData); |
| 176 |
#endif |
| 177 |
} |
| 178 |
|
| 179 |
m_rotation += 1.0f; |
| 180 |
QMatrix4x4 mvp = m_proj; |
| 181 |
mvp.translate(m_translation); |
| 182 |
mvp.scale(m_scale); |
| 183 |
mvp.rotate(m_rotation, 0, 1, 0); |
| 184 |
resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData()); |
| 185 |
|
| 186 |
m_opacity += m_opacityDir * 0.005f; |
| 187 |
if (m_opacity < 0.0f || m_opacity > 1.0f) { |
| 188 |
m_opacityDir *= -1; |
| 189 |
m_opacity = qBound(0.0f, m_opacity, 1.0f); |
| 190 |
} |
| 191 |
resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &m_opacity); |
| 192 |
} |
| 193 |
|
| 194 |
void TriangleRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels) |
| 195 |
{ |
| 196 |
cb->setGraphicsPipeline(m_ps); |
| 197 |
cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); |
| 198 |
cb->setShaderResources(); |
| 199 |
const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf, 0); |
| 200 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 201 |
cb->draw(3); |
| 202 |
} |