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.
#if defined(Q_OS_WIN)
graphicsApi = QRhi::D3D11;
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#elif QT_CONFIG(metal)
graphicsApi = QRhi::Metal;
#elif QT_CONFIG(vulkan)
graphicsApi = QRhi::Vulkan;

View File

@ -146,7 +146,7 @@ void RhiWindow::init()
}
#endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#if QT_CONFIG(metal)
if (m_graphicsApi == QRhi::Metal) {
QRhiMetalInitParams 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
)
qt_internal_extend_target(Gui CONDITION IOS OR MACOS
qt_internal_extend_target(Gui CONDITION QT_FEATURE_metal
SOURCES
rhi/qrhimetal.mm rhi/qrhimetal_p.h
PUBLIC_LIBRARIES

View File

@ -811,6 +811,10 @@ qt_feature("vulkan" PUBLIC
LABEL "Vulkan"
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
SECTION "Platform plugins"
LABEL "VK_KHR_display"
@ -1288,6 +1292,7 @@ qt_configure_add_summary_entry(ARGS "opengles31")
qt_configure_add_summary_entry(ARGS "opengles32")
qt_configure_end_summary_section() # end of "OpenGL" section
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 "sessionmanager")
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)
QRhiD3D12InitParams params;
rhi.reset(QRhi::create(QRhi::D3D12, &params));
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#elif QT_CONFIG(metal)
QRhiMetalInitParams params;
rhi.reset(QRhi::create(QRhi::Metal, &params));
#elif QT_CONFIG(vulkan)

View File

@ -107,7 +107,7 @@ bool QBackingStoreRhiSupport::create()
}
#endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#if QT_CONFIG(metal)
if (!rhi && m_config.api() == QPlatformBackingStoreRhiConfig::Metal) {
QRhiMetalInitParams params;
// 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 defined(Q_OS_WIN)
config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#elif QT_CONFIG(metal)
config.setApi(QPlatformBackingStoreRhiConfig::Metal);
#elif QT_CONFIG(opengl)
config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
@ -291,7 +291,7 @@ bool QBackingStoreRhiSupport::checkForceRhi(QPlatformBackingStoreRhiConfig *outC
if (backend == QStringLiteral("d3d12"))
config.setApi(QPlatformBackingStoreRhiConfig::D3D12);
#endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#if QT_CONFIG(metal)
if (backend == QStringLiteral("metal"))
config.setApi(QPlatformBackingStoreRhiConfig::Metal);
#endif

View File

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

View File

@ -23,7 +23,7 @@
#include <QtGui/qvulkaninstance.h>
#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(MTLCommandQueue);
Q_FORWARD_DECLARE_OBJC_CLASS(MTLCommandBuffer);
@ -150,7 +150,7 @@ struct Q_GUI_EXPORT QRhiD3D12CommandBufferNativeHandles : public QRhiNativeHandl
#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
{

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@
#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"
#define HAVE_NATIVE_WINDOW
#endif

View File

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

View File

@ -9,7 +9,7 @@ Window::Window(QRhi::Implementation graphicsApi)
: m_graphicsApi(graphicsApi)
{
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");
#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");
@ -142,7 +142,7 @@ void Window::init()
}
#endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#if QT_CONFIG(metal)
if (m_graphicsApi == QRhi::Metal) {
QRhiMetalInitParams params;
m_rhi.reset(QRhi::create(QRhi::Metal, &params, rhiFlags));

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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