glw/timer.h
2025-06-03 14:29:27 -04:00

63 lines
2.0 KiB
C++
Executable File

// =====================================================================================================================
// glw, an open-source library that wraps OpenGL structures into classes.
// Copyright (C) 2024 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#ifndef GLW_TIMER_H
#define GLW_TIMER_H
#include <cassert>
#include "common.h"
namespace glw
{
class timer
{
private:
enum
{
begin = 0
, end = 1
};
public:
timer() : handles_{ 0 }, running_(false) { glCreateQueries(GL_TIMESTAMP, 2, handles_); }
timer(const timer&) = delete;
timer(timer&&) = default;
~timer() { glDeleteQueries(2, handles_); }
void start() { assert(not running_); glQueryCounter(handles_[begin], GL_TIMESTAMP); running_ = true; }
void stop() { assert(running_); glQueryCounter(handles_[end], GL_TIMESTAMP); running_ = false; }
int64_t poll()
{
assert(not running_);
int32_t available = false;
while(not available) glGetQueryObjectiv(handles_[end], GL_QUERY_RESULT_AVAILABLE, &available);
int64_t start; glGetQueryObjecti64v(handles_[begin], GL_QUERY_RESULT, &start);
int64_t stop; glGetQueryObjecti64v(handles_[end], GL_QUERY_RESULT, &stop);
return stop - start;
}
private:
handle_t handles_[2];
bool running_;
};
}
#endif //TIMER_H