| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2020 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
#include "window.h" |
| 52 |
#include <QPlatformSurfaceEvent> |
| 53 |
|
| 54 |
Window::Window(QRhi::Implementation graphicsApi) |
| 55 |
: m_graphicsApi(graphicsApi) |
| 56 |
{ |
| 57 |
switch (graphicsApi) { |
| 58 |
case QRhi::OpenGLES2: |
| 59 |
setSurfaceType(OpenGLSurface); |
| 60 |
#if QT_CONFIG(opengl) |
| 61 |
setFormat(QRhiGles2InitParams::adjustedFormat()); |
| 62 |
#endif |
| 63 |
break; |
| 64 |
case QRhi::Vulkan: |
| 65 |
setSurfaceType(VulkanSurface); |
| 66 |
break; |
| 67 |
case QRhi::D3D11: |
| 68 |
setSurfaceType(OpenGLSurface); |
| 69 |
break; |
| 70 |
case QRhi::Metal: |
| 71 |
setSurfaceType(MetalSurface); |
| 72 |
break; |
| 73 |
default: |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void Window::exposeEvent(QExposeEvent *) |
| 79 |
{ |
| 80 |
// initialize and start rendering when the window becomes usable for graphics purposes |
| 81 |
if (isExposed() && !m_running) { |
| 82 |
qDebug("init"); |
| 83 |
m_running = true; |
| 84 |
init(); |
| 85 |
resizeSwapChain(); |
| 86 |
} |
| 87 |
|
| 88 |
const QSize surfaceSize = m_hasSwapChain ? m_sc->surfacePixelSize() : QSize(); |
| 89 |
|
| 90 |
// stop pushing frames when not exposed (or size is 0) |
| 91 |
if ((!isExposed() || (m_hasSwapChain && surfaceSize.isEmpty())) && m_running && !m_notExposed) { |
| 92 |
qDebug("not exposed"); |
| 93 |
m_notExposed = true; |
| 94 |
} |
| 95 |
|
| 96 |
// Continue when exposed again and the surface has a valid size. Note that |
| 97 |
// surfaceSize can be (0, 0) even though size() reports a valid one, hence |
| 98 |
// trusting surfacePixelSize() and not QWindow. |
| 99 |
if (isExposed() && m_running && m_notExposed && !surfaceSize.isEmpty()) { |
| 100 |
qDebug("exposed again"); |
| 101 |
m_notExposed = false; |
| 102 |
m_newlyExposed = true; |
| 103 |
} |
| 104 |
|
| 105 |
// always render a frame on exposeEvent() (when exposed) in order to update |
| 106 |
// immediately on window resize. |
| 107 |
if (isExposed() && !surfaceSize.isEmpty()) |
| 108 |
render(); |
| 109 |
} |
| 110 |
|
| 111 |
bool Window::event(QEvent *e) |
| 112 |
{ |
| 113 |
switch (e->type()) { |
| 114 |
case QEvent::UpdateRequest: |
| 115 |
render(); |
| 116 |
break; |
| 117 |
|
| 118 |
case QEvent::PlatformSurface: |
| 119 |
// this is the proper time to tear down the swapchain (while the native window and surface are still around) |
| 120 |
if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) |
| 121 |
releaseSwapChain(); |
| 122 |
break; |
| 123 |
|
| 124 |
default: |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
return QWindow::event(e); |
| 129 |
} |
| 130 |
|
| 131 |
void Window::init() |
| 132 |
{ |
| 133 |
QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers | QRhi::EnableProfiling; |
| 134 |
|
| 135 |
if (m_graphicsApi == QRhi::Null) { |
| 136 |
QRhiNullInitParams params; |
| 137 |
m_rhi.reset(QRhi::create(QRhi::Null, ¶ms, rhiFlags)); |
| 138 |
} |
| 139 |
|
| 140 |
#if QT_CONFIG(opengl) |
| 141 |
if (m_graphicsApi == QRhi::OpenGLES2) { |
| 142 |
m_fallbackSurface.reset(QRhiGles2InitParams::newFallbackSurface()); |
| 143 |
QRhiGles2InitParams params; |
| 144 |
params.fallbackSurface = m_fallbackSurface.get(); |
| 145 |
params.window = this; |
| 146 |
m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms, rhiFlags)); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
#if QT_CONFIG(vulkan) |
| 151 |
if (m_graphicsApi == QRhi::Vulkan) { |
| 152 |
QRhiVulkanInitParams params; |
| 153 |
params.inst = vulkanInstance(); |
| 154 |
params.window = this; |
| 155 |
m_rhi.reset(QRhi::create(QRhi::Vulkan, ¶ms, rhiFlags)); |
| 156 |
} |
| 157 |
#endif |
| 158 |
|
| 159 |
#ifdef Q_OS_WIN |
| 160 |
if (m_graphicsApi == QRhi::D3D11) { |
| 161 |
QRhiD3D11InitParams params; |
| 162 |
params.enableDebugLayer = true; |
| 163 |
m_rhi.reset(QRhi::create(QRhi::D3D11, ¶ms, rhiFlags)); |
| 164 |
} |
| 165 |
#endif |
| 166 |
|
| 167 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 168 |
if (m_graphicsApi == QRhi::Metal) { |
| 169 |
QRhiMetalInitParams params; |
| 170 |
m_rhi.reset(QRhi::create(QRhi::Metal, ¶ms, rhiFlags)); |
| 171 |
} |
| 172 |
#endif |
| 173 |
|
| 174 |
if (!m_rhi) |
| 175 |
qFatal("Failed to create RHI backend"); |
| 176 |
|
| 177 |
m_sc.reset(m_rhi->newSwapChain()); |
| 178 |
m_ds.reset(m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, |
| 179 |
QSize(), // no need to set the size here, due to UsedWithSwapChainOnly |
| 180 |
1, |
| 181 |
QRhiRenderBuffer::UsedWithSwapChainOnly)); |
| 182 |
m_sc->setWindow(this); |
| 183 |
m_sc->setDepthStencil(m_ds.get()); |
| 184 |
m_rp.reset(m_sc->newCompatibleRenderPassDescriptor()); |
| 185 |
m_sc->setRenderPassDescriptor(m_rp.get()); |
| 186 |
|
| 187 |
customInit(); |
| 188 |
} |
| 189 |
|
| 190 |
void Window::resizeSwapChain() |
| 191 |
{ |
| 192 |
m_hasSwapChain = m_sc->buildOrResize(); // also handles m_ds |
| 193 |
|
| 194 |
const QSize outputSize = m_sc->currentPixelSize(); |
| 195 |
m_proj = m_rhi->clipSpaceCorrMatrix(); |
| 196 |
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 197 |
m_proj.translate(0, 0, -4); |
| 198 |
} |
| 199 |
|
| 200 |
void Window::releaseSwapChain() |
| 201 |
{ |
| 202 |
if (m_hasSwapChain) { |
| 203 |
m_hasSwapChain = false; |
| 204 |
m_sc->release(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void Window::render() |
| 209 |
{ |
| 210 |
if (!m_hasSwapChain || m_notExposed) |
| 211 |
return; |
| 212 |
|
| 213 |
// If the window got resized or newly exposed, resize the swapchain. (the |
| 214 |
// newly-exposed case is not actually required by some platforms, but |
| 215 |
// f.ex. Vulkan on Windows seems to need it) |
| 216 |
// |
| 217 |
// This (exposeEvent + the logic here) is the only safe way to perform |
| 218 |
// resize handling. Note the usage of the RHI's surfacePixelSize(), and |
| 219 |
// never QWindow::size(). (the two may or may not be the same under the hood, |
| 220 |
// depending on the backend and platform) |
| 221 |
// |
| 222 |
if (m_sc->currentPixelSize() != m_sc->surfacePixelSize() || m_newlyExposed) { |
| 223 |
resizeSwapChain(); |
| 224 |
if (!m_hasSwapChain) |
| 225 |
return; |
| 226 |
m_newlyExposed = false; |
| 227 |
} |
| 228 |
|
| 229 |
QRhi::FrameOpResult r = m_rhi->beginFrame(m_sc.get()); |
| 230 |
if (r == QRhi::FrameOpSwapChainOutOfDate) { |
| 231 |
resizeSwapChain(); |
| 232 |
if (!m_hasSwapChain) |
| 233 |
return; |
| 234 |
r = m_rhi->beginFrame(m_sc.get()); |
| 235 |
} |
| 236 |
if (r != QRhi::FrameOpSuccess) { |
| 237 |
qDebug("beginFrame failed with %d, retry", r); |
| 238 |
requestUpdate(); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
customRender(); |
| 243 |
|
| 244 |
m_rhi->endFrame(m_sc.get()); |
| 245 |
|
| 246 |
requestUpdate(); |
| 247 |
} |
| 248 |
|
| 249 |
void Window::customInit() |
| 250 |
{ |
| 251 |
} |
| 252 |
|
| 253 |
void Window::customRender() |
| 254 |
{ |
| 255 |
} |