1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "state.h"
|
---|
8 | #include "state/cr_statetypes.h"
|
---|
9 | #include "state/cr_statefuncs.h"
|
---|
10 | #include "state_internals.h"
|
---|
11 | #include "cr_mem.h"
|
---|
12 |
|
---|
13 | void
|
---|
14 | crStateOcclusionInit(CRContext *ctx)
|
---|
15 | {
|
---|
16 | CROcclusionState *o = &ctx->occlusion;
|
---|
17 |
|
---|
18 | o->objects = crAllocHashtable();
|
---|
19 | o->currentQueryObject = 0;
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | void
|
---|
24 | crStateOcclusionDestroy(CRContext *ctx)
|
---|
25 | {
|
---|
26 | CROcclusionState *o = &(ctx->occlusion);
|
---|
27 | crFreeHashtable(o->objects, crFree);
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | static CROcclusionObject *
|
---|
32 | NewQueryObject(GLenum target, GLuint id)
|
---|
33 | {
|
---|
34 | CROcclusionObject *q = (CROcclusionObject *) crAlloc(sizeof(CROcclusionObject));
|
---|
35 | if (q) {
|
---|
36 | q->target = target;
|
---|
37 | q->name = id;
|
---|
38 | q->passedCounter = 0;
|
---|
39 | q->active = GL_FALSE;
|
---|
40 | }
|
---|
41 | return q;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | void STATE_APIENTRY
|
---|
46 | crStateDeleteQueriesARB(GLsizei n, const GLuint *ids)
|
---|
47 | {
|
---|
48 | CRContext *g = GetCurrentContext();
|
---|
49 | CROcclusionState *o = &(g->occlusion);
|
---|
50 | /*CRStateBits *sb = GetCurrentBits();*/
|
---|
51 | /*CROcclusionBits *bb = &(sb->occlusion);*/
|
---|
52 | int i;
|
---|
53 |
|
---|
54 | FLUSH();
|
---|
55 |
|
---|
56 | if (g->current.inBeginEnd) {
|
---|
57 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
58 | "glDeleteQueriesARB called in Begin/End");
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | for (i = 0; i < n; i++) {
|
---|
63 | if (ids[i]) {
|
---|
64 | CROcclusionObject *q = (CROcclusionObject *)
|
---|
65 | crHashtableSearch(o->objects, ids[i]);
|
---|
66 | if (q) {
|
---|
67 | crHashtableDelete(o->objects, ids[i], crFree);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void STATE_APIENTRY
|
---|
75 | crStateGenQueriesARB(GLsizei n, GLuint * queries)
|
---|
76 | {
|
---|
77 | CRContext *g = GetCurrentContext();
|
---|
78 | CROcclusionState *o = &(g->occlusion);
|
---|
79 | GLint start;
|
---|
80 |
|
---|
81 | FLUSH();
|
---|
82 |
|
---|
83 | if (g->current.inBeginEnd) {
|
---|
84 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
85 | "glGenQueriesARB called in Begin/End");
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (n < 0) {
|
---|
90 | crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
|
---|
91 | "glGenQueriesARB(n < 0)");
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | start = crHashtableAllocKeys(o->objects, n);
|
---|
96 | if (start) {
|
---|
97 | GLint i;
|
---|
98 | for (i = 0; i < n; i++)
|
---|
99 | queries[i] = (GLuint) (start + i);
|
---|
100 | }
|
---|
101 | else {
|
---|
102 | crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenQueriesARB");
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | GLboolean STATE_APIENTRY
|
---|
108 | crStateIsQueryARB(GLuint id)
|
---|
109 | {
|
---|
110 | CRContext *g = GetCurrentContext();
|
---|
111 | CROcclusionState *o = &(g->occlusion);
|
---|
112 |
|
---|
113 | FLUSH();
|
---|
114 |
|
---|
115 | if (g->current.inBeginEnd) {
|
---|
116 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
117 | "glIsQueryARB called in begin/end");
|
---|
118 | return GL_FALSE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (id && crHashtableIsKeyUsed(o->objects, id))
|
---|
122 | return GL_TRUE;
|
---|
123 | else
|
---|
124 | return GL_FALSE;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | void STATE_APIENTRY
|
---|
129 | crStateGetQueryivARB(GLenum target, GLenum pname, GLint *params)
|
---|
130 | {
|
---|
131 | CRContext *g = GetCurrentContext();
|
---|
132 | CROcclusionState *o = &(g->occlusion);
|
---|
133 | (void)target;
|
---|
134 |
|
---|
135 | FLUSH();
|
---|
136 |
|
---|
137 | if (g->current.inBeginEnd) {
|
---|
138 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
139 | "glGetGetQueryivARB called in begin/end");
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | switch (pname) {
|
---|
144 | case GL_QUERY_COUNTER_BITS_ARB:
|
---|
145 | *params = 8 * sizeof(GLuint);
|
---|
146 | break;
|
---|
147 | case GL_CURRENT_QUERY_ARB:
|
---|
148 | *params = o->currentQueryObject;
|
---|
149 | break;
|
---|
150 | default:
|
---|
151 | crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
152 | "glGetGetQueryivARB(pname)");
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | void STATE_APIENTRY
|
---|
159 | crStateGetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
|
---|
160 | {
|
---|
161 | CRContext *g = GetCurrentContext();
|
---|
162 | CROcclusionState *o = &(g->occlusion);
|
---|
163 | CROcclusionObject *q;
|
---|
164 |
|
---|
165 | FLUSH();
|
---|
166 |
|
---|
167 | if (g->current.inBeginEnd) {
|
---|
168 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
169 | "glGetGetQueryObjectivARB called in begin/end");
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
|
---|
174 | if (!q || q->active) {
|
---|
175 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
176 | "glGetQueryObjectivARB");
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | switch (pname) {
|
---|
181 | case GL_QUERY_RESULT_ARB:
|
---|
182 | *params = q->passedCounter;
|
---|
183 | break;
|
---|
184 | case GL_QUERY_RESULT_AVAILABLE_ARB:
|
---|
185 | *params = GL_TRUE;
|
---|
186 | break;
|
---|
187 | default:
|
---|
188 | crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
189 | "glGetQueryObjectivARB(pname)");
|
---|
190 | return;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | void STATE_APIENTRY
|
---|
196 | crStateGetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
|
---|
197 | {
|
---|
198 | CRContext *g = GetCurrentContext();
|
---|
199 | CROcclusionState *o = &(g->occlusion);
|
---|
200 | CROcclusionObject *q;
|
---|
201 |
|
---|
202 | FLUSH();
|
---|
203 |
|
---|
204 | if (g->current.inBeginEnd) {
|
---|
205 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
206 | "glGetGetQueryObjectuivARB called in begin/end");
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
|
---|
211 | if (!q || q->active) {
|
---|
212 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
213 | "glGetQueryObjectuivARB");
|
---|
214 | return;
|
---|
215 | }
|
---|
216 |
|
---|
217 | switch (pname) {
|
---|
218 | case GL_QUERY_RESULT_ARB:
|
---|
219 | *params = q->passedCounter;
|
---|
220 | break;
|
---|
221 | case GL_QUERY_RESULT_AVAILABLE_ARB:
|
---|
222 | /* XXX revisit when we have a hardware implementation! */
|
---|
223 | *params = GL_TRUE;
|
---|
224 | break;
|
---|
225 | default:
|
---|
226 | crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
227 | "glGetQueryObjectuivARB(pname)");
|
---|
228 | return;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | void STATE_APIENTRY
|
---|
234 | crStateBeginQueryARB(GLenum target, GLuint id)
|
---|
235 | {
|
---|
236 | CRContext *g = GetCurrentContext();
|
---|
237 | CROcclusionState *o = &(g->occlusion);
|
---|
238 | CROcclusionObject *q;
|
---|
239 |
|
---|
240 | FLUSH();
|
---|
241 |
|
---|
242 | if (g->current.inBeginEnd) {
|
---|
243 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
244 | "glGetGetQueryObjectuivARB called in begin/end");
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (target != GL_SAMPLES_PASSED_ARB) {
|
---|
249 | crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
250 | "glBeginQueryARB(target)");
|
---|
251 | return;
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (o->currentQueryObject) {
|
---|
255 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
256 | "glBeginQueryARB(target)");
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
|
---|
261 | if (q && q->active) {
|
---|
262 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBeginQueryARB");
|
---|
263 | return;
|
---|
264 | }
|
---|
265 | else if (!q) {
|
---|
266 | q = NewQueryObject(target, id);
|
---|
267 | if (!q) {
|
---|
268 | crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBeginQueryARB");
|
---|
269 | return;
|
---|
270 | }
|
---|
271 | crHashtableAdd(o->objects, id, q);
|
---|
272 | }
|
---|
273 |
|
---|
274 | q->active = GL_TRUE;
|
---|
275 | q->passedCounter = 0;
|
---|
276 | q->active = GL_TRUE;
|
---|
277 | q->passedCounter = 0;
|
---|
278 | o->currentQueryObject = id;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | void STATE_APIENTRY
|
---|
283 | crStateEndQueryARB(GLenum target)
|
---|
284 | {
|
---|
285 | CRContext *g = GetCurrentContext();
|
---|
286 | CROcclusionState *o = &(g->occlusion);
|
---|
287 | CROcclusionObject *q;
|
---|
288 |
|
---|
289 | FLUSH();
|
---|
290 |
|
---|
291 | if (g->current.inBeginEnd) {
|
---|
292 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
293 | "glGetGetQueryObjectuivARB called in begin/end");
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (target != GL_SAMPLES_PASSED_ARB) {
|
---|
298 | crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEndQueryARB(target)");
|
---|
299 | return;
|
---|
300 | }
|
---|
301 |
|
---|
302 | q = (CROcclusionObject *) crHashtableSearch(o->objects, o->currentQueryObject);
|
---|
303 | if (!q || !q->active) {
|
---|
304 | crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
|
---|
305 | "glEndQueryARB with glBeginQueryARB");
|
---|
306 | return;
|
---|
307 | }
|
---|
308 |
|
---|
309 | q->passedCounter = 0;
|
---|
310 | q->active = GL_FALSE;
|
---|
311 | o->currentQueryObject = 0;
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | void crStateOcclusionDiff(CROcclusionBits *bb, CRbitvalue *bitID,
|
---|
316 | CRContext *fromCtx, CRContext *toCtx)
|
---|
317 | {
|
---|
318 | /* Apparently, no occlusion state differencing needed */
|
---|
319 | (void)bb; (void)bitID; (void)fromCtx; (void)toCtx;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * XXX this function might need some testing/fixing.
|
---|
325 | */
|
---|
326 | void crStateOcclusionSwitch(CROcclusionBits *bb, CRbitvalue *bitID,
|
---|
327 | CRContext *fromCtx, CRContext *toCtx)
|
---|
328 | {
|
---|
329 | /* Apparently, no occlusion state switching needed */
|
---|
330 | /* Note: we better not do a switch while we're inside a glBeginQuery/
|
---|
331 | * glEndQuery sequence.
|
---|
332 | */
|
---|
333 | (void)bb; (void)bitID; (void)fromCtx; (void)toCtx;
|
---|
334 | CRASSERT(!fromCtx->occlusion.currentQueryObject);
|
---|
335 | }
|
---|