2020-02-02 16:54:59 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
#include <QPlatformSurfaceEvent>
|
|
|
|
|
|
|
|
Window::Window(QRhi::Implementation graphicsApi)
|
|
|
|
: m_graphicsApi(graphicsApi)
|
|
|
|
{
|
|
|
|
switch (graphicsApi) {
|
|
|
|
case QRhi::OpenGLES2:
|
|
|
|
setSurfaceType(OpenGLSurface);
|
|
|
|
#if QT_CONFIG(opengl)
|
|
|
|
setFormat(QRhiGles2InitParams::adjustedFormat());
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case QRhi::Vulkan:
|
|
|
|
setSurfaceType(VulkanSurface);
|
|
|
|
break;
|
|
|
|
case QRhi::D3D11:
|
Add a dedicated QWindow surface type for D3D
Even though there is no D3D-specific logic in the windows platform
plugin, meaning a QWindow with either OpenGLSurface or VulkanSurface
(or anything really) is DXGI/D3D-compatible, it now looks like it is
beneficial, and more future proof, if there is a dedicated surface
type.
As the linked report shows, there are OpenGL-specific workarounds
accumulated in the platform plugin, while not being clear if these
are relevant to non-OpenGL content, or if they are relevant at all
still. (and some of these can be difficult/impossible to retest and
verify in practice)
When D3D-based windows use the same surface type, all these are
active for those windows as well, while Vulkan-based windows have
their own type and so some of these old workarounds are not active
for those. To reduce confusion, having a dedicated surface type for
D3D as well allows the logic to skip the old OpenGL workarounds,
giving us (and users) a more clear overall behavior when it comes
to OpenGL vs. Vulkan vs. D3D.
The change is compatible with any existing code in other modules
because any code that uses OpenGLSurface for D3D will continue to
work, using the new type can be introduced incrementally.
Task-number: QTBUG-89715
Change-Id: Ieba86a580bf5a3636730952184dc3a3ab7669b26
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
2021-01-04 09:27:11 +00:00
|
|
|
setSurfaceType(Direct3DSurface);
|
2020-02-02 16:54:59 +00:00
|
|
|
break;
|
|
|
|
case QRhi::Metal:
|
|
|
|
setSurfaceType(MetalSurface);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::exposeEvent(QExposeEvent *)
|
|
|
|
{
|
|
|
|
// initialize and start rendering when the window becomes usable for graphics purposes
|
|
|
|
if (isExposed() && !m_running) {
|
|
|
|
qDebug("init");
|
|
|
|
m_running = true;
|
|
|
|
init();
|
|
|
|
resizeSwapChain();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QSize surfaceSize = m_hasSwapChain ? m_sc->surfacePixelSize() : QSize();
|
|
|
|
|
|
|
|
// stop pushing frames when not exposed (or size is 0)
|
|
|
|
if ((!isExposed() || (m_hasSwapChain && surfaceSize.isEmpty())) && m_running && !m_notExposed) {
|
|
|
|
qDebug("not exposed");
|
|
|
|
m_notExposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue when exposed again and the surface has a valid size. Note that
|
|
|
|
// surfaceSize can be (0, 0) even though size() reports a valid one, hence
|
|
|
|
// trusting surfacePixelSize() and not QWindow.
|
|
|
|
if (isExposed() && m_running && m_notExposed && !surfaceSize.isEmpty()) {
|
|
|
|
qDebug("exposed again");
|
|
|
|
m_notExposed = false;
|
|
|
|
m_newlyExposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// always render a frame on exposeEvent() (when exposed) in order to update
|
|
|
|
// immediately on window resize.
|
|
|
|
if (isExposed() && !surfaceSize.isEmpty())
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::event(QEvent *e)
|
|
|
|
{
|
|
|
|
switch (e->type()) {
|
|
|
|
case QEvent::UpdateRequest:
|
|
|
|
render();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::PlatformSurface:
|
|
|
|
// this is the proper time to tear down the swapchain (while the native window and surface are still around)
|
|
|
|
if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
|
|
|
|
releaseSwapChain();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWindow::event(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::init()
|
|
|
|
{
|
|
|
|
QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers | QRhi::EnableProfiling;
|
|
|
|
|
|
|
|
if (m_graphicsApi == QRhi::Null) {
|
|
|
|
QRhiNullInitParams params;
|
|
|
|
m_rhi.reset(QRhi::create(QRhi::Null, ¶ms, rhiFlags));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_CONFIG(opengl)
|
|
|
|
if (m_graphicsApi == QRhi::OpenGLES2) {
|
|
|
|
m_fallbackSurface.reset(QRhiGles2InitParams::newFallbackSurface());
|
|
|
|
QRhiGles2InitParams params;
|
|
|
|
params.fallbackSurface = m_fallbackSurface.get();
|
|
|
|
params.window = this;
|
|
|
|
m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms, rhiFlags));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if QT_CONFIG(vulkan)
|
|
|
|
if (m_graphicsApi == QRhi::Vulkan) {
|
|
|
|
QRhiVulkanInitParams params;
|
|
|
|
params.inst = vulkanInstance();
|
|
|
|
params.window = this;
|
|
|
|
m_rhi.reset(QRhi::create(QRhi::Vulkan, ¶ms, rhiFlags));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (m_graphicsApi == QRhi::D3D11) {
|
|
|
|
QRhiD3D11InitParams params;
|
|
|
|
params.enableDebugLayer = true;
|
|
|
|
m_rhi.reset(QRhi::create(QRhi::D3D11, ¶ms, rhiFlags));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
|
|
|
|
if (m_graphicsApi == QRhi::Metal) {
|
|
|
|
QRhiMetalInitParams params;
|
2020-02-20 09:37:53 +00:00
|
|
|
m_rhi.reset(QRhi::create(QRhi::Metal, ¶ms, rhiFlags));
|
2020-02-02 16:54:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!m_rhi)
|
|
|
|
qFatal("Failed to create RHI backend");
|
|
|
|
|
|
|
|
m_sc.reset(m_rhi->newSwapChain());
|
|
|
|
m_ds.reset(m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
|
|
|
|
QSize(), // no need to set the size here, due to UsedWithSwapChainOnly
|
|
|
|
1,
|
|
|
|
QRhiRenderBuffer::UsedWithSwapChainOnly));
|
|
|
|
m_sc->setWindow(this);
|
|
|
|
m_sc->setDepthStencil(m_ds.get());
|
|
|
|
m_rp.reset(m_sc->newCompatibleRenderPassDescriptor());
|
|
|
|
m_sc->setRenderPassDescriptor(m_rp.get());
|
|
|
|
|
|
|
|
customInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::resizeSwapChain()
|
|
|
|
{
|
2020-05-27 15:44:49 +00:00
|
|
|
m_hasSwapChain = m_sc->createOrResize(); // also handles m_ds
|
2020-02-02 16:54:59 +00:00
|
|
|
|
|
|
|
const QSize outputSize = m_sc->currentPixelSize();
|
|
|
|
m_proj = m_rhi->clipSpaceCorrMatrix();
|
|
|
|
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
|
|
|
|
m_proj.translate(0, 0, -4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::releaseSwapChain()
|
|
|
|
{
|
|
|
|
if (m_hasSwapChain) {
|
|
|
|
m_hasSwapChain = false;
|
2020-05-27 15:44:49 +00:00
|
|
|
m_sc->destroy();
|
2020-02-02 16:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::render()
|
|
|
|
{
|
|
|
|
if (!m_hasSwapChain || m_notExposed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the window got resized or newly exposed, resize the swapchain. (the
|
|
|
|
// newly-exposed case is not actually required by some platforms, but
|
|
|
|
// f.ex. Vulkan on Windows seems to need it)
|
|
|
|
//
|
|
|
|
// This (exposeEvent + the logic here) is the only safe way to perform
|
|
|
|
// resize handling. Note the usage of the RHI's surfacePixelSize(), and
|
|
|
|
// never QWindow::size(). (the two may or may not be the same under the hood,
|
|
|
|
// depending on the backend and platform)
|
|
|
|
//
|
|
|
|
if (m_sc->currentPixelSize() != m_sc->surfacePixelSize() || m_newlyExposed) {
|
|
|
|
resizeSwapChain();
|
|
|
|
if (!m_hasSwapChain)
|
|
|
|
return;
|
|
|
|
m_newlyExposed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRhi::FrameOpResult r = m_rhi->beginFrame(m_sc.get());
|
|
|
|
if (r == QRhi::FrameOpSwapChainOutOfDate) {
|
|
|
|
resizeSwapChain();
|
|
|
|
if (!m_hasSwapChain)
|
|
|
|
return;
|
|
|
|
r = m_rhi->beginFrame(m_sc.get());
|
|
|
|
}
|
|
|
|
if (r != QRhi::FrameOpSuccess) {
|
|
|
|
qDebug("beginFrame failed with %d, retry", r);
|
|
|
|
requestUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
customRender();
|
|
|
|
|
|
|
|
m_rhi->endFrame(m_sc.get());
|
|
|
|
|
|
|
|
requestUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::customInit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::customRender()
|
|
|
|
{
|
|
|
|
}
|