diff options
Diffstat (limited to 'tests/manual/rhi/triquadcube')
-rw-r--r-- | tests/manual/rhi/triquadcube/quadrenderer.cpp | 148 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/quadrenderer.h | 87 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/texturedcuberenderer.cpp | 222 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/texturedcuberenderer.h | 86 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/triangleoncuberenderer.cpp | 292 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/triangleoncuberenderer.h | 95 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/trianglerenderer.cpp | 202 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/trianglerenderer.h | 93 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/triquadcube.cpp | 272 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/triquadcube.pro | 18 | ||||
-rw-r--r-- | tests/manual/rhi/triquadcube/triquadcube.qrc | 9 |
11 files changed, 1524 insertions, 0 deletions
diff --git a/tests/manual/rhi/triquadcube/quadrenderer.cpp b/tests/manual/rhi/triquadcube/quadrenderer.cpp new file mode 100644 index 0000000000..6137fb1b9a --- /dev/null +++ b/tests/manual/rhi/triquadcube/quadrenderer.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** 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 "quadrenderer.h" +#include <QFile> +#include <QtGui/private/qshader_p.h> + +// Renders a quad using indexed drawing. No QRhiGraphicsPipeline is created, it +// expects to reuse the one created by TriangleRenderer. A separate +// QRhiShaderResourceBindings is still needed, this will override the one the +// QRhiGraphicsPipeline references. + +static float vertexData[] = +{ // Y up (note m_proj), CCW + -0.5f, 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, + 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f +}; + +static quint16 indexData[] = +{ + 0, 1, 2, 0, 2, 3 +}; + +void QuadRenderer::initResources(QRhiRenderPassDescriptor *) +{ + m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); + m_vbuf->build(); + m_vbufReady = false; + + m_ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData)); + m_ibuf->build(); + + m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); + m_ubuf->build(); + + m_srb = m_r->newShaderResourceBindings(); + m_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf) + }); + m_srb->build(); +} + +void QuadRenderer::setPipeline(QRhiGraphicsPipeline *ps) +{ + m_ps = ps; +} + +void QuadRenderer::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 QuadRenderer::releaseResources() +{ + delete m_srb; + m_srb = nullptr; + + delete m_ubuf; + m_ubuf = nullptr; + + delete m_ibuf; + m_ibuf = nullptr; + + delete m_vbuf; + m_vbuf = nullptr; +} + +void QuadRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) +{ + if (!m_vbufReady) { + m_vbufReady = true; + resourceUpdates->uploadStaticBuffer(m_vbuf, vertexData); + resourceUpdates->uploadStaticBuffer(m_ibuf, indexData); + } + + m_rotation += 1.0f; + QMatrix4x4 mvp = m_proj; + mvp.translate(m_translation); + mvp.rotate(m_rotation, 0, 1, 0); + resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData()); + + if (!m_opacityReady) { + m_opacityReady = true; + const float opacity = 1.0f; + resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &opacity); + } +} + +void QuadRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &/*outputSizeInPixels*/) +{ + cb->setGraphicsPipeline(m_ps); + //cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); + cb->setShaderResources(m_srb); + const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding, m_ibuf, 0, QRhiCommandBuffer::IndexUInt16); + cb->drawIndexed(6); +} diff --git a/tests/manual/rhi/triquadcube/quadrenderer.h b/tests/manual/rhi/triquadcube/quadrenderer.h new file mode 100644 index 0000000000..79d5b8209d --- /dev/null +++ b/tests/manual/rhi/triquadcube/quadrenderer.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef QUADRENDERER_H +#define QUADRENDERER_H + +#include <QtGui/private/qrhi_p.h> + +class QuadRenderer +{ +public: + void setRhi(QRhi *r) { m_r = r; } + void setSampleCount(int samples) { m_sampleCount = samples; } + int sampleCount() const { return m_sampleCount; } + void setTranslation(const QVector3D &v) { m_translation = v; } + void initResources(QRhiRenderPassDescriptor *rp); + void releaseResources(); + void setPipeline(QRhiGraphicsPipeline *ps); + void resize(const QSize &pixelSize); + void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates); + void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels); + +private: + QRhi *m_r; + + QRhiBuffer *m_vbuf = nullptr; + bool m_vbufReady = false; + QRhiBuffer *m_ibuf = nullptr; + bool m_opacityReady = false; + QRhiBuffer *m_ubuf = nullptr; + QRhiGraphicsPipeline *m_ps = nullptr; + QRhiShaderResourceBindings *m_srb = nullptr; + + QVector3D m_translation; + QMatrix4x4 m_proj; + float m_rotation = 0; + int m_sampleCount = 1; // no MSAA by default +}; + +#endif diff --git a/tests/manual/rhi/triquadcube/texturedcuberenderer.cpp b/tests/manual/rhi/triquadcube/texturedcuberenderer.cpp new file mode 100644 index 0000000000..3f15881e2d --- /dev/null +++ b/tests/manual/rhi/triquadcube/texturedcuberenderer.cpp @@ -0,0 +1,222 @@ +/**************************************************************************** +** +** 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 "texturedcuberenderer.h" +#include <QFile> +#include <QtGui/private/qshader_p.h> + +#include "../shared/cube.h" + +const bool MIPMAP = true; +const bool AUTOGENMIPMAP = true; + +static QShader getShader(const QString &name) +{ + QFile f(name); + if (f.open(QIODevice::ReadOnly)) + return QShader::fromSerialized(f.readAll()); + + return QShader(); +} + +void TexturedCubeRenderer::initResources(QRhiRenderPassDescriptor *rp) +{ + m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); + m_vbuf->setName(QByteArrayLiteral("Cube vbuf (textured)")); + m_vbuf->build(); + m_vbufReady = false; + + m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4); + m_ubuf->setName(QByteArrayLiteral("Cube ubuf (textured)")); + m_ubuf->build(); + + m_image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888); + QRhiTexture::Flags texFlags = 0; + if (MIPMAP) + texFlags |= QRhiTexture::MipMapped; + if (AUTOGENMIPMAP) + texFlags |= QRhiTexture::UsedWithGenerateMips; + m_tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(m_image.width(), m_image.height()), 1, texFlags); + m_tex->setName(QByteArrayLiteral("Qt texture")); + m_tex->build(); + + m_sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, MIPMAP ? QRhiSampler::Linear : QRhiSampler::None, + QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); + m_sampler->build(); + + m_srb = m_r->newShaderResourceBindings(); + m_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, m_tex, m_sampler) + }); + m_srb->build(); + + m_ps = m_r->newGraphicsPipeline(); + + // No blending but the texture has alpha which we do not want to write out. + // Be nice. (would not matter for an onscreen window but makes a difference + // when reading back and saving into image files f.ex.) + QRhiGraphicsPipeline::TargetBlend blend; + blend.colorWrite = QRhiGraphicsPipeline::R | QRhiGraphicsPipeline::G | QRhiGraphicsPipeline::B; + m_ps->setTargetBlends({ blend }); + + m_ps->setDepthTest(true); + m_ps->setDepthWrite(true); + m_ps->setDepthOp(QRhiGraphicsPipeline::Less); + + m_ps->setCullMode(QRhiGraphicsPipeline::Back); + m_ps->setFrontFace(QRhiGraphicsPipeline::CCW); + + m_ps->setSampleCount(m_sampleCount); + + QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); + Q_ASSERT(vs.isValid()); + QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); + Q_ASSERT(fs.isValid()); + m_ps->setShaderStages({ + { QRhiShaderStage::Vertex, vs }, + { QRhiShaderStage::Fragment, fs } + }); + + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 3 * sizeof(float) }, + { 2 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float3, 0 }, + { 1, 1, QRhiVertexInputAttribute::Float2, 0 } + }); + + m_ps->setVertexInputLayout(inputLayout); + m_ps->setShaderResourceBindings(m_srb); + m_ps->setRenderPassDescriptor(rp); + + m_ps->build(); +} + +void TexturedCubeRenderer::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 TexturedCubeRenderer::releaseResources() +{ + delete m_ps; + m_ps = nullptr; + + delete m_srb; + m_srb = nullptr; + + delete m_sampler; + m_sampler = nullptr; + + delete m_tex; + m_tex = nullptr; + + delete m_ubuf; + m_ubuf = nullptr; + + delete m_vbuf; + m_vbuf = nullptr; +} + +void TexturedCubeRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) +{ + if (!m_vbufReady) { + m_vbufReady = true; + resourceUpdates->uploadStaticBuffer(m_vbuf, cube); + qint32 flip = 0; + resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &flip); + } + + if (!m_image.isNull()) { + if (MIPMAP) { + QRhiTextureUploadDescription desc; + if (!AUTOGENMIPMAP) { + // the ghetto mipmap generator... + for (int i = 0, ie = m_r->mipLevelsForSize(m_image.size()); i != ie; ++i) { + QImage image = m_image.scaled(m_r->sizeForMipLevel(i, m_image.size())); + desc.append({ 0, i, image }); + } + } else { + desc.append({ 0, 0, m_image }); + } + resourceUpdates->uploadTexture(m_tex, desc); + if (AUTOGENMIPMAP) + resourceUpdates->generateMips(m_tex); + } else { + resourceUpdates->uploadTexture(m_tex, m_image); + } + m_image = QImage(); + } + + m_rotation += 1.0f; + QMatrix4x4 mvp = m_proj; + mvp.translate(m_translation); + mvp.scale(0.5f); + mvp.rotate(m_rotation, 0, 1, 0); + resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData()); +} + +void TexturedCubeRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels) +{ + cb->setGraphicsPipeline(m_ps); + cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBindings[] = { + { m_vbuf, 0 }, + { m_vbuf, 36 * 3 * sizeof(float) } + }; + cb->setVertexInput(0, 2, vbufBindings); + cb->draw(36); +} diff --git a/tests/manual/rhi/triquadcube/texturedcuberenderer.h b/tests/manual/rhi/triquadcube/texturedcuberenderer.h new file mode 100644 index 0000000000..879f875209 --- /dev/null +++ b/tests/manual/rhi/triquadcube/texturedcuberenderer.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef TEXTUREDCUBERENDERER_H +#define TEXTUREDCUBERENDERER_H + +#include <QtGui/private/qrhi_p.h> + +class TexturedCubeRenderer +{ +public: + void setRhi(QRhi *r) { m_r = r; } + void setSampleCount(int samples) { m_sampleCount = samples; } + void setTranslation(const QVector3D &v) { m_translation = v; } + void initResources(QRhiRenderPassDescriptor *rp); + void releaseResources(); + void resize(const QSize &pixelSize); + void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates); + void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels); + +private: + QRhi *m_r; + + QRhiBuffer *m_vbuf = nullptr; + bool m_vbufReady = false; + QRhiBuffer *m_ubuf = nullptr; + QImage m_image; + QRhiTexture *m_tex = nullptr; + QRhiSampler *m_sampler = nullptr; + QRhiShaderResourceBindings *m_srb = nullptr; + QRhiGraphicsPipeline *m_ps = nullptr; + + QVector3D m_translation; + QMatrix4x4 m_proj; + float m_rotation = 0; + int m_sampleCount = 1; // no MSAA by default +}; + +#endif diff --git a/tests/manual/rhi/triquadcube/triangleoncuberenderer.cpp b/tests/manual/rhi/triquadcube/triangleoncuberenderer.cpp new file mode 100644 index 0000000000..250ae3a2ee --- /dev/null +++ b/tests/manual/rhi/triquadcube/triangleoncuberenderer.cpp @@ -0,0 +1,292 @@ +/**************************************************************************** +** +** 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 "triangleoncuberenderer.h" +#include <QFile> +#include <QtGui/private/qshader_p.h> + +// toggle to test the preserved content (no clear) path +const bool IMAGE_UNDER_OFFSCREEN_RENDERING = false; +const bool UPLOAD_UNDERLAY_ON_EVERY_FRAME = false; + +const bool DS_ATT = false; // have a depth-stencil attachment for the offscreen pass + +const bool DEPTH_TEXTURE = false; // offscreen pass uses a depth texture (verify with renderdoc etc., ignore valid.layer about ps slot 0) +const bool MRT = false; // two textures, the second is just cleared as the shader does not write anything (valid.layer may warn but for testing that's ok) + +#include "../shared/cube.h" + +static QShader getShader(const QString &name) +{ + QFile f(name); + if (f.open(QIODevice::ReadOnly)) + return QShader::fromSerialized(f.readAll()); + + return QShader(); +} + +static const QSize OFFSCREEN_SIZE(512, 512); + +void TriangleOnCubeRenderer::initResources(QRhiRenderPassDescriptor *rp) +{ + m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); + m_vbuf->setName(QByteArrayLiteral("Cube vbuf (textured with offscreen)")); + m_vbuf->build(); + m_vbufReady = false; + + m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4); + m_ubuf->setName(QByteArrayLiteral("Cube ubuf (textured with offscreen)")); + m_ubuf->build(); + + if (IMAGE_UNDER_OFFSCREEN_RENDERING) { + m_image = QImage(QLatin1String(":/qt256.png")).scaled(OFFSCREEN_SIZE).convertToFormat(QImage::Format_RGBA8888); + if (m_r->isYUpInFramebuffer()) + m_image = m_image.mirrored(); // just cause we'll flip texcoord Y when y up so accommodate our static background image as well + } + + m_tex = m_r->newTexture(QRhiTexture::RGBA8, OFFSCREEN_SIZE, 1, QRhiTexture::RenderTarget); + m_tex->setName(QByteArrayLiteral("Texture for offscreen content")); + m_tex->build(); + + if (MRT) { + m_tex2 = m_r->newTexture(QRhiTexture::RGBA8, OFFSCREEN_SIZE, 1, QRhiTexture::RenderTarget); + m_tex2->build(); + } + + if (DS_ATT) { + m_offscreenTriangle.setDepthWrite(true); + m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil, m_tex->pixelSize()); + m_ds->build(); + } + + if (DEPTH_TEXTURE) { + m_offscreenTriangle.setDepthWrite(true); + m_depthTex = m_r->newTexture(QRhiTexture::D32F, OFFSCREEN_SIZE, 1, QRhiTexture::RenderTarget); + m_depthTex->build(); + } + + m_sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); + m_sampler->build(); + + m_srb = m_r->newShaderResourceBindings(); + m_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, m_tex, m_sampler) + }); + m_srb->build(); + + m_ps = m_r->newGraphicsPipeline(); + + m_ps->setDepthTest(true); + m_ps->setDepthWrite(true); + m_ps->setDepthOp(QRhiGraphicsPipeline::Less); + + m_ps->setCullMode(QRhiGraphicsPipeline::Back); + m_ps->setFrontFace(QRhiGraphicsPipeline::CCW); + + m_ps->setSampleCount(m_sampleCount); + + QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); + Q_ASSERT(vs.isValid()); + QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); + Q_ASSERT(fs.isValid()); + m_ps->setShaderStages({ + { QRhiShaderStage::Vertex, vs }, + { QRhiShaderStage::Fragment, fs } + }); + + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 3 * sizeof(float) }, + { 2 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float3, 0 }, + { 1, 1, QRhiVertexInputAttribute::Float2, 0 } + }); + + m_ps->setVertexInputLayout(inputLayout); + m_ps->setShaderResourceBindings(m_srb); + m_ps->setRenderPassDescriptor(rp); + + m_ps->build(); + + QRhiTextureRenderTarget::Flags rtFlags = 0; + if (IMAGE_UNDER_OFFSCREEN_RENDERING) + rtFlags |= QRhiTextureRenderTarget::PreserveColorContents; + + if (DEPTH_TEXTURE) { + QRhiTextureRenderTargetDescription desc; + desc.setDepthTexture(m_depthTex); + m_rt = m_r->newTextureRenderTarget(desc, rtFlags); + } else { + QRhiTextureRenderTargetDescription desc; + QRhiColorAttachment color0 { m_tex }; + if (DS_ATT) + desc.setDepthStencilBuffer(m_ds); + if (MRT) { + m_offscreenTriangle.setColorAttCount(2); + QRhiColorAttachment color1 { m_tex2 }; + desc.setColorAttachments({ color0, color1 }); + } else { + desc.setColorAttachments({ color0 }); + } + m_rt = m_r->newTextureRenderTarget(desc, rtFlags); + } + + m_rp = m_rt->newCompatibleRenderPassDescriptor(); + m_rt->setRenderPassDescriptor(m_rp); + + m_rt->build(); + + m_offscreenTriangle.setRhi(m_r); + m_offscreenTriangle.initResources(m_rp); + m_offscreenTriangle.setScale(2); + // m_tex and the offscreen triangle are never multisample +} + +void TriangleOnCubeRenderer::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); + + m_offscreenTriangle.resize(pixelSize); +} + +void TriangleOnCubeRenderer::releaseResources() +{ + m_offscreenTriangle.releaseResources(); + + delete m_ps; + m_ps = nullptr; + + delete m_srb; + m_srb = nullptr; + + delete m_rt; + m_rt = nullptr; + + delete m_rp; + m_rp = nullptr; + + delete m_sampler; + m_sampler = nullptr; + + delete m_depthTex; + m_depthTex = nullptr; + + delete m_tex2; + m_tex2 = nullptr; + + delete m_tex; + m_tex = nullptr; + + delete m_ds; + m_ds = nullptr; + + delete m_ubuf; + m_ubuf = nullptr; + + delete m_vbuf; + m_vbuf = nullptr; +} + +void TriangleOnCubeRenderer::queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates) +{ + if (!m_vbufReady) { + m_vbufReady = true; + resourceUpdates->uploadStaticBuffer(m_vbuf, cube); + qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0; + resourceUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &flip); + } + + m_rotation += 1.0f; + QMatrix4x4 mvp = m_proj; + mvp.translate(m_translation); + mvp.scale(0.5f); + mvp.rotate(m_rotation, 1, 0, 0); + resourceUpdates->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData()); + + // ### +// if (DEPTH_TEXTURE) { +// // m_tex is basically undefined here, be nice and transition the layout properly at least +// resourceUpdates->prepareTextureForUse(m_tex, QRhiResourceUpdateBatch::TextureRead); +// } +} + +void TriangleOnCubeRenderer::queueOffscreenPass(QRhiCommandBuffer *cb) +{ + QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); + m_offscreenTriangle.queueResourceUpdates(u); + + if (IMAGE_UNDER_OFFSCREEN_RENDERING && !m_image.isNull()) { + u->uploadTexture(m_tex, m_image); + if (!UPLOAD_UNDERLAY_ON_EVERY_FRAME) + m_image = QImage(); + } + + cb->beginPass(m_rt, QColor::fromRgbF(0.0f, 0.4f, 0.7f, 1.0f), { 1.0f, 0 }, u); + m_offscreenTriangle.queueDraw(cb, OFFSCREEN_SIZE); + cb->endPass(); +} + +void TriangleOnCubeRenderer::queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels) +{ + cb->setGraphicsPipeline(m_ps); + cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBindings[] = { + { m_vbuf, 0 }, + { m_vbuf, 36 * 3 * sizeof(float) } + }; + cb->setVertexInput(0, 2, vbufBindings); + cb->draw(36); +} diff --git a/tests/manual/rhi/triquadcube/triangleoncuberenderer.h b/tests/manual/rhi/triquadcube/triangleoncuberenderer.h new file mode 100644 index 0000000000..5a56bf6bd1 --- /dev/null +++ b/tests/manual/rhi/triquadcube/triangleoncuberenderer.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef TRIANGLEONCUBERENDERER_H +#define TRIANGLEONCUBERENDERER_H + +#include "trianglerenderer.h" + +class TriangleOnCubeRenderer +{ +public: + void setRhi(QRhi *r) { m_r = r; } + void setSampleCount(int samples) { m_sampleCount = samples; } + void setTranslation(const QVector3D &v) { m_translation = v; } + void initResources(QRhiRenderPassDescriptor *rp); + void releaseResources(); + void resize(const QSize &pixelSize); + void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates); + void queueOffscreenPass(QRhiCommandBuffer *cb); + void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels); + +private: + QRhi *m_r; + + QRhiBuffer *m_vbuf = nullptr; + bool m_vbufReady = false; + QRhiBuffer *m_ubuf = nullptr; + QRhiTexture *m_tex = nullptr; + QRhiRenderBuffer *m_ds = nullptr; + QRhiTexture *m_tex2 = nullptr; + QRhiTexture *m_depthTex = nullptr; + QRhiSampler *m_sampler = nullptr; + QRhiTextureRenderTarget *m_rt = nullptr; + QRhiRenderPassDescriptor *m_rp = nullptr; + QRhiShaderResourceBindings *m_srb = nullptr; + QRhiGraphicsPipeline *m_ps = nullptr; + + QVector3D m_translation; + QMatrix4x4 m_proj; + float m_rotation = 0; + int m_sampleCount = 1; // no MSAA by default + + TriangleRenderer m_offscreenTriangle; + + QImage m_image; +}; + +#endif diff --git a/tests/manual/rhi/triquadcube/trianglerenderer.cpp b/tests/manual/rhi/triquadcube/trianglerenderer.cpp new file mode 100644 index 0000000000..0980acca49 --- /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 <QFile> +#include <QtGui/private/qshader_p.h> + +//#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<QRhiGraphicsPipeline::TargetBlend> 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({ + { QRhiShaderStage::Vertex, vs }, + { QRhiShaderStage::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); +} diff --git a/tests/manual/rhi/triquadcube/trianglerenderer.h b/tests/manual/rhi/triquadcube/trianglerenderer.h new file mode 100644 index 0000000000..53d6926a62 --- /dev/null +++ b/tests/manual/rhi/triquadcube/trianglerenderer.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef TRIANGLERENDERER_H +#define TRIANGLERENDERER_H + +#include <QtGui/private/qrhi_p.h> + +class TriangleRenderer +{ +public: + void setRhi(QRhi *r) { m_r = r; } + void setSampleCount(int samples) { m_sampleCount = samples; } + int sampleCount() const { return m_sampleCount; } + void setTranslation(const QVector3D &v) { m_translation = v; } + void setScale(float f) { m_scale = f; } + void setDepthWrite(bool enable) { m_depthWrite = enable; } + void setColorAttCount(int count) { m_colorAttCount = count; } + QRhiGraphicsPipeline *pipeline() const { return m_ps; } + void initResources(QRhiRenderPassDescriptor *rp); + void releaseResources(); + void resize(const QSize &pixelSize); + void queueResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates); + void queueDraw(QRhiCommandBuffer *cb, const QSize &outputSizeInPixels); + +private: + QRhi *m_r; + + QRhiBuffer *m_vbuf = nullptr; + bool m_vbufReady = false; + QRhiBuffer *m_ubuf = nullptr; + QRhiShaderResourceBindings *m_srb = nullptr; + QRhiGraphicsPipeline *m_ps = nullptr; + + QVector3D m_translation; + float m_scale = 1; + bool m_depthWrite = false; + int m_colorAttCount = 1; + QMatrix4x4 m_proj; + float m_rotation = 0; + float m_opacity = 1; + int m_opacityDir = -1; + int m_sampleCount = 1; // no MSAA by default +}; + +#endif diff --git a/tests/manual/rhi/triquadcube/triquadcube.cpp b/tests/manual/rhi/triquadcube/triquadcube.cpp new file mode 100644 index 0000000000..8c7f5db042 --- /dev/null +++ b/tests/manual/rhi/triquadcube/triquadcube.cpp @@ -0,0 +1,272 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// An example exercising more than a single feature. Enables profiling +// (resource logging to a file) and inserts debug markers and sets some +// object names. Can also be used to test MSAA swapchains, swapchain image +// readback, requesting an sRGB swapchain, and some texture features. + +#define EXAMPLEFW_PREINIT +#include "../shared/examplefw.h" +#include "trianglerenderer.h" +#include "quadrenderer.h" +#include "texturedcuberenderer.h" +#include "triangleoncuberenderer.h" + +#include <QFileInfo> +#include <QFile> +#include <QtGui/private/qrhiprofiler_p.h> + +#define PROFILE_TO_FILE +//#define SKIP_PRESENT +//#define USE_MSAA +//#define USE_SRGB_SWAPCHAIN +//#define READBACK_SWAPCHAIN +//#define NO_VSYNC +//#define USE_MIN_SWAPCHAIN_BUFFERS + +struct { + TriangleRenderer triRenderer; + QuadRenderer quadRenderer; + TexturedCubeRenderer cubeRenderer; + TriangleOnCubeRenderer liveTexCubeRenderer; + bool onScreenOnly = false; + bool triangleOnly = false; + QSize lastOutputSize; + int frameCount = 0; + QFile profOut; +} d; + +void preInit() +{ +#ifdef PROFILE_TO_FILE + rhiFlags |= QRhi::EnableProfiling; + const QString profFn = QFileInfo(QLatin1String("rhiprof.txt")).absoluteFilePath(); + qDebug("Writing profiling output to %s", qPrintable(profFn)); + d.profOut.setFileName(profFn); + d.profOut.open(QIODevice::WriteOnly); +#endif + +#ifdef SKIP_PRESENT + endFrameFlags |= QRhi::SkipPresent; +#endif + +#ifdef USE_MSAA + sampleCount = 4; // enable 4x MSAA (except for the render-to-texture pass) +#endif + +#ifdef READBACK_SWAPCHAIN + scFlags |= QRhiSwapChain::UsedAsTransferSource; +#endif + +#ifdef USE_SRGB_SWAPCHAIN + scFlags |= QRhiSwapChain::sRGB; +#endif + +#ifdef NO_VSYNC + scFlags |= QRhiSwapChain::NoVSync; +#endif + +#ifdef USE_MIN_SWAPCHAIN_BUFFERS + scFlags |= QRhiSwapChain::MinimalBufferCount; +#endif + + // For OpenGL some of these are incorporated into the QSurfaceFormat by + // examplefw.h after returning from here as that is out of the RHI's control. +} + +void Window::customInit() +{ +#ifdef PROFILE_TO_FILE + m_r->profiler()->setDevice(&d.profOut); +#endif + + d.triRenderer.setRhi(m_r); + d.triRenderer.setSampleCount(sampleCount); + d.triRenderer.initResources(m_rp); + + if (!d.triangleOnly) { + d.triRenderer.setTranslation(QVector3D(0, 0.5f, 0)); + + d.quadRenderer.setRhi(m_r); + d.quadRenderer.setSampleCount(sampleCount); + d.quadRenderer.setPipeline(d.triRenderer.pipeline()); + d.quadRenderer.initResources(m_rp); + d.quadRenderer.setTranslation(QVector3D(1.5f, -0.5f, 0)); + + d.cubeRenderer.setRhi(m_r); + d.cubeRenderer.setSampleCount(sampleCount); + d.cubeRenderer.initResources(m_rp); + d.cubeRenderer.setTranslation(QVector3D(0, -0.5f, 0)); + } + + if (!d.onScreenOnly) { + d.liveTexCubeRenderer.setRhi(m_r); + d.liveTexCubeRenderer.setSampleCount(sampleCount); + d.liveTexCubeRenderer.initResources(m_rp); + d.liveTexCubeRenderer.setTranslation(QVector3D(-2.0f, 0, 0)); + } + + // Put the gpu mem allocator statistics to the profiling stream after doing + // all the init. (where applicable) + m_r->profiler()->addVMemAllocatorStats(); + + // Check some features/limits. + qDebug("isFeatureSupported(MultisampleTexture): %d", m_r->isFeatureSupported(QRhi::MultisampleTexture)); + qDebug("isFeatureSupported(MultisampleRenderBuffer): %d", m_r->isFeatureSupported(QRhi::MultisampleRenderBuffer)); + qDebug("isFeatureSupported(DebugMarkers): %d", m_r->isFeatureSupported(QRhi::DebugMarkers)); + qDebug("isFeatureSupported(Timestamps): %d", m_r->isFeatureSupported(QRhi::Timestamps)); + qDebug("isFeatureSupported(Instancing): %d", m_r->isFeatureSupported(QRhi::Instancing)); + qDebug("isFeatureSupported(CustomInstanceStepRate): %d", m_r->isFeatureSupported(QRhi::CustomInstanceStepRate)); + qDebug("isFeatureSupported(PrimitiveRestart): %d", m_r->isFeatureSupported(QRhi::PrimitiveRestart)); + qDebug("isFeatureSupported(NonDynamicUniformBuffers): %d", m_r->isFeatureSupported(QRhi::NonDynamicUniformBuffers)); + qDebug("isFeatureSupported(NonFourAlignedEffectiveIndexBufferOffset): %d", m_r->isFeatureSupported(QRhi::NonFourAlignedEffectiveIndexBufferOffset)); + qDebug("isFeatureSupported(NPOTTextureRepeat): %d", m_r->isFeatureSupported(QRhi::NPOTTextureRepeat)); + qDebug("isFeatureSupported(RedOrAlpha8IsRed): %d", m_r->isFeatureSupported(QRhi::RedOrAlpha8IsRed)); + qDebug("isFeatureSupported(ElementIndexUint): %d", m_r->isFeatureSupported(QRhi::ElementIndexUint)); + qDebug("isFeatureSupported(Compute): %d", m_r->isFeatureSupported(QRhi::Compute)); + qDebug("Min 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMin)); + qDebug("Max 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMax)); + qDebug("Max color attachment count: %d", m_r->resourceLimit(QRhi::MaxColorAttachments)); +} + +void Window::customRelease() +{ + d.triRenderer.releaseResources(); + + if (!d.triangleOnly) { + d.quadRenderer.releaseResources(); + d.cubeRenderer.releaseResources(); + } + + if (!d.onScreenOnly) + d.liveTexCubeRenderer.releaseResources(); +} + +void Window::customRender() +{ + const QSize outputSize = m_sc->currentPixelSize(); + QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); + + if (outputSize != d.lastOutputSize) { + d.triRenderer.resize(outputSize); + if (!d.triangleOnly) { + d.quadRenderer.resize(outputSize); + d.cubeRenderer.resize(outputSize); + } + if (!d.onScreenOnly) + d.liveTexCubeRenderer.resize(outputSize); + d.lastOutputSize = outputSize; + } + + if (!d.onScreenOnly) { + cb->debugMarkBegin("Offscreen triangle pass"); + d.liveTexCubeRenderer.queueOffscreenPass(cb); + cb->debugMarkEnd(); + } + + QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); + d.triRenderer.queueResourceUpdates(u); + if (!d.triangleOnly) { + d.quadRenderer.queueResourceUpdates(u); + d.cubeRenderer.queueResourceUpdates(u); + } + if (!d.onScreenOnly) + d.liveTexCubeRenderer.queueResourceUpdates(u); + + cb->beginPass(m_sc->currentFrameRenderTarget(), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f), { 1.0f, 0 }, u); + cb->debugMarkBegin(QByteArrayLiteral("Triangle")); + d.triRenderer.queueDraw(cb, outputSize); + cb->debugMarkEnd(); + if (!d.triangleOnly) { + cb->debugMarkMsg(QByteArrayLiteral("More stuff")); + cb->debugMarkBegin(QByteArrayLiteral("Quad")); + d.quadRenderer.queueDraw(cb, outputSize); + cb->debugMarkEnd(); + cb->debugMarkBegin(QByteArrayLiteral("Cube")); + d.cubeRenderer.queueDraw(cb, outputSize); + cb->debugMarkEnd(); + } + if (!d.onScreenOnly) { + cb->debugMarkMsg(QByteArrayLiteral("Even more stuff")); + cb->debugMarkBegin(QByteArrayLiteral("Cube with offscreen triangle")); + d.liveTexCubeRenderer.queueDraw(cb, outputSize); + cb->debugMarkEnd(); + } + + QRhiResourceUpdateBatch *passEndUpdates = nullptr; +#ifdef READBACK_SWAPCHAIN + passEndUpdates = m_r->nextResourceUpdateBatch(); + QRhiReadbackDescription rb; // no texture given -> backbuffer + QRhiReadbackResult *rbResult = new QRhiReadbackResult; + int frameNo = d.frameCount; + rbResult->completed = [this, rbResult, frameNo] { + { + QImage::Format fmt = rbResult->format == QRhiTexture::BGRA8 ? QImage::Format_ARGB32_Premultiplied + : QImage::Format_RGBA8888_Premultiplied; + const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData()); + QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt); + QString fn = QString::asprintf("frame%d.png", frameNo); + fn = QFileInfo(fn).absoluteFilePath(); + qDebug("Saving into %s", qPrintable(fn)); + if (m_r->isYUpInFramebuffer()) + image.mirrored().save(fn); + else + image.save(fn); + } + delete rbResult; + }; + passEndUpdates->readBackTexture(rb, rbResult); +#endif + + cb->endPass(passEndUpdates); + + d.frameCount += 1; +} diff --git a/tests/manual/rhi/triquadcube/triquadcube.pro b/tests/manual/rhi/triquadcube/triquadcube.pro new file mode 100644 index 0000000000..0002b3cad1 --- /dev/null +++ b/tests/manual/rhi/triquadcube/triquadcube.pro @@ -0,0 +1,18 @@ +TEMPLATE = app + +QT += gui-private + +SOURCES = \ + triquadcube.cpp \ + trianglerenderer.cpp \ + quadrenderer.cpp \ + texturedcuberenderer.cpp \ + triangleoncuberenderer.cpp + +HEADERS = \ + trianglerenderer.h \ + quadrenderer.h \ + texturedcuberenderer.h \ + triangleoncuberenderer.h + +RESOURCES = triquadcube.qrc diff --git a/tests/manual/rhi/triquadcube/triquadcube.qrc b/tests/manual/rhi/triquadcube/triquadcube.qrc new file mode 100644 index 0000000000..41b72edc41 --- /dev/null +++ b/tests/manual/rhi/triquadcube/triquadcube.qrc @@ -0,0 +1,9 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file alias="color.vert.qsb">../shared/color.vert.qsb</file> + <file alias="color.frag.qsb">../shared/color.frag.qsb</file> + <file alias="texture.vert.qsb">../shared/texture.vert.qsb</file> + <file alias="texture.frag.qsb">../shared/texture.frag.qsb</file> + <file alias="qt256.png">../shared/qt256.png</file> +</qresource> +</RCC> |