Add configure feature for Metal

Simplifies maintenance of code paths that rely on Metal.

Change-Id: I1d1f705fffc14dbafde346eeb555b43be6d5be54
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Tor Arne Vestbø 2024-03-18 21:55:12 +01:00
parent e8e029e2a5
commit 14ec2ab89f
23 changed files with 39 additions and 34 deletions

View File

@ -14,7 +14,7 @@ int main(int argc, char **argv)
// Use platform-specific defaults when no command-line arguments given. // Use platform-specific defaults when no command-line arguments given.
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = QRhi::D3D11; graphicsApi = QRhi::D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = QRhi::Metal; graphicsApi = QRhi::Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = QRhi::Vulkan; graphicsApi = QRhi::Vulkan;

View File

@ -146,7 +146,7 @@ void RhiWindow::init()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (m_graphicsApi == QRhi::Metal) { if (m_graphicsApi == QRhi::Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
m_rhi.reset(QRhi::create(QRhi::Metal, &params)); m_rhi.reset(QRhi::create(QRhi::Metal, &params));

View File

@ -1006,7 +1006,7 @@ qt_internal_extend_target(Gui CONDITION QT_FEATURE_xkbcommon AND UNIX
XKB::XKB XKB::XKB
) )
qt_internal_extend_target(Gui CONDITION IOS OR MACOS qt_internal_extend_target(Gui CONDITION QT_FEATURE_metal
SOURCES SOURCES
rhi/qrhimetal.mm rhi/qrhimetal_p.h rhi/qrhimetal.mm rhi/qrhimetal_p.h
PUBLIC_LIBRARIES PUBLIC_LIBRARIES

View File

@ -811,6 +811,10 @@ qt_feature("vulkan" PUBLIC
LABEL "Vulkan" LABEL "Vulkan"
CONDITION QT_FEATURE_library AND QT_FEATURE_vkgen AND WrapVulkanHeaders_FOUND CONDITION QT_FEATURE_library AND QT_FEATURE_vkgen AND WrapVulkanHeaders_FOUND
) )
qt_feature("metal" PUBLIC
LABEL "Metal"
CONDITION MACOS OR IOS
)
qt_feature("vkkhrdisplay" PRIVATE qt_feature("vkkhrdisplay" PRIVATE
SECTION "Platform plugins" SECTION "Platform plugins"
LABEL "VK_KHR_display" LABEL "VK_KHR_display"
@ -1288,6 +1292,7 @@ qt_configure_add_summary_entry(ARGS "opengles31")
qt_configure_add_summary_entry(ARGS "opengles32") qt_configure_add_summary_entry(ARGS "opengles32")
qt_configure_end_summary_section() # end of "OpenGL" section qt_configure_end_summary_section() # end of "OpenGL" section
qt_configure_add_summary_entry(ARGS "vulkan") qt_configure_add_summary_entry(ARGS "vulkan")
qt_configure_add_summary_entry(ARGS "metal")
qt_configure_add_summary_entry(ARGS "graphicsframecapture") qt_configure_add_summary_entry(ARGS "graphicsframecapture")
qt_configure_add_summary_entry(ARGS "sessionmanager") qt_configure_add_summary_entry(ARGS "sessionmanager")
qt_configure_end_summary_section() # end of "Qt Gui" section qt_configure_end_summary_section() # end of "Qt Gui" section

View File

@ -18,7 +18,7 @@ int main(int argc, char **argv)
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
QRhiD3D12InitParams params; QRhiD3D12InitParams params;
rhi.reset(QRhi::create(QRhi::D3D12, &params)); rhi.reset(QRhi::create(QRhi::D3D12, &params));
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
QRhiMetalInitParams params; QRhiMetalInitParams params;
rhi.reset(QRhi::create(QRhi::Metal, &params)); rhi.reset(QRhi::create(QRhi::Metal, &params));
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)

View File

@ -107,7 +107,7 @@ bool QBackingStoreRhiSupport::create()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (!rhi && m_config.api() == QPlatformBackingStoreRhiConfig::Metal) { if (!rhi && m_config.api() == QPlatformBackingStoreRhiConfig::Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
// For parity with Qt Quick, fall back to OpenGL when there is no Metal (f.ex. in macOS virtual machines). // For parity with Qt Quick, fall back to OpenGL when there is no Metal (f.ex. in macOS virtual machines).
@ -271,7 +271,7 @@ bool QBackingStoreRhiSupport::checkForceRhi(QPlatformBackingStoreRhiConfig *outC
if (config.isEnabled()) { if (config.isEnabled()) {
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
config.setApi(QPlatformBackingStoreRhiConfig::D3D11); config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
config.setApi(QPlatformBackingStoreRhiConfig::Metal); config.setApi(QPlatformBackingStoreRhiConfig::Metal);
#elif QT_CONFIG(opengl) #elif QT_CONFIG(opengl)
config.setApi(QPlatformBackingStoreRhiConfig::OpenGL); config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
@ -291,7 +291,7 @@ bool QBackingStoreRhiSupport::checkForceRhi(QPlatformBackingStoreRhiConfig *outC
if (backend == QStringLiteral("d3d12")) if (backend == QStringLiteral("d3d12"))
config.setApi(QPlatformBackingStoreRhiConfig::D3D12); config.setApi(QPlatformBackingStoreRhiConfig::D3D12);
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (backend == QStringLiteral("metal")) if (backend == QStringLiteral("metal"))
config.setApi(QPlatformBackingStoreRhiConfig::Metal); config.setApi(QPlatformBackingStoreRhiConfig::Metal);
#endif #endif

View File

@ -16,7 +16,7 @@
#include "qrhid3d11_p.h" #include "qrhid3d11_p.h"
#include "qrhid3d12_p.h" #include "qrhid3d12_p.h"
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
#include "qrhimetal_p.h" #include "qrhimetal_p.h"
#endif #endif
@ -8477,7 +8477,7 @@ QRhi *QRhi::create(Implementation impl, QRhiInitParams *params, Flags flags, QRh
break; break;
#endif #endif
case Metal: case Metal:
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
r->d = new QRhiMetal(static_cast<QRhiMetalInitParams *>(params), r->d = new QRhiMetal(static_cast<QRhiMetalInitParams *>(params),
static_cast<QRhiMetalNativeHandles *>(importDevice)); static_cast<QRhiMetalNativeHandles *>(importDevice));
break; break;
@ -8536,7 +8536,7 @@ bool QRhi::probe(QRhi::Implementation impl, QRhiInitParams *params)
// create() and then drop the result. // create() and then drop the result.
if (impl == Metal) { if (impl == Metal) {
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
ok = QRhiMetal::probe(static_cast<QRhiMetalInitParams *>(params)); ok = QRhiMetal::probe(static_cast<QRhiMetalInitParams *>(params));
#endif #endif
} else { } else {
@ -8587,7 +8587,7 @@ bool QRhi::probe(QRhi::Implementation impl, QRhiInitParams *params)
*/ */
QRhiSwapChainProxyData QRhi::updateSwapChainProxyData(QRhi::Implementation impl, QWindow *window) QRhiSwapChainProxyData QRhi::updateSwapChainProxyData(QRhi::Implementation impl, QWindow *window)
{ {
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (impl == Metal) if (impl == Metal)
return QRhiMetal::updateSwapChainProxyData(window); return QRhiMetal::updateSwapChainProxyData(window);
#else #else

View File

@ -23,7 +23,7 @@
#include <QtGui/qvulkaninstance.h> #include <QtGui/qvulkaninstance.h>
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) || defined(Q_QDOC) #if QT_CONFIG(metal) || defined(Q_QDOC)
Q_FORWARD_DECLARE_OBJC_CLASS(MTLDevice); Q_FORWARD_DECLARE_OBJC_CLASS(MTLDevice);
Q_FORWARD_DECLARE_OBJC_CLASS(MTLCommandQueue); Q_FORWARD_DECLARE_OBJC_CLASS(MTLCommandQueue);
Q_FORWARD_DECLARE_OBJC_CLASS(MTLCommandBuffer); Q_FORWARD_DECLARE_OBJC_CLASS(MTLCommandBuffer);
@ -150,7 +150,7 @@ struct Q_GUI_EXPORT QRhiD3D12CommandBufferNativeHandles : public QRhiNativeHandl
#endif // WIN/QDOC #endif // WIN/QDOC
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) || defined(Q_QDOC) #if QT_CONFIG(metal) || defined(Q_QDOC)
struct Q_GUI_EXPORT QRhiMetalInitParams : public QRhiInitParams struct Q_GUI_EXPORT QRhiMetalInitParams : public QRhiInitParams
{ {

View File

@ -4,7 +4,7 @@
#include "qgraphicsframecapture_p.h" #include "qgraphicsframecapture_p.h"
#if (defined (Q_OS_WIN) || defined(Q_OS_LINUX)) && QT_CONFIG(library) #if (defined (Q_OS_WIN) || defined(Q_OS_LINUX)) && QT_CONFIG(library)
#include "qgraphicsframecapturerenderdoc_p_p.h" #include "qgraphicsframecapturerenderdoc_p_p.h"
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
#include "qgraphicsframecapturemetal_p_p.h" #include "qgraphicsframecapturemetal_p_p.h"
#else #else
#include "qgraphicsframecapture_p_p.h" #include "qgraphicsframecapture_p_p.h"
@ -28,7 +28,7 @@ QGraphicsFrameCapture::QGraphicsFrameCapture()
{ {
#if (defined (Q_OS_WIN) || defined(Q_OS_LINUX)) && QT_CONFIG(library) #if (defined (Q_OS_WIN) || defined(Q_OS_LINUX)) && QT_CONFIG(library)
d.reset(new QGraphicsFrameCaptureRenderDoc); d.reset(new QGraphicsFrameCaptureRenderDoc);
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
d.reset(new QGraphicsFrameCaptureMetal); d.reset(new QGraphicsFrameCaptureMetal);
#endif #endif
} }

View File

@ -41,7 +41,7 @@ static void *glNativeContext(QOpenGLContext *context) {
#endif #endif
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
nctx = context->nativeInterface<QNativeInterface::QCocoaGLContext>()->nativeContext(); nctx = context->nativeInterface<QNativeInterface::QCocoaGLContext>()->nativeContext();
#endif #endif
} }

View File

@ -31,7 +31,7 @@
# define TST_D3D12 # define TST_D3D12
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
# define TST_MTL # define TST_MTL
#endif #endif

View File

@ -84,7 +84,7 @@ void tst_QRhiWidget::testData()
#endif #endif
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
QRhiMetalInitParams metalInitParams; QRhiMetalInitParams metalInitParams;
if (QRhi::probe(QRhi::Metal, &metalInitParams)) if (QRhi::probe(QRhi::Metal, &metalInitParams))
QTest::newRow("Metal") << QRhiWidget::Api::Metal; QTest::newRow("Metal") << QRhiWidget::Api::Metal;

View File

@ -3,7 +3,7 @@
#include <QtGui> #include <QtGui>
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) || defined(Q_OS_WIN) || QT_CONFIG(xcb) || defined(ANDROID) #if QT_CONFIG(metal) || defined(Q_OS_WIN) || QT_CONFIG(xcb) || defined(ANDROID)
#include "../../shared/nativewindow.h" #include "../../shared/nativewindow.h"
#define HAVE_NATIVE_WINDOW #define HAVE_NATIVE_WINDOW
#endif #endif

View File

@ -84,7 +84,7 @@ QRhi::Implementation graphicsApiFromCmd(const QGuiApplication &app) {
QRhi::Implementation graphicsApi; QRhi::Implementation graphicsApi;
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = QRhi::D3D11; graphicsApi = QRhi::D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = QRhi::Metal; graphicsApi = QRhi::Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = QRhi::Vulkan; graphicsApi = QRhi::Vulkan;

View File

@ -9,7 +9,7 @@ Window::Window(QRhi::Implementation graphicsApi)
: m_graphicsApi(graphicsApi) : m_graphicsApi(graphicsApi)
{ {
m_capturer.reset(new QGraphicsFrameCapture); m_capturer.reset(new QGraphicsFrameCapture);
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
qDebug("This example uses Metal Capture Manager In App API to capture frames. Press F9 to capture a frame and F10 to open it for analysis"); qDebug("This example uses Metal Capture Manager In App API to capture frames. Press F9 to capture a frame and F10 to open it for analysis");
#else #else
qDebug("This example uses RenderDoc In App API to capture frames. Press F9 to capture a frame and F10 to open it for analysis"); qDebug("This example uses RenderDoc In App API to capture frames. Press F9 to capture a frame and F10 to open it for analysis");
@ -142,7 +142,7 @@ void Window::init()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (m_graphicsApi == QRhi::Metal) { if (m_graphicsApi == QRhi::Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
m_rhi.reset(QRhi::create(QRhi::Metal, &params, rhiFlags)); m_rhi.reset(QRhi::create(QRhi::Metal, &params, rhiFlags));

View File

@ -37,7 +37,7 @@ int main(int argc, char **argv)
QRhi::Implementation graphicsApi; QRhi::Implementation graphicsApi;
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = QRhi::D3D11; graphicsApi = QRhi::D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = QRhi::Metal; graphicsApi = QRhi::Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = QRhi::Vulkan; graphicsApi = QRhi::Vulkan;

View File

@ -120,7 +120,7 @@ void Window::init()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (m_graphicsApi == QRhi::Metal) { if (m_graphicsApi == QRhi::Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
m_rhi.reset(QRhi::create(QRhi::Metal, &params, rhiFlags)); m_rhi.reset(QRhi::create(QRhi::Metal, &params, rhiFlags));

View File

@ -91,7 +91,7 @@ void createRhi()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (graphicsApi == Metal) { if (graphicsApi == Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
r.r = QRhi::create(QRhi::Metal, &params); r.r = QRhi::create(QRhi::Metal, &params);
@ -469,7 +469,7 @@ int main(int argc, char **argv)
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = D3D11; graphicsApi = D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = Metal; graphicsApi = Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = Vulkan; graphicsApi = Vulkan;

View File

@ -321,7 +321,7 @@ void Renderer::createRhi()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (graphicsApi == Metal) { if (graphicsApi == Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
r = QRhi::create(QRhi::Metal, &params, rhiFlags); r = QRhi::create(QRhi::Metal, &params, rhiFlags);
@ -654,7 +654,7 @@ int main(int argc, char **argv)
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = D3D11; graphicsApi = D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = Metal; graphicsApi = Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = Vulkan; graphicsApi = Vulkan;

View File

@ -68,7 +68,7 @@ int main(int argc, char **argv)
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = D3D11; graphicsApi = D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = Metal; graphicsApi = Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = Vulkan; graphicsApi = Vulkan;
@ -154,7 +154,7 @@ int main(int argc, char **argv)
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (graphicsApi == Metal) { if (graphicsApi == Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
r = QRhi::create(QRhi::Metal, &params, rhiFlags); r = QRhi::create(QRhi::Metal, &params, rhiFlags);

View File

@ -261,7 +261,7 @@ void Window::init()
} }
#endif #endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (graphicsApi == Metal) { if (graphicsApi == Metal) {
QRhiMetalInitParams params; QRhiMetalInitParams params;
m_r = QRhi::create(QRhi::Metal, &params, rhiFlags); m_r = QRhi::create(QRhi::Metal, &params, rhiFlags);
@ -426,7 +426,7 @@ int main(int argc, char **argv)
// Defaults. // Defaults.
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
graphicsApi = D3D11; graphicsApi = D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) #elif QT_CONFIG(metal)
graphicsApi = Metal; graphicsApi = Metal;
#elif QT_CONFIG(vulkan) #elif QT_CONFIG(vulkan)
graphicsApi = Vulkan; graphicsApi = Vulkan;

View File

@ -192,7 +192,7 @@ void Window::customRender()
if (d.testStage == 6) { if (d.testStage == 6) {
const QRhiTexture::NativeTexture nativeTexture = d.tex->nativeTexture(); const QRhiTexture::NativeTexture nativeTexture = d.tex->nativeTexture();
if (nativeTexture.object) { if (nativeTexture.object) {
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
if (graphicsApi == Metal) { if (graphicsApi == Metal) {
qDebug() << "Metal texture: " << nativeTexture.object; qDebug() << "Metal texture: " << nativeTexture.object;
// Now could cast to id<MTLTexture> and do something with // Now could cast to id<MTLTexture> and do something with

View File

@ -53,7 +53,7 @@ private:
Handle m_handle = {}; Handle m_handle = {};
}; };
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) #if QT_CONFIG(metal)
@interface View : VIEW_BASE @interface View : VIEW_BASE
@end @end