场景图 - QML 下的 Metal

展示如何在Qt Quick 场景下直接使用 Metal 渲染。

QML 下的 Metal 示例展示了应用程序如何利用QQuickWindow::beforeRendering() 和QQuickWindow::beforeRenderPassRecording() 信号在Qt Quick 场景下绘制自定义的 Metal 内容。该信号在场景图开始渲染前的每一帧开始时发出,因此响应该信号的任何 Metal 绘制调用都会堆叠到Qt Quick 项目下。之所以有两个信号,是因为自定义的 Metal 命令是通过场景图使用的同一个渲染命令编码器记录到同一个命令缓冲区中的。beforeRendering() 本身并不足以实现这一点,因为它是在帧开始时,在MTLRenderCommandEncoder可用之前发出的。通过连接 beforeRenderPassRecording(),应用程序可以访问必要的本地对象。

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

在这个例子中,我们还将看到如何让 QML 暴露的值影响 Metal 渲染。我们使用 QML 文件中的NumberAnimation 来动画阈值,绘制松鼠的 Metal 着色程序会使用该值。

该示例在大多数方面与OpenGL Under QMLDirect3D 11 Under QMLVulkan Under QML示例相当,它们都能渲染相同的自定义内容,只是通过了不同的本地 API。

示例项目 @ code.qt.io

© 2025 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.