QOpenGLTimeMonitor Class

QOpenGLTimeMonitor 类封装了一系列 OpenGL 定时器查询对象。更多

Header: #include <QOpenGLTimeMonitor>
CMake: find_package(Qt6 REQUIRED COMPONENTS OpenGL)
target_link_libraries(mytarget PRIVATE Qt6::OpenGL)
qmake: QT += opengl
继承: QObject

公共函数

QOpenGLTimeMonitor(QObject *parent = nullptr)
virtual ~QOpenGLTimeMonitor()
bool create()
void destroy()
bool isCreated() const
bool isResultAvailable() const
QList<GLuint> objectIds() const
int recordSample()
void reset()
int sampleCount() const
void setSampleCount(int sampleCount)
QList<GLuint64> waitForIntervals() const
QList<GLuint64> waitForSamples() const

详细说明

QOpenGLTimeMonitor 类是对 OpenGL 定时器查询对象集合的方便封装,用于按照渲染应用程序所需的粒度水平测量 GPU 上的时间间隔。

OpenGL 定时器查询对象会被依次查询,以记录渲染代码中相关位置的 GPU 时间戳。一旦所有问题的定时器查询结果可用,就可以获取结果,QOpenGLTimerMonitor 将为您计算记录的时间间隔。

该类的典型用例是对应用程序的渲染算法进行剖析,或实时调整这些算法以实现动态性能/质量平衡。

在渲染函数中使用 QOpenGLTimeMonitor 之前,您应调用 setSamples() 设置所需的采样点数量。请注意,测量 N 个采样点将产生 N-1 个时间间隔。设置好采样点数量后,使用有效的当前 OpenGL 上下文调用create() 函数来创建必要的查询定时器对象。这些步骤通常只需在初始化函数中执行一次。

使用recordSample() 函数对包含希望计时的 OpenGL 命令的代码块进行定界。您可以使用isResultAvailable() 检查生成的时间样本和时间间隔是否可用。计算出的时间间隔和原始时间戳样本可分别使用阻塞式waitForIntervals() 和waitForSamples() 函数检索。

获取结果后,在开始新一轮采样之前(例如在下一帧),请务必调用reset() 函数,该函数将清除缓存结果,并将定时器索引重置回第一个定时器对象。

另请参阅 QOpenGLTimerQuery

成员函数文档

[explicit] QOpenGLTimeMonitor::QOpenGLTimeMonitor(QObject *parent = nullptr)

使用给定的parent 创建一个 QOpenGLTimeMonitor 实例。使用前必须使用有效的 OpenGL 上下文调用create() 。

另请参阅 setSampleCount() 和create() 。

[virtual noexcept] QOpenGLTimeMonitor::~QOpenGLTimeMonitor()

销毁QOpenGLTimeMonitor 和任何底层 OpenGL 资源。

bool QOpenGLTimeMonitor::create()

实例化sampleCount() OpenGL 定时器查询对象,用于跟踪连续调用recordSample() 之间执行 OpenGL 命令所花费的时间。

如果可以创建 OpenGL 定时器查询对象,则返回true

另请参阅 destroy()、setSampleCount() 和recordSample()。

void QOpenGLTimeMonitor::destroy()

销毁此实例中使用的任何 OpenGL 定时器查询对象。

另请参见 create()。

bool QOpenGLTimeMonitor::isCreated() const

如果底层 OpenGL 查询对象已创建,则返回true 。如果返回true 且相关的 OpenGL 上下文是当前的,则可以使用此对象记录时间采样。

bool QOpenGLTimeMonitor::isResultAvailable() const

如果 OpenGL 定时器查询结果可用,则返回true

另请参阅 waitForSamples() 和waitForIntervals()。

QList<GLuint> QOpenGLTimeMonitor::objectIds() const

返回QList ,其中包含 OpenGL 定时器查询对象的对象 Ids。

int QOpenGLTimeMonitor::recordSample()

在 OpenGL 命令队列的这一点上发出 OpenGL 定时器查询。在应用程序的渲染函数中按顺序调用此函数,将积累在连续调用此函数之间执行 OpenGL 命令所需的 GPU 时间的详细信息。

另请参见 setSampleCount()、isResultAvailable()、waitForSamples() 和waitForIntervals()。

void QOpenGLTimeMonitor::reset()

重置时间监控器,以便在下一帧渲染中使用。在获得上一帧结果后,在下一帧首次调用recordSample() 之前,请调用此函数。

另请参阅 recordSample()。

int QOpenGLTimeMonitor::sampleCount() const

返回通过setSampleCount() 申请的采样点数。如果在setSampleCount() 之后成功调用了创建,那么返回的值就是可以使用的实际采样点数。

采样点数的默认值为 2,因此只能测量一个区间。

另请参见 setSampleCount()。

void QOpenGLTimeMonitor::setSampleCount(int sampleCount)

将采样点数设置为sampleCount 。使用此函数设置采样点数后,必须调用create() 来实例化底层 OpenGL 定时器查询对象。

新的sampleCount 必须至少为 2。

另请参阅 sampleCount()、create() 和recordSample()。

QList<GLuint64> QOpenGLTimeMonitor::waitForIntervals() const

返回QList ,其中包含调用recordSample() 所限定的时间间隔。生成的向量将少包含一个元素,因为它代表的是时间间隔,而不是实际的时间戳样本。

此函数将阻塞,直到 OpenGL 显示结果可用。建议在使用isResultAvailable() 调用此函数之前检查结果是否可用。

另请参阅 waitForSamples() 和isResultAvailable()。

QList<GLuint64> QOpenGLTimeMonitor::waitForSamples() const

返回QList ,其中包含使用recordSample() 提取的 GPU 时间戳。

此函数将阻塞,直到 OpenGL 显示结果可用。建议在使用isResultAvailable() 调用此函数之前检查结果是否可用。

注意: 此函数仅适用于 OpenGL >=3.3 或 ARB_timer_query 扩展的系统。详情请参见QOpenGLTimerQuery

另请参阅 waitForIntervals() 和isResultAvailable()。

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