1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
* Mesa 3-D graphics library
* Version: 6.5
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "glheader.h"
#include "context.h"
#include "hash.h"
#include "imports.h"
#include "occlude.h"
#include "mtypes.h"
/**
* Allocate a new query object. This is a fallback routine called via
* ctx->Driver.NewQueryObject().
* \param ctx - rendering context
* \param id - the new object's ID
* \return pointer to new query_object object or NULL if out of memory.
*/
struct gl_query_object *
_mesa_new_query_object(GLcontext *ctx, GLuint id)
{
struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
if (q) {
q->Id = id;
q->Result = 0;
q->Active = GL_FALSE;
q->Ready = GL_TRUE; /* correct, see spec */
}
return q;
}
/**
* Delete an occlusion query object.
* Not removed from hash table here.
* XXX maybe add Delete() method to gl_query_object class and call that instead
*/
static void
delete_query_object(struct gl_query_object *q)
{
FREE(q);
}
struct gl_query_object *
lookup_query_object(GLcontext *ctx, GLuint id)
{
return (struct gl_query_object *)
_mesa_HashLookup(ctx->Query.QueryObjects, id);
}
void GLAPIENTRY
_mesa_GenQueriesARB(GLsizei n, GLuint *ids)
{
GLuint first;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
return;
}
/* No query objects can be active at this time! */
if (ctx->Query.CurrentOcclusionObject ||
ctx->Query.CurrentTimerObject) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
return;
}
first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
if (first) {
GLsizei i;
for (i = 0; i < n; i++) {
struct gl_query_object *q
= ctx->Driver.NewQueryObject(ctx, first + i);
if (!q) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
return;
}
ids[i] = first + i;
_mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
}
}
}
void GLAPIENTRY
_mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
{
GLint i;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
return;
}
/* No query objects can be active at this time! */
if (ctx->Query.CurrentOcclusionObject ||
ctx->Query.CurrentTimerObject) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
return;
}
for (i = 0; i < n; i++) {
if (ids[i] > 0) {
struct gl_query_object *q = lookup_query_object(ctx, ids[i]);
if (q) {
ASSERT(!q->Active); /* should be caught earlier */
_mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
delete_query_object(q);
}
}
}
}
GLboolean GLAPIENTRY
_mesa_IsQueryARB(GLuint id)
{
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
if (id && lookup_query_object(ctx, id))
return GL_TRUE;
else
return GL_FALSE;
}
void GLAPIENTRY
_mesa_BeginQueryARB(GLenum target, GLuint id)
{
struct gl_query_object *q;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
FLUSH_VERTICES(ctx, _NEW_DEPTH);
switch (target) {
case GL_SAMPLES_PASSED_ARB:
if (!ctx->Extensions.ARB_occlusion_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
return;
}
if (ctx->Query.CurrentOcclusionObject) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB");
return;
}
break;
case GL_TIME_ELAPSED_EXT:
if (!ctx->Extensions.EXT_timer_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
return;
}
if (ctx->Query.CurrentTimerObject) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB");
return;
}
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
return;
}
if (id == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(id==0)");
return;
}
q = lookup_query_object(ctx, id);
if (!q) {
/* create new object */
q = ctx->Driver.NewQueryObject(ctx, id);
if (!q) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
return;
}
_mesa_HashInsert(ctx->Query.QueryObjects, id, q);
}
else {
/* pre-existing object */
if (q->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginQueryARB(query already active)");
return;
}
}
q->Active = GL_TRUE;
q->Result = 0;
q->Ready = GL_FALSE;
if (target == GL_SAMPLES_PASSED_ARB) {
ctx->Query.CurrentOcclusionObject = q;
}
else if (target == GL_TIME_ELAPSED_EXT) {
ctx->Query.CurrentTimerObject = q;
}
if (ctx->Driver.BeginQuery) {
ctx->Driver.BeginQuery(ctx, target, q);
}
}
void GLAPIENTRY
_mesa_EndQueryARB(GLenum target)
{
struct gl_query_object *q;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
FLUSH_VERTICES(ctx, _NEW_DEPTH);
switch (target) {
case GL_SAMPLES_PASSED_ARB:
if (!ctx->Extensions.ARB_occlusion_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
return;
}
q = ctx->Query.CurrentOcclusionObject;
ctx->Query.CurrentOcclusionObject = NULL;
break;
case GL_TIME_ELAPSED_EXT:
if (!ctx->Extensions.EXT_timer_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
return;
}
q = ctx->Query.CurrentTimerObject;
ctx->Query.CurrentTimerObject = NULL;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
return;
}
if (!q || !q->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glEndQueryARB(no matching glBeginQueryARB)");
return;
}
q->Active = GL_FALSE;
if (ctx->Driver.EndQuery) {
ctx->Driver.EndQuery(ctx, target, q);
}
else {
/* if we're using software rendering/querying */
q->Ready = GL_TRUE;
}
}
void GLAPIENTRY
_mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
{
struct gl_query_object *q;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
switch (target) {
case GL_SAMPLES_PASSED_ARB:
if (!ctx->Extensions.ARB_occlusion_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
return;
}
q = ctx->Query.CurrentOcclusionObject;
break;
case GL_TIME_ELAPSED_EXT:
if (!ctx->Extensions.EXT_timer_query) {
_mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
return;
}
q = ctx->Query.CurrentTimerObject;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
return;
}
switch (pname) {
case GL_QUERY_COUNTER_BITS_ARB:
*params = 8 * sizeof(q->Result);
break;
case GL_CURRENT_QUERY_ARB:
*params = q ? q->Id : 0;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
return;
}
}
void GLAPIENTRY
_mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
{
struct gl_query_object *q = NULL;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (id)
q = lookup_query_object(ctx, id);
if (!q || q->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetQueryObjectivARB(id=%d is active)", id);
return;
}
switch (pname) {
case GL_QUERY_RESULT_ARB:
while (!q->Ready) {
/* Wait for the query to finish! */
/* If using software rendering, the result will always be ready
* by time we get here. Otherwise, we must be using hardware!
*/
ASSERT(ctx->Driver.EndQuery);
}
*params = q->Result;
break;
case GL_QUERY_RESULT_AVAILABLE_ARB:
/* XXX revisit when we have a hardware implementation! */
*params = q->Ready;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
return;
}
}
void GLAPIENTRY
_mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
{
struct gl_query_object *q = NULL;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (id)
q = lookup_query_object(ctx, id);
if (!q || q->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetQueryObjectuivARB(id=%d is active)", id);
return;
}
switch (pname) {
case GL_QUERY_RESULT_ARB:
while (!q->Ready) {
/* Wait for the query to finish! */
/* If using software rendering, the result will always be ready
* by time we get here. Otherwise, we must be using hardware!
*/
ASSERT(ctx->Driver.EndQuery);
}
*params = q->Result;
break;
case GL_QUERY_RESULT_AVAILABLE_ARB:
/* XXX revisit when we have a hardware implementation! */
*params = q->Ready;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
return;
}
}
/**
* Allocate/init the context state related to query objects.
*/
void
_mesa_init_query(GLcontext *ctx)
{
#if FEATURE_ARB_occlusion_query
ctx->Query.QueryObjects = _mesa_NewHashTable();
ctx->Query.CurrentOcclusionObject = NULL;
#endif
}
/**
* Free the context state related to query objects.
*/
void
_mesa_free_query_data(GLcontext *ctx)
{
while (1) {
GLuint id = _mesa_HashFirstEntry(ctx->Query.QueryObjects);
if (id) {
struct gl_query_object *q = lookup_query_object(ctx, id);
ASSERT(q);
delete_query_object(q);
_mesa_HashRemove(ctx->Query.QueryObjects, id);
}
else {
break;
}
}
_mesa_DeleteHashTable(ctx->Query.QueryObjects);
}
|