| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 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 <QApplication> |
| 52 |
#include <QWidget> |
| 53 |
#include <QLabel> |
| 54 |
#include <QPlainTextEdit> |
| 55 |
#include <QPushButton> |
| 56 |
#include <QCheckBox> |
| 57 |
#include <QVBoxLayout> |
| 58 |
|
| 59 |
#include <QCommandLineParser> |
| 60 |
#include <QWindow> |
| 61 |
#include <QPlatformSurfaceEvent> |
| 62 |
#include <QElapsedTimer> |
| 63 |
|
| 64 |
#include <QtGui/private/qshader_p.h> |
| 65 |
#include <QFile> |
| 66 |
|
| 67 |
#ifndef QT_NO_OPENGL |
| 68 |
#include <QtGui/private/qrhigles2_p.h> |
| 69 |
#include <QOffscreenSurface> |
| 70 |
#endif |
| 71 |
|
| 72 |
#if QT_CONFIG(vulkan) |
| 73 |
#include <QLoggingCategory> |
| 74 |
#include <QtGui/private/qrhivulkan_p.h> |
| 75 |
#endif |
| 76 |
|
| 77 |
#ifdef Q_OS_WIN |
| 78 |
#include <QtGui/private/qrhid3d11_p.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 82 |
#include <QtGui/private/qrhimetal_p.h> |
| 83 |
#endif |
| 84 |
|
| 85 |
enum GraphicsApi |
| 86 |
{ |
| 87 |
OpenGL, |
| 88 |
Vulkan, |
| 89 |
D3D11, |
| 90 |
Metal |
| 91 |
}; |
| 92 |
|
| 93 |
static GraphicsApi graphicsApi; |
| 94 |
|
| 95 |
static QString graphicsApiName() |
| 96 |
{ |
| 97 |
switch (graphicsApi) { |
| 98 |
case OpenGL: |
| 99 |
return QLatin1String("OpenGL 2.x"); |
| 100 |
case Vulkan: |
| 101 |
return QLatin1String("Vulkan"); |
| 102 |
case D3D11: |
| 103 |
return QLatin1String("Direct3D 11"); |
| 104 |
case Metal: |
| 105 |
return QLatin1String("Metal"); |
| 106 |
default: |
| 107 |
break; |
| 108 |
} |
| 109 |
return QString(); |
| 110 |
} |
| 111 |
|
| 112 |
static struct { |
| 113 |
#if QT_CONFIG(vulkan) |
| 114 |
QVulkanInstance *instance = nullptr; |
| 115 |
#endif |
| 116 |
QRhi *r = nullptr; |
| 117 |
#ifndef QT_NO_OPENGL |
| 118 |
QOffscreenSurface *fallbackSurface = nullptr; |
| 119 |
#endif |
| 120 |
} r; |
| 121 |
|
| 122 |
void createRhi() |
| 123 |
{ |
| 124 |
#ifndef QT_NO_OPENGL |
| 125 |
if (graphicsApi == OpenGL) { |
| 126 |
r.fallbackSurface = QRhiGles2InitParams::newFallbackSurface(); |
| 127 |
QRhiGles2InitParams params; |
| 128 |
params.fallbackSurface = r.fallbackSurface; |
| 129 |
//params.window = this; |
| 130 |
r.r = QRhi::create(QRhi::OpenGLES2, ¶ms); |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
#if QT_CONFIG(vulkan) |
| 135 |
if (graphicsApi == Vulkan) { |
| 136 |
QRhiVulkanInitParams params; |
| 137 |
params.inst = r.instance; |
| 138 |
//params.window = this; |
| 139 |
r.r = QRhi::create(QRhi::Vulkan, ¶ms); |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
#ifdef Q_OS_WIN |
| 144 |
if (graphicsApi == D3D11) { |
| 145 |
QRhiD3D11InitParams params; |
| 146 |
params.enableDebugLayer = true; |
| 147 |
r.r = QRhi::create(QRhi::D3D11, ¶ms); |
| 148 |
} |
| 149 |
#endif |
| 150 |
|
| 151 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 152 |
if (graphicsApi == Metal) { |
| 153 |
QRhiMetalInitParams params; |
| 154 |
r.r = QRhi::create(QRhi::Metal, ¶ms); |
| 155 |
} |
| 156 |
#endif |
| 157 |
|
| 158 |
if (!r.r) |
| 159 |
qFatal("Failed to create RHI backend"); |
| 160 |
} |
| 161 |
|
| 162 |
void destroyRhi() |
| 163 |
{ |
| 164 |
delete r.r; |
| 165 |
|
| 166 |
#ifndef QT_NO_OPENGL |
| 167 |
delete r.fallbackSurface; |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
struct { |
| 172 |
QVector<QWindow *> windows; |
| 173 |
|
| 174 |
QRhiBuffer *vbuf = nullptr; |
| 175 |
QRhiBuffer *ubuf = nullptr; |
| 176 |
QRhiShaderResourceBindings *srb = nullptr; |
| 177 |
QRhiGraphicsPipeline *ps = nullptr; |
| 178 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 179 |
} d; |
| 180 |
|
| 181 |
static float vertexData[] = { |
| 182 |
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, |
| 183 |
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, |
| 184 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, |
| 185 |
}; |
| 186 |
|
| 187 |
static QShader getShader(const QString &name) |
| 188 |
{ |
| 189 |
QFile f(name); |
| 190 |
if (f.open(QIODevice::ReadOnly)) |
| 191 |
return QShader::fromSerialized(f.readAll()); |
| 192 |
|
| 193 |
return QShader(); |
| 194 |
} |
| 195 |
|
| 196 |
// can use just one rpd from whichever window comes first since they are |
| 197 |
// actually compatible due to all windows using the same config (have |
| 198 |
// depth-stencil, sample count 1, same format). this means the same pso can be |
| 199 |
// reused too. |
| 200 |
void ensureSharedResources(QRhiRenderPassDescriptor *rp) |
| 201 |
{ |
| 202 |
if (!d.vbuf) { |
| 203 |
d.vbuf = r.r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); |
| 204 |
d.vbuf->build(); |
| 205 |
d.initialUpdates = r.r->nextResourceUpdateBatch(); |
| 206 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, vertexData); |
| 207 |
} |
| 208 |
|
| 209 |
if (!d.ubuf) { |
| 210 |
d.ubuf = r.r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 211 |
d.ubuf->build(); |
| 212 |
} |
| 213 |
|
| 214 |
if (!d.srb) { |
| 215 |
d.srb = r.r->newShaderResourceBindings(); |
| 216 |
d.srb->setBindings({ |
| 217 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf) |
| 218 |
}); |
| 219 |
d.srb->build(); |
| 220 |
} |
| 221 |
|
| 222 |
if (!d.ps) { |
| 223 |
d.ps = r.r->newGraphicsPipeline(); |
| 224 |
|
| 225 |
QRhiGraphicsPipeline::TargetBlend premulAlphaBlend; |
| 226 |
premulAlphaBlend.enable = true; |
| 227 |
d.ps->setTargetBlends({ premulAlphaBlend }); |
| 228 |
|
| 229 |
const QShader vs = getShader(QLatin1String(":/color.vert.qsb")); |
| 230 |
if (!vs.isValid()) |
| 231 |
qFatal("Failed to load shader pack (vertex)"); |
| 232 |
const QShader fs = getShader(QLatin1String(":/color.frag.qsb")); |
| 233 |
if (!fs.isValid()) |
| 234 |
qFatal("Failed to load shader pack (fragment)"); |
| 235 |
|
| 236 |
d.ps->setShaderStages({ |
| 237 |
{ QRhiShaderStage::Vertex, vs }, |
| 238 |
{ QRhiShaderStage::Fragment, fs } |
| 239 |
}); |
| 240 |
|
| 241 |
QRhiVertexInputLayout inputLayout; |
| 242 |
inputLayout.setBindings({ |
| 243 |
{ 5 * sizeof(float) } |
| 244 |
}); |
| 245 |
inputLayout.setAttributes({ |
| 246 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 247 |
{ 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) } |
| 248 |
}); |
| 249 |
|
| 250 |
d.ps->setVertexInputLayout(inputLayout); |
| 251 |
d.ps->setShaderResourceBindings(d.srb); |
| 252 |
d.ps->setRenderPassDescriptor(rp); |
| 253 |
|
| 254 |
d.ps->build(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void destroySharedResources() |
| 259 |
{ |
| 260 |
delete d.ps; |
| 261 |
d.ps = nullptr; |
| 262 |
|
| 263 |
delete d.srb; |
| 264 |
d.srb = nullptr; |
| 265 |
|
| 266 |
delete d.vbuf; |
| 267 |
d.vbuf = nullptr; |
| 268 |
|
| 269 |
delete d.ubuf; |
| 270 |
d.ubuf = nullptr; |
| 271 |
} |
| 272 |
|
| 273 |
class Window : public QWindow |
| 274 |
{ |
| 275 |
public: |
| 276 |
Window(const QString &title, const QColor &bgColor, int axis, bool noVSync); |
| 277 |
~Window(); |
| 278 |
|
| 279 |
protected: |
| 280 |
void init(); |
| 281 |
void releaseResources(); |
| 282 |
void resizeSwapChain(); |
| 283 |
void releaseSwapChain(); |
| 284 |
void render(); |
| 285 |
|
| 286 |
void exposeEvent(QExposeEvent *) override; |
| 287 |
bool event(QEvent *) override; |
| 288 |
|
| 289 |
QColor m_bgColor; |
| 290 |
int m_rotationAxis; |
| 291 |
bool m_noVSync; |
| 292 |
|
| 293 |
bool m_running = false; |
| 294 |
bool m_notExposed = false; |
| 295 |
bool m_newlyExposed = false; |
| 296 |
|
| 297 |
QMatrix4x4 m_proj; |
| 298 |
QVector<QRhiResource *> m_releasePool; |
| 299 |
|
| 300 |
bool m_hasSwapChain = false; |
| 301 |
QRhiSwapChain *m_sc = nullptr; |
| 302 |
QRhiRenderBuffer *m_ds = nullptr; |
| 303 |
QRhiRenderPassDescriptor *m_rp = nullptr; |
| 304 |
|
| 305 |
float m_rotation = 0; |
| 306 |
float m_opacity = 1; |
| 307 |
int m_opacityDir = -1; |
| 308 |
}; |
| 309 |
|
| 310 |
Window::Window(const QString &title, const QColor &bgColor, int axis, bool noVSync) |
| 311 |
: m_bgColor(bgColor), |
| 312 |
m_rotationAxis(axis), |
| 313 |
m_noVSync(noVSync) |
| 314 |
{ |
| 315 |
switch (graphicsApi) { |
| 316 |
case OpenGL: |
| 317 |
{ |
| 318 |
#if QT_CONFIG(opengl) |
| 319 |
setSurfaceType(OpenGLSurface); |
| 320 |
QSurfaceFormat fmt = QRhiGles2InitParams::adjustedFormat(); // default + depth/stencil |
| 321 |
fmt.setSwapInterval(noVSync ? 0 : 1); // + swap interval |
| 322 |
setFormat(fmt); |
| 323 |
#endif |
| 324 |
} |
| 325 |
break; |
| 326 |
case Vulkan: |
| 327 |
#if QT_CONFIG(vulkan) |
| 328 |
setSurfaceType(VulkanSurface); |
| 329 |
setVulkanInstance(r.instance); |
| 330 |
#endif |
| 331 |
break; |
| 332 |
case D3D11: |
| 333 |
setSurfaceType(OpenGLSurface); // not a typo |
| 334 |
break; |
| 335 |
case Metal: |
| 336 |
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) |
| 337 |
setSurfaceType(MetalSurface); |
| 338 |
#endif |
| 339 |
break; |
| 340 |
default: |
| 341 |
break; |
| 342 |
} |
| 343 |
|
| 344 |
resize(800, 600); |
| 345 |
setTitle(title); |
| 346 |
} |
| 347 |
|
| 348 |
Window::~Window() |
| 349 |
{ |
| 350 |
releaseResources(); |
| 351 |
} |
| 352 |
|
| 353 |
void Window::exposeEvent(QExposeEvent *) |
| 354 |
{ |
| 355 |
// initialize and start rendering when the window becomes usable for graphics purposes |
| 356 |
if (isExposed() && !m_running) { |
| 357 |
m_running = true; |
| 358 |
init(); |
| 359 |
resizeSwapChain(); |
| 360 |
} |
| 361 |
|
| 362 |
// stop pushing frames when not exposed (or size is 0) |
| 363 |
if ((!isExposed() || (m_hasSwapChain && m_sc->surfacePixelSize().isEmpty())) && m_running) |
| 364 |
m_notExposed = true; |
| 365 |
|
| 366 |
// continue when exposed again and the surface has a valid size. |
| 367 |
// note that the surface size can be (0, 0) even though size() reports a valid one... |
| 368 |
if (isExposed() && m_running && m_notExposed && !m_sc->surfacePixelSize().isEmpty()) { |
| 369 |
m_notExposed = false; |
| 370 |
m_newlyExposed = true; |
| 371 |
} |
| 372 |
|
| 373 |
// always render a frame on exposeEvent() (when exposed) in order to update |
| 374 |
// immediately on window resize. |
| 375 |
if (isExposed() && !m_sc->surfacePixelSize().isEmpty()) |
| 376 |
render(); |
| 377 |
} |
| 378 |
|
| 379 |
bool Window::event(QEvent *e) |
| 380 |
{ |
| 381 |
switch (e->type()) { |
| 382 |
case QEvent::UpdateRequest: |
| 383 |
render(); |
| 384 |
break; |
| 385 |
|
| 386 |
case QEvent::PlatformSurface: |
| 387 |
// this is the proper time to tear down the swapchain (while the native window and surface are still around) |
| 388 |
if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) |
| 389 |
releaseSwapChain(); |
| 390 |
break; |
| 391 |
|
| 392 |
default: |
| 393 |
break; |
| 394 |
} |
| 395 |
|
| 396 |
return QWindow::event(e); |
| 397 |
} |
| 398 |
|
| 399 |
void Window::init() |
| 400 |
{ |
| 401 |
m_sc = r.r->newSwapChain(); |
| 402 |
m_ds = r.r->newRenderBuffer(QRhiRenderBuffer::DepthStencil, |
| 403 |
QSize(), |
| 404 |
1, |
| 405 |
QRhiRenderBuffer::UsedWithSwapChainOnly); |
| 406 |
m_releasePool << m_ds; |
| 407 |
m_sc->setWindow(this); |
| 408 |
m_sc->setDepthStencil(m_ds); |
| 409 |
if (m_noVSync) |
| 410 |
m_sc->setFlags(QRhiSwapChain::NoVSync); |
| 411 |
|
| 412 |
m_rp = m_sc->newCompatibleRenderPassDescriptor(); |
| 413 |
m_releasePool << m_rp; |
| 414 |
m_sc->setRenderPassDescriptor(m_rp); |
| 415 |
|
| 416 |
ensureSharedResources(m_rp); |
| 417 |
} |
| 418 |
|
| 419 |
void Window::releaseResources() |
| 420 |
{ |
| 421 |
qDeleteAll(m_releasePool); |
| 422 |
m_releasePool.clear(); |
| 423 |
|
| 424 |
delete m_sc; |
| 425 |
m_sc = nullptr; |
| 426 |
} |
| 427 |
|
| 428 |
void Window::resizeSwapChain() |
| 429 |
{ |
| 430 |
m_hasSwapChain = m_sc->buildOrResize(); |
| 431 |
|
| 432 |
const QSize outputSize = m_sc->currentPixelSize(); |
| 433 |
m_proj = r.r->clipSpaceCorrMatrix(); |
| 434 |
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 435 |
m_proj.translate(0, 0, -4); |
| 436 |
} |
| 437 |
|
| 438 |
void Window::releaseSwapChain() |
| 439 |
{ |
| 440 |
if (m_hasSwapChain) { |
| 441 |
m_hasSwapChain = false; |
| 442 |
m_sc->release(); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
void Window::render() |
| 447 |
{ |
| 448 |
if (!m_hasSwapChain || m_notExposed) |
| 449 |
return; |
| 450 |
|
| 451 |
// If the window got resized or got newly exposed, resize the swapchain. |
| 452 |
// (the newly-exposed case is not actually required by some |
| 453 |
// platforms/backends, but f.ex. Vulkan on Windows seems to need it) |
| 454 |
if (m_sc->currentPixelSize() != m_sc->surfacePixelSize() || m_newlyExposed) { |
| 455 |
resizeSwapChain(); |
| 456 |
if (!m_hasSwapChain) |
| 457 |
return; |
| 458 |
m_newlyExposed = false; |
| 459 |
} |
| 460 |
|
| 461 |
QRhi::FrameOpResult result = r.r->beginFrame(m_sc); |
| 462 |
if (result == QRhi::FrameOpSwapChainOutOfDate) { |
| 463 |
resizeSwapChain(); |
| 464 |
if (!m_hasSwapChain) |
| 465 |
return; |
| 466 |
result = r.r->beginFrame(m_sc); |
| 467 |
} |
| 468 |
if (result != QRhi::FrameOpSuccess) { |
| 469 |
requestUpdate(); |
| 470 |
return; |
| 471 |
} |
| 472 |
|
| 473 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 474 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 475 |
|
| 476 |
QRhiResourceUpdateBatch *u = r.r->nextResourceUpdateBatch(); |
| 477 |
if (d.initialUpdates) { |
| 478 |
u->merge(d.initialUpdates); |
| 479 |
d.initialUpdates->release(); |
| 480 |
d.initialUpdates = nullptr; |
| 481 |
} |
| 482 |
|
| 483 |
m_rotation += 1.0f; |
| 484 |
QMatrix4x4 mvp = m_proj; |
| 485 |
mvp.rotate(m_rotation, m_rotationAxis == 0 ? 1 : 0, m_rotationAxis == 1 ? 1 : 0, m_rotationAxis == 2 ? 1 : 0); |
| 486 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 487 |
m_opacity += m_opacityDir * 0.005f; |
| 488 |
if (m_opacity < 0.0f || m_opacity > 1.0f) { |
| 489 |
m_opacityDir *= -1; |
| 490 |
m_opacity = qBound(0.0f, m_opacity, 1.0f); |
| 491 |
} |
| 492 |
u->updateDynamicBuffer(d.ubuf, 64, 4, &m_opacity); |
| 493 |
|
| 494 |
cb->beginPass(m_sc->currentFrameRenderTarget(), |
| 495 |
QColor::fromRgbF(float(m_bgColor.redF()), float(m_bgColor.greenF()), float(m_bgColor.blueF()), 1.0f), |
| 496 |
{ 1.0f, 0 }, |
| 497 |
u); |
| 498 |
|
| 499 |
cb->setGraphicsPipeline(d.ps); |
| 500 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 501 |
cb->setShaderResources(); |
| 502 |
const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 503 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 504 |
cb->draw(3); |
| 505 |
|
| 506 |
cb->endPass(); |
| 507 |
|
| 508 |
r.r->endFrame(m_sc); |
| 509 |
|
| 510 |
requestUpdate(); |
| 511 |
} |
| 512 |
|
| 513 |
void createWindow(bool noVSync) |
| 514 |
{ |
| 515 |
static QColor colors[] = { Qt::red, Qt::green, Qt::blue, Qt::yellow, Qt::cyan, Qt::gray }; |
| 516 |
const int n = d.windows.count(); |
| 517 |
d.windows.append(new Window(QString::asprintf("Window #%d%s", n, noVSync ? " (no vsync)" : ""), colors[n % 6], n % 3, noVSync)); |
| 518 |
d.windows.last()->show(); |
| 519 |
} |
| 520 |
|
| 521 |
void closeWindow() |
| 522 |
{ |
| 523 |
delete d.windows.takeLast(); |
| 524 |
} |
| 525 |
|
| 526 |
int main(int argc, char **argv) |
| 527 |
{ |
| 528 |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); |
| 529 |
QApplication app(argc, argv); |
| 530 |
|
| 531 |
#if defined(Q_OS_WIN) |
| 532 |
graphicsApi = D3D11; |
| 533 |
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 534 |
graphicsApi = Metal; |
| 535 |
#elif QT_CONFIG(vulkan) |
| 536 |
graphicsApi = Vulkan; |
| 537 |
#else |
| 538 |
graphicsApi = OpenGL; |
| 539 |
#endif |
| 540 |
|
| 541 |
QCommandLineParser cmdLineParser; |
| 542 |
cmdLineParser.addHelpOption(); |
| 543 |
QCommandLineOption glOption({ "g", "opengl" }, QLatin1String("OpenGL (2.x)")); |
| 544 |
cmdLineParser.addOption(glOption); |
| 545 |
QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan")); |
| 546 |
cmdLineParser.addOption(vkOption); |
| 547 |
QCommandLineOption d3dOption({ "d", "d3d11" }, QLatin1String("Direct3D 11")); |
| 548 |
cmdLineParser.addOption(d3dOption); |
| 549 |
QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal")); |
| 550 |
cmdLineParser.addOption(mtlOption); |
| 551 |
cmdLineParser.process(app); |
| 552 |
if (cmdLineParser.isSet(glOption)) |
| 553 |
graphicsApi = OpenGL; |
| 554 |
if (cmdLineParser.isSet(vkOption)) |
| 555 |
graphicsApi = Vulkan; |
| 556 |
if (cmdLineParser.isSet(d3dOption)) |
| 557 |
graphicsApi = D3D11; |
| 558 |
if (cmdLineParser.isSet(mtlOption)) |
| 559 |
graphicsApi = Metal; |
| 560 |
|
| 561 |
qDebug("Selected graphics API is %s", qPrintable(graphicsApiName())); |
| 562 |
qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText())); |
| 563 |
|
| 564 |
#if QT_CONFIG(vulkan) |
| 565 |
r.instance = new QVulkanInstance; |
| 566 |
if (graphicsApi == Vulkan) { |
| 567 |
#ifndef Q_OS_ANDROID |
| 568 |
r.instance->setLayers({ "VK_LAYER_LUNARG_standard_validation" }); |
| 569 |
#else |
| 570 |
r.instance->setLayers(QByteArrayList() |
| 571 |
<< "VK_LAYER_GOOGLE_threading" |
| 572 |
<< "VK_LAYER_LUNARG_parameter_validation" |
| 573 |
<< "VK_LAYER_LUNARG_object_tracker" |
| 574 |
<< "VK_LAYER_LUNARG_core_validation" |
| 575 |
<< "VK_LAYER_LUNARG_image" |
| 576 |
<< "VK_LAYER_LUNARG_swapchain" |
| 577 |
<< "VK_LAYER_GOOGLE_unique_objects"); |
| 578 |
#endif |
| 579 |
if (!r.instance->create()) { |
| 580 |
qWarning("Failed to create Vulkan instance, switching to OpenGL"); |
| 581 |
graphicsApi = OpenGL; |
| 582 |
} |
| 583 |
} |
| 584 |
#endif |
| 585 |
|
| 586 |
createRhi(); |
| 587 |
|
| 588 |
int winCount = 0; |
| 589 |
QWidget w; |
| 590 |
w.resize(800, 600); |
| 591 |
w.setWindowTitle(QCoreApplication::applicationName() + QLatin1String(" - ") + graphicsApiName()); |
| 592 |
QVBoxLayout *layout = new QVBoxLayout(&w); |
| 593 |
|
| 594 |
QPlainTextEdit *info = new QPlainTextEdit( |
| 595 |
QLatin1String("This application tests rendering with the same QRhi instance (and so the same Vulkan/Metal/D3D device or OpenGL context) " |
| 596 |
"to multiple windows via multiple QRhiSwapChain objects, from the same one thread. Some resources are shared across all windows.\n" |
| 597 |
"\nNote that the behavior may differ depending on the underlying graphics API implementation and the number of windows. " |
| 598 |
"One challenge here is the vsync throttling: with the default vsync/fifo presentation mode the behavior may differ between " |
| 599 |
"platforms, drivers, and APIs as we present different swapchains' images in a row on the same thread. As a potential solution, " |
| 600 |
"setting NoVSync on the second, third, and later window swapchains is offered as an option.\n" |
| 601 |
"\n\nUsing API: ") + graphicsApiName()); |
| 602 |
info->setReadOnly(true); |
| 603 |
layout->addWidget(info); |
| 604 |
QLabel *label = new QLabel(QLatin1String("Window count: 0")); |
| 605 |
layout->addWidget(label); |
| 606 |
QCheckBox *vsCb = new QCheckBox(QLatin1String("Set NoVSync on all swapchains except the first")); |
| 607 |
vsCb->setChecked(false); |
| 608 |
layout->addWidget(vsCb); |
| 609 |
QPushButton *btn = new QPushButton(QLatin1String("New window")); |
| 610 |
QObject::connect(btn, &QPushButton::clicked, btn, [label, vsCb, &winCount] { |
| 611 |
winCount += 1; |
| 612 |
label->setText(QString::asprintf("Window count: %d", winCount)); |
| 613 |
const bool noVSync = vsCb->isChecked() && winCount > 1; |
| 614 |
createWindow(noVSync); |
| 615 |
}); |
| 616 |
layout->addWidget(btn); |
| 617 |
btn = new QPushButton(QLatin1String("Close window")); |
| 618 |
QObject::connect(btn, &QPushButton::clicked, btn, [label, &winCount] { |
| 619 |
if (winCount > 0) { |
| 620 |
winCount -= 1; |
| 621 |
label->setText(QString::asprintf("Window count: %d", winCount)); |
| 622 |
closeWindow(); |
| 623 |
} |
| 624 |
}); |
| 625 |
layout->addWidget(btn); |
| 626 |
w.show(); |
| 627 |
|
| 628 |
int result = app.exec(); |
| 629 |
|
| 630 |
qDeleteAll(d.windows); |
| 631 |
|
| 632 |
destroySharedResources(); |
| 633 |
destroyRhi(); |
| 634 |
|
| 635 |
#if QT_CONFIG(vulkan) |
| 636 |
delete r.instance; |
| 637 |
#endif |
| 638 |
|
| 639 |
return result; |
| 640 |
} |