From 53599592e09edd215bfa1eaa7e6f3a9f3fc50ae6 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Fri, 22 Mar 2019 09:55:03 +0100 Subject: Introduce the Qt graphics abstraction as private QtGui helpers Comes with backends for Vulkan, Metal, Direct3D 11.1, and OpenGL (ES). All APIs are private for now. Shader conditioning (i.e. generating a QRhiShader in memory or on disk from some shader source code) is done via the tools and APIs provided by qt-labs/qtshadertools. The OpenGL support follows the cross-platform tradition of requiring ES 2.0 only, while optionally using some (ES) 3.x features. It can operate in core profile contexts as well. Task-number: QTBUG-70287 Change-Id: I246f2e36d562e404012c05db2aa72487108aa7cc Reviewed-by: Lars Knoll --- tests/manual/rhi/triquadcube/trianglerenderer.cpp | 202 ++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 tests/manual/rhi/triquadcube/trianglerenderer.cpp (limited to 'tests/manual/rhi/triquadcube/trianglerenderer.cpp') diff --git a/tests/manual/rhi/triquadcube/trianglerenderer.cpp b/tests/manual/rhi/triquadcube/trianglerenderer.cpp new file mode 100644 index 0000000000..aac76d4110 --- /dev/null +++ b/tests/manual/rhi/triquadcube/trianglerenderer.cpp @@ -0,0 +1,202 @@ +/**************************************************************************** +** +** Copyright (C) 2018 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 "trianglerenderer.h" +#include +#include + +//#define VBUF_IS_DYNAMIC + +static float vertexData[] = { // Y up (note m_proj), CCW + 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f +}; + +static QShader getShader(const QString &name) +{ + QFile f(name); + if (f.open(QIODevice::ReadOnly)) + return QShader::fromSerialized(f.readAll()); + + return QShader(); +} + +void TriangleRenderer::initResources(QRhiRenderPassDescriptor *rp) +{ +#ifdef VBUF_IS_DYNAMIC + m_vbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, sizeof(vertexData)); +#else + m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); +#endif + m_vbuf->setName(QByteArrayLiteral("Triangle vbuf")); + m_vbuf->build(); + m_vbufReady = false; + + m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); + m_ubuf->setName(QByteArrayLiteral("Triangle ubuf")); + m_ubuf->build(); + + m_srb = m_r->newShaderResourceBindings(); + m_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf) + }); + m_srb->build(); + + m_ps = m_r->newGraphicsPipeline(); + + QRhiGraphicsPipeline::TargetBlend premulAlphaBlend; // convenient defaults... + premulAlphaBlend.enable = true; + QVector rtblends; + for (int i = 0; i < m_colorAttCount; ++i) + rtblends << premulAlphaBlend; + + m_ps->setTargetBlends(rtblends); + m_ps->setSampleCount(m_sampleCount); + + if (m_depthWrite) { // TriangleOnCube may want to exercise this + m_ps->setDepthTest(true); + m_ps->setDepthOp(QRhiGraphicsPipeline::Always); + m_ps->setDepthWrite(true); + } + + QShader vs = getShader(QLatin1String(":/color.vert.qsb")); + Q_ASSERT(vs.isValid()); + QShader fs = getShader(QLatin1String(":/color.frag.qsb")); + Q_ASSERT(fs.isValid()); + m_ps->setShaderStages({ + { QRhiGraphicsShaderStage::Vertex, vs }, + { QRhiGraphicsShaderStage::Fragment, fs } + }); + + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 7 * 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); + m_ps->setRenderPassDescriptor(rp); + + m_ps->build(); +} + +void TriangleRenderer::resize(const QSize &pixelSize) +{ + m_proj = m_r->clipSpaceCorrMatrix(); + m_proj.perspective(45.0f, pixelSize.width() / (float) pixelSize.height(), 0.01f, 100.0f); + m_proj.translate(0, 0, -4); +} + +void TriangleRenderer::releaseResources() +{ + delete m_ps; + m_ps = nullptr; + + delete m_srb; + m_srb = nullptr; + + delete m_ubuf; + m_ubuf = nullptr; + + delete m_vbuf; + m_vbuf = nullptr; +} + +void TriangleRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) +{ +#if 0 + static int messWithBufferTrigger = 0; + // recreate the underlying VkBuffer every second frame + // to exercise setShaderResources' built-in smartness + if (!(messWithBufferTrigger & 1)) { + m_ubuf->release(); + m_ubuf->build(); + } + ++messWithBufferTrigger; +#endif + + if (!m_vbufReady) { + m_vbufReady = true; +#ifdef VBUF_IS_DYNAMIC + resourceUpdates->updateDynamicBuffer(m_vbuf, 0, m_vbuf->size(), vertexData); +#else + resourceUpdates->uploadStaticBuffer(m_vbuf, vertexData); +#endif + } + + m_rotation += 1.0f; + QMatrix4x4 mvp = m_proj; + mvp.translate(m_translation); + mvp.scale(m_scale); + mvp.rotate(m_rotation, 0, 1, 0); + resourceUpdates->updateDynamicBuffer(m_ubuf, 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); + } + resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &m_opacity); +} + +void TriangleRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels) +{ + cb->setGraphicsPipeline(m_ps); + cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding); + cb->draw(3); +} -- cgit v1.2.3 From 6f4aa5413183f3f18dd1b15dbc90bcee9ef85bdd Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 12 Jun 2019 14:22:33 +0200 Subject: rhi: Add compute api and implement for Vulkan and Metal D3D11 and GL (4.3+, ES 3.1+) will come separately at a later time. Change-Id: If30f2f3d062fa27e57e9912674669225b82a7b93 Reviewed-by: Lars Knoll --- tests/manual/rhi/triquadcube/trianglerenderer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/manual/rhi/triquadcube/trianglerenderer.cpp') diff --git a/tests/manual/rhi/triquadcube/trianglerenderer.cpp b/tests/manual/rhi/triquadcube/trianglerenderer.cpp index aac76d4110..0980acca49 100644 --- a/tests/manual/rhi/triquadcube/trianglerenderer.cpp +++ b/tests/manual/rhi/triquadcube/trianglerenderer.cpp @@ -112,8 +112,8 @@ void TriangleRenderer::initResources(QRhiRenderPassDescriptor *rp) QShader fs = getShader(QLatin1String(":/color.frag.qsb")); Q_ASSERT(fs.isValid()); m_ps->setShaderStages({ - { QRhiGraphicsShaderStage::Vertex, vs }, - { QRhiGraphicsShaderStage::Fragment, fs } + { QRhiShaderStage::Vertex, vs }, + { QRhiShaderStage::Fragment, fs } }); QRhiVertexInputLayout inputLayout; -- cgit v1.2.3