Implemented GL_ARB_occlusion_query (not 100% finalized).
[mesa.git] / src / mesa / main / occlude.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * \brief Functions to implement the GL_ARB_occlusion_query extension.
28 */
29
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "hash.h"
34 #include "imports.h"
35 #include "occlude.h"
36 #include "mtypes.h"
37
38 #ifndef GL_SAMPLES_PASSED_ARB
39 #define GL_SAMPLES_PASSED_ARB 0x8914
40 #define GL_QUERY_COUNTER_BITS_ARB 0x8864
41 #define GL_CURRENT_QUERY_ARB 0x8865
42 #define GL_QUERY_RESULT_ARB 0x8866
43 #define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867
44 #endif
45
46
47 struct occlusion_query
48 {
49 GLenum Target;
50 GLuint Id;
51 GLuint PassedCounter;
52 GLboolean Active;
53 };
54
55
56 /**
57 * Allocate a new occlusion query object.
58 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
59 * \param id - the object's ID
60 * \return pointer to new occlusion_query object or NULL if out of memory.
61 */
62 static struct occlusion_query *
63 new_query_object(GLenum target, GLuint id)
64 {
65 struct occlusion_query *q = MALLOC_STRUCT(occlusion_query);
66 if (q) {
67 q->Target = target;
68 q->Id = id;
69 q->PassedCounter = 0;
70 q->Active = GL_FALSE;
71 }
72 return q;
73 }
74
75
76 /**
77 * Delete an occlusion query object.
78 */
79 static void
80 delete_query_object(struct occlusion_query *q)
81 {
82 FREE(q);
83 }
84
85
86 void
87 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
88 {
89 GET_CURRENT_CONTEXT(ctx);
90 GLuint first;
91 ASSERT_OUTSIDE_BEGIN_END(ctx);
92
93 if (n < 0) {
94 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
95 return;
96 }
97
98 if (ctx->Occlusion.Active) {
99 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
100 return;
101 }
102
103 first = _mesa_HashFindFreeKeyBlock(ctx->Occlusion.QueryObjects, n);
104 if (first) {
105 GLuint i;
106 for (i = 0; i < n; i++) {
107 struct occlusion_query *q = new_query_object(GL_SAMPLES_PASSED_ARB,
108 first + i);
109 if (!q) {
110 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
111 return;
112 }
113 ids[i] = first + i;
114 _mesa_HashInsert(ctx->Occlusion.QueryObjects, first + i, q);
115 }
116 }
117 }
118
119
120 void
121 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
122 {
123 GET_CURRENT_CONTEXT(ctx);
124 GLint i;
125 ASSERT_OUTSIDE_BEGIN_END(ctx);
126
127 if (n < 0) {
128 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
129 return;
130 }
131
132 if (ctx->Occlusion.Active) {
133 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
134 return;
135 }
136
137 for (i = 0; i < n; i++) {
138 if (ids[i] > 0) {
139 struct occlusion_query *q = (struct occlusion_query *)
140 _mesa_HashLookup(ctx->Occlusion.QueryObjects, ids[i]);
141 if (q) {
142 _mesa_HashRemove(ctx->Occlusion.QueryObjects, ids[i]);
143 delete_query_object(q);
144 }
145 }
146 }
147 }
148
149
150 GLboolean
151 _mesa_IsQueryARB(GLuint id)
152 {
153 GET_CURRENT_CONTEXT(ctx);
154 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
155
156 if (id && _mesa_HashLookup(ctx->Occlusion.QueryObjects, id))
157 return GL_TRUE;
158 else
159 return GL_FALSE;
160 }
161
162
163 void
164 _mesa_BeginQueryARB(GLenum target, GLuint id)
165 {
166 GET_CURRENT_CONTEXT(ctx);
167 struct occlusion_query *q;
168 ASSERT_OUTSIDE_BEGIN_END(ctx);
169
170 FLUSH_VERTICES(ctx, _NEW_DEPTH);
171
172 if (target != GL_SAMPLES_PASSED_ARB) {
173 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
174 return;
175 }
176
177 if (ctx->Occlusion.CurrentQueryObject) {
178 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(target)");
179 return;
180 }
181
182 q = (struct occlusion_query *)
183 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
184 if (q && q->Active) {
185 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB");
186 return;
187 }
188 else if (!q) {
189 q = new_query_object(target, id);
190 if (!q) {
191 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
192 return;
193 }
194 _mesa_HashInsert(ctx->Occlusion.QueryObjects, id, q);
195 }
196
197 q->Active = GL_TRUE;
198 q->PassedCounter = 0;
199 ctx->Occlusion.Active = GL_TRUE;
200 ctx->Occlusion.CurrentQueryObject = id;
201 ctx->Occlusion.PassedCounter = 0;
202 }
203
204
205 void
206 _mesa_EndQueryARB(GLenum target)
207 {
208 GET_CURRENT_CONTEXT(ctx);
209 struct occlusion_query *q;
210 ASSERT_OUTSIDE_BEGIN_END(ctx);
211
212 FLUSH_VERTICES(ctx, _NEW_DEPTH);
213
214 if (target != GL_SAMPLES_PASSED_ARB) {
215 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
216 return;
217 }
218
219 q = (struct occlusion_query *)
220 _mesa_HashLookup(ctx->Occlusion.QueryObjects,
221 ctx->Occlusion.CurrentQueryObject);
222 if (!q || !q->Active) {
223 _mesa_problem(ctx, "bad query object in glEndQueryARB");
224 return;
225 }
226
227 q->PassedCounter = ctx->Occlusion.PassedCounter;
228 q->Active = GL_FALSE;
229 ctx->Occlusion.Active = GL_FALSE;
230 ctx->Occlusion.CurrentQueryObject = 0;
231 }
232
233
234 void
235 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
236 {
237 GET_CURRENT_CONTEXT(ctx);
238 ASSERT_OUTSIDE_BEGIN_END(ctx);
239
240 if (target != GL_SAMPLES_PASSED_ARB) {
241 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
242 return;
243 }
244
245 switch (pname) {
246 case GL_QUERY_COUNTER_BITS_ARB:
247 *params = 8 * sizeof(GLuint);
248 break;
249 case GL_CURRENT_QUERY_ARB:
250 *params = ctx->Occlusion.CurrentQueryObject;
251 break;
252 default:
253 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
254 return;
255 }
256 }
257
258
259 void
260 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
261 {
262 GET_CURRENT_CONTEXT(ctx);
263 struct occlusion_query *q;
264 ASSERT_OUTSIDE_BEGIN_END(ctx);
265
266 q = (struct occlusion_query *)
267 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
268 if (!q || q->Active) {
269 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB");
270 return;
271 }
272
273 switch (pname) {
274 case GL_QUERY_RESULT_ARB:
275 *params = q->PassedCounter;
276 break;
277 case GL_QUERY_RESULT_AVAILABLE_ARB:
278 /* XXX revisit when we have a hardware implementation! */
279 *params = GL_TRUE;
280 break;
281 default:
282 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
283 return;
284 }
285 }
286
287
288 void
289 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
290 {
291 GET_CURRENT_CONTEXT(ctx);
292 struct occlusion_query *q;
293 ASSERT_OUTSIDE_BEGIN_END(ctx);
294
295 q = (struct occlusion_query *)
296 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
297 if (!q || q->Active) {
298 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectuivARB");
299 return;
300 }
301
302 switch (pname) {
303 case GL_QUERY_RESULT_ARB:
304 *params = q->PassedCounter;
305 break;
306 case GL_QUERY_RESULT_AVAILABLE_ARB:
307 /* XXX revisit when we have a hardware implementation! */
308 *params = GL_TRUE;
309 break;
310 default:
311 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
312 return;
313 }
314 }