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