Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glHardwareQuery.cpp
Go to the documentation of this file.
1
2
5
6namespace Divide {
7
9 : _enabled(false)
10{
11}
12
13void glHardwareQuery::create([[maybe_unused]] const gl46core::GLenum queryType)
14{
15 destroy();
16 gl46core::glGenQueries(1, &_queryID);
17}
18
20{
21 if (_queryID != 0u)
22 {
23 gl46core::glDeleteQueries(1, &_queryID);
24 }
25 _queryID = 0u;
26}
27
29{
30 gl46core::GLint available = 0;
31 gl46core::glGetQueryObjectiv(getID(), gl46core::GL_QUERY_RESULT_AVAILABLE, &available);
32 return available != 0;
33}
34
36{
37 gl46core::GLint64 res = 0;
38 gl46core::glGetQueryObjecti64v(getID(), gl46core::GL_QUERY_RESULT, &res);
39 return res;
40}
41
43{
44 gl46core::GLint64 res = 0;
45 gl46core::glGetQueryObjecti64v(getID(), gl46core::GL_QUERY_RESULT_NO_WAIT, &res);
46 return res;
47}
48
49glHardwareQueryRing::glHardwareQueryRing(GFXDevice& context, const gl46core::GLenum queryType, const U16 queueLength, const U32 id)
50 : RingBufferSeparateWrite(queueLength, true),
51 _context(context),
52 _id(id),
53 _queryType(queryType)
54{
55 _queries.reserve(queueLength);
57}
58
60{
61 for (glHardwareQuery& query : _queries) {
62 query.destroy();
63 }
64 _queries.clear();
65}
66
68{
69 return _queries[queueReadIndex()];
70}
71
73{
74 return _queries[queueWriteIndex()];
75}
76
77void glHardwareQueryRing::resize(const U16 queueLength)
78{
80
81 const size_t crtCount = _queries.size();
82 if (queueLength < crtCount)
83 {
84 while (queueLength < crtCount)
85 {
86 _queries.back().destroy();
87 _queries.pop_back();
88 }
89 }
90 else
91 {
92 const size_t countToAdd = queueLength - crtCount;
93
94 for (size_t i = 0; i < countToAdd; ++i)
95 {
96 _queries.emplace_back().create(_queryType);
97
98 //Prime the query
99 gl46core::glBeginQuery(_queryType, _queries.back().getID());
100 gl46core::glEndQuery(_queryType);
101 }
102 }
103}
104
105
107{
108 gl46core::glBeginQuery(_queryType, writeQuery().getID());
109}
110
112{
113 gl46core::glEndQuery(_queryType);
114}
115
117 : _context(context)
118{
119}
120
122{
123 destroy();
124}
125
127{
128 destroy();
129 for (auto [type, size] : sizes)
130 {
131 const U32 j = std::max(size, 1u);
132 _index[type] = 0;
133 auto& pool = _queryPool[type];
134 for (U32 i = 0; i < j; ++i)
135 {
136 pool.emplace_back(std::make_unique<glHardwareQueryRing>(_context, type, 1, i));
137 }
138 }
139}
140
142{
143 _queryPool.clear();
144}
145
146glHardwareQueryRing& glHardwareQueryPool::allocate(const gl46core::GLenum queryType)
147{
148 return *_queryPool[queryType][++_index[queryType]];
149}
150
152{
153 const gl46core::GLenum type = query.type();
154 U32& index = _index[type];
155 auto& pool = _queryPool[type];
156 for (U32 i = 0; i < index; ++i)
157 {
158 if (pool[i]->id() == query.id())
159 {
160 std::swap(pool[i], pool[index - 1]);
161 --index;
162 return;
163 }
164 }
165
167}
168
169} //namespace Divide
#define DIVIDE_UNEXPECTED_CALL()
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
I32 queueReadIndex() const noexcept
Definition: RingBuffer.h:63
virtual void resize(U16 queueLength)
Definition: RingBuffer.cpp:14
U16 queueLength() const noexcept
Definition: RingBuffer.h:46
I32 queueWriteIndex() const noexcept
Definition: RingBuffer.h:51
U32 getID() const noexcept
void create(gl46core::GLenum queryType)
hashMap< gl46core::GLenum, vector< std::unique_ptr< glHardwareQueryRing > > > _queryPool
glHardwareQueryPool(GFXDevice &context)
void init(const hashMap< gl46core::GLenum, U32 > &sizes)
glHardwareQueryRing & allocate(gl46core::GLenum queryType)
void deallocate(const glHardwareQueryRing &query)
hashMap< gl46core::GLenum, U32 > _index
vector< glHardwareQuery > _queries
glHardwareQueryRing(GFXDevice &context, gl46core::GLenum queryType, U16 queueLength, U32 id=0)
const glHardwareQuery & readQuery() const
void resize(U16 queueLength) override
gl46core::GLenum type() const noexcept
const glHardwareQuery & writeQuery() const
U32 id() const noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
uint16_t U16
int64_t I64
uint32_t U32