Add OpenGL query helper
This commit is contained in:
parent
49cf0b2359
commit
0c00505d44
1 changed files with 86 additions and 0 deletions
86
libs/gfx/include/psemek/gfx/query.hpp
Normal file
86
libs/gfx/include/psemek/gfx/query.hpp
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <psemek/gfx/gl.hpp>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace psemek::gfx
|
||||||
|
{
|
||||||
|
|
||||||
|
struct query_array
|
||||||
|
{
|
||||||
|
query_array() = default;
|
||||||
|
query_array(std::size_t size);
|
||||||
|
~query_array();
|
||||||
|
|
||||||
|
std::size_t size() const { return ids_.size(); }
|
||||||
|
|
||||||
|
void resize(std::size_t size);
|
||||||
|
|
||||||
|
GLuint operator[](std::size_t i) const;
|
||||||
|
|
||||||
|
struct query_scope
|
||||||
|
{
|
||||||
|
GLenum type;
|
||||||
|
|
||||||
|
query_scope(GLenum type)
|
||||||
|
: type{type}
|
||||||
|
{}
|
||||||
|
|
||||||
|
query_scope(query_scope const &) = delete;
|
||||||
|
query_scope(query_scope &&) = delete;
|
||||||
|
|
||||||
|
query_scope & operator = (query_scope const &) = delete;
|
||||||
|
query_scope & operator = (query_scope &&) = delete;
|
||||||
|
|
||||||
|
~query_scope()
|
||||||
|
{
|
||||||
|
gl::EndQuery(type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
query_scope begin(std::size_t i, GLenum type) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<GLuint> ids_;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline query_array::query_array(std::size_t size)
|
||||||
|
: ids_(size)
|
||||||
|
{
|
||||||
|
gl::GenQueries(size, ids_.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline query_array::~query_array()
|
||||||
|
{
|
||||||
|
gl::DeleteQueries(ids_.size(), ids_.data());
|
||||||
|
ids_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void query_array::resize(std::size_t size)
|
||||||
|
{
|
||||||
|
if (size < ids_.size())
|
||||||
|
{
|
||||||
|
gl::DeleteQueries(ids_.size() - size, ids_.data() + size);
|
||||||
|
ids_.resize(size);
|
||||||
|
}
|
||||||
|
else if (size > ids_.size())
|
||||||
|
{
|
||||||
|
std::size_t old_size = ids_.size();
|
||||||
|
ids_.resize(size);
|
||||||
|
gl::GenQueries(size - old_size, ids_.data() + old_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline GLuint query_array::operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
return ids_[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline query_array::query_scope query_array::begin(std::size_t i, GLenum type) const
|
||||||
|
{
|
||||||
|
gl::BeginQuery(type, ids_[i]);
|
||||||
|
return query_scope{type};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue