diff options
Diffstat (limited to 'tests/manual/rhi/shared/examplefw.h')
-rw-r--r-- | tests/manual/rhi/shared/examplefw.h | 189 |
1 files changed, 95 insertions, 94 deletions
diff --git a/tests/manual/rhi/shared/examplefw.h b/tests/manual/rhi/shared/examplefw.h index 6b2860d772..f2ca57341c 100644 --- a/tests/manual/rhi/shared/examplefw.h +++ b/tests/manual/rhi/shared/examplefw.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only // Adapted from hellominimalcrossgfxtriangle with the frame rendering stripped out. // Include this file and implement Window::customInit, release and render. @@ -60,26 +13,13 @@ #include <QTimer> #include <QLoggingCategory> #include <QColorSpace> - -#include <QtGui/private/qshader_p.h> #include <QFile> -#include <QtGui/private/qrhinull_p.h> - -#ifndef QT_NO_OPENGL -#include <QtGui/private/qrhigles2_p.h> #include <QOffscreenSurface> -#endif - -#if QT_CONFIG(vulkan) -#include <QtGui/private/qrhivulkan_p.h> -#endif - -#ifdef Q_OS_WIN -#include <QtGui/private/qrhid3d11_p.h> -#endif +#include <rhi/qrhi.h> -#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) -#include <QtGui/private/qrhimetal_p.h> +#ifdef EXAMPLEFW_IMGUI +#include "qrhiimgui_p.h" +#include "imgui.h" #endif QShader getShader(const QString &name) @@ -91,12 +31,22 @@ QShader getShader(const QString &name) return QShader(); } +QByteArray getResource(const QString &name) +{ + QFile f(name); + if (f.open(QIODevice::ReadOnly)) + return f.readAll(); + + return QByteArray(); +} + enum GraphicsApi { Null, OpenGL, Vulkan, D3D11, + D3D12, Metal }; @@ -113,6 +63,8 @@ QString graphicsApiName() return QLatin1String("Vulkan"); case D3D11: return QLatin1String("Direct3D 11"); + case D3D12: + return QLatin1String("Direct3D 12"); case Metal: return QLatin1String("Metal"); default: @@ -121,14 +73,15 @@ QString graphicsApiName() return QString(); } -QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers; +QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers | QRhi::EnableTimestamps; int sampleCount = 1; QRhiSwapChain::Flags scFlags; QRhi::BeginFrameFlags beginFrameFlags; QRhi::EndFrameFlags endFrameFlags; -int framesUntilTdr = -1; bool transparentBackground = false; bool debugLayer = true; +QRhiSwapChain::Format swapchainFormat = QRhiSwapChain::SDR; +float imguiHDRMultiplier = 0.0f; class Window : public QWindow { @@ -146,6 +99,9 @@ protected: void customInit(); void customRelease(); void customRender(); +#ifdef EXAMPLEFW_IMGUI + void customGui(); +#endif void exposeEvent(QExposeEvent *) override; bool event(QEvent *) override; @@ -174,6 +130,11 @@ protected: QColor m_clearColor; +#ifdef EXAMPLEFW_IMGUI + QRhiImguiRenderer *m_imguiRenderer; + QRhiImgui m_imgui; +#endif + friend int main(int, char**); }; @@ -182,15 +143,13 @@ Window::Window() // Tell the platform plugin what we want. switch (graphicsApi) { case OpenGL: -#if QT_CONFIG(opengl) setSurfaceType(OpenGLSurface); - setFormat(QRhiGles2InitParams::adjustedFormat()); -#endif break; case Vulkan: setSurfaceType(VulkanSurface); break; case D3D11: + case D3D12: setSurfaceType(Direct3DSurface); break; case Metal: @@ -250,6 +209,10 @@ bool Window::event(QEvent *e) break; default: +#ifdef EXAMPLEFW_IMGUI + if (m_imgui.processEvent(e)) + return true; +#endif break; } @@ -288,15 +251,17 @@ void Window::init() if (debugLayer) qDebug("Enabling D3D11 debug layer"); params.enableDebugLayer = debugLayer; - if (framesUntilTdr > 0) { - params.framesUntilKillingDeviceViaTdr = framesUntilTdr; - params.repeatDeviceKill = true; - } m_r = QRhi::create(QRhi::D3D11, ¶ms, rhiFlags); + } else if (graphicsApi == D3D12) { + QRhiD3D12InitParams params; + if (debugLayer) + qDebug("Enabling D3D12 debug layer"); + params.enableDebugLayer = debugLayer; + m_r = QRhi::create(QRhi::D3D12, ¶ms, rhiFlags); } #endif -#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) +#if QT_CONFIG(metal) if (graphicsApi == Metal) { QRhiMetalInitParams params; m_r = QRhi::create(QRhi::Metal, ¶ms, rhiFlags); @@ -309,7 +274,6 @@ void Window::init() // now onto the backend-independent init m_sc = m_r->newSwapChain(); - // allow depth-stencil, although we do not actually enable depth test/write for the triangle m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(), // no need to set the size here, due to UsedWithSwapChainOnly sampleCount, @@ -318,9 +282,28 @@ void Window::init() m_sc->setDepthStencil(m_ds); m_sc->setSampleCount(sampleCount); m_sc->setFlags(scFlags); + if (!m_sc->isFormatSupported(swapchainFormat)) + qWarning("Swapchain reports that requested format %d is not supported", int(swapchainFormat)); + else + m_sc->setFormat(swapchainFormat); m_rp = m_sc->newCompatibleRenderPassDescriptor(); m_sc->setRenderPassDescriptor(m_rp); +#ifdef EXAMPLEFW_IMGUI + ImGuiIO &io(ImGui::GetIO()); + io.FontAllowUserScaling = true; // enable ctrl+wheel on windows + io.IniFilename = nullptr; // no imgui.ini + + QByteArray font = getResource(QLatin1String(":/fonts/RobotoMono-Medium.ttf")); + ImFontConfig fontCfg; + fontCfg.FontDataOwnedByAtlas = false; + io.Fonts->Clear(); + io.Fonts->AddFontFromMemoryTTF(font.data(), font.size(), 20.0f, &fontCfg); + m_imgui.rebuildFontAtlas(); + + m_imguiRenderer = new QRhiImguiRenderer; +#endif + customInit(); } @@ -337,6 +320,11 @@ void Window::releaseResources() delete m_sc; m_sc = nullptr; +#ifdef EXAMPLEFW_IMGUI + delete m_imguiRenderer; + m_imguiRenderer = nullptr; +#endif + delete m_r; m_r = nullptr; @@ -405,6 +393,20 @@ void Window::render() m_frameCount = 0; } +#ifdef EXAMPLEFW_IMGUI + m_imgui.nextFrame(size(), devicePixelRatio(), QPointF(0, 0), std::bind(&Window::customGui, this)); + m_imgui.syncRenderer(m_imguiRenderer); + + QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); + QRhiRenderTarget *rt = m_sc->currentFrameRenderTarget(); + const QSize outputSizeInPixels = m_sc->currentPixelSize(); + const float dpr = devicePixelRatio(); + + QMatrix4x4 guiMvp = m_r->clipSpaceCorrMatrix(); + guiMvp.ortho(0, outputSizeInPixels.width() / dpr, outputSizeInPixels.height() / dpr, 0, 1, -1); + m_imguiRenderer->prepare(m_r, rt, cb, guiMvp, 1.0f, imguiHDRMultiplier); +#endif + customRender(); m_r->endFrame(m_sc, endFrameFlags); @@ -424,7 +426,7 @@ int main(int argc, char **argv) // Defaults. #if defined(Q_OS_WIN) graphicsApi = D3D11; -#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) +#elif QT_CONFIG(metal) graphicsApi = Metal; #elif QT_CONFIG(vulkan) graphicsApi = Vulkan; @@ -441,8 +443,10 @@ int main(int argc, char **argv) cmdLineParser.addOption(glOption); QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan")); cmdLineParser.addOption(vkOption); - QCommandLineOption d3dOption({ "d", "d3d11" }, QLatin1String("Direct3D 11")); - cmdLineParser.addOption(d3dOption); + QCommandLineOption d3d11Option({ "d", "d3d11" }, QLatin1String("Direct3D 11")); + cmdLineParser.addOption(d3d11Option); + QCommandLineOption d3d12Option({ "D", "d3d12" }, QLatin1String("Direct3D 12")); + cmdLineParser.addOption(d3d12Option); QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal")); cmdLineParser.addOption(mtlOption); // Testing cleanup both with QWindow::close() (hitting X or Alt-F4) and @@ -452,12 +456,7 @@ int main(int argc, char **argv) cmdLineParser.addOption(sdOption); QCommandLineOption coreProfOption({ "c", "core" }, QLatin1String("Request a core profile context for OpenGL")); cmdLineParser.addOption(coreProfOption); - // Attempt testing device lost situations on D3D at least. - QCommandLineOption tdrOption(QLatin1String("curse"), QLatin1String("Curse the graphics device. " - "(generate a device reset every <count> frames when on D3D11)"), - QLatin1String("count")); - cmdLineParser.addOption(tdrOption); - // Allow testing preferring the software adapter (D3D). + // Allow testing preferring the software adapter (D3D, Vulkan). QCommandLineOption swOption(QLatin1String("software"), QLatin1String("Prefer a software renderer when choosing the adapter. " "Only applicable with some APIs and platforms.")); cmdLineParser.addOption(swOption); @@ -472,8 +471,10 @@ int main(int argc, char **argv) graphicsApi = OpenGL; if (cmdLineParser.isSet(vkOption)) graphicsApi = Vulkan; - if (cmdLineParser.isSet(d3dOption)) + if (cmdLineParser.isSet(d3d11Option)) graphicsApi = D3D11; + if (cmdLineParser.isSet(d3d12Option)) + graphicsApi = D3D12; if (cmdLineParser.isSet(mtlOption)) graphicsApi = Metal; @@ -522,11 +523,14 @@ int main(int argc, char **argv) inst.setLayers({ "VK_LAYER_KHRONOS_validation" }); } const QVersionNumber supportedVersion = inst.supportedApiVersion(); - qDebug() << "Supported Vulkan API version:" << supportedVersion; - if (supportedVersion >= QVersionNumber(1, 1)) { - qDebug("Requesting Vulkan API 1.1 on the VkInstance"); + if (supportedVersion >= QVersionNumber(1, 3)) + inst.setApiVersion(QVersionNumber(1, 3)); + else if (supportedVersion >= QVersionNumber(1, 2)) + inst.setApiVersion(QVersionNumber(1, 2)); + else if (supportedVersion >= QVersionNumber(1, 1)) inst.setApiVersion(QVersionNumber(1, 1)); - } + qDebug() << "Requesting Vulkan API" << inst.apiVersion().toString(); + qDebug() << "Instance-level version was reported as" << supportedVersion.toString(); inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions()); if (!inst.create()) { qWarning("Failed to create Vulkan instance, switching to OpenGL"); @@ -535,9 +539,6 @@ int main(int argc, char **argv) } #endif - if (cmdLineParser.isSet(tdrOption)) - framesUntilTdr = cmdLineParser.value(tdrOption).toInt(); - if (cmdLineParser.isSet(swOption)) rhiFlags |= QRhi::PreferSoftwareRenderer; |