QOpenGLTimerQuery Class
QOpenGLTimerQuery 类封装了一个 OpenGL 定时器查询对象。更多
Header: | #include <QOpenGLTimerQuery> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
qmake: | QT += opengl |
继承: | QObject |
- 所有成员(包括继承成员)的列表
- QOpenGLTimerQuery 是3D 渲染的一部分。
公共函数
QOpenGLTimerQuery(QObject *parent = nullptr) | |
virtual | ~QOpenGLTimerQuery() |
void | begin() |
bool | create() |
void | destroy() |
void | end() |
bool | isCreated() const |
bool | isResultAvailable() const |
GLuint | objectId() const |
void | recordTimestamp() |
GLuint64 | waitForResult() const |
GLuint64 | waitForTimestamp() const |
详细说明
OpenGL 定时器查询对象是 OpenGL 管理的资源,用于测量 GPU 上 OpenGL 命令序列的执行时间。
OpenGL 为定时器查询提供了不同级别的支持,具体取决于所使用的 OpenGL 版本以及是否存在 ARB_timer_query 或 EXT_timer_query 扩展。这些支持可概括为
- OpenGL >=3.3 完全支持所有定时器查询功能。
- 带有 ARB_timer_query 扩展的 OpenGL 3.2 完全支持所有定时器查询功能。
- 使用 EXT_timer_query 扩展的 OpenGL <=3.2 提供了有限的支持,即无法查询 GPU 的时间戳。这对 Qt 类提供的函数有影响的地方将在函数文档中突出显示。
- OpenGL ES 2(和 OpenGL ES 3)不支持 OpenGL 定时器查询。
OpenGL 表示时间的粒度为 1 纳秒(1e-9 秒)。因此,32 位整数只能提供约 4 秒的总持续时间,这在性能不佳或冗长的操作中很难超过。因此,OpenGL 使用 64 位整数类型来表示时间。GLuint64 变量的宽度足以容纳数百年的持续时间,足以满足实时渲染的需要。
与其他Qt OpenGL 类一样,QOpenGLTimerQuery 也有一个create() 函数来创建底层 OpenGL 对象。这是为了让开发人员确保当时有一个有效的当前 OpenGL 上下文。
创建完成后,定时器查询可以通过几种方式之一发出。最简单的方法是通过调用begin() 和end() 来划分命令块。这将指示 OpenGL 测量从完成begin() 之前发出的所有命令到完成end() 之前发出的所有命令所花费的时间。
在一帧结束时,我们可以调用waitForResult() 来获取结果。顾名思义,该函数会阻塞 CPU 的执行,直到 OpenGL 通知定时器查询结果可用。为了避免阻塞,可以通过调用isResultAvailable() 来检查查询结果是否可用。请注意,现代 GPU 采用深度流水线设计,查询结果可能在发出后 1-5 帧内才可用。
请注意,OpenGL 不允许使用begin() 和end() 嵌套或交错多个定时器查询。使用多个定时器查询和recordTimestamp() 可以避免这一限制。使用recordTimestamp() 时,可在稍后时间使用isResultAvailable() 和waitForResult() 获取结果。Qt XML 提供了方便类QOpenGLTimeMonitor ,有助于使用多个查询对象。
另请参见 QOpenGLTimeMonitor 。
成员函数文档
[explicit]
QOpenGLTimerQuery::QOpenGLTimerQuery(QObject *parent = nullptr)
使用给定的parent 创建一个 QOpenGLTimerQuery 实例。使用前必须使用有效的 OpenGL 上下文调用create() 。
[virtual noexcept]
QOpenGLTimerQuery::~QOpenGLTimerQuery()
销毁QOpenGLTimerQuery 和底层 OpenGL 资源。
void QOpenGLTimerQuery::begin()
标记 OpenGL 命令队列中的起点,以便此查询对象对命令序列进行计时。
这对简单的使用情况很有用。通常最好使用recordTimestamp()。
另请参阅 end()、isResultAvailable()、waitForResult() 和recordTimestamp()。
bool QOpenGLTimerQuery::create()
创建底层 OpenGL 定时器查询对象。必须有支持当前查询对象的有效 OpenGL 上下文,此函数才能成功执行。
如果 OpenGL 定时器查询对象创建成功,则返回true
。
void QOpenGLTimerQuery::destroy()
销毁底层 OpenGL 定时器查询对象。调用此函数时,调用create() 时的上下文必须是当前的。
void QOpenGLTimerQuery::end()
在 OpenGL 命令队列中标记由该查询对象计时的命令序列的终点。
这对简单的用例很有用。通常最好使用recordTimestamp()。
另请参阅 begin()、isResultAvailable()、waitForResult() 和recordTimestamp()。
bool QOpenGLTimerQuery::isCreated() const
如果底层 OpenGL 查询对象已创建,则返回true
。如果返回true
,且相关的 OpenGL 上下文是当前的,则可以对该对象进行查询。
bool QOpenGLTimerQuery::isResultAvailable() const
如果 OpenGL 定时器查询结果可用,则返回true
。
此函数为非阻塞函数,最好在调用waitForResult() 之前检查查询结果是否可用。
另请参阅 waitForResult()。
GLuint QOpenGLTimerQuery::objectId() const
返回底层 OpenGL 查询对象的 ID。
void QOpenGLTimerQuery::recordTimestamp()
在 GPU 的 OpenGL 命令队列中放置一个标记,记录 GPU 到达该标记时的时间戳。此函数是非阻塞的,结果将在稍后的某个时间可用。
可使用isResultAvailable() 检查结果是否可用。可以使用waitForResult() 获取结果,如果结果尚未可用,该函数将阻塞。
另请参见 waitForResult()、isResultAvailable()、begin() 和end()。
GLuint64 QOpenGLTimerQuery::waitForResult() const
返回 OpenGL 定时器查询的结果。
此函数将阻塞,直到 OpenGL 提供结果。建议调用isResultAvailable() 以确保结果可用,从而避免不必要的阻塞和停滞。
另请参阅 isResultAvailable()。
GLuint64 QOpenGLTimerQuery::waitForTimestamp() const
返回 GPU 当前的时间戳,此时所有之前发出的 OpenGL 命令都已接收到,但不一定被 GPU 执行。
此函数会阻塞,直到返回结果。
另请参阅 recordTimestamp().
© 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.