2020-02-02 16:54:59 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "hellowindow.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QtGui/private/qshader_p.h>
|
|
|
|
|
|
|
|
static float vertexData[] = {
|
|
|
|
// Y up (note clipSpaceCorrMatrix in m_proj), CCW
|
|
|
|
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
|
|
|
|
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
|
|
|
|
};
|
|
|
|
|
|
|
|
HelloWindow::HelloWindow(QRhi::Implementation graphicsApi)
|
|
|
|
: Window(graphicsApi)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QShader HelloWindow::getShader(const QString &name)
|
|
|
|
{
|
|
|
|
QFile f(name);
|
|
|
|
if (f.open(QIODevice::ReadOnly))
|
|
|
|
return QShader::fromSerialized(f.readAll());
|
|
|
|
|
|
|
|
return QShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWindow::customInit()
|
|
|
|
{
|
|
|
|
m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)));
|
2020-05-27 15:44:49 +00:00
|
|
|
m_vbuf->create();
|
2020-02-02 16:54:59 +00:00
|
|
|
m_vbufReady = false;
|
|
|
|
|
|
|
|
m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68));
|
2020-05-27 15:44:49 +00:00
|
|
|
m_ubuf->create();
|
2020-02-02 16:54:59 +00:00
|
|
|
|
|
|
|
m_srb.reset(m_rhi->newShaderResourceBindings());
|
|
|
|
m_srb->setBindings({
|
|
|
|
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
|
|
|
|
m_ubuf.get())
|
|
|
|
});
|
2020-05-27 15:44:49 +00:00
|
|
|
m_srb->create();
|
2020-02-02 16:54:59 +00:00
|
|
|
|
|
|
|
m_ps.reset(m_rhi->newGraphicsPipeline());
|
|
|
|
|
|
|
|
QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
|
|
|
|
premulAlphaBlend.enable = true;
|
|
|
|
m_ps->setTargetBlends({ premulAlphaBlend });
|
|
|
|
|
|
|
|
const QShader vs = getShader(QLatin1String(":/color.vert.qsb"));
|
|
|
|
if (!vs.isValid())
|
|
|
|
qFatal("Failed to load shader pack (vertex)");
|
|
|
|
const QShader fs = getShader(QLatin1String(":/color.frag.qsb"));
|
|
|
|
if (!fs.isValid())
|
|
|
|
qFatal("Failed to load shader pack (fragment)");
|
|
|
|
|
|
|
|
m_ps->setShaderStages({
|
|
|
|
{ QRhiShaderStage::Vertex, vs },
|
|
|
|
{ QRhiShaderStage::Fragment, fs }
|
|
|
|
});
|
|
|
|
|
|
|
|
QRhiVertexInputLayout inputLayout;
|
|
|
|
inputLayout.setBindings({
|
|
|
|
{ 5 * sizeof(float) }
|
|
|
|
});
|
|
|
|
inputLayout.setAttributes({
|
|
|
|
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 },
|
|
|
|
{ 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
|
|
|
|
});
|
|
|
|
|
|
|
|
m_ps->setVertexInputLayout(inputLayout);
|
|
|
|
m_ps->setShaderResourceBindings(m_srb.get());
|
|
|
|
m_ps->setRenderPassDescriptor(m_rp.get());
|
|
|
|
|
2020-05-27 15:44:49 +00:00
|
|
|
m_ps->create();
|
2020-02-02 16:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called once per frame
|
|
|
|
void HelloWindow::customRender()
|
|
|
|
{
|
|
|
|
QRhiResourceUpdateBatch *u = m_rhi->nextResourceUpdateBatch();
|
|
|
|
if (!m_vbufReady) {
|
|
|
|
m_vbufReady = true;
|
|
|
|
u->uploadStaticBuffer(m_vbuf.get(), vertexData);
|
|
|
|
}
|
|
|
|
m_rotation += 1.0f;
|
|
|
|
QMatrix4x4 mvp = m_proj;
|
|
|
|
mvp.rotate(m_rotation, 0, 1, 0);
|
|
|
|
u->updateDynamicBuffer(m_ubuf.get(), 0, 64, mvp.constData());
|
|
|
|
m_opacity += m_opacityDir * 0.005f;
|
|
|
|
if (m_opacity < 0.0f || m_opacity > 1.0f) {
|
|
|
|
m_opacityDir *= -1;
|
|
|
|
m_opacity = qBound(0.0f, m_opacity, 1.0f);
|
|
|
|
}
|
|
|
|
u->updateDynamicBuffer(m_ubuf.get(), 64, 4, &m_opacity);
|
|
|
|
|
|
|
|
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
|
|
|
|
const QSize outputSizeInPixels = m_sc->currentPixelSize();
|
|
|
|
|
|
|
|
cb->beginPass(m_sc->currentFrameRenderTarget(), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f), { 1.0f, 0 }, u);
|
|
|
|
|
|
|
|
cb->setGraphicsPipeline(m_ps.get());
|
|
|
|
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
|
|
|
|
cb->setShaderResources();
|
|
|
|
|
|
|
|
const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0);
|
|
|
|
cb->setVertexInput(0, 1, &vbufBinding);
|
|
|
|
cb->draw(3);
|
|
|
|
|
|
|
|
cb->endPass();
|
|
|
|
}
|