]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/query_backend.cpp
Track different kinds of bindings separately
[libs/gl.git] / source / backends / opengl / query_backend.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_occlusion_query.h>
3 #include <msp/gl/extensions/arb_occlusion_query2.h>
4 #include "query.h"
5 #include "query_backend.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 OpenGLQueryPool::OpenGLQueryPool(unsigned t):
13         gl_type(get_gl_query_type(t))
14 {
15         if(t==OCCLUSION_QUERY)
16         {
17                 static Require req(ARB_occlusion_query);
18                 static Require req2(ARB_occlusion_query2);
19         }
20 }
21
22 OpenGLQueryPool::OpenGLQueryPool(OpenGLQueryPool &&other):
23         gl_type(other.gl_type),
24         queries(move(other.queries))
25 {
26         other.queries.clear();
27 }
28
29 OpenGLQueryPool::~OpenGLQueryPool()
30 {
31         glDeleteQueries(queries.size(), queries.data());
32 }
33
34 void OpenGLQueryPool::resize()
35 {
36         unsigned size = static_cast<const QueryPool *>(this)->size;
37         if(size<queries.size())
38                 glDeleteQueries(queries.size()-size, queries.data()+size);
39
40         unsigned old_size = queries.size();
41         queries.resize(size);
42         if(size>old_size)
43                 glGenQueries(size-old_size, queries.data()+old_size);
44 }
45
46 unsigned OpenGLQueryPool::get_result(unsigned i) const
47 {
48         unsigned result = 0;
49         glGetQueryObjectuiv(queries[i], GL_QUERY_RESULT, &result);
50         return result;
51 }
52
53
54 unsigned get_gl_query_type(unsigned t)
55 {
56         switch(t)
57         {
58         case OCCLUSION_QUERY: return GL_ANY_SAMPLES_PASSED;
59         default: throw invalid_argument("get_gl_query_type");
60         }
61 }
62
63 } // namespace GL
64 } // namespace Msp