1 | #include "OcclusionQuery.h"
|
---|
2 | #include <iostream>
|
---|
3 | #include <glInterface.h>
|
---|
4 |
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 | bool OcclusionQuery::sUseArbQueries = true;
|
---|
11 |
|
---|
12 |
|
---|
13 | OcclusionQuery::OcclusionQuery()
|
---|
14 | {
|
---|
15 | GLuint id;
|
---|
16 |
|
---|
17 | if (sUseArbQueries)
|
---|
18 | {
|
---|
19 | glGenQueriesARB(1, &id);
|
---|
20 | }
|
---|
21 | else
|
---|
22 | {
|
---|
23 | glGenOcclusionQueriesNV(1, &id);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | OcclusionQuery::OcclusionQuery(const unsigned int idx):
|
---|
28 | mId(idx)
|
---|
29 | {}
|
---|
30 |
|
---|
31 |
|
---|
32 | OcclusionQuery::~OcclusionQuery()
|
---|
33 | {
|
---|
34 | if (sUseArbQueries)
|
---|
35 | {
|
---|
36 | glDeleteQueriesARB(1, &mId);
|
---|
37 | }
|
---|
38 | else
|
---|
39 | {
|
---|
40 | glDeleteOcclusionQueriesNV(1, &mId);
|
---|
41 | }
|
---|
42 |
|
---|
43 | }
|
---|
44 | //-----------------------------------------------------------------------
|
---|
45 | void OcclusionQuery::BeginQuery()
|
---|
46 | {
|
---|
47 | if (sUseArbQueries)
|
---|
48 | {
|
---|
49 | glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mId);
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | glBeginOcclusionQueryNV(mId);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | //-----------------------------------------------------------------------
|
---|
57 | void OcclusionQuery::EndQuery()
|
---|
58 | {
|
---|
59 | if (sUseArbQueries)
|
---|
60 | {
|
---|
61 | glEndQueryARB(GL_SAMPLES_PASSED_ARB);
|
---|
62 | }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | glEndOcclusionQueryNV();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | unsigned int OcclusionQuery::GetQueryId() const
|
---|
71 | {
|
---|
72 | return mId;
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool OcclusionQuery::ResultAvailable() const
|
---|
76 | {
|
---|
77 | GLuint available;
|
---|
78 |
|
---|
79 | if (sUseArbQueries)
|
---|
80 | {
|
---|
81 | //GLint available;
|
---|
82 | glGetQueryObjectuivARB(mId,
|
---|
83 | GL_QUERY_RESULT_AVAILABLE_ARB,
|
---|
84 | &available);
|
---|
85 | return available == GL_TRUE;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 |
|
---|
90 | glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
|
---|
91 |
|
---|
92 | return available == GL_TRUE;
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | unsigned int OcclusionQuery::GetQueryResult() const
|
---|
99 | {
|
---|
100 | GLuint sampleCount;
|
---|
101 |
|
---|
102 | if (sUseArbQueries)
|
---|
103 | {
|
---|
104 | // GLint sampleCount;
|
---|
105 | glGetQueryObjectuivARB(mId, GL_QUERY_RESULT_ARB, &sampleCount);
|
---|
106 | return sampleCount;
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 |
|
---|
111 | glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_NV, &sampleCount);
|
---|
112 | return sampleCount;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | void OcclusionQuery::GenQueries(std::vector<OcclusionQuery * > &queries, const int numQueries)
|
---|
118 | {
|
---|
119 | if ((int)queries.size() < numQueries)
|
---|
120 | {
|
---|
121 | const int n = numQueries - (int)queries.size();
|
---|
122 | unsigned int *newQueries = new unsigned int[n];
|
---|
123 |
|
---|
124 |
|
---|
125 | if (sUseArbQueries)
|
---|
126 | {
|
---|
127 | glGenQueriesARB(n, (unsigned int *)newQueries);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | glGenOcclusionQueriesNV(n, (unsigned int *)newQueries);
|
---|
132 | }
|
---|
133 |
|
---|
134 | for (int i = 0; i < n; ++ i)
|
---|
135 | {
|
---|
136 | queries.push_back(new OcclusionQuery(newQueries[i]));
|
---|
137 | //cout << "q: " << i << " id: " << newQueries[i] << endl;
|
---|
138 | }
|
---|
139 |
|
---|
140 | delete [] newQueries;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | } // namespace |
---|