File: SoGLCacheContextElement.h

package info (click to toggle)
inventor 2.1.5-10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 35,712 kB
  • sloc: ansic: 33,864; lisp: 7,361; cpp: 3,874; yacc: 369; sh: 360; perl: 234; awk: 141; makefile: 80; csh: 35; sed: 11
file content (302 lines) | stat: -rw-r--r-- 10,845 bytes parent folder | download | duplicates (12)
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
/*
 *
 *  Copyright (C) 2000 Silicon Graphics, Inc.  All Rights Reserved. 
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  Further, this software is distributed without any warranty that it is
 *  free of the rightful claim of any third person regarding infringement
 *  or the like.  Any license provided herein, whether implied or
 *  otherwise, applies only to this software file.  Patent licenses, if
 *  any, provided herein do not apply to combinations of this program with
 *  other software, or any other product whatsoever.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 *  Mountain View, CA  94043, or:
 * 
 *  http://www.sgi.com 
 * 
 *  For further information regarding this notice, see: 
 * 
 *  http://oss.sgi.com/projects/GenInfo/NoticeExplan/
 *
 */

//  -*- C++ -*-

/*
 * Copyright (C) 1990,91   Silicon Graphics, Inc.
 *
 _______________________________________________________________________
 ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
 |
 |   $Revision: 1.1.1.1 $
 |
 |   Description:
 |	This file defines the SoGLCacheContextElement class.
 |
 |   Author(s)		: Gavin Bell
 |
 ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
 _______________________________________________________________________
 */

#ifndef  _SO_CACHE_CONTEXT_ELEMENT
#define  _SO_CACHE_CONTEXT_ELEMENT

#include <GL/gl.h>
#include <Inventor/elements/SoSubElement.h>

//////////////////////////////////////////////////////////////////////////////
//
//  Class: SoCacheContextElement
//
//  Element that stores the cache context.  There is a method on the
//  render action to set this; if you are not rendering onto multiple
//  displays, you will not need to set this (assuming that caches are
//  shareable between different windows on the same display, which is
//  true of GLX).
//
//  This element should be set before traversal starts, and must not
//  be changed during traversal (you'll get a debug error if a node
//  tries to set the cache context during traversal).
//
//  This method also has API for quickly finding out whether or not
//  OpenGL extensions are supported.  Code that uses extension "foo"
//  should look something like:
//  #ifdef GL_EXT_foo
//      static int fooExtInt = -1;
//      if (fooExtInt == -1)
//          fooExtInt = SoGLCacheContextElement::getExtID("GL_EXT_foo");
//      if (SoGLCacheContextElement::extSupported(state, fooExtInt)) {
//          glFoo(... make extension foo calls...);
//      } else {
//  #endif
//          Extension not supported, make regular GL calls
//  #ifdef GL_EXT_foo
//      }
//  #endif
//
//  Arranging the code that way ensures that it both compiles on
//  systems that don't support the extension AND will run on any
//  OpenGL-capable machine, even if the application displays on
//  multiple displays (only some of which may support the extension).
//
//////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
//
//  Class SoGLDisplayList:
//
//  A helper class used to store OpenGL display-list-like objects.
//  Currently, it can store either texture objects (which must be
//  treated like display lists; texture objects bound inside a display
//  list must be reference counted, etc) and display lists.
//
////////////////////////////////////////////////////////////////////////

SoEXTENDER class SoGLDisplayList {

  public:
    //
    // Constructor.  Takes state, type, and number of lists to
    // allocate.  Calls either glGenLists() or glGenTexturesEXT() to
    // create empty objects/display lists.  If texture objects are not
    // supported, the type will revert to DISPLAY_LIST.
    //
    enum Type {
	DISPLAY_LIST,
	TEXTURE_OBJECT
	};
    SoGLDisplayList(SoState *state, Type type, int numToAllocate=1);

    //
    // Because display lists may contain textures objects or other
    // display lists, they must be reference counted.
    // You can pass a NULL state to unref(); deletion of the display
    // list or texture object will then be delayed until a render
    // action is applied in the correct context.
    void ref();
    void unref(SoState *state = NULL);

    //
    // Open/close a display list.  Display lists are done in
    // COMPILE_AND_EXECUTE mode, so you don't need to call() the
    // display list after close().
    // Opening a texture object binds it; closing it does nothing.
    void open(SoState *state, int index = 0);
    void close(SoState *state);

    //
    // Call a display list, or bind a texture object.  This
    // automatically sets up a dependency if there is another display
    // list open in the state.
    // You can also use the get() methods below and make the OpenGL
    // calls yourself, in which case you should call the
    // addDependency() method to do the correct reference counting...
    void call(SoState *state, int index = 0);
    void addDependency(SoState *state);

    //
    // Get methods
    //
    Type getType() { return type; }
    int getNumAllocated() { return num; }
    GLuint getFirstIndex() { return startIndex; }
    int getContext() { return context; }

  private:
    ~SoGLDisplayList();

    Type type;
    GLuint startIndex;
    int num;
    int refCount;
    int	context;
    static int texture_object_extensionID;
friend class SoGLCacheContextElement;
};

class SbIntList;

SoEXTENDER class SoGLCacheContextElement : public SoElement {

    SO_ELEMENT_HEADER(SoGLCacheContextElement);

  public:
    // Initializes element
    virtual void	init(SoState *state);

    // Sets the current context.  This is done by the renderAction;
    // theoretically, a node that redirected the GL rendering context
    // to another X server would also set this.  This also frees up
    // any display lists that are waiting for the context to become
    // valid. The third argument indicates whether 2-pass
    // transparency is in effect (as for DELAYED or SORTED blending),
    // which means that caches containing transparent objects may not
    // be valid.  The fourth argument should be TRUE if remote
    // rendering is being done; the SoSeparator auto-caching algorithm
    // uses this information to help decide whether or not to build
    // caches.
    static void		set(SoState *state, int context,
			    SbBool is2PassTransparency,
			    SbBool isRemoteRendering);

    // Gets the current context
    static int		get(SoState *state);

    // Methods to quickly figure out if an OpenGL extension is
    // available at run-time.

    // This is the slow routine-- convert from a string to an integer.
    // The integer should be saved to do quick lookups:
    static int		getExtID(const char *str);

    // This routine is pretty quick:
    static SbBool	extSupported(SoState *state, int ext);

    // TRUE if mip-mapped textures are known to be fast:
    static SbBool	areMipMapsFast(SoState *state);

    // Methods used to improve auto-caching algorithm:

    // Two bits are stored.  Nodes that should be cached will set the
    // DO_AUTO_CACHE bit, nodes that should NOT be cached will set the
    // DONT_AUTO_CACHE bit.  By default, DO_AUTO_CACHE is FALSE unless
    // remote rendering is being done.  DONT_AUTO_CACHE is FALSE by
    // default.  Separators will auto cache if DO_AUTO_CACHE is TRUE
    // and DONT_AUTO_CACHE is FALSE, otherwise they won't auto-cache.
    enum {
	DO_AUTO_CACHE = 1,   // Hack warning: I rely on TRUE==DO_AUTO_CACHE
	DONT_AUTO_CACHE = 2
    };

    // Called by nodes to say that they should/shouldn't be
    // auto-cached (pass TRUE if should, FALSE if shouldn't, don't
    // call this method at all if the node doesn't care):
    static void		shouldAutoCache(SoState *state, int bits)
    {
	SoGLCacheContextElement *elt = (SoGLCacheContextElement *)
	    state->getElementNoPush(classStackIndex);
	elt->autoCacheBits |= bits;
    }

    // Used by Separators to set/reset the auto-caching bits:
    static void		setAutoCacheBits(SoState *state, int bits)
	{ ((SoGLCacheContextElement *)state->getElementNoPush(
	    classStackIndex))->autoCacheBits = bits;
        }

    static int		resetAutoCacheBits(SoState *state)
	{
	    SoGLCacheContextElement *elt = (SoGLCacheContextElement *)
		state->getElementNoPush(classStackIndex);
	    int result = elt->autoCacheBits;
	    // Hack warning: I rely on TRUE==DO_AUTO_CACHE
	    elt->autoCacheBits = elt->isRemoteRendering;
	    
	    return result;
	}

    static SbBool	getIsRemoteRendering(SoState *state)
	{
	    const SoGLCacheContextElement *elt =
		(const SoGLCacheContextElement *)
		    state->getConstElement(classStackIndex);
	    return elt->isRemoteRendering;
	}

    // Standard element stuff:

    // Returns TRUE if the widths match in both elements
    virtual SbBool	matches(const SoElement *elt) const;

    // Create and return a copy of this element
    virtual SoElement	*copyMatchInfo() const;

    // Prints element (for debugging)
    virtual void	print(FILE *fp) const;

  SoINTERNAL public:
    static void		initClass();

  protected:
    int			context;
    SbBool		is2PassTransp;
    SbBool		isRemoteRendering;
    int			autoCacheBits;

    virtual ~SoGLCacheContextElement();

  private:
    // Frees up the given display lists right away, if possible.  If
    // not possible (because the state passed in is NULL or has a
    // different cache context than the display lists' state), this
    // adds the given display list/count to the list of display lists
    // that need to be freed the next time the given context is valid.
    // This method is necessary because nodes with caches can be
    // deleted at any time, but we can't necessarily send GL commands
    // to free up a display list at any time.
    static void		freeList(SoState *state,
				 SoGLDisplayList *list);

    static SbPList *	waitingToBeFreed;	// Allocated in ::init
    static SbPList *	extensionList;		// Allocated in ::init
    static SbIntList *	mipmapSupportList;	// Allocated in ::init

friend class SoGLDisplayList;
};

#endif /* _SO_CACHE_CONTEXT_ELEMENT */