Skip to content

GPUTimer

Concrete implementation of the Timer class for GPU-based timing using OpenGL queries. More...

#include "Utils/Timers.h"

Inherits from Timer

Public Functions

Name
GPUTimer(std::string name)<br>Constructor.
~GPUTimer() override<br>Destructor.
virtual doubleget() override<br>Retrieves the elapsed GPU time for the most recent interval.
virtual doublegetAverage() override<br>Retrieves the average GPU time over all measured intervals.
virtual unsigned intgetCounter() const override<br>Retrieves the number of recorded intervals.
GLuintgetId(bool start) const<br>Returns the OpenGL query ID for start or stop query.
virtual voidreset() override<br>Resets the GPU timer, clearing all recorded data.
virtual voidstart(bool sync =false) override<br>Starts the GPU timer.
virtual voidstop(bool sync =false) override<br>Stops the GPU timer.

Additional inherited members

Public Functions inherited from Timer

Name
Timer() =default
Timer(double intervalSeconds, sol::protected_function callback, bool oneShot =false)
Timer(std::string name)<br>Constructor.
virtual~Timer() =default<br>Virtual destructor.
const std::string &getName() const<br>Retrieves the name of the timer.
voidtick(double dt)

Protected Attributes inherited from Timer

Name
unsigned intcounter <br>Number of recorded intervals.
std::stringname <br>Name of the timer.
GLuint64previousFrameQueryStart
doublepreviousFrameTimeStart <br>Start time of the previous frame.
GLuintquery <br>OpenGL query object for timing.
GLuintqueryStart
GLuintqueryStop <br>OpenGL query objects for timing.
doubletime <br>Time elapsed for the current interval.
doubletimeStart <br>Start time of the current interval.
doubletimeStop <br>Stop time of the current interval.
doubletimeTotal <br>Total accumulated time.

Friends inherited from Timer

Name
classChronos

Detailed Description

cpp
class GPUTimer;
class GPUTimer;

Concrete implementation of the Timer class for GPU-based timing using OpenGL queries.

The GPUTimer class uses OpenGL timestamp queries to measure the time spent on the GPU. It creates query objects for start and stop timestamps and calculates elapsed time based on them.

Public Functions Documentation

function GPUTimer

cpp
inline GPUTimer(
    std::string name
)
inline GPUTimer(
    std::string name
)

Constructor.

Parameters:

  • name Name of the timer.

function ~GPUTimer

cpp
inline ~GPUTimer() override
inline ~GPUTimer() override

Destructor.

Deletes the created query objects (if any).

function get

cpp
inline virtual double get() override
inline virtual double get() override

Retrieves the elapsed GPU time for the most recent interval.

Return: The elapsed time in seconds.

Reimplements: Timer::get

Reads back timestamp query results to compute elapsed GPU time. Accumulates this time into timeTotal and increments counter. The returned value is converted to seconds (1e-9 for nanoseconds).

function getAverage

cpp
inline virtual double getAverage() override
inline virtual double getAverage() override

Retrieves the average GPU time over all measured intervals.

Return: The average time in seconds.

Reimplements: Timer::getAverage

Uses [get()](/cpp-api-reference/classes/classGPUTimer#function-get) to ensure the last interval is accounted for, then returns timeTotal / counter (converted to seconds).

function getCounter

cpp
inline virtual unsigned int getCounter() const override
inline virtual unsigned int getCounter() const override

Retrieves the number of recorded intervals.

Return: The interval count.

Reimplements: Timer::getCounter

function getId

cpp
inline GLuint getId(
    bool start
) const
inline GLuint getId(
    bool start
) const

Returns the OpenGL query ID for start or stop query.

Parameters:

  • start If true, returns queryStart; otherwise returns queryStop.

Return: The OpenGL query object ID.

function reset

cpp
inline virtual void reset() override
inline virtual void reset() override

Resets the GPU timer, clearing all recorded data.

Reimplements: Timer::reset

function start

cpp
inline virtual void start(
    bool sync =false
) override
inline virtual void start(
    bool sync =false
) override

Starts the GPU timer.

Parameters:

  • sync Whether to synchronize with external operations before starting.

Reimplements: Timer::start

Creates query objects if not already created. Optionally calls glFinish() if sync is true. Places a timestamp query to mark the start of the measurement.

function stop

cpp
inline virtual void stop(
    bool sync =false
) override
inline virtual void stop(
    bool sync =false
) override

Stops the GPU timer.

Parameters:

  • sync Whether to synchronize with external operations before stopping.

Reimplements: Timer::stop

Optionally calls glFinish() if sync is true. Places a timestamp query to mark the end of the measurement.


Updated on 2025-05-31 at 12:55:30 +0000