| 1 |
// Copyright (C) 2020 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "window.h" |
| 5 |
#include <QPlatformSurfaceEvent> |
| 6 |
#include <QTimer> |
| 7 |
|
| 8 |
Window::Window(QRhi::Implementation graphicsApi) |
| 9 |
: m_graphicsApi(graphicsApi) |
| 10 |
{ |
| 11 |
switch (graphicsApi) { |
| 12 |
case QRhi::OpenGLES2: |
| 13 |
setSurfaceType(OpenGLSurface); |
| 14 |
break; |
| 15 |
case QRhi::Vulkan: |
| 16 |
setSurfaceType(VulkanSurface); |
| 17 |
break; |
| 18 |
case QRhi::D3D11: |
| 19 |
case QRhi::D3D12: |
| 20 |
setSurfaceType(Direct3DSurface); |
| 21 |
break; |
| 22 |
case QRhi::Metal: |
| 23 |
setSurfaceType(MetalSurface); |
| 24 |
break; |
| 25 |
default: |
| 26 |
break; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
void Window::exposeEvent(QExposeEvent *) |
| 31 |
{ |
| 32 |
// initialize and start rendering when the window becomes usable for graphics purposes |
| 33 |
if (isExposed() && !m_running) { |
| 34 |
qDebug("init"); |
| 35 |
m_running = true; |
| 36 |
init(); |
| 37 |
resizeSwapChain(); |
| 38 |
} |
| 39 |
|
| 40 |
const QSize surfaceSize = m_hasSwapChain ? m_sc->surfacePixelSize() : QSize(); |
| 41 |
|
| 42 |
// stop pushing frames when not exposed (or size is 0) |
| 43 |
if ((!isExposed() || (m_hasSwapChain && surfaceSize.isEmpty())) && m_running && !m_notExposed) { |
| 44 |
qDebug("not exposed"); |
| 45 |
m_notExposed = true; |
| 46 |
} |
| 47 |
|
| 48 |
// Continue when exposed again and the surface has a valid size. Note that |
| 49 |
// surfaceSize can be (0, 0) even though size() reports a valid one, hence |
| 50 |
// trusting surfacePixelSize() and not QWindow. |
| 51 |
if (isExposed() && m_running && m_notExposed && !surfaceSize.isEmpty()) { |
| 52 |
qDebug("exposed again"); |
| 53 |
m_notExposed = false; |
| 54 |
m_newlyExposed = true; |
| 55 |
} |
| 56 |
|
| 57 |
// always render a frame on exposeEvent() (when exposed) in order to update |
| 58 |
// immediately on window resize. |
| 59 |
if (isExposed() && !surfaceSize.isEmpty()) |
| 60 |
render(); |
| 61 |
} |
| 62 |
|
| 63 |
bool Window::event(QEvent *e) |
| 64 |
{ |
| 65 |
switch (e->type()) { |
| 66 |
case QEvent::UpdateRequest: |
| 67 |
render(); |
| 68 |
break; |
| 69 |
|
| 70 |
case QEvent::PlatformSurface: |
| 71 |
// this is the proper time to tear down the swapchain (while the native window and surface are still around) |
| 72 |
if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) |
| 73 |
releaseSwapChain(); |
| 74 |
break; |
| 75 |
|
| 76 |
default: |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
return QWindow::event(e); |
| 81 |
} |
| 82 |
|
| 83 |
void Window::init() |
| 84 |
{ |
| 85 |
QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers; |
| 86 |
|
| 87 |
if (m_graphicsApi == QRhi::Null) { |
| 88 |
QRhiNullInitParams params; |
| 89 |
m_rhi.reset(QRhi::create(QRhi::Null, ¶ms, rhiFlags)); |
| 90 |
} |
| 91 |
|
| 92 |
#if QT_CONFIG(opengl) |
| 93 |
if (m_graphicsApi == QRhi::OpenGLES2) { |
| 94 |
m_fallbackSurface.reset(QRhiGles2InitParams::newFallbackSurface()); |
| 95 |
QRhiGles2InitParams params; |
| 96 |
params.fallbackSurface = m_fallbackSurface.get(); |
| 97 |
params.window = this; |
| 98 |
m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms, rhiFlags)); |
| 99 |
} |
| 100 |
#endif |
| 101 |
|
| 102 |
#if QT_CONFIG(vulkan) |
| 103 |
if (m_graphicsApi == QRhi::Vulkan) { |
| 104 |
QRhiVulkanInitParams params; |
| 105 |
params.inst = vulkanInstance(); |
| 106 |
params.window = this; |
| 107 |
m_rhi.reset(QRhi::create(QRhi::Vulkan, ¶ms, rhiFlags)); |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
#ifdef Q_OS_WIN |
| 112 |
if (m_graphicsApi == QRhi::D3D11) { |
| 113 |
QRhiD3D11InitParams params; |
| 114 |
params.enableDebugLayer = true; |
| 115 |
m_rhi.reset(QRhi::create(QRhi::D3D11, ¶ms, rhiFlags)); |
| 116 |
} else if (m_graphicsApi == QRhi::D3D12) { |
| 117 |
QRhiD3D12InitParams params; |
| 118 |
params.enableDebugLayer = true; |
| 119 |
m_rhi.reset(QRhi::create(QRhi::D3D12, ¶ms, rhiFlags)); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
#if QT_CONFIG(metal) |
| 124 |
if (m_graphicsApi == QRhi::Metal) { |
| 125 |
QRhiMetalInitParams params; |
| 126 |
m_rhi.reset(QRhi::create(QRhi::Metal, ¶ms, rhiFlags)); |
| 127 |
} |
| 128 |
#endif |
| 129 |
|
| 130 |
if (!m_rhi) |
| 131 |
qFatal("Failed to create RHI backend"); |
| 132 |
|
| 133 |
m_sc.reset(m_rhi->newSwapChain()); |
| 134 |
m_ds.reset(m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, |
| 135 |
QSize(), // no need to set the size here, due to UsedWithSwapChainOnly |
| 136 |
1, |
| 137 |
QRhiRenderBuffer::UsedWithSwapChainOnly)); |
| 138 |
m_sc->setWindow(this); |
| 139 |
m_sc->setDepthStencil(m_ds.get()); |
| 140 |
m_rp.reset(m_sc->newCompatibleRenderPassDescriptor()); |
| 141 |
m_sc->setRenderPassDescriptor(m_rp.get()); |
| 142 |
|
| 143 |
customInit(); |
| 144 |
} |
| 145 |
|
| 146 |
void Window::resizeSwapChain() |
| 147 |
{ |
| 148 |
m_hasSwapChain = m_sc->createOrResize(); // also handles m_ds |
| 149 |
|
| 150 |
const QSize outputSize = m_sc->currentPixelSize(); |
| 151 |
m_proj = m_rhi->clipSpaceCorrMatrix(); |
| 152 |
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 153 |
m_proj.translate(0, 0, -4); |
| 154 |
} |
| 155 |
|
| 156 |
void Window::releaseSwapChain() |
| 157 |
{ |
| 158 |
if (m_hasSwapChain) { |
| 159 |
m_hasSwapChain = false; |
| 160 |
m_sc->destroy(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void Window::render() |
| 165 |
{ |
| 166 |
if (!m_hasSwapChain || m_notExposed) |
| 167 |
return; |
| 168 |
|
| 169 |
// If the window got resized or newly exposed, resize the swapchain. (the |
| 170 |
// newly-exposed case is not actually required by some platforms, but |
| 171 |
// f.ex. Vulkan on Windows seems to need it) |
| 172 |
// |
| 173 |
// This (exposeEvent + the logic here) is the only safe way to perform |
| 174 |
// resize handling. Note the usage of the RHI's surfacePixelSize(), and |
| 175 |
// never QWindow::size(). (the two may or may not be the same under the hood, |
| 176 |
// depending on the backend and platform) |
| 177 |
// |
| 178 |
if (m_sc->currentPixelSize() != m_sc->surfacePixelSize() || m_newlyExposed) { |
| 179 |
resizeSwapChain(); |
| 180 |
if (!m_hasSwapChain) |
| 181 |
return; |
| 182 |
m_newlyExposed = false; |
| 183 |
} |
| 184 |
|
| 185 |
QRhi::FrameOpResult r = m_rhi->beginFrame(m_sc.get()); |
| 186 |
if (r == QRhi::FrameOpSwapChainOutOfDate) { |
| 187 |
resizeSwapChain(); |
| 188 |
if (!m_hasSwapChain) |
| 189 |
return; |
| 190 |
r = m_rhi->beginFrame(m_sc.get()); |
| 191 |
} |
| 192 |
if (r != QRhi::FrameOpSuccess) { |
| 193 |
qDebug("beginFrame failed with %d, retry", r); |
| 194 |
requestUpdate(); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
customRender(); |
| 199 |
|
| 200 |
m_rhi->endFrame(m_sc.get()); |
| 201 |
|
| 202 |
// Always request the next frame via requestUpdate(). On some platforms this is backed |
| 203 |
// by a platform-specific solution, e.g. CVDisplayLink on macOS, which is potentially |
| 204 |
// more efficient than a timer, queued metacalls, etc. |
| 205 |
// |
| 206 |
// However, the rendering behavior is identical no matter how the next round of |
| 207 |
// rendering is triggered: the rendering thread is throttled to the presentation rate |
| 208 |
// (either in beginFrame() or endFrame()) so the triangle should rotate at the exact |
| 209 |
// same speed no matter which approach is taken here. |
| 210 |
|
| 211 |
#if 1 |
| 212 |
requestUpdate(); |
| 213 |
#else |
| 214 |
QTimer::singleShot(0, this, [this] { render(); }); |
| 215 |
#endif |
| 216 |
} |
| 217 |
|
| 218 |
void Window::customInit() |
| 219 |
{ |
| 220 |
} |
| 221 |
|
| 222 |
void Window::customRender() |
| 223 |
{ |
| 224 |
} |