diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2017-02-12 15:08:52 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2017-03-22 15:40:57 +0000 |
commit | 8d72aba9e3358fdf51578533ab7f46602f7fd103 (patch) | |
tree | 5e6ff0ec0ee399bf343654fc9280012eafa29a3d /examples/vulkan/hellovulkanwidget | |
parent | 66e162e8f19f8a1f9b4f96008f2853be7fc1822e (diff) |
Introduce QVulkanWindow
A convenience subclass of QWindow that provides a Vulkan-capable
window with a double-buffered FIFO swapchain.
While advanced use cases are better served by a custom QWindow
subclass, many applications can benefit from having a convenient
helper that makes getting started easier.
Add also three examples of increasing complexity, and a variant that
shows embeddeding into widgets via QWindowContainer.
[ChangeLog][QtGui] Added QVulkanWindow, a convenience subclass of
QWindow.
Task-number: QTBUG-55981
Change-Id: I6cdc9ff1390ac6258e278377233fd369a0bfeddc
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'examples/vulkan/hellovulkanwidget')
5 files changed, 395 insertions, 0 deletions
diff --git a/examples/vulkan/hellovulkanwidget/hellovulkanwidget.cpp b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.cpp new file mode 100644 index 0000000000..ecab104399 --- /dev/null +++ b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.cpp @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 "hellovulkanwidget.h" +#include <QVulkanFunctions> +#include <QApplication> +#include <QVBoxLayout> +#include <QTextEdit> +#include <QPushButton> +#include <QLCDNumber> +#include <QFileDialog> +#include <QMessageBox> + +MainWindow::MainWindow(VulkanWindow *w) + : m_window(w) +{ + QWidget *wrapper = QWidget::createWindowContainer(w); + + m_info = new QTextEdit; + m_info->setReadOnly(true); + + m_number = new QLCDNumber(3); + m_number->setSegmentStyle(QLCDNumber::Filled); + + QPushButton *grabButton = new QPushButton(tr("&Grab")); + grabButton->setFocusPolicy(Qt::NoFocus); + + connect(grabButton, &QPushButton::clicked, this, &MainWindow::onGrabRequested); + + QPushButton *quitButton = new QPushButton(tr("&Quit")); + quitButton->setFocusPolicy(Qt::NoFocus); + + connect(quitButton, &QPushButton::clicked, qApp, &QCoreApplication::quit); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(m_info, 2); + layout->addWidget(m_number, 1); + layout->addWidget(wrapper, 5); + layout->addWidget(grabButton, 1); + layout->addWidget(quitButton, 1); + setLayout(layout); +} + +void MainWindow::onVulkanInfoReceived(const QString &text) +{ + m_info->setText(text); +} + +void MainWindow::onFrameQueued(int colorValue) +{ + m_number->display(colorValue); +} + +void MainWindow::onGrabRequested() +{ + if (!m_window->supportsGrab()) { + QMessageBox::warning(this, tr("Cannot grab"), tr("This swapchain does not support readbacks.")); + return; + } + + QImage img = m_window->grab(); + + // Our startNextFrame() implementation is synchronous so img is ready to be + // used right here. + + QFileDialog fd(this); + fd.setAcceptMode(QFileDialog::AcceptSave); + fd.setDefaultSuffix("png"); + fd.selectFile("test.png"); + if (fd.exec() == QDialog::Accepted) + img.save(fd.selectedFiles().first()); +} + +QVulkanWindowRenderer *VulkanWindow::createRenderer() +{ + return new VulkanRenderer(this); +} + +VulkanRenderer::VulkanRenderer(VulkanWindow *w) + : TriangleRenderer(w) +{ +} + +void VulkanRenderer::initResources() +{ + TriangleRenderer::initResources(); + + QVulkanInstance *inst = m_window->vulkanInstance(); + m_devFuncs = inst->deviceFunctions(m_window->device()); + + QString info; + info += QString().sprintf("Number of physical devices: %d\n", m_window->availablePhysicalDevices().count()); + + QVulkanFunctions *f = inst->functions(); + VkPhysicalDeviceProperties props; + f->vkGetPhysicalDeviceProperties(m_window->physicalDevice(), &props); + info += QString().sprintf("Active physical device name: '%s' version %d.%d.%d\nAPI version %d.%d.%d\n", + props.deviceName, + VK_VERSION_MAJOR(props.driverVersion), VK_VERSION_MINOR(props.driverVersion), + VK_VERSION_PATCH(props.driverVersion), + VK_VERSION_MAJOR(props.apiVersion), VK_VERSION_MINOR(props.apiVersion), + VK_VERSION_PATCH(props.apiVersion)); + + info += QStringLiteral("Supported instance layers:\n"); + for (const QVulkanLayer &layer : inst->supportedLayers()) + info += QString().sprintf(" %s v%u\n", layer.name.constData(), layer.version); + info += QStringLiteral("Enabled instance layers:\n"); + for (const QByteArray &layer : inst->layers()) + info += QString().sprintf(" %s\n", layer.constData()); + + info += QStringLiteral("Supported instance extensions:\n"); + for (const QVulkanExtension &ext : inst->supportedExtensions()) + info += QString().sprintf(" %s v%u\n", ext.name.constData(), ext.version); + info += QStringLiteral("Enabled instance extensions:\n"); + for (const QByteArray &ext : inst->extensions()) + info += QString().sprintf(" %s\n", ext.constData()); + + info += QString().sprintf("Color format: %u\nDepth-stencil format: %u\n", + m_window->colorFormat(), m_window->depthStencilFormat()); + + info += QStringLiteral("Supported sample counts:"); + QList<int> sampleCounts = m_window->supportedSampleCounts().toList(); + std::sort(sampleCounts.begin(), sampleCounts.end()); + for (int count : sampleCounts) + info += QLatin1Char(' ') + QString::number(count); + info += QLatin1Char('\n'); + + emit static_cast<VulkanWindow *>(m_window)->vulkanInfoReceived(info); +} + +void VulkanRenderer::startNextFrame() +{ + TriangleRenderer::startNextFrame(); + emit static_cast<VulkanWindow *>(m_window)->frameQueued(int(m_rotation) % 360); +} diff --git a/examples/vulkan/hellovulkanwidget/hellovulkanwidget.h b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.h new file mode 100644 index 0000000000..b1f4824006 --- /dev/null +++ b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 "../shared/trianglerenderer.h" +#include <QWidget> + +class VulkanWindow; + +QT_BEGIN_NAMESPACE +class QTextEdit; +class QLCDNumber; +QT_END_NAMESPACE + +class MainWindow : public QWidget +{ + Q_OBJECT + +public: + MainWindow(VulkanWindow *w); + +public slots: + void onVulkanInfoReceived(const QString &text); + void onFrameQueued(int colorValue); + void onGrabRequested(); + +private: + VulkanWindow *m_window; + QTextEdit *m_info; + QLCDNumber *m_number; +}; + +class VulkanRenderer : public TriangleRenderer +{ +public: + VulkanRenderer(VulkanWindow *w); + + void initResources() override; + void startNextFrame() override; +}; + +class VulkanWindow : public QVulkanWindow +{ + Q_OBJECT + +public: + QVulkanWindowRenderer *createRenderer() override; + +signals: + void vulkanInfoReceived(const QString &text); + void frameQueued(int colorValue); +}; diff --git a/examples/vulkan/hellovulkanwidget/hellovulkanwidget.pro b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.pro new file mode 100644 index 0000000000..7b87d7f210 --- /dev/null +++ b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.pro @@ -0,0 +1,16 @@ +QT += widgets + +HEADERS += \ + hellovulkanwidget.h \ + ../shared/trianglerenderer.h + +SOURCES += \ + hellovulkanwidget.cpp \ + main.cpp \ + ../shared/trianglerenderer.cpp + +RESOURCES += hellovulkanwidget.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/vulkan/hellovulkanwidget +INSTALLS += target diff --git a/examples/vulkan/hellovulkanwidget/hellovulkanwidget.qrc b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.qrc new file mode 100644 index 0000000000..489fc7295a --- /dev/null +++ b/examples/vulkan/hellovulkanwidget/hellovulkanwidget.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file alias="color_vert.spv">../shared/color_vert.spv</file> + <file alias="color_frag.spv">../shared/color_frag.spv</file> +</qresource> +</RCC> diff --git a/examples/vulkan/hellovulkanwidget/main.cpp b/examples/vulkan/hellovulkanwidget/main.cpp new file mode 100644 index 0000000000..320e015e67 --- /dev/null +++ b/examples/vulkan/hellovulkanwidget/main.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 <QApplication> +#include <QVulkanInstance> +#include <QLoggingCategory> +#include "hellovulkanwidget.h" + +Q_LOGGING_CATEGORY(lcVk, "qt.vulkan") + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QLoggingCategory::setFilterRules(QStringLiteral("qt.vulkan=true")); + + QVulkanInstance inst; + +#ifndef Q_OS_ANDROID + inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation"); +#else + inst.setLayers(QByteArrayList() + << "VK_LAYER_GOOGLE_threading" + << "VK_LAYER_LUNARG_parameter_validation" + << "VK_LAYER_LUNARG_object_tracker" + << "VK_LAYER_LUNARG_core_validation" + << "VK_LAYER_LUNARG_image" + << "VK_LAYER_LUNARG_swapchain" + << "VK_LAYER_GOOGLE_unique_objects"); +#endif + + if (!inst.create()) + qFatal("Failed to create Vulkan instance: %d", inst.errorCode()); + + VulkanWindow *vulkanWindow = new VulkanWindow; + vulkanWindow->setVulkanInstance(&inst); + + MainWindow mainWindow(vulkanWindow); + QObject::connect(vulkanWindow, &VulkanWindow::vulkanInfoReceived, &mainWindow, &MainWindow::onVulkanInfoReceived); + QObject::connect(vulkanWindow, &VulkanWindow::frameQueued, &mainWindow, &MainWindow::onFrameQueued); + + mainWindow.resize(1024, 768); + mainWindow.show(); + + return app.exec(); +} |