Skip to content

Commit

Permalink
Add CLI to switch to Vulkan/Metal
Browse files Browse the repository at this point in the history
Signed-off-by: Matias N. Goldberg <[email protected]>
  • Loading branch information
darksylinc committed Dec 11, 2022
1 parent 1286165 commit 964a568
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
5 changes: 4 additions & 1 deletion include/gz/gui/Application.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
56 changes: 46 additions & 10 deletions src/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,48 @@ 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;

this->setOrganizationName("Gazebo");
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
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 964a568

Please sign in to comment.