Debian Codegraph @

lib/database/src/so/elements/SoGLCacheContextElement.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.2 $
44 |
45 | Classes:
46 | SoGLCacheContextElement
47 |
48 | Author(s) : Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <cstdlib>
55 #include <Inventor/caches/SoGLRenderCache.h>
56 #include <Inventor/elements/SoCacheElement.h>
57 #include <Inventor/elements/SoGLCacheContextElement.h>
58 #include <Inventor/errors/SoDebugError.h>
59 #include <Inventor/misc/SoState.h>
60 #include <Inventor/SbString.h>
61 #include <Inventor/SbPList.h>
62
63 SO_ELEMENT_SOURCE(SoGLCacheContextElement);
64
65 SbPList *SoGLCacheContextElement::waitingToBeFreed = NULL;
66 SbPList *SoGLCacheContextElement::extensionList = NULL;
67 SbIntList *SoGLCacheContextElement::mipmapSupportList = NULL;
68
69 // Internal struct:
70 struct extInfo {
71 SbString string;
72 SbIntList support;
73 };
74
75 ////////////////////////////////////////////////////////////////////////
76 //
77 // Description:
78 // Destructor.
79 //
80 // Use: private
81
82 SoGLCacheContextElement::~SoGLCacheContextElement()
83 //
84 ////////////////////////////////////////////////////////////////////////
85 {
86 }
87
88 ////////////////////////////////////////////////////////////////////////
89 //
90 // Description:
91 // Initializes element
92 //
93 // Use: public
94
95 void
96 SoGLCacheContextElement::init(SoState *)
97 //
98 ////////////////////////////////////////////////////////////////////////
99 {
100 context = 0;
101 is2PassTransp = FALSE;
102 autoCacheBits = 0;
103 }
104
105 ////////////////////////////////////////////////////////////////////////
106 //
107 // Description:
108 // Sets cache context
109 //
110 // Use: public, static
111
112 void
113 SoGLCacheContextElement::set(SoState *state, int ctx,
114 SbBool is2PassTransparency,
115 SbBool remoteRender)
116 //
117 ////////////////////////////////////////////////////////////////////////
118 {
119 #ifdef DEBUG
120 if (state->getDepth() != 1) {
121 SoDebugError::post("SoGLCacheContextElement::set",
122 "must not be set during traversal");
123 }
124 #endif
125
126 SoGLCacheContextElement *elt = (SoGLCacheContextElement *)
127 state->getElementNoPush(classStackIndex);
128
129 elt->context = ctx;
130 elt->is2PassTransp = is2PassTransparency;
131 elt->isRemoteRendering = remoteRender;
132 if (remoteRender) elt->autoCacheBits = DO_AUTO_CACHE;
133 else elt->autoCacheBits = 0;
134
135 // Look through the list of display lists waiting to be freed, and
136 // free any that match the context:
137 for (int i = waitingToBeFreed->getLength()-1; i >= 0; i--) {
138 SoGLDisplayList *dl = (SoGLDisplayList *)(*waitingToBeFreed)[i];
139 if (dl->getContext() == ctx) {
140 waitingToBeFreed->remove(i);
141 delete dl;
142 }
143 }
144 }
145
146 ////////////////////////////////////////////////////////////////////////
147 //
148 // Description:
149 // Gets the cache context. GL render caches do this when they are
150 // opened (they need the context to free themselves properly).
151 //
152 // Use: public, static
153
154 int
155 SoGLCacheContextElement::get(SoState *state)
156 //
157 ////////////////////////////////////////////////////////////////////////
158 {
159 const SoGLCacheContextElement *elt;
160
161 elt = (const SoGLCacheContextElement *)
162 getConstElement(state, classStackIndex);
163
164 return elt->context;
165 }
166
167 ////////////////////////////////////////////////////////////////////////
168 //
169 // Description:
170 // Frees up the given display lists right away, if possible. If
171 // not possible (because the state passed in is NULL or has a
172 // different cache context than the display lists' state), this
173 // adds the given display list/count to the list of display lists
174 // that need to be freed the next time the given context is valid.
175 // This method is necessary because nodes with caches can be
176 // deleted at any time, but we can't necessarily send GL commands
177 // to free up a display list at any time.
178 //
179 // Use: public, static
180
181 void
182 SoGLCacheContextElement::freeList(SoState *state,
183 SoGLDisplayList *dl)
184 //
185 ////////////////////////////////////////////////////////////////////////
186 {
187 if (state != NULL && get(state) == dl->getContext()) {
188 delete dl;
189 } else {
190 waitingToBeFreed->append(dl);
191 }
192 }
193
194 ////////////////////////////////////////////////////////////////////////
195 //
196 // Description:
197 // Converts an extension string to a unique integer to be used for
198 // faster lookup (to avoid string comparisons)
199 //
200 // Use: public
201
202 int
203 SoGLCacheContextElement::getExtID(const char *str)
204 //
205 ////////////////////////////////////////////////////////////////////////
206 {
207 for (int i = 0; i < extensionList->getLength(); i++) {
208 extInfo *e = (extInfo *)(*extensionList)[i];
209 if (e->string == str) return i;
210 }
211 extInfo *e = new extInfo;
212 e->string = str;
213 extensionList->append(e);
214 return extensionList->getLength()-1;
215 }
216
217 ////////////////////////////////////////////////////////////////////////
218 //
219 // Description:
220 // Given a unique extension ID, return TRUE if that extension is
221 // supported in this rendering context.
222 //
223 SbBool
224 SoGLCacheContextElement::extSupported(SoState *state, int ext)
225 //
226 ////////////////////////////////////////////////////////////////////////
227 {
228 #ifdef DEBUG
229 if (ext >= extensionList->getLength()) {
230 SoDebugError::post("SoGLCacheContextElement::extSupported",
231 "Bad extension ID passed; "
232 "you MUST use SoGLCacheContextElement::getExtID");
233 }
234 #endif
235 extInfo *e = (extInfo *)(*extensionList)[ext];
236 int ctx = get(state);
237
238 // The support list is a list of context,flag pairs (flag is TRUE
239 // if the render context supports the extension). This linear
240 // search assumes that there will be a small number of render
241 // contexts.
242 for (int i = 0; i < e->support.getLength(); i+=2) {
243 if (e->support[i] == ctx) return e->support[i+1];
244 }
245 // Ask GL if supported:
246 SbBool supported =
247 strstr((const char *)glGetString(GL_EXTENSIONS),
248 e->string.getString())
249 != NULL;
250 e->support.append(ctx);
251 e->support.append(supported);
252
253 return supported;
254 }
255
256 ////////////////////////////////////////////////////////////////////////
257 //
258 // Description:
259 // Returns TRUE if the current rendering context does
260 // mip-mapped textures quickly.
261 //
262 SbBool
263 SoGLCacheContextElement::areMipMapsFast(SoState *state)
264 //
265 ////////////////////////////////////////////////////////////////////////
266 {
267 int ctx = get(state);
268
269 // See if we already know:
270 for (int i = 0; i < mipmapSupportList->getLength(); i+=2) {
271 if ((*mipmapSupportList)[i] == ctx)
272 return (*mipmapSupportList)[i+1];
273 }
274 // Don't know, figure it out:
275
276 SbBool result;
277 if (strncmp((const char *)glGetString(GL_VENDOR), "SGI", 3) == 0) {
278
279 const char *renderer = (const char *)glGetString(GL_RENDERER);
280 // Indy and XL
281 if (strncmp(renderer, "NEWPORT", 7) == 0)
282 result = FALSE;
283 // Personal Iris, XS, XZ, Extreme...
284 else if (strncmp(renderer, "GR", 2) == 0)
285 result = FALSE;
286 else if (strncmp(renderer, "GU", 2) == 0)
287 result = FALSE;
288 // VGX and VGXT
289 else if (strncmp(renderer, "VGX", 3) == 0)
290 result = FALSE;
291 // Indigo Entry
292 else if (strncmp(renderer, "LIGHT", 5) == 0)
293 result = FALSE;
294 // IndigoII Impact
295 else if (strncmp(renderer, "IMPACT", 6) == 0)
296 result = TRUE;
297 else
298 result = TRUE; // Re's are fast, assume all future SGI
299 // machines will do fast texturing.
300 }
301 else result = FALSE; // Assume non-SGI machines do texturing slowly
302
303 mipmapSupportList->append(ctx);
304 mipmapSupportList->append(result);
305 return result;
306 }
307
308 ////////////////////////////////////////////////////////////////////////
309 //
310 // Description:
311 // Overrides this method to compare contexts.
312 //
313 // Use: public
314
315 SbBool
316 SoGLCacheContextElement::matches(const SoElement *elt) const
317 //
318 ////////////////////////////////////////////////////////////////////////
319 {
320
321 const SoGLCacheContextElement *cacheElt;
322
323 cacheElt = (const SoGLCacheContextElement *) elt;
324
325 return (context == cacheElt->context &&
326 is2PassTransp == cacheElt->is2PassTransp);
327 }
328
329 ////////////////////////////////////////////////////////////////////////
330 //
331 // Description:
332 // Create a copy of this instance suitable for calling matches()
333 // on.
334 //
335 // Use: protected
336
337 SoElement *
338 SoGLCacheContextElement::copyMatchInfo() const
339 //
340 ////////////////////////////////////////////////////////////////////////
341 {
342 SoGLCacheContextElement *result =
343 (SoGLCacheContextElement *)getTypeId().createInstance();
344
345 result->context = context;
346 result->is2PassTransp = is2PassTransp;
347
348 return result;
349 }
350
351 ////////////////////////////////////////////////////////////////////////
352 //
353 // Description:
354 // Prints element for debugging.
355 //
356 // Use: public
357 //
358 ////////////////////////////////////////////////////////////////////////
359
360 #ifdef DEBUG
361 void
362 SoGLCacheContextElement::print(FILE *fp) const
363 {
364 SoElement::print(fp);
365
366 fprintf(fp, "\tCache context = %uL, is2PassTransp = %s\n",
367 context, is2PassTransp ? "TRUE" : "FALSE");
368 }
369 #else /* DEBUG */
370 void
371 SoGLCacheContextElement::print(FILE *) const
372 {
373 }
374 #endif /* DEBUG */
375
376
377 int SoGLDisplayList::texture_object_extensionID = -1;
378
379 //
380 // Workaround for nasty OpenGL bug: we don't use the texture object
381 // extension if the user sets the IV_NO_TEXTURE_OBJECT environment
382 // variable, because texture objects are not correctly shared between
383 // contexts on RE and impact so multi-windowed applications will
384 // display textures in only one window.
385 // -- gavin 10/3/95
386 //
387 static int never_use_texture_object = -1; // "don't know" initial value
388
389 ////////////////////////////////////////////////////////////////////////
390 //
391 // Description:
392 // Constructor
393 //
394 // Use: public
395
396 SoGLDisplayList::SoGLDisplayList(SoState *state, Type _type,
397 int numToAllocate)
398 //
399 ////////////////////////////////////////////////////////////////////////
400 {
401 refCount = 0;
402
403 num = numToAllocate;
404
405 // We must depend on the GL cache context; we can't assume that a
406 // cache is valid between any two render actions, since the render
407 // actions could be directed at different X servers on different
408 // machines (with different ideas about which display lists have
409 // been created).
410 context = SoGLCacheContextElement::get(state);
411
412 #ifdef GL_EXT_texture_object
413 if (texture_object_extensionID == -1) {
414 texture_object_extensionID =
415 SoGLCacheContextElement::getExtID("GL_EXT_texture_object");
416 }
417 int texObjSupported = SoGLCacheContextElement::extSupported(state,
418 texture_object_extensionID);
419
420 if (never_use_texture_object == -1) {
421 if (getenv("IV_NO_TEXTURE_OBJECT"))
422 never_use_texture_object = 1;
423 else
424 never_use_texture_object = 0;
425 }
426 if (never_use_texture_object) texObjSupported = 0;
427
428 #else
429 int texObjSupported = FALSE;
430 #endif
431
432 // Always use display lists if texture objects are not supported:
433 if (_type == TEXTURE_OBJECT && !texObjSupported)
434 type = DISPLAY_LIST;
435 else
436 type = _type;
437
438 if (type == TEXTURE_OBJECT) {
439 #ifdef GL_EXT_texture_object
440 #ifdef GL_VERSION_1_1
441 glGenTextures(1, &startIndex);
442 #else
443 glGenTexturesEXT(1, &startIndex);
444 #endif
445 #ifdef DEBUG
446 if (num != 1)
447 SoDebugError::post("SoGLDisplayList", "Sorry, can only "
448 "construct 1 texture object at a time");
449 #endif
450 #else
451 #ifdef DEBUG
452 SoDebugError::post("SoGLDisplayList", "Texture objects not "
453 "supported, but type not reset!");
454 #endif
455 #endif
456 } else {
457 startIndex = glGenLists(num);
458 }
459 }
460
461 ////////////////////////////////////////////////////////////////////////
462 //
463 // Description:
464 //
465 //
466 // Use: public
467
468 void
469 SoGLDisplayList::ref()
470 //
471 ////////////////////////////////////////////////////////////////////////
472 {
473 ++refCount;
474 }
475
476 ////////////////////////////////////////////////////////////////////////
477 //
478 // Description:
479 //
480 //
481 // Use: public
482
483 void
484 SoGLDisplayList::unref(SoState *state)
485 //
486 ////////////////////////////////////////////////////////////////////////
487 {
488 --refCount;
489 if (refCount <= 0) {
490 // Let the CacheContextElement delete us:
491 SoGLCacheContextElement::freeList(state, this);
492 }
493 }
494
495 ////////////////////////////////////////////////////////////////////////
496 //
497 // Description:
498 //
499 //
500 // Use: public
501
502 void
503 SoGLDisplayList::open(SoState *, int index)
504 //
505 ////////////////////////////////////////////////////////////////////////
506 {
507 if (type == TEXTURE_OBJECT) {
508 #ifdef GL_EXT_texture_object
509 #ifdef GL_VERSION_1_1
510 glBindTexture(GL_TEXTURE_2D, startIndex+index);
511 #else
512 glBindTextureEXT(GL_TEXTURE_2D, startIndex+index);
513 #endif
514 #endif
515 } else {
516 glNewList(startIndex+index, GL_COMPILE_AND_EXECUTE);
517 }
518 }
519
520 ////////////////////////////////////////////////////////////////////////
521 //
522 // Description:
523 //
524 //
525 // Use: public
526
527 void
528 SoGLDisplayList::close(SoState *)
529 //
530 ////////////////////////////////////////////////////////////////////////
531 {
532 if (type != TEXTURE_OBJECT) {
533 glEndList();
534 }
535 }
536
537 ////////////////////////////////////////////////////////////////////////
538 //
539 // Description:
540 //
541 //
542 // Use: public
543
544 void
545 SoGLDisplayList::call(SoState *state, int index)
546 //
547 ////////////////////////////////////////////////////////////////////////
548 {
549 if (type == TEXTURE_OBJECT) {
550 #ifdef GL_EXT_texture_object
551 #ifdef GL_VERSION_1_1
552 glBindTexture(GL_TEXTURE_2D, startIndex+index);
553 #else
554 glBindTextureEXT(GL_TEXTURE_2D, startIndex+index);
555 #endif
556 #endif
557 } else {
558 glCallList(startIndex+index);
559 }
560 addDependency(state);
561 }
562
563 ////////////////////////////////////////////////////////////////////////
564 //
565 // Description:
566 //
567 //
568 // Use: public
569
570 void
571 SoGLDisplayList::addDependency(SoState *state)
572 //
573 ////////////////////////////////////////////////////////////////////////
574 {
575 if (state->isCacheOpen()) {
576 SoGLRenderCache *c = (SoGLRenderCache *)
577 SoCacheElement::getCurrentCache(state);
578 c->addNestedCache(this);
579 }
580 }
581
582 ////////////////////////////////////////////////////////////////////////
583 //
584 // Description:
585 //
586 //
587 // Use: public
588
589 SoGLDisplayList::~SoGLDisplayList()
590 //
591 ////////////////////////////////////////////////////////////////////////
592 {
593 if (type == TEXTURE_OBJECT) {
594 #ifdef GL_EXT_texture_object
595 #ifdef GL_VERSION_1_1
596 glDeleteTextures(1, &startIndex);
597 #else
598 glDeleteTexturesEXT(1, &startIndex);
599 #endif
600 #endif
601 } else {
602 glDeleteLists(startIndex, num);
603 }
604 }