From 964a5682905f0f07a2fd5a45cf6ccdc49ac983ea Mon Sep 17 00:00:00 2001 From: "Matias N. Goldberg" Date: Sat, 24 Sep 2022 17:50:02 -0300 Subject: [PATCH] Add CLI to switch to Vulkan/Metal Signed-off-by: Matias N. Goldberg --- include/gz/gui/Application.hh | 5 +++- src/Application.cc | 56 ++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/include/gz/gui/Application.hh b/include/gz/gui/Application.hh index c5e73f9a5..edabeee15 100644 --- a/include/gz/gui/Application.hh +++ b/include/gz/gui/Application.hh @@ -70,8 +70,11 @@ namespace gz /// \param[in] _argc Argument count. /// \param[in] _argv Argument values. /// \param[in] _type Window type, by default it's a main window. + /// \param[in] _renderEngineGuiApiBackend --render-engine-gui-api-backend + /// option public: Application(int &_argc, char **_argv, - const WindowType _type = WindowType::kMainWindow); + const WindowType _type = WindowType::kMainWindow, + const char *_renderEngineGuiApiBackend = nullptr); /// \brief Destructor public: virtual ~Application(); diff --git a/src/Application.cc b/src/Application.cc index 1c5265daa..3568d5ab3 100644 --- a/src/Application.cc +++ b/src/Application.cc @@ -89,9 +89,18 @@ namespace gz using namespace gz; using namespace gui; +enum class GZ_COMMON_HIDDEN AvailableAPIs +{ + OpenGL, + Vulkan, + Metal +}; + ///////////////////////////////////////////////// -Application::Application(int &_argc, char **_argv, const WindowType _type) - : QApplication(_argc, _argv), dataPtr(new ApplicationPrivate) +Application::Application(int &_argc, char **_argv, const WindowType _type, + const char *_renderEngineGuiApiBackend) : + QApplication(_argc, _argv), + dataPtr(new ApplicationPrivate) { gzdbg << "Initializing application." << std::endl; @@ -99,10 +108,29 @@ Application::Application(int &_argc, char **_argv, const WindowType _type) this->setOrganizationDomain("gazebosim.org"); this->setApplicationName("Gazebo GUI"); -#if __APPLE__ - // Use the Metal graphics API on macOS. - gzdbg << "Qt using Metal graphics interface" << std::endl; - QQuickWindow::setSceneGraphBackend(QSGRendererInterface::MetalRhi); +#ifdef __APPLE__ + AvailableAPIs api = AvailableAPIs::Metal; +#else + AvailableAPIs api = AvailableAPIs::OpenGL; +#endif + if (_renderEngineGuiApiBackend) + { + const std::string renderEngineGuiApiBackend = _renderEngineGuiApiBackend; + if (renderEngineGuiApiBackend == "vulkan") + api = AvailableAPIs::Vulkan; +#ifdef __APPLE__ + if (renderEngineGuiApiBackend == "metal") + api = AvailableAPIs::Metal; +#endif + } + +#ifdef __APPLE__ + if (api == AvailableAPIs::Metal) + { + // Use the Metal graphics API on macOS. + gzdbg << "Qt using Metal graphics interface" << std::endl; + QQuickWindow::setSceneGraphBackend(QSGRendererInterface::MetalRhi); + } // TODO(srmainwaring): implement facility for overriding the default // graphics API in macOS, in which case there are restrictions on @@ -120,9 +148,7 @@ Application::Application(int &_argc, char **_argv, const WindowType _type) #else // Otherwise use OpenGL (or Vulkan when supported and requested) - const bool useVulkan = true; // TODO(anyone) - - if (useVulkan) + if (api == AvailableAPIs::Vulkan) { gzdbg << "Qt using Vulkan graphics interface" << std::endl; @@ -180,10 +206,20 @@ Application::Application(int &_argc, char **_argv, const WindowType _type) } else { - if (useVulkan) + switch (api) { + case AvailableAPIs::OpenGL: + this->dataPtr->mainWin->setProperty("renderEngineBackendApiName", + "opengl"); + break; + case AvailableAPIs::Vulkan: this->dataPtr->mainWin->setProperty("renderEngineBackendApiName", "vulkan"); + break; + case AvailableAPIs::Metal: + this->dataPtr->mainWin->setProperty("renderEngineBackendApiName", + "metal"); + break; } } }