root/OpenSceneGraph/trunk/include/osg/Texture @ 10597

Revision 10597, 50.7 kB (checked in by robert, 4 years ago)

Added handling of resetting of the texture object format

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_TEXTURE
15#define OSG_TEXTURE 1
16
17#include <osg/GL>
18#include <osg/Image>
19#include <osg/StateAttribute>
20#include <osg/GraphicsContext>
21#include <osg/ref_ptr>
22#include <osg/Vec4>
23#include <osg/Vec4d>
24#include <osg/buffered_value>
25
26#include <list>
27#include <map>
28
29// If not defined by gl.h use the definition found in:
30// http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_filter_anisotropic.txt
31#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
32    #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
33#endif
34
35#ifndef GL_ARB_texture_compression
36    #define GL_COMPRESSED_ALPHA_ARB                 0x84E9
37    #define GL_COMPRESSED_LUMINANCE_ARB             0x84EA
38    #define GL_COMPRESSED_LUMINANCE_ALPHA_ARB       0x84EB
39    #define GL_COMPRESSED_INTENSITY_ARB             0x84EC
40    #define GL_COMPRESSED_RGB_ARB                   0x84ED
41    #define GL_COMPRESSED_RGBA_ARB                  0x84EE
42    #define GL_TEXTURE_COMPRESSION_HINT_ARB         0x84EF
43    #define GL_TEXTURE_COMPRESSED_ARB               0x86A1
44    #define GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB   0x86A2
45    #define GL_COMPRESSED_TEXTURE_FORMATS_ARB       0x86A3
46#endif
47
48#ifndef GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB
49    #define GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB    0x86A0
50#endif
51
52#ifndef GL_EXT_texture_compression_s3tc
53    #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT         0x83F0
54    #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT        0x83F1
55    #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT        0x83F2
56    #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT        0x83F3
57#endif
58
59#ifndef GL_ARB_INTERNAL_TEXTURE_FORMAT
60    #define GL_RGBA32F_ARB                           0x8814
61    #define GL_RGB32F_ARB                            0x8815
62    #define GL_ALPHA32F_ARB                          0x8816
63    #define GL_INTENSITY32F_ARB                      0x8817
64    #define GL_LUMINANCE32F_ARB                      0x8818
65    #define GL_LUMINANCE_ALPHA32F_ARB                0x8819
66    #define GL_RGBA16F_ARB                           0x881A
67    #define GL_RGB16F_ARB                            0x881B
68    #define GL_ALPHA16F_ARB                          0x881C
69    #define GL_INTENSITY16F_ARB                      0x881D
70    #define GL_LUMINANCE16F_ARB                      0x881E
71    #define GL_LUMINANCE_ALPHA16F_ARB                0x881F
72#endif
73
74#ifndef GL_ARB_PIXEL_DATA
75    #define GL_HALF_FLOAT_ARB                        0x140B
76#endif
77
78#ifndef GL_NV_texture_shader
79    #define GL_HILO_NV                              0x86F4
80    #define GL_DSDT_NV                              0x86F5
81    #define GL_DSDT_MAG_NV                          0x86F6
82    #define GL_DSDT_MAG_VIB_NV                      0x86F7
83    #define GL_HILO16_NV                            0x86F8
84    #define GL_SIGNED_HILO_NV                       0x86F9
85    #define GL_SIGNED_HILO16_NV                     0x86FA
86    #define GL_SIGNED_RGBA_NV                       0x86FB
87    #define GL_SIGNED_RGBA8_NV                      0x86FC
88    #define GL_SIGNED_RGB_NV                        0x86FE
89    #define GL_SIGNED_RGB8_NV                       0x86FF
90    #define GL_SIGNED_LUMINANCE_NV                  0x8701
91    #define GL_SIGNED_LUMINANCE8_NV                 0x8702
92    #define GL_SIGNED_LUMINANCE_ALPHA_NV            0x8703
93    #define GL_SIGNED_LUMINANCE8_ALPHA8_NV          0x8704
94    #define GL_SIGNED_ALPHA_NV                      0x8705
95    #define GL_SIGNED_ALPHA8_NV                     0x8706
96    #define GL_SIGNED_INTENSITY_NV                  0x8707
97    #define GL_SIGNED_INTENSITY8_NV                 0x8708
98    #define GL_DSDT8_NV                             0x8709
99    #define GL_DSDT8_MAG8_NV                        0x870A
100    #define GL_DSDT8_MAG8_INTENSITY8_NV             0x870B
101    #define GL_SIGNED_RGB_UNSIGNED_ALPHA_NV         0x870C
102    #define GL_SIGNED_RGB8_UNSIGNED_ALPHA8_NV       0x870D
103#endif
104
105#ifndef GL_NV_float_buffer
106    #define GL_FLOAT_R_NV                           0x8880
107    #define GL_FLOAT_RG_NV                          0x8881
108    #define GL_FLOAT_RGB_NV                         0x8882
109    #define GL_FLOAT_RGBA_NV                        0x8883
110    #define GL_FLOAT_R16_NV                         0x8884
111    #define GL_FLOAT_R32_NV                         0x8885
112    #define GL_FLOAT_RG16_NV                        0x8886
113    #define GL_FLOAT_RG32_NV                        0x8887
114    #define GL_FLOAT_RGB16_NV                       0x8888
115    #define GL_FLOAT_RGB32_NV                       0x8889
116    #define GL_FLOAT_RGBA16_NV                      0x888A
117    #define GL_FLOAT_RGBA32_NV                      0x888B
118#endif
119
120#ifndef GL_NV_half_float
121    #define GL_HALF_FLOAT_NV                        0x140B
122#endif
123
124#ifndef GL_ATI_texture_float
125    #define GL_RGBA_FLOAT32_ATI                     0x8814
126    #define GL_RGB_FLOAT32_ATI                      0x8815
127    #define GL_ALPHA_FLOAT32_ATI                    0x8816
128    #define GL_INTENSITY_FLOAT32_ATI                0x8817
129    #define GL_LUMINANCE_FLOAT32_ATI                0x8818
130    #define GL_LUMINANCE_ALPHA_FLOAT32_ATI          0x8819
131    #define GL_RGBA_FLOAT16_ATI                     0x881A
132    #define GL_RGB_FLOAT16_ATI                      0x881B
133    #define GL_ALPHA_FLOAT16_ATI                    0x881C
134    #define GL_INTENSITY_FLOAT16_ATI                0x881D
135    #define GL_LUMINANCE_FLOAT16_ATI                0x881E
136    #define GL_LUMINANCE_ALPHA_FLOAT16_ATI          0x881F
137#endif
138
139#ifndef GL_MIRRORED_REPEAT_IBM
140    #define GL_MIRRORED_REPEAT_IBM            0x8370
141#endif
142
143#ifndef GL_CLAMP_TO_EDGE
144    #define GL_CLAMP_TO_EDGE                  0x812F
145#endif
146
147#ifndef GL_CLAMP_TO_BORDER_ARB
148    #define GL_CLAMP_TO_BORDER_ARB            0x812D
149#endif
150
151#ifndef GL_GENERATE_MIPMAP_SGIS
152    #define GL_GENERATE_MIPMAP_SGIS           0x8191
153    #define GL_GENERATE_MIPMAP_HINT_SGIS      0x8192
154#endif
155
156#ifndef GL_TEXTURE_3D
157    #define GL_TEXTURE_3D                     0x806F
158#endif
159
160#ifndef GL_TEXTURE_2D_ARRAY_EXT
161    #define GL_TEXTURE_2D_ARRAY_EXT           0x8C1A
162    #define GL_TEXTURE_2D_ARRAY_EXT                        0x8C1A
163    #define GL_PROXY_TEXTURE_2D_ARRAY_EXT                  0x8C1B
164    #define GL_TEXTURE_BINDING_2D_ARRAY_EXT                0x8C1D
165    #define GL_MAX_ARRAY_TEXTURE_LAYERS_EXT                0x88FF
166    #define GL_COMPARE_REF_DEPTH_TO_TEXTURE_EXT            0x884E
167    #define GL_SAMPLER_2D_ARRAY_EXT                        0x8DC1
168    #define GL_SAMPLER_2D_ARRAY_SHADOW_EXT                 0x8DC4
169    #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER_EXT    0x8CD4
170#endif
171
172#ifndef GL_TEXTURE_BINDING_3D
173    #define GL_TEXTURE_BINDING_3D             0x806A
174#endif
175
176#ifndef GL_DEPTH_TEXTURE_MODE_ARB
177    #define GL_DEPTH_TEXTURE_MODE_ARB         0x884B
178#endif
179
180#ifndef GL_TEXTURE_COMPARE_MODE_ARB
181    #define GL_TEXTURE_COMPARE_MODE_ARB       0x884C
182#endif
183#ifndef GL_TEXTURE_COMPARE_FUNC_ARB
184    #define GL_TEXTURE_COMPARE_FUNC_ARB       0x884D
185#endif
186#ifndef GL_COMPARE_R_TO_TEXTURE_ARB
187    #define GL_COMPARE_R_TO_TEXTURE_ARB       0x884E
188#endif
189
190#ifndef TEXTURE_COMPARE_FAIL_VALUE_ARB
191    #define TEXTURE_COMPARE_FAIL_VALUE_ARB    0x80BF
192#endif
193
194#if !defined( GL_MAX_TEXTURE_UNITS )
195    #define GL_MAX_TEXTURE_UNITS              0x84E2
196#endif
197
198#ifndef  GL_TEXTURE_DEPTH
199    #define GL_TEXTURE_DEPTH                  0x8071
200#endif
201
202// Integer teture extension as in http://www.opengl.org/registry/specs/EXT/texture_integer.txt
203#ifndef GL_EXT_texture_integer
204    #define GL_RGBA32UI_EXT                                    0x8D70
205    #define GL_RGB32UI_EXT                                     0x8D71
206    #define GL_ALPHA32UI_EXT                                   0x8D72
207    #define GL_INTENSITY32UI_EXT                               0x8D73
208    #define GL_LUMINANCE32UI_EXT                               0x8D74
209    #define GL_LUMINANCE_ALPHA32UI_EXT                         0x8D75
210
211    #define GL_RGBA16UI_EXT                                    0x8D76
212    #define GL_RGB16UI_EXT                                     0x8D77
213    #define GL_ALPHA16UI_EXT                                   0x8D78
214    #define GL_INTENSITY16UI_EXT                               0x8D79
215    #define GL_LUMINANCE16UI_EXT                               0x8D7A
216    #define GL_LUMINANCE_ALPHA16UI_EXT                         0x8D7B
217
218    #define GL_RGBA8UI_EXT                                     0x8D7C
219    #define GL_RGB8UI_EXT                                      0x8D7D
220    #define GL_ALPHA8UI_EXT                                    0x8D7E
221    #define GL_INTENSITY8UI_EXT                                0x8D7F
222    #define GL_LUMINANCE8UI_EXT                                0x8D80
223    #define GL_LUMINANCE_ALPHA8UI_EXT                          0x8D81
224
225    #define GL_RGBA32I_EXT                                     0x8D82
226    #define GL_RGB32I_EXT                                      0x8D83
227    #define GL_ALPHA32I_EXT                                    0x8D84
228    #define GL_INTENSITY32I_EXT                                0x8D85
229    #define GL_LUMINANCE32I_EXT                                0x8D86
230    #define GL_LUMINANCE_ALPHA32I_EXT                          0x8D87
231
232    #define GL_RGBA16I_EXT                                     0x8D88
233    #define GL_RGB16I_EXT                                      0x8D89
234    #define GL_ALPHA16I_EXT                                    0x8D8A
235    #define GL_INTENSITY16I_EXT                                0x8D8B
236    #define GL_LUMINANCE16I_EXT                                0x8D8C
237    #define GL_LUMINANCE_ALPHA16I_EXT                          0x8D8D
238
239    #define GL_RGBA8I_EXT                                      0x8D8E
240    #define GL_RGB8I_EXT                                       0x8D8F
241    #define GL_ALPHA8I_EXT                                     0x8D90
242    #define GL_INTENSITY8I_EXT                                 0x8D91
243    #define GL_LUMINANCE8I_EXT                                 0x8D92
244    #define GL_LUMINANCE_ALPHA8I_EXT                           0x8D93
245
246    #define GL_RED_INTEGER_EXT                                 0x8D94
247    #define GL_GREEN_INTEGER_EXT                               0x8D95
248    #define GL_BLUE_INTEGER_EXT                                0x8D96
249    #define GL_ALPHA_INTEGER_EXT                               0x8D97
250    #define GL_RGB_INTEGER_EXT                                 0x8D98
251    #define GL_RGBA_INTEGER_EXT                                0x8D99
252    #define GL_BGR_INTEGER_EXT                                 0x8D9A
253    #define GL_BGRA_INTEGER_EXT                                0x8D9B
254    #define GL_LUMINANCE_INTEGER_EXT                           0x8D9C
255    #define GL_LUMINANCE_ALPHA_INTEGER_EXT                     0x8D9D
256
257    #define GL_RGBA_INTEGER_MODE_EXT                           0x8D9E
258#endif
259
260namespace osg {
261
262
263/** Texture pure virtual base class that encapsulates OpenGl texture
264  * functionality common to the various types of OSG textures.
265*/
266class OSG_EXPORT Texture : public osg::StateAttribute
267{
268
269    public :
270   
271        static unsigned int s_numberTextureReusedLastInLastFrame;
272        static unsigned int s_numberNewTextureInLastFrame;
273        static unsigned int s_numberDeletedTextureInLastFrame;
274
275        Texture();
276
277        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
278        Texture(const Texture& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
279
280        virtual osg::Object* cloneType() const = 0;
281        virtual osg::Object* clone(const CopyOp& copyop) const = 0;
282        virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Texture *>(obj)!=NULL; }
283        virtual const char* libraryName() const { return "osg"; }
284        virtual const char* className() const { return "Texture"; }
285
286        /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
287        virtual Texture* asTexture() { return this; }
288       
289        /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
290        virtual const Texture* asTexture() const { return this; }
291
292        virtual Type getType() const { return TEXTURE; }
293
294        virtual bool isTextureAttribute() const { return true; }
295
296        virtual GLenum getTextureTarget() const = 0;
297
298        virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
299        {
300            usage.usesTextureMode(getTextureTarget());
301            return true;
302        }
303
304        virtual int getTextureWidth() const { return 0; }
305        virtual int getTextureHeight() const { return 0; }
306        virtual int getTextureDepth() const { return 0; }
307
308        enum WrapParameter {
309            WRAP_S,
310            WRAP_T,
311            WRAP_R
312        };
313
314        enum WrapMode {
315            CLAMP  = GL_CLAMP,
316            CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
317            CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER_ARB,
318            REPEAT = GL_REPEAT,
319            MIRROR = GL_MIRRORED_REPEAT_IBM
320        };
321
322        /** Sets the texture wrap mode. */
323        void setWrap(WrapParameter which, WrapMode wrap);
324        /** Gets the texture wrap mode. */
325        WrapMode getWrap(WrapParameter which) const;
326
327
328        /** Sets the border color. Only used when wrap mode is CLAMP_TO_BORDER.
329         * The border color will be casted to the appropriate type to match the
330         * internal pixel format of the texture. */
331        void setBorderColor(const Vec4d& color) { _borderColor = color; dirtyTextureParameters(); }
332
333        /** Gets the border color. */
334        const Vec4d& getBorderColor() const { return _borderColor; }
335
336        /** Sets the border width. */
337        void setBorderWidth(GLint width) { _borderWidth = width; dirtyTextureParameters(); }
338       
339        GLint getBorderWidth() const { return _borderWidth; }
340
341        enum FilterParameter {
342            MIN_FILTER,
343            MAG_FILTER
344        };
345
346        enum FilterMode {
347            LINEAR                    = GL_LINEAR,
348            LINEAR_MIPMAP_LINEAR      = GL_LINEAR_MIPMAP_LINEAR,
349            LINEAR_MIPMAP_NEAREST     = GL_LINEAR_MIPMAP_NEAREST,
350            NEAREST                   = GL_NEAREST,
351            NEAREST_MIPMAP_LINEAR     = GL_NEAREST_MIPMAP_LINEAR,
352            NEAREST_MIPMAP_NEAREST    = GL_NEAREST_MIPMAP_NEAREST
353        };
354
355
356        /** Sets the texture filter mode. */
357        void setFilter(FilterParameter which, FilterMode filter);
358
359        /** Gets the texture filter mode. */
360        FilterMode getFilter(FilterParameter which) const;
361
362        /** Sets the maximum anisotropy value, default value is 1.0 for no
363          * anisotropic filtering. If hardware does not support anisotropic
364          * filtering, use normal filtering (equivalent to a max anisotropy
365          * value of 1.0. Valid range is 1.0f upwards.  The maximum value
366          * depends on the graphics system. */
367        void setMaxAnisotropy(float anis);
368       
369        /** Gets the maximum anisotropy value. */
370        inline float getMaxAnisotropy() const { return _maxAnisotropy; }
371
372        /** Sets the hardware mipmap generation hint. If enabled, it will
373          * only be used if supported in the graphics system. */
374        inline void setUseHardwareMipMapGeneration(bool useHardwareMipMapGeneration) { _useHardwareMipMapGeneration = useHardwareMipMapGeneration; }
375
376        /** Gets the hardware mipmap generation hint. */
377        inline bool getUseHardwareMipMapGeneration() const { return _useHardwareMipMapGeneration; }
378
379        /** Sets whether or not the apply() function will unreference the image
380          * data. If enabled, and the image data is only referenced by this
381          * Texture, apply() will delete the image data. */
382        inline void setUnRefImageDataAfterApply(bool flag) { _unrefImageDataAfterApply = flag; }
383       
384        /** Gets whether or not apply() unreferences image data. */
385        inline bool getUnRefImageDataAfterApply() const { return _unrefImageDataAfterApply; }
386
387        /** Sets whether to use client storage for the texture, if supported
388          * by the graphics system. Note: If enabled, and the graphics system
389          * supports it, the osg::Image(s) associated with this texture cannot
390          * be deleted, so the UnRefImageDataAfterApply flag would be ignored. */
391        inline void setClientStorageHint(bool flag) { _clientStorageHint = flag; }
392
393        /** Gets whether to use client storage for the texture. */
394        inline bool getClientStorageHint() const { return _clientStorageHint; }
395
396        /** Sets whether to force the texture to resize images that have dimensions
397          * that are not a power of two. If enabled, NPOT images will be resized,
398          * whether or not NPOT textures are supported by the hardware. If disabled,
399          * NPOT images will not be resized if supported by hardware. */
400        inline void setResizeNonPowerOfTwoHint(bool flag) { _resizeNonPowerOfTwoHint = flag; }
401
402        /** Gets whether texture will force non power to two images to be resized. */
403        inline bool getResizeNonPowerOfTwoHint() const { return _resizeNonPowerOfTwoHint; }
404
405        enum InternalFormatMode {
406            USE_IMAGE_DATA_FORMAT,
407            USE_USER_DEFINED_FORMAT,
408            USE_ARB_COMPRESSION,
409            USE_S3TC_DXT1_COMPRESSION,
410            USE_S3TC_DXT3_COMPRESSION,
411            USE_S3TC_DXT5_COMPRESSION
412        };
413
414        /** Sets the internal texture format mode. Note: If the texture format is
415          * USE_IMAGE_DATA_FORMAT, USE_ARB_COMPRESSION, or USE_S3TC_COMPRESSION,
416          * the internal format mode is set automatically and will overwrite the
417          * previous _internalFormat. */
418        inline void setInternalFormatMode(InternalFormatMode mode) { _internalFormatMode = mode; }
419
420        /** Gets the internal texture format mode. */
421        inline InternalFormatMode getInternalFormatMode() const { return _internalFormatMode; }
422
423        /** Sets the internal texture format. Implicitly sets the
424          * internalFormatMode to USE_USER_DEFINED_FORMAT.
425          * The corresponding internal format type will be computed. */
426        inline void setInternalFormat(GLint internalFormat)
427        {
428            _internalFormatMode = USE_USER_DEFINED_FORMAT;
429            _internalFormat = internalFormat;
430            computeInternalFormatType();
431        }
432
433
434        /** Gets the internal texture format. */
435        inline GLint getInternalFormat() const { if (_internalFormat==0) computeInternalFormat(); return _internalFormat; }
436       
437        /** Return true if the internal format is one of the compressed formats.*/
438        bool isCompressedInternalFormat() const;
439
440        /** Sets the external source image format, used as a fallback when no osg::Image is attached to provide the source image format. */
441        inline void setSourceFormat(GLenum sourceFormat) { _sourceFormat = sourceFormat; }
442
443        /** Gets the external source image format. */
444        inline GLenum getSourceFormat() const { return _sourceFormat; }
445
446        /** Sets the external source data type, used as a fallback when no osg::Image is attached to provide the source image format.*/
447        inline void setSourceType(GLenum sourceType) { _sourceType = sourceType; }
448
449        /** Gets the external source data type.*/
450        inline GLenum getSourceType() const { return _sourceType; }
451
452        /** Texture type determined by the internal texture format */
453        enum InternalFormatType{
454
455            //! default OpenGL format (clamped values to [0,1) or [0,255])
456            NORMALIZED = 0x0,
457
458            //! float values, Shader Model 3.0 (see ARB_texture_float)
459            FLOAT = 0x1,
460
461            //! Signed integer values (see EXT_texture_integer)
462            SIGNED_INTEGER = 0x2,
463
464            //! Unsigned integer value (see EXT_texture_integer)
465            UNSIGNED_INTEGER = 0x4
466        };
467       
468        /** Get the internal texture format type. */
469        inline InternalFormatType getInternalFormatType() const { return _internalFormatType; }
470       
471        class TextureObject;
472
473        /** Returns a pointer to the TextureBbject for the current context. */
474        inline TextureObject* getTextureObject(unsigned int contextID) const
475        {
476            return _textureObjectBuffer[contextID].get();
477        }
478
479        inline void setTextureObject(unsigned int contextID, TextureObject* to)
480        {
481            _textureObjectBuffer[contextID] = to;
482        }
483
484        /** Forces a recompile on next apply() of associated OpenGL texture
485          * objects. */
486        void dirtyTextureObject();
487
488        /** Returns true if the texture objects for all the required graphics
489          * contexts are loaded. */
490        bool areAllTextureObjectsLoaded() const;
491
492
493        /** Gets the dirty flag for the current contextID. */
494        inline unsigned int& getTextureParameterDirty(unsigned int contextID) const
495        {
496            return _texParametersDirtyList[contextID];
497        }
498
499
500        /** Force a reset on next apply() of associated OpenGL texture
501          * parameters. */
502        void dirtyTextureParameters();
503
504        /** Force a manual allocation of the mipmap levels on the next apply() call.
505          * User is responsible for filling the mipmap levels with valid data.
506          * The OpenGL's glGenerateMipmapEXT function is used to generate the mipmap levels.
507          * If glGenerateMipmapEXT is not supported or texture's internal format is not supported
508          * by the glGenerateMipmapEXT, then empty mipmap levels will
509          * be allocated manually. The mipmap levels are also allocated if a non-mipmapped
510          * min filter is used. */
511        void allocateMipmapLevels();
512
513
514        /** Sets GL_TEXTURE_COMPARE_MODE_ARB to GL_COMPARE_R_TO_TEXTURE_ARB
515          * See http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow.txt. */
516        void setShadowComparison(bool flag) { _use_shadow_comparison = flag; }
517
518        enum ShadowCompareFunc {
519            NEVER = GL_NEVER,
520            LESS = GL_LESS,
521            EQUAL = GL_EQUAL,
522            LEQUAL = GL_LEQUAL,
523            GREATER = GL_GREATER,
524            NOTEQUAL = GL_NOTEQUAL,
525            GEQUAL = GL_GEQUAL,
526            ALWAYS = GL_ALWAYS
527        };
528
529        /** Sets shadow texture comparison function. */
530        void setShadowCompareFunc(ShadowCompareFunc func) { _shadow_compare_func = func; }
531        ShadowCompareFunc getShadowCompareFunc() const { return _shadow_compare_func; }
532
533        enum ShadowTextureMode {
534            LUMINANCE = GL_LUMINANCE,
535            INTENSITY = GL_INTENSITY,
536            ALPHA = GL_ALPHA
537        };
538
539        /** Sets shadow texture mode after comparison. */
540        void setShadowTextureMode(ShadowTextureMode mode) { _shadow_texture_mode = mode; }
541        ShadowTextureMode getShadowTextureMode() const { return _shadow_texture_mode; }
542
543        /** Sets the TEXTURE_COMPARE_FAIL_VALUE_ARB texture parameter. See
544          * http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow_ambient.txt. */
545        void setShadowAmbient(float shadow_ambient) { _shadow_ambient = shadow_ambient; }
546        float getShadowAmbient() const { return _shadow_ambient; }
547       
548
549        /** Sets the texture image for the specified face. */
550        virtual void setImage(unsigned int face, Image* image) = 0;
551
552        /** Gets the texture image for the specified face. */
553        virtual Image* getImage(unsigned int face) = 0;
554
555        /** Gets the const texture image for specified face. */
556        virtual const Image* getImage(unsigned int face) const = 0;
557
558        /** Gets the number of images that can be assigned to this Texture. */
559        virtual unsigned int getNumImages() const = 0;
560
561
562        /** Set the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
563        void setReadPBuffer(GraphicsContext* context) { _readPBuffer = context; }
564
565        /** Get the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
566        GraphicsContext* getReadPBuffer() { return _readPBuffer.get(); }
567
568        /** Get the const PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
569        const GraphicsContext* getReadPBuffer() const { return _readPBuffer.get(); }
570
571        /** Texture is a pure virtual base class, apply must be overridden. */
572        virtual void apply(State& state) const = 0;
573
574        /** Calls apply(state) to compile the texture. */
575        virtual void compileGLObjects(State& state) const;
576
577        /** Resize any per context GLObject buffers to specified size. */
578        virtual void resizeGLObjectBuffers(unsigned int maxSize);
579
580        /** If State is non-zero, this function releases OpenGL objects for
581          * the specified graphics context. Otherwise, releases OpenGL objects
582          * for all graphics contexts. */
583        virtual void releaseGLObjects(State* state=0) const;
584
585        /** Encapsulates queries of extension availability, obtains extension
586          * function pointers, and provides convenience wrappers for
587          * calling extension functions. */       
588        class OSG_EXPORT Extensions : public osg::Referenced
589        {
590            public:
591                Extensions(unsigned int contextID);
592
593                Extensions(const Extensions& rhs);
594               
595                void lowestCommonDenominator(const Extensions& rhs);
596               
597                void setupGLExtensions(unsigned int contextID);
598
599                void setMultiTexturingSupported(bool flag) { _isMultiTexturingSupported=flag; }
600                bool isMultiTexturingSupported() const { return _isMultiTexturingSupported; }
601
602                void setTextureFilterAnisotropicSupported(bool flag) { _isTextureFilterAnisotropicSupported=flag; }
603                bool isTextureFilterAnisotropicSupported() const { return _isTextureFilterAnisotropicSupported; }
604               
605                void setTextureCompressionARBSupported(bool flag) { _isTextureCompressionARBSupported=flag; }
606                bool isTextureCompressionARBSupported() const { return _isTextureCompressionARBSupported; }
607
608                void setTextureCompressionS3TCSupported(bool flag) { _isTextureCompressionS3TCSupported=flag; }
609                bool isTextureCompressionS3TCSupported() const { return _isTextureCompressionS3TCSupported; }
610
611                void setTextureMirroredRepeatSupported(bool flag) { _isTextureMirroredRepeatSupported=flag; }
612                bool isTextureMirroredRepeatSupported() const { return _isTextureMirroredRepeatSupported; }
613
614                void setTextureEdgeClampSupported(bool flag) { _isTextureEdgeClampSupported=flag; }
615                bool isTextureEdgeClampSupported() const { return _isTextureEdgeClampSupported; }
616
617                void setTextureBorderClampSupported(bool flag) { _isTextureBorderClampSupported=flag; }
618                bool isTextureBorderClampSupported() const { return _isTextureBorderClampSupported; }
619
620                void setGenerateMipMapSupported(bool flag) { _isGenerateMipMapSupported=flag; }
621                bool isGenerateMipMapSupported() const { return _isGenerateMipMapSupported; }
622
623                void setShadowSupported(bool flag) { _isShadowSupported = flag; }
624                bool isShadowSupported() const { return _isShadowSupported; }
625
626                void setShadowAmbientSupported(bool flag) { _isShadowAmbientSupported = flag; }
627                bool isShadowAmbientSupported() const { return _isShadowAmbientSupported; }
628
629                void setTextureMaxLevelSupported(bool flag) { _isTextureMaxLevelSupported = flag; }
630                bool isTextureMaxLevelSupported() const { return _isTextureMaxLevelSupported; }
631
632                void setMaxTextureSize(GLint maxsize) { _maxTextureSize=maxsize; }
633                GLint maxTextureSize() const { return _maxTextureSize; }
634
635                void setNumTextureUnits(GLint nunits ) { _numTextureUnits=nunits; }
636                GLint numTextureUnits() const { return _numTextureUnits; }
637
638                bool isCompressedTexImage2DSupported() const { return _glCompressedTexImage2D!=0; }
639                bool isCompressedTexSubImage2DSupported() const { return _glCompressedTexSubImage2D!=0; }
640               
641                void glCompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data) const;
642                void glCompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei type, const GLvoid *data) const;
643                void glGetCompressedTexImage(GLenum target, GLint level, GLvoid *data) const;
644
645                bool isClientStorageSupported() const { return _isClientStorageSupported; }
646
647                bool isNonPowerOfTwoTextureSupported(GLenum filter) const
648                {
649                    return (filter==GL_LINEAR || filter==GL_NEAREST) ?
650                            _isNonPowerOfTwoTextureNonMipMappedSupported :
651                            _isNonPowerOfTwoTextureMipMappedSupported;
652                }
653
654                void setTextureIntegerSupported(bool flag) { _isTextureIntegerEXTSupported=flag; }
655                bool isTextureIntegerSupported() const { return _isTextureIntegerEXTSupported; }
656
657                void glTexParameterIiv(GLenum target, GLenum pname, const GLint* data) const;
658                void glTexParameterIuiv(GLenum target, GLenum pname, const GLuint* data) const;
659
660            protected:
661
662                ~Extensions() {}
663               
664                bool    _isMultiTexturingSupported;
665                bool    _isTextureFilterAnisotropicSupported;
666                bool    _isTextureCompressionARBSupported;
667                bool    _isTextureCompressionS3TCSupported;
668                bool    _isTextureMirroredRepeatSupported;
669                bool    _isTextureEdgeClampSupported;
670                bool    _isTextureBorderClampSupported;
671                bool    _isGenerateMipMapSupported;
672                bool    _isShadowSupported;
673                bool    _isShadowAmbientSupported;
674                bool    _isClientStorageSupported;
675                bool    _isNonPowerOfTwoTextureMipMappedSupported;
676                bool    _isNonPowerOfTwoTextureNonMipMappedSupported;
677                bool    _isTextureIntegerEXTSupported;
678                bool    _isTextureMaxLevelSupported;
679
680                GLint   _maxTextureSize;
681                GLint   _numTextureUnits;
682
683                typedef void (APIENTRY * CompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
684                typedef void (APIENTRY * CompressedTexSubImage2DArbProc) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *data);
685                typedef void (APIENTRY * GetCompressedTexImageArbProc) (GLenum target, GLint level, GLvoid *data);
686                typedef void (APIENTRY * TexParameterIivProc)(GLenum target, GLenum pname, const GLint* data);
687                typedef void (APIENTRY * TexParameterIuivProc)(GLenum target, GLenum pname, const GLuint* data);
688
689                CompressedTexImage2DArbProc     _glCompressedTexImage2D;
690                CompressedTexSubImage2DArbProc  _glCompressedTexSubImage2D;
691                GetCompressedTexImageArbProc    _glGetCompressedTexImage;
692                TexParameterIivProc             _glTexParameterIiv;
693                TexParameterIuivProc            _glTexParameterIuiv;
694
695        };
696       
697        /** Gets the extension for the specified context. Creates the
698          * Extensions object for that context if it doesn't exist.
699          * Returns NULL if the Extensions object for the context doesn't
700          * exist and the createIfNotInitalized flag is false. */
701        static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized);
702
703        /** Overrides Extensions objects across graphics contexts. Typically
704          * used to ensure the same lowest common denominator of extensions
705          * on systems with different graphics pipes. */
706        static void setExtensions(unsigned int contextID,Extensions* extensions);
707
708        /** Determine whether the given internalFormat is a compressed
709          * image format. */
710        static bool isCompressedInternalFormat(GLint internalFormat);
711       
712        /** Determine the size of a compressed image, given the internalFormat,
713          * the width, the height, and the depth of the image. The block size
714          * and the size are output parameters. */
715        static void getCompressedSize(GLenum internalFormat, GLint width, GLint height, GLint depth, GLint& blockSize, GLint& size);
716
717
718        /** Helper method. Creates the texture, but doesn't set or use a
719          * texture binding. Note: Don't call this method directly unless
720          * you're implementing a subload callback. */
721        void applyTexImage2D_load(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height,GLsizei numMipmapLevels) const;
722       
723        /** Helper method. Subloads images into the texture, but doesn't set
724          * or use a texture binding. Note: Don't call this method directly
725          * unless you're implementing a subload callback. */
726        void applyTexImage2D_subload(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height, GLint inInternalFormat, GLsizei numMipmapLevels) const;
727
728        /** Returned by mipmapBeforeTexImage() to indicate what
729          * mipmapAfterTexImage() should do */
730        enum GenerateMipmapMode
731        {
732            GENERATE_MIPMAP_NONE,
733            GENERATE_MIPMAP,
734            GENERATE_MIPMAP_TEX_PARAMETER
735        };
736
737    protected :
738
739        virtual ~Texture();
740
741        virtual void computeInternalFormat() const = 0;
742       
743        void computeInternalFormatWithImage(const osg::Image& image) const;
744
745        void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& numMipmapLevels) const;
746       
747        void computeInternalFormatType() const;
748
749        /** Helper method. Sets texture parameters. */
750        void applyTexParameters(GLenum target, State& state) const;
751
752        /** Returns true if _useHardwareMipMapGeneration is true and either
753          * glGenerateMipmapEXT() or GL_GENERATE_MIPMAP_SGIS are supported. */
754        bool isHardwareMipmapGenerationEnabled(const State& state) const;
755
756        /** Helper methods to be called before and after calling
757          * gl[Compressed][Copy]Tex[Sub]Image2D to handle generating mipmaps. */
758        GenerateMipmapMode mipmapBeforeTexImage(const State& state, bool hardwareMipmapOn) const;
759        void mipmapAfterTexImage(State& state, GenerateMipmapMode beforeResult) const;
760
761        /** Helper method to generate mipmap levels by calling of glGenerateMipmapEXT.
762          * If it is not supported, then call the virtual allocateMipmap() method */
763        void generateMipmap(State& state) const;
764
765        /** Allocate mipmap levels of the texture by subsequent calling of glTexImage* function. */
766        virtual void allocateMipmap(State& state) const = 0;
767
768        /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
769        int compareTexture(const Texture& rhs) const;
770
771        /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
772        int compareTextureObjects(const Texture& rhs) const;
773
774        typedef buffered_value<unsigned int> TexParameterDirtyList;
775        mutable TexParameterDirtyList _texParametersDirtyList;
776        mutable TexParameterDirtyList _texMipmapGenerationDirtyList;
777
778        WrapMode _wrap_s;
779        WrapMode _wrap_t;
780        WrapMode _wrap_r;
781
782        FilterMode      _min_filter;
783        FilterMode      _mag_filter;
784        float           _maxAnisotropy;
785        bool            _useHardwareMipMapGeneration;
786        bool            _unrefImageDataAfterApply;
787        bool            _clientStorageHint;
788        bool            _resizeNonPowerOfTwoHint;
789
790        Vec4d           _borderColor;
791        GLint           _borderWidth;
792
793        InternalFormatMode          _internalFormatMode;
794        mutable InternalFormatType  _internalFormatType;
795        mutable GLint       _internalFormat;
796        mutable GLenum      _sourceFormat;
797        mutable GLenum      _sourceType;
798
799        bool                _use_shadow_comparison;
800        ShadowCompareFunc   _shadow_compare_func;
801        ShadowTextureMode   _shadow_texture_mode;
802        float               _shadow_ambient;
803
804    public:
805
806        struct TextureProfile
807        {
808            inline TextureProfile(GLenum target):
809                _target(target),
810                _numMipmapLevels(0),
811                _internalFormat(0),
812                _width(0),
813                _height(0),
814                _depth(0),
815                _border(0),
816                _size(0) {}
817
818            inline TextureProfile(GLenum    target,
819                                 GLint     numMipmapLevels,
820                                 GLenum    internalFormat,
821                                 GLsizei   width,
822                                 GLsizei   height,
823                                 GLsizei   depth,
824                                 GLint     border):
825                _target(target),
826                _numMipmapLevels(numMipmapLevels),
827                _internalFormat(internalFormat),
828                _width(width),
829                _height(height),
830                _depth(depth),
831                _border(border),
832                _size(0) { computeSize(); }
833
834
835            #define LESSTHAN(A,B) if (A<B) return true; if (B<A) return false;
836            #define FINALLESSTHAN(A,B) return (A<B);
837
838            bool operator < (const TextureProfile& rhs) const
839            {
840                LESSTHAN(_size,rhs._size);
841                LESSTHAN(_target,rhs._target);
842                LESSTHAN(_numMipmapLevels,rhs._numMipmapLevels);
843                LESSTHAN(_internalFormat,rhs._internalFormat);
844                LESSTHAN(_width,rhs._width);
845                LESSTHAN(_height,rhs._height);
846                LESSTHAN(_depth,rhs._depth);
847                FINALLESSTHAN(_border, rhs._border);
848            }
849
850            bool operator == (const TextureProfile& rhs) const
851            {
852                return _target == rhs._target &&
853                       _numMipmapLevels == rhs._numMipmapLevels &&
854                       _internalFormat == rhs._internalFormat &&
855                       _width == rhs._width &&
856                       _height == rhs._height &&
857                       _depth == rhs._depth &&
858                       _border == rhs._border;
859            }
860
861            inline void set(GLint numMipmapLevels,
862                            GLenum    internalFormat,
863                            GLsizei   width,
864                            GLsizei   height,
865                            GLsizei   depth,
866                            GLint     border)
867            {
868                _numMipmapLevels = numMipmapLevels;
869                _internalFormat = internalFormat;
870                _width = width;
871                _height = height;
872                _depth = depth;
873                _border = border;
874            }
875
876            inline bool match(GLenum    target,
877                       GLint     numMipmapLevels,
878                       GLenum    internalFormat,
879                       GLsizei   width,
880                       GLsizei   height,
881                       GLsizei   depth,
882                       GLint     border)
883            {
884                return (_target == target) &&
885                       (_numMipmapLevels == numMipmapLevels) &&
886                       (_internalFormat == internalFormat) &&
887                       (_width == width) &&
888                       (_height == height) &&
889                       (_depth == depth) &&
890                       (_border == border);
891            }
892
893            void computeSize();
894
895            GLenum       _target;
896            GLint        _numMipmapLevels;
897            GLenum       _internalFormat;
898            GLsizei      _width;
899            GLsizei      _height;
900            GLsizei      _depth;
901            GLint        _border;
902            unsigned int _size;
903        };
904
905        // forward declare
906        class TextureObjectSet;
907        class TextureObjectManager;
908
909        class OSG_EXPORT TextureObject : public osg::Referenced
910        {
911        public:
912
913            inline TextureObject(Texture* texture, GLuint id, GLenum target):
914                _id(id),
915                _profile(target),
916                _set(0),
917                _previous(0),
918                _next(0),
919                _texture(texture),
920                _allocated(false),
921                _timeStamp(0) {}
922
923            inline TextureObject(Texture* texture, GLuint id, const TextureProfile& profile):
924                _id(id),
925                _profile(profile),
926                _set(0),
927                _previous(0),
928                _next(0),
929                _texture(texture),
930                _allocated(false),
931                _timeStamp(0) {}
932
933            inline TextureObject(Texture* texture,
934                          GLuint    id,
935                          GLenum    target,
936                          GLint     numMipmapLevels,
937                          GLenum    internalFormat,
938                          GLsizei   width,
939                          GLsizei   height,
940                          GLsizei   depth,
941                          GLint     border):
942                _id(id),
943                _profile(target,numMipmapLevels,internalFormat,width,height,depth,border),
944                _set(0),
945                _previous(0),
946                _next(0),
947                _texture(texture),
948                _allocated(false),
949                _timeStamp(0) {}
950
951            inline bool match(GLenum    target,
952                       GLint     numMipmapLevels,
953                       GLenum    internalFormat,
954                       GLsizei   width,
955                       GLsizei   height,
956                       GLsizei   depth,
957                       GLint     border)
958            {
959                return isReusable() &&
960                       _profile.match(target,numMipmapLevels,internalFormat,width,height,depth,border);
961            }
962
963            void bind();
964
965            inline GLenum id() const { return _id; }
966            inline GLenum target() const { return _profile._target; }
967
968            inline unsigned int size() const { return _profile._size; }
969
970            inline void setTexture(Texture* texture) { _texture = texture; }
971            inline Texture* getTexture() const { return _texture; }
972
973            inline void setTimeStamp(double timestamp) { _timeStamp = timestamp; }
974            inline double getTimeStamp() const { return _timeStamp; }
975
976            inline void setAllocated(bool allocated=true) { _allocated = allocated; }
977
978            void setAllocated(GLint     numMipmapLevels,
979                              GLenum    internalFormat,
980                              GLsizei   width,
981                              GLsizei   height,
982                              GLsizei   depth,
983                              GLint     border);
984
985            inline bool isAllocated() const { return _allocated; }
986
987            inline bool isReusable() const { return _allocated && _profile._width!=0; }
988
989
990            GLuint              _id;
991            TextureProfile      _profile;
992            TextureObjectSet*   _set;
993            TextureObject*      _previous;
994            TextureObject*      _next;
995            Texture*            _texture;
996            bool                _allocated;
997            unsigned int        _frameLastUsed;
998            double              _timeStamp;
999        };
1000
1001        typedef std::list< ref_ptr<TextureObject> > TextureObjectList;
1002
1003        class TextureObjectSet : public Referenced
1004        {
1005        public:
1006            TextureObjectSet(TextureObjectManager* parent, const TextureProfile& profile);
1007
1008            void handlePendingOrphandedTextureObjects();
1009            void flushAllDeletedTextureObjects();
1010            void discardAllDeletedTextureObjects();
1011            void flushDeletedTextureObjects(double currentTime, double& availableTime);
1012
1013            TextureObject* takeFromOrphans(Texture* texture);
1014            TextureObject* takeOrGenerate(Texture* texture);
1015            void moveToBack(TextureObject* to);
1016            void addToBack(TextureObject* to);
1017            void orphan(TextureObject* to);
1018            void remove(TextureObject* to);
1019
1020            unsigned int size() const { return _profile._size * _numOfTextureObjects; }
1021
1022            bool makeSpace(unsigned int& size);
1023
1024            bool checkConsistency() const;
1025
1026            TextureObjectManager* getParent() { return _parent; }
1027
1028        protected:
1029
1030            virtual ~TextureObjectSet();
1031
1032            OpenThreads::Mutex  _mutex;
1033
1034            TextureObjectManager*       _parent;
1035            unsigned int                _contextID;
1036            TextureProfile              _profile;
1037            unsigned int                _numOfTextureObjects;
1038            TextureObjectList           _orphanedTextureObjects;
1039            TextureObjectList           _pendingOrphanedTextureObjects;
1040
1041            TextureObject*              _head;
1042            TextureObject*              _tail;
1043
1044        };
1045
1046        class OSG_EXPORT TextureObjectManager : public osg::Referenced
1047        {
1048        public:
1049            TextureObjectManager(unsigned int contextID);
1050
1051            unsigned int getContextID() const { return _contextID; }
1052
1053
1054
1055            void setNumberActiveTextureObjects(unsigned int size) { _numActiveTextureObjects = size; }
1056            unsigned int& getNumberActiveTextureObjects() { return _numActiveTextureObjects; }
1057            unsigned int getNumberActiveTextureObjects() const { return _numActiveTextureObjects; }
1058
1059            void setNumberOrphanedTextureObjects(unsigned int size) { _numOrphanedTextureObjects = size; }
1060            unsigned int& getNumberOrphanedTextureObjects() { return _numOrphanedTextureObjects; }
1061            unsigned int getNumberOrphanedTextureObjects() const { return _numOrphanedTextureObjects; }
1062
1063            void setCurrTexturePoolSize(unsigned int size) { _currTexturePoolSize = size; }
1064            unsigned int& getCurrTexturePoolSize() { return _currTexturePoolSize; }
1065            unsigned int getCurrTexturePoolSize() const { return _currTexturePoolSize; }
1066
1067            void setMaxTexturePoolSize(unsigned int size);
1068            unsigned int getMaxTexturePoolSize() const { return _maxTexturePoolSize; }
1069
1070            bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; }
1071            bool makeSpace(unsigned int size);
1072
1073            TextureObject* generateTextureObject(const Texture* texture, GLenum target);
1074            TextureObject* generateTextureObject(const Texture* texture,
1075                                                        GLenum    target,
1076                                                        GLint     numMipmapLevels,
1077                                                        GLenum    internalFormat,
1078                                                        GLsizei   width,
1079                                                        GLsizei   height,
1080                                                        GLsizei   depth,
1081                                                        GLint     border);
1082            void handlePendingOrphandedTextureObjects();
1083            void flushAllDeletedTextureObjects();
1084            void discardAllDeletedTextureObjects();
1085            void flushDeletedTextureObjects(double currentTime, double& availableTime);
1086            void releaseTextureObject(TextureObject* to);
1087
1088            TextureObjectSet* getTextureObjectSet(const TextureProfile& profile);
1089
1090            void newFrame(osg::FrameStamp* fs);
1091            void resetStats();
1092            void reportStats();
1093
1094            unsigned int& getFrameNumber() { return _frameNumber; }
1095            unsigned int& getNumberFrames() { return _numFrames; }
1096
1097            unsigned int& getNumberDeleted() { return _numDeleted; }
1098            double& getDeleteTime() { return _deleteTime; }
1099
1100            unsigned int& getNumberGenerated() { return _numGenerated; }
1101            double& getGenerateTime() { return _generateTime; }
1102
1103            unsigned int& getNumberApplied() { return _numApplied; }
1104            double& getApplyTime() { return _applyTime; }
1105
1106
1107        protected:
1108
1109            typedef std::map< TextureProfile, osg::ref_ptr<TextureObjectSet> > TextureSetMap;
1110            unsigned int        _contextID;
1111            unsigned int        _numActiveTextureObjects;
1112            unsigned int        _numOrphanedTextureObjects;
1113            unsigned int        _currTexturePoolSize;
1114            unsigned int        _maxTexturePoolSize;
1115            TextureSetMap       _textureSetMap;
1116
1117            unsigned int        _frameNumber;
1118
1119            unsigned int        _numFrames;
1120            unsigned int        _numDeleted;
1121            double              _deleteTime;
1122
1123            unsigned int        _numGenerated;
1124            double              _generateTime;
1125
1126            unsigned int        _numApplied;
1127            double              _applyTime;
1128
1129        };
1130
1131        static osg::ref_ptr<Texture::TextureObjectManager>&  getTextureObjectManager(unsigned int contextID);
1132
1133        static TextureObject* generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target);
1134
1135        static TextureObject* generateTextureObject(const Texture* texture,
1136                                                     unsigned int contextID,
1137                                                     GLenum    target,
1138                                                     GLint     numMipmapLevels,
1139                                                     GLenum    internalFormat,
1140                                                     GLsizei   width,
1141                                                     GLsizei   height,
1142                                                     GLsizei   depth,
1143                                                     GLint     border);
1144
1145
1146        /** Set the minimum number of texture objects to retain in the deleted display list cache. */
1147        static void setMinimumNumberOfTextureObjectsToRetainInCache(unsigned int minimum);
1148
1149        /** Get the minimum number of display lists to retain in the deleted display list cache. */
1150        static unsigned int getMinimumNumberOfTextureObjectsToRetainInCache();
1151
1152        static void flushAllDeletedTextureObjects(unsigned int contextID);
1153
1154        static void discardAllDeletedTextureObjects(unsigned int contextID);
1155
1156        static void flushDeletedTextureObjects(unsigned int contextID,double currentTime, double& availableTime);
1157
1158        static void releaseTextureObject(unsigned int contextID, TextureObject* to);
1159
1160    protected:
1161
1162        typedef buffered_object< ref_ptr<TextureObject> >  TextureObjectBuffer;
1163        mutable TextureObjectBuffer         _textureObjectBuffer;
1164        mutable ref_ptr<GraphicsContext>    _readPBuffer;
1165
1166};
1167
1168}
1169
1170#endif
Note: See TracBrowser for help on using the browser.