场景图——QML下的Metal

演示了如何在Qt Quick 场景下直接使用Metal进行渲染。

在 <span translate=Qt Quick 场景中使用 Metal 渲染的带文字叠加的动画椭圆" src="images/metalunderqml-example.jpg" title="在 Qt Quick 场景中使用 Metal 渲染的带文字叠加的动画椭圆"/>

“QML下的Metal”示例演示了应用程序如何利用QQuickWindow::beforeRendering()和QQuickWindow::beforeRenderPassRecording()信号,在Qt Quick 场景下绘制自定义的Metal内容。该信号在每帧开始时触发,即在场景图开始渲染之前,因此任何作为对此信号响应而发出的Metal绘制调用,都将堆叠在Qt Quick 项之下。 之所以有两个信号,是因为自定义 Metal 命令会被记录到与场景图所用的渲染命令编码器相同的命令缓冲区中。仅靠 beforeRendering() 无法实现这一目的,因为它在帧开始时触发,而此时尚未获得MTLRenderCommandEncoder。 通过同时连接到 beforeRenderPassRecording(),应用程序即可访问必要的原生对象。

作为替代方案,希望在Qt Quick 场景上渲染Metal内容的应用程序,可以通过连接到QQuickWindow::afterRendering()和QQuickWindow::afterRenderPassRecording()信号来实现。

在本示例中,我们还将看到如何将影响 Metal 渲染的值暴露给 QML。我们使用 QML 文件中的NumberAnimation 来动画化阈值,而 Metal 着色器程序会使用该值来绘制“squircles”。

该示例在多数方面与“OpenGL Under QML”、“Direct3D 11 Under QML”以及“Vulkan Under QML”示例等效,它们都渲染相同的自定义内容,只是通过不同的原生 API 实现。

示例项目 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.