Skip to content

Commit 2c86cd2

Browse files
committed
QueryPool Reset for OpenGL
1 parent 7c5eb07 commit 2c86cd2

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

src/nbl/video/COpenGLCommandBuffer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,8 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
913913
case impl::ECT_RESET_QUERY_POOL:
914914
{
915915
auto& c = cmd.get<impl::ECT_RESET_QUERY_POOL>();
916-
_NBL_TODO();
916+
const COpenGLQueryPool* qp = static_cast<const COpenGLQueryPool*>(c.queryPool.get());
917+
qp->resetQueries(gl, c.query, c.queryCount);
917918
}
918919
break;
919920
case impl::ECT_BEGIN_QUERY:
@@ -1017,7 +1018,7 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
10171018
{
10181019
auto& c = cmd.get<impl::ECT_WRITE_TIMESTAMP>();
10191020
const COpenGLQueryPool* qp = static_cast<const COpenGLQueryPool*>(c.queryPool.get());
1020-
GLuint query = qp->getQueryAt(c.query);
1021+
const GLuint query = qp->getQueries()[c.query];
10211022
assert(qp->getCreationParameters().queryType == IQueryPool::E_QUERY_TYPE::EQT_TIMESTAMP);
10221023
gl->glQuery.pglQueryCounter(query, GL_TIMESTAMP);
10231024
}

src/nbl/video/COpenGLCommandBuffer.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,9 +1072,13 @@ class COpenGLCommandBuffer final : public IGPUCommandBuffer
10721072

10731073
bool resetQueryPool(IQueryPool* queryPool, uint32_t firstQuery, uint32_t queryCount) override
10741074
{
1075-
// If multiple queries are issued using the same query object id before calling glGetQueryObject or glGetQueryBufferObject:
1076-
// the results of the most recent query will be returned. In this case, when issuing a new query, the results of the previous query are discarded.
1077-
// So just ignore :)
1075+
if (!this->isCompatibleDevicewise(queryPool))
1076+
return false;
1077+
SCmd<impl::ECT_RESET_QUERY_POOL> cmd;
1078+
cmd.queryPool = core::smart_refctd_ptr<const IQueryPool>(queryPool);
1079+
cmd.query = firstQuery;
1080+
cmd.queryCount = queryCount;
1081+
pushCommand(std::move(cmd));
10781082
return true;
10791083
}
10801084
bool beginQuery(IQueryPool* queryPool, uint32_t query, IQueryPool::E_QUERY_CONTROL_FLAGS flags = static_cast<IQueryPool::E_QUERY_CONTROL_FLAGS>(0)) override

src/nbl/video/COpenGLQueryPool.h

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class COpenGLQueryPool final : public IQueryPool
2828
{
2929
if(_params.queryType == EQT_OCCLUSION)
3030
{
31-
queries.resize(params.queryCount);
31+
queries.resize(_params.queryCount);
3232
gl->extGlCreateQueries(GL_SAMPLES_PASSED, _params.queryCount, queries.data());
3333
}
3434
else if(_params.queryType == EQT_TIMESTAMP)
3535
{
36-
queries.resize(params.queryCount);
36+
queries.resize(_params.queryCount);
3737
gl->extGlCreateQueries(GL_TIMESTAMP, _params.queryCount, queries.data());
3838
}
3939
else if(_params.queryType == EQT_TRANSFORM_FEEDBACK_STREAM_EXT)
@@ -42,9 +42,9 @@ class COpenGLQueryPool final : public IQueryPool
4242
// The first integer is the number of primitives successfully written to the corresponding transform feedback buffer
4343
// and the second is the number of primitives output to the vertex stream.
4444
// But in OpenGL there you need twice the queries to get both values.
45-
queries.resize(params.queryCount * 2);
45+
queries.resize(_params.queryCount * 2);
4646
gl->extGlCreateQueries(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, _params.queryCount, queries.data());
47-
gl->extGlCreateQueries(GL_PRIMITIVES_GENERATED, _params.queryCount, queries.data() + params.queryCount);
47+
gl->extGlCreateQueries(GL_PRIMITIVES_GENERATED, _params.queryCount, queries.data() + _params.queryCount);
4848
}
4949
else
5050
{
@@ -57,18 +57,6 @@ class COpenGLQueryPool final : public IQueryPool
5757
return core::SRange<const GLuint>(queries.data(), queries.data() + queries.size());
5858
}
5959

60-
inline GLuint getQueryAt(uint32_t index) const
61-
{
62-
if(index < queries.size())
63-
{
64-
return queries[index];
65-
}
66-
else
67-
{
68-
return 0; // is 0 an invalid GLuint?
69-
}
70-
}
71-
7260
inline void beginQuery(IOpenGL_FunctionTable* gl, uint32_t queryIndex, E_QUERY_CONTROL_FLAGS flags) const
7361
{
7462
if(gl != nullptr)
@@ -174,6 +162,59 @@ class COpenGLQueryPool final : public IQueryPool
174162
}
175163
}
176164
}
165+
166+
inline bool resetQueries(IOpenGL_FunctionTable* gl, uint32_t query, uint32_t queryCount)
167+
{
168+
// NOTE: There is no Reset Queries on OpenGL but to make the queries invalid/unavailable and not return the previous ones we just delete the queries and recreate them.
169+
// TODO: Needs test
170+
size_t logicalQuerySize = 0ull;
171+
if(params.queryType == EQT_TRANSFORM_FEEDBACK_STREAM_EXT)
172+
logicalQuerySize = queries.size() / 2ull;
173+
else
174+
logicalQuerySize = queries.size();
175+
176+
if(query + queryCount > logicalQuerySize)
177+
{
178+
assert(false);
179+
return false;
180+
}
181+
182+
if(params.queryType == EQT_OCCLUSION)
183+
{
184+
gl->glQuery.pglDeleteQueries(queryCount, queries.data() + query);
185+
gl->extGlCreateQueries(GL_SAMPLES_PASSED, queryCount, queries.data() + query);
186+
}
187+
else if(params.queryType == EQT_TIMESTAMP)
188+
{
189+
gl->glQuery.pglDeleteQueries(queryCount, queries.data() + query);
190+
gl->extGlCreateQueries(GL_TIMESTAMP, queryCount, queries.data() + query);
191+
}
192+
else if(params.queryType == EQT_TRANSFORM_FEEDBACK_STREAM_EXT)
193+
{
194+
gl->glQuery.pglDeleteQueries(queryCount, queries.data() + query);
195+
gl->glQuery.pglDeleteQueries(queryCount, (queries.data() + logicalQuerySize) + query);
196+
gl->extGlCreateQueries(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, queryCount, queries.data() + query);
197+
gl->extGlCreateQueries(GL_PRIMITIVES_GENERATED, queryCount, (queries.data() + logicalQuerySize) + query );
198+
}
199+
200+
return true;
201+
}
202+
203+
protected:
204+
205+
inline GLuint getQueryAt(uint32_t index) const
206+
{
207+
if(index < queries.size())
208+
{
209+
return queries[index];
210+
}
211+
else
212+
{
213+
assert(false);
214+
return 0u; // is 0 an invalid GLuint?
215+
}
216+
}
217+
177218
};
178219

179220
} // end namespace nbl::video

0 commit comments

Comments
 (0)