openscenegraph
Texture
Go to the documentation of this file.
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/Vec4i>
25#include <osg/buffered_value>
26#include <osg/GLExtensions>
27
28#include <list>
29#include <map>
30
31// If not defined by gl.h use the definition found in:
32// http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_filter_anisotropic.txt
33#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
34 #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
35#endif
36
37// If not defined, use the definition found in:
38// http://www.opengl.org/registry/specs/ARB/texture_swizzle.txt
39#ifndef GL_TEXTURE_SWIZZLE_RGBA
40 #define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
41#endif
42
43#ifndef GL_ARB_texture_compression
44 #define GL_COMPRESSED_ALPHA_ARB 0x84E9
45 #define GL_COMPRESSED_LUMINANCE_ARB 0x84EA
46 #define GL_COMPRESSED_LUMINANCE_ALPHA_ARB 0x84EB
47 #define GL_COMPRESSED_INTENSITY_ARB 0x84EC
48 #define GL_COMPRESSED_RGB_ARB 0x84ED
49 #define GL_COMPRESSED_RGBA_ARB 0x84EE
50 #define GL_TEXTURE_COMPRESSION_HINT_ARB 0x84EF
51 #define GL_TEXTURE_COMPRESSED_ARB 0x86A1
52 #define GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A2
53 #define GL_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A3
54#endif
55
56#ifndef GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB
57 #define GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB 0x86A0
58#endif
59
60#ifndef GL_EXT_texture_compression_s3tc
61 #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
62 #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
63 #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
64 #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
65#endif
66
67#ifndef GL_EXT_texture_compression_rgtc
68 #define GL_COMPRESSED_RED_RGTC1_EXT 0x8DBB
69 #define GL_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
70 #define GL_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
71 #define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
72#endif
73
74#ifndef GL_IMG_texture_compression_pvrtc
75 #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
76 #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
77 #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
78 #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
79#endif
80
81#ifndef GL_OES_compressed_ETC1_RGB8_texture
82 #define GL_ETC1_RGB8_OES 0x8D64
83#endif
84
85#ifndef GL_ARB_INTERNAL_TEXTURE_FORMAT
86 #define GL_RGBA32F_ARB 0x8814
87 #define GL_RGB32F_ARB 0x8815
88 #define GL_ALPHA32F_ARB 0x8816
89 #define GL_INTENSITY32F_ARB 0x8817
90 #define GL_LUMINANCE32F_ARB 0x8818
91 #define GL_LUMINANCE_ALPHA32F_ARB 0x8819
92 #define GL_RGBA16F_ARB 0x881A
93 #define GL_RGB16F_ARB 0x881B
94 #define GL_ALPHA16F_ARB 0x881C
95 #define GL_INTENSITY16F_ARB 0x881D
96 #define GL_LUMINANCE16F_ARB 0x881E
97 #define GL_LUMINANCE_ALPHA16F_ARB 0x881F
98#endif
99
100#ifndef GL_HALF_FLOAT
101 #define GL_HALF_FLOAT 0x140B
102#endif
103
104#ifndef GL_NV_texture_shader
105 #define GL_HILO_NV 0x86F4
106 #define GL_DSDT_NV 0x86F5
107 #define GL_DSDT_MAG_NV 0x86F6
108 #define GL_DSDT_MAG_VIB_NV 0x86F7
109 #define GL_HILO16_NV 0x86F8
110 #define GL_SIGNED_HILO_NV 0x86F9
111 #define GL_SIGNED_HILO16_NV 0x86FA
112 #define GL_SIGNED_RGBA_NV 0x86FB
113 #define GL_SIGNED_RGBA8_NV 0x86FC
114 #define GL_SIGNED_RGB_NV 0x86FE
115 #define GL_SIGNED_RGB8_NV 0x86FF
116 #define GL_SIGNED_LUMINANCE_NV 0x8701
117 #define GL_SIGNED_LUMINANCE8_NV 0x8702
118 #define GL_SIGNED_LUMINANCE_ALPHA_NV 0x8703
119 #define GL_SIGNED_LUMINANCE8_ALPHA8_NV 0x8704
120 #define GL_SIGNED_ALPHA_NV 0x8705
121 #define GL_SIGNED_ALPHA8_NV 0x8706
122 #define GL_SIGNED_INTENSITY_NV 0x8707
123 #define GL_SIGNED_INTENSITY8_NV 0x8708
124 #define GL_DSDT8_NV 0x8709
125 #define GL_DSDT8_MAG8_NV 0x870A
126 #define GL_DSDT8_MAG8_INTENSITY8_NV 0x870B
127 #define GL_SIGNED_RGB_UNSIGNED_ALPHA_NV 0x870C
128 #define GL_SIGNED_RGB8_UNSIGNED_ALPHA8_NV 0x870D
129#endif
130
131#ifndef GL_NV_float_buffer
132 #define GL_FLOAT_R_NV 0x8880
133 #define GL_FLOAT_RG_NV 0x8881
134 #define GL_FLOAT_RGB_NV 0x8882
135 #define GL_FLOAT_RGBA_NV 0x8883
136 #define GL_FLOAT_R16_NV 0x8884
137 #define GL_FLOAT_R32_NV 0x8885
138 #define GL_FLOAT_RG16_NV 0x8886
139 #define GL_FLOAT_RG32_NV 0x8887
140 #define GL_FLOAT_RGB16_NV 0x8888
141 #define GL_FLOAT_RGB32_NV 0x8889
142 #define GL_FLOAT_RGBA16_NV 0x888A
143 #define GL_FLOAT_RGBA32_NV 0x888B
144#endif
145
146#ifndef GL_ATI_texture_float
147 #define GL_RGBA_FLOAT32_ATI 0x8814
148 #define GL_RGB_FLOAT32_ATI 0x8815
149 #define GL_ALPHA_FLOAT32_ATI 0x8816
150 #define GL_INTENSITY_FLOAT32_ATI 0x8817
151 #define GL_LUMINANCE_FLOAT32_ATI 0x8818
152 #define GL_LUMINANCE_ALPHA_FLOAT32_ATI 0x8819
153 #define GL_RGBA_FLOAT16_ATI 0x881A
154 #define GL_RGB_FLOAT16_ATI 0x881B
155 #define GL_ALPHA_FLOAT16_ATI 0x881C
156 #define GL_INTENSITY_FLOAT16_ATI 0x881D
157 #define GL_LUMINANCE_FLOAT16_ATI 0x881E
158 #define GL_LUMINANCE_ALPHA_FLOAT16_ATI 0x881F
159#endif
160
161#ifndef GL_MIRRORED_REPEAT_IBM
162 #define GL_MIRRORED_REPEAT_IBM 0x8370
163#endif
164
165#ifndef GL_CLAMP_TO_EDGE
166 #define GL_CLAMP_TO_EDGE 0x812F
167#endif
168
169#ifndef GL_CLAMP
170 #define GL_CLAMP 0x2900
171#endif
172
173#ifndef GL_CLAMP_TO_BORDER_ARB
174 #define GL_CLAMP_TO_BORDER_ARB 0x812D
175#endif
176
177#ifndef GL_INTENSITY
178 // OpenGL ES1 and ES2 doesn't provide GL_INTENSITY
179 #define GL_INTENSITY 0x8049
180#endif
181
182#ifndef GL_GENERATE_MIPMAP_SGIS
183 #define GL_GENERATE_MIPMAP_SGIS 0x8191
184 #define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192
185#endif
186
187#ifndef GL_TEXTURE_3D
188 #define GL_TEXTURE_3D 0x806F
189#endif
190
191
192#ifndef GL_TEXTURE_CUBE_MAP
193 #define GL_TEXTURE_CUBE_MAP 0x8513
194 #define GL_TEXTURE_BINDING_CUBE_MAP 0x8514
195 #define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
196 #define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
197 #define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
198 #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
199 #define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
200 #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
201 #define GL_PROXY_TEXTURE_CUBE_MAP 0x851B
202 #define GL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C
203#endif
204
205#ifndef GL_TEXTURE_BINDING_3D
206 #define GL_TEXTURE_BINDING_3D 0x806A
207#endif
208
209#ifndef GL_DEPTH_TEXTURE_MODE_ARB
210 #define GL_DEPTH_TEXTURE_MODE_ARB 0x884B
211#endif
212
213#ifndef GL_TEXTURE_COMPARE_MODE_ARB
214 #define GL_TEXTURE_COMPARE_MODE_ARB 0x884C
215#endif
216#ifndef GL_TEXTURE_COMPARE_FUNC_ARB
217 #define GL_TEXTURE_COMPARE_FUNC_ARB 0x884D
218#endif
219#ifndef GL_COMPARE_R_TO_TEXTURE_ARB
220 #define GL_COMPARE_R_TO_TEXTURE_ARB 0x884E
221#endif
222
223#ifndef TEXTURE_COMPARE_FAIL_VALUE_ARB
224 #define TEXTURE_COMPARE_FAIL_VALUE_ARB 0x80BF
225#endif
226
227#if !defined( GL_MAX_TEXTURE_UNITS )
228 #define GL_MAX_TEXTURE_UNITS 0x84E2
229#endif
230
231#ifndef GL_TEXTURE_DEPTH
232 #define GL_TEXTURE_DEPTH 0x8071
233#endif
234
235#ifndef GL_TEXTURE_2D_MULTISAMPLE
236 #define GL_TEXTURE_2D_MULTISAMPLE 0x9100
237#endif
238
239// Integer texture extension as in http://www.opengl.org/registry/specs/EXT/texture_integer.txt
240#ifndef GL_EXT_texture_integer
241 #define GL_RGBA32UI_EXT 0x8D70
242 #define GL_RGB32UI_EXT 0x8D71
243 #define GL_ALPHA32UI_EXT 0x8D72
244 #define GL_INTENSITY32UI_EXT 0x8D73
245 #define GL_LUMINANCE32UI_EXT 0x8D74
246 #define GL_LUMINANCE_ALPHA32UI_EXT 0x8D75
247
248 #define GL_RGBA16UI_EXT 0x8D76
249 #define GL_RGB16UI_EXT 0x8D77
250 #define GL_ALPHA16UI_EXT 0x8D78
251 #define GL_INTENSITY16UI_EXT 0x8D79
252 #define GL_LUMINANCE16UI_EXT 0x8D7A
253 #define GL_LUMINANCE_ALPHA16UI_EXT 0x8D7B
254
255 #define GL_RGBA8UI_EXT 0x8D7C
256 #define GL_RGB8UI_EXT 0x8D7D
257 #define GL_ALPHA8UI_EXT 0x8D7E
258 #define GL_INTENSITY8UI_EXT 0x8D7F
259 #define GL_LUMINANCE8UI_EXT 0x8D80
260 #define GL_LUMINANCE_ALPHA8UI_EXT 0x8D81
261
262 #define GL_RGBA32I_EXT 0x8D82
263 #define GL_RGB32I_EXT 0x8D83
264 #define GL_ALPHA32I_EXT 0x8D84
265 #define GL_INTENSITY32I_EXT 0x8D85
266 #define GL_LUMINANCE32I_EXT 0x8D86
267 #define GL_LUMINANCE_ALPHA32I_EXT 0x8D87
268
269 #define GL_RGBA16I_EXT 0x8D88
270 #define GL_RGB16I_EXT 0x8D89
271 #define GL_ALPHA16I_EXT 0x8D8A
272 #define GL_INTENSITY16I_EXT 0x8D8B
273 #define GL_LUMINANCE16I_EXT 0x8D8C
274 #define GL_LUMINANCE_ALPHA16I_EXT 0x8D8D
275
276 #define GL_RGBA8I_EXT 0x8D8E
277 #define GL_RGB8I_EXT 0x8D8F
278 #define GL_ALPHA8I_EXT 0x8D90
279 #define GL_INTENSITY8I_EXT 0x8D91
280 #define GL_LUMINANCE8I_EXT 0x8D92
281 #define GL_LUMINANCE_ALPHA8I_EXT 0x8D93
282
283 #define GL_RED_INTEGER_EXT 0x8D94
284 #define GL_GREEN_INTEGER_EXT 0x8D95
285 #define GL_BLUE_INTEGER_EXT 0x8D96
286 #define GL_ALPHA_INTEGER_EXT 0x8D97
287 #define GL_RGB_INTEGER_EXT 0x8D98
288 #define GL_RGBA_INTEGER_EXT 0x8D99
289 #define GL_BGR_INTEGER_EXT 0x8D9A
290 #define GL_BGRA_INTEGER_EXT 0x8D9B
291 #define GL_LUMINANCE_INTEGER_EXT 0x8D9C
292 #define GL_LUMINANCE_ALPHA_INTEGER_EXT 0x8D9D
293
294 #define GL_RGBA_INTEGER_MODE_EXT 0x8D9E
295#endif
296
297#ifndef GL_VERSION_1_1
298#define GL_R3_G3_B2 0x2A10
299#define GL_RGB4 0x804F
300#define GL_RGB5 0x8050
301#define GL_RGB8 0x8051
302#define GL_RGB10 0x8052
303#define GL_RGB12 0x8053
304#define GL_RGB16 0x8054
305#define GL_RGBA2 0x8055
306#define GL_RGBA4 0x8056
307#define GL_RGB5_A1 0x8057
308#define GL_RGBA8 0x8058
309#define GL_RGB10_A2 0x8059
310#define GL_RGBA12 0x805A
311#define GL_RGBA16 0x805B
312#define GL_BGR_EXT 0x80E0
313#define GL_BGRA_EXT 0x80E1
314#endif
315
316#ifndef GL_ARB_texture_rg
317 #define GL_RG 0x8227
318 #define GL_RG_INTEGER 0x8228
319 #define GL_R8 0x8229
320 #define GL_R16 0x822A
321 #define GL_RG8 0x822B
322 #define GL_RG16 0x822C
323 #define GL_R16F 0x822D
324 #define GL_R32F 0x822E
325 #define GL_RG16F 0x822F
326 #define GL_RG32F 0x8230
327 #define GL_R8I 0x8231
328 #define GL_R8UI 0x8232
329 #define GL_R16I 0x8233
330 #define GL_R16UI 0x8234
331 #define GL_R32I 0x8235
332 #define GL_R32UI 0x8236
333 #define GL_RG8I 0x8237
334 #define GL_RG8UI 0x8238
335 #define GL_RG16I 0x8239
336 #define GL_RG16UI 0x823A
337 #define GL_RG32I 0x823B
338 #define GL_RG32UI 0x823C
339#endif
340
341#ifndef GL_ARB_shader_image_load_store
342 #define GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT 0x00000001
343 #define GL_ELEMENT_ARRAY_BARRIER_BIT 0x00000002
344 #define GL_UNIFORM_BARRIER_BIT 0x00000004
345 #define GL_TEXTURE_FETCH_BARRIER_BIT 0x00000008
346 #define GL_SHADER_IMAGE_ACCESS_BARRIER_BIT 0x00000020
347 #define GL_COMMAND_BARRIER_BIT 0x00000040
348 #define GL_PIXEL_BUFFER_BARRIER_BIT 0x00000080
349 #define GL_TEXTURE_UPDATE_BARRIER_BIT 0x00000100
350 #define GL_BUFFER_UPDATE_BARRIER_BIT 0x00000200
351 #define GL_FRAMEBUFFER_BARRIER_BIT 0x00000400
352 #define GL_TRANSFORM_FEEDBACK_BARRIER_BIT 0x00000800
353 #define GL_ATOMIC_COUNTER_BARRIER_BIT 0x00001000
354 #define GL_ALL_BARRIER_BITS 0xFFFFFFFF
355 #define GL_MAX_IMAGE_UNITS 0x8F38
356 #define GL_MAX_COMBINED_IMAGE_UNITS_AND_FRAGMENT_OUTPUTS 0x8F39
357 #define GL_IMAGE_BINDING_NAME 0x8F3A
358 #define GL_IMAGE_BINDING_LEVEL 0x8F3B
359 #define GL_IMAGE_BINDING_LAYERED 0x8F3C
360 #define GL_IMAGE_BINDING_LAYER 0x8F3D
361 #define GL_IMAGE_BINDING_ACCESS 0x8F3E
362 #define GL_IMAGE_1D 0x904C
363 #define GL_IMAGE_2D 0x904D
364 #define GL_IMAGE_3D 0x904E
365 #define GL_IMAGE_2D_RECT 0x904F
366 #define GL_IMAGE_CUBE 0x9050
367 #define GL_IMAGE_BUFFER 0x9051
368 #define GL_IMAGE_1D_ARRAY 0x9052
369 #define GL_IMAGE_2D_ARRAY 0x9053
370 #define GL_IMAGE_CUBE_MAP_ARRAY 0x9054
371 #define GL_IMAGE_2D_MULTISAMPLE 0x9055
372 #define GL_IMAGE_2D_MULTISAMPLE_ARRAY 0x9056
373 #define GL_INT_IMAGE_1D 0x9057
374 #define GL_INT_IMAGE_2D 0x9058
375 #define GL_INT_IMAGE_3D 0x9059
376 #define GL_INT_IMAGE_2D_RECT 0x905A
377 #define GL_INT_IMAGE_CUBE 0x905B
378 #define GL_INT_IMAGE_BUFFER 0x905C
379 #define GL_INT_IMAGE_1D_ARRAY 0x905D
380 #define GL_INT_IMAGE_2D_ARRAY 0x905E
381 #define GL_INT_IMAGE_CUBE_MAP_ARRAY 0x905F
382 #define GL_INT_IMAGE_2D_MULTISAMPLE 0x9060
383 #define GL_INT_IMAGE_2D_MULTISAMPLE_ARRAY 0x9061
384 #define GL_UNSIGNED_INT_IMAGE_1D 0x9062
385 #define GL_UNSIGNED_INT_IMAGE_2D 0x9063
386 #define GL_UNSIGNED_INT_IMAGE_3D 0x9064
387 #define GL_UNSIGNED_INT_IMAGE_2D_RECT 0x9065
388 #define GL_UNSIGNED_INT_IMAGE_CUBE 0x9066
389 #define GL_UNSIGNED_INT_IMAGE_BUFFER 0x9067
390 #define GL_UNSIGNED_INT_IMAGE_1D_ARRAY 0x9068
391 #define GL_UNSIGNED_INT_IMAGE_2D_ARRAY 0x9069
392 #define GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY 0x906A
393 #define GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE 0x906B
394 #define GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE_ARRAY 0x906C
395 #define GL_MAX_IMAGE_SAMPLES 0x906D
396 #define GL_IMAGE_BINDING_FORMAT 0x906E
397 #define GL_IMAGE_FORMAT_COMPATIBILITY_TYPE 0x90C7
398 #define GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE 0x90C8
399 #define GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS 0x90C9
400 #define GL_MAX_VERTEX_IMAGE_UNIFORMS 0x90CA
401 #define GL_MAX_TESS_CONTROL_IMAGE_UNIFORMS 0x90CB
402 #define GL_MAX_TESS_EVALUATION_IMAGE_UNIFORMS 0x90CC
403 #define GL_MAX_GEOMETRY_IMAGE_UNIFORMS 0x90CD
404 #define GL_MAX_FRAGMENT_IMAGE_UNIFORMS 0x90CE
405 #define GL_MAX_COMBINED_IMAGE_UNIFORMS 0x90CF
406#endif
407
408//#define OSG_COLLECT_TEXTURE_APPLIED_STATS 1
409
410namespace osg {
411
412
413// forward declare
414class TextureObjectSet;
415class TextureObjectManager;
416
417
418/** Texture pure virtual base class that encapsulates OpenGL texture
419 * functionality common to the various types of OSG textures.
420*/
421class OSG_EXPORT Texture : public osg::StateAttribute
422{
423
424 public :
425
426 Texture();
427
428 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
429 Texture(const Texture& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
430
431 virtual osg::Object* cloneType() const = 0;
432 virtual osg::Object* clone(const CopyOp& copyop) const = 0;
433 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Texture *>(obj)!=NULL; }
434 virtual const char* libraryName() const { return "osg"; }
435 virtual const char* className() const { return "Texture"; }
436
437 /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
438 virtual Texture* asTexture() { return this; }
439
440 /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
441 virtual const Texture* asTexture() const { return this; }
442
443 virtual Type getType() const { return TEXTURE; }
444
445 virtual bool isTextureAttribute() const { return true; }
446
447 virtual GLenum getTextureTarget() const = 0;
448
449 #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
450 virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
451 {
452 usage.usesTextureMode(getTextureTarget());
453 return true;
454 }
455 #endif
456
457 virtual int getTextureWidth() const { return 0; }
458 virtual int getTextureHeight() const { return 0; }
459 virtual int getTextureDepth() const { return 0; }
460
461 enum WrapParameter {
462 WRAP_S,
463 WRAP_T,
464 WRAP_R
465 };
466
467 enum WrapMode {
468 CLAMP = GL_CLAMP,
469 CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
470 CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER_ARB,
471 REPEAT = GL_REPEAT,
472 MIRROR = GL_MIRRORED_REPEAT_IBM
473 };
474
475 /** Sets the texture wrap mode. */
476 void setWrap(WrapParameter which, WrapMode wrap);
477 /** Gets the texture wrap mode. */
478 WrapMode getWrap(WrapParameter which) const;
479
480
481 /** Sets the border color. Only used when wrap mode is CLAMP_TO_BORDER.
482 * The border color will be casted to the appropriate type to match the
483 * internal pixel format of the texture. */
484 void setBorderColor(const Vec4d& color) { _borderColor = color; dirtyTextureParameters(); }
485
486 /** Gets the border color. */
487 const Vec4d& getBorderColor() const { return _borderColor; }
488
489 /** Sets the border width. */
490 void setBorderWidth(GLint width) { _borderWidth = width; dirtyTextureParameters(); }
491
492 GLint getBorderWidth() const { return _borderWidth; }
493
494 enum FilterParameter {
495 MIN_FILTER,
496 MAG_FILTER
497 };
498
499 enum FilterMode {
500 LINEAR = GL_LINEAR,
501 LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
502 LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
503 NEAREST = GL_NEAREST,
504 NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
505 NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST
506 };
507
508 /** Sets the texture filter mode. */
509 void setFilter(FilterParameter which, FilterMode filter);
510
511 /** Gets the texture filter mode. */
512 FilterMode getFilter(FilterParameter which) const;
513
514 /** Sets the maximum anisotropy value, default value is 1.0 for no
515 * anisotropic filtering. If hardware does not support anisotropic
516 * filtering, use normal filtering (equivalent to a max anisotropy
517 * value of 1.0. Valid range is 1.0f upwards. The maximum value
518 * depends on the graphics system. */
519 void setMaxAnisotropy(float anis);
520
521 /** Gets the maximum anisotropy value. */
522 inline float getMaxAnisotropy() const { return _maxAnisotropy; }
523
524 /** Sets the minimum level of detail value. */
525 void setMinLOD(float minlod);
526
527 /** Gets the minimum level of detail value. */
528 inline float getMinLOD() const { return _minlod; }
529
530 /** Sets the maximum level of detail value. */
531 void setMaxLOD(float maxlod);
532
533 /** Gets the maximum level of detail value. */
534 inline float getMaxLOD() const { return _maxlod; }
535
536 /** Gets the level of detail bias value. */
537 void setLODBias(float lodbias);
538
539 /** Sets the level of detail bias value. */
540 inline float getLODBias() const { return _lodbias; }
541
542 /** Configure the source of texture swizzling for all channels */
543 inline void setSwizzle(const Vec4i& swizzle) { _swizzle = swizzle; dirtyTextureParameters(); };
544
545 /** Gets the source of texture swizzling for all channels */
546 inline const Vec4i& getSwizzle() const { return _swizzle; }
547
548 /** Sets the hardware mipmap generation hint. If enabled, it will
549 * only be used if supported in the graphics system. */
550 inline void setUseHardwareMipMapGeneration(bool useHardwareMipMapGeneration) { _useHardwareMipMapGeneration = useHardwareMipMapGeneration; }
551
552 /** Gets the hardware mipmap generation hint. */
553 inline bool getUseHardwareMipMapGeneration() const { return _useHardwareMipMapGeneration; }
554
555 /** Sets whether or not the apply() function will unreference the image
556 * data. If enabled, and the image data is only referenced by this
557 * Texture, apply() will delete the image data. */
558 inline void setUnRefImageDataAfterApply(bool flag) { _unrefImageDataAfterApply = flag; }
559
560 /** Gets whether or not apply() unreferences image data. */
561 inline bool getUnRefImageDataAfterApply() const { return _unrefImageDataAfterApply; }
562
563 /** Sets whether to use client storage for the texture, if supported
564 * by the graphics system. Note: If enabled, and the graphics system
565 * supports it, the osg::Image(s) associated with this texture cannot
566 * be deleted, so the UnRefImageDataAfterApply flag would be ignored. */
567 inline void setClientStorageHint(bool flag) { _clientStorageHint = flag; }
568
569 /** Gets whether to use client storage for the texture. */
570 inline bool getClientStorageHint() const { return _clientStorageHint; }
571
572 /** Sets whether to force the texture to resize images that have dimensions
573 * that are not a power of two. If enabled, NPOT images will be resized,
574 * whether or not NPOT textures are supported by the hardware. If disabled,
575 * NPOT images will not be resized if supported by hardware. */
576 inline void setResizeNonPowerOfTwoHint(bool flag) { _resizeNonPowerOfTwoHint = flag; }
577
578 /** Gets whether texture will force non power to two images to be resized. */
579 inline bool getResizeNonPowerOfTwoHint() const { return _resizeNonPowerOfTwoHint; }
580
581 enum InternalFormatMode {
582 USE_IMAGE_DATA_FORMAT,
583 USE_USER_DEFINED_FORMAT,
584 USE_ARB_COMPRESSION,
585 USE_S3TC_DXT1_COMPRESSION,
586 USE_S3TC_DXT3_COMPRESSION,
587 USE_S3TC_DXT5_COMPRESSION,
588 USE_PVRTC_2BPP_COMPRESSION,
589 USE_PVRTC_4BPP_COMPRESSION,
590 USE_ETC_COMPRESSION,
591 USE_ETC2_COMPRESSION,
592 USE_RGTC1_COMPRESSION,
593 USE_RGTC2_COMPRESSION,
594 USE_S3TC_DXT1c_COMPRESSION,
595 USE_S3TC_DXT1a_COMPRESSION
596 };
597
598 /** Sets the internal texture format mode. Note: If the texture format is
599 * USE_IMAGE_DATA_FORMAT, USE_ARB_COMPRESSION, or USE_S3TC_COMPRESSION,
600 * the internal format mode is set automatically and will overwrite the
601 * previous _internalFormat. */
602 inline void setInternalFormatMode(InternalFormatMode mode) { _internalFormatMode = mode; }
603
604 /** Gets the internal texture format mode. */
605 inline InternalFormatMode getInternalFormatMode() const { return _internalFormatMode; }
606
607 /** Sets the internal texture format. Implicitly sets the
608 * internalFormatMode to USE_USER_DEFINED_FORMAT.
609 * The corresponding internal format type will be computed. */
610 inline void setInternalFormat(GLint internalFormat)
611 {
612 _internalFormatMode = USE_USER_DEFINED_FORMAT;
613 _internalFormat = internalFormat;
614 computeInternalFormatType();
615 }
616
617
618 /** Gets the internal texture format. */
619 inline GLint getInternalFormat() const { if (_internalFormat==0) computeInternalFormat(); return _internalFormat; }
620
621 /** Return true if the internal format is one of the compressed formats.*/
622 bool isCompressedInternalFormat() const;
623
624 /** Sets the external source image format, used as a fallback when no osg::Image is attached to provide the source image format. */
625 inline void setSourceFormat(GLenum sourceFormat) { _sourceFormat = sourceFormat; }
626
627 /** Gets the external source image format. */
628 inline GLenum getSourceFormat() const { return _sourceFormat; }
629
630 /** Sets the external source data type, used as a fallback when no osg::Image is attached to provide the source image format.*/
631 inline void setSourceType(GLenum sourceType) { _sourceType = sourceType; }
632
633 /** Gets the external source data type.*/
634 inline GLenum getSourceType() const { return _sourceType; }
635
636 /** Texture type determined by the internal texture format */
637 enum InternalFormatType{
638
639 //! default OpenGL format (clamped values to [0,1) or [0,255])
640 NORMALIZED = 0x0,
641
642 //! float values, Shader Model 3.0 (see ARB_texture_float)
643 FLOAT = 0x1,
644
645 //! Signed integer values (see EXT_texture_integer)
646 SIGNED_INTEGER = 0x2,
647
648 //! Unsigned integer value (see EXT_texture_integer)
649 UNSIGNED_INTEGER = 0x4
650 };
651
652 /** Get the internal texture format type. */
653 inline InternalFormatType getInternalFormatType() const { return _internalFormatType; }
654
655 /* select the size internal format to use based on Image when available or Texture format settings.*/
656 GLenum selectSizedInternalFormat(const osg::Image* image=0) const;
657
658 class TextureObject;
659
660 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
661 virtual bool isDirty(unsigned int /*contextID*/) const { return false; }
662
663
664 /** Returns a pointer to the TextureObject for the current context. */
665 inline TextureObject* getTextureObject(unsigned int contextID) const
666 {
667 return _textureObjectBuffer[contextID].get();
668 }
669
670 inline void setTextureObject(unsigned int contextID, TextureObject* to)
671 {
672 _textureObjectBuffer[contextID] = to;
673 }
674
675 /** Forces a recompile on next apply() of associated OpenGL texture
676 * objects. */
677 void dirtyTextureObject();
678
679 /** Returns true if the texture objects for all the required graphics
680 * contexts are loaded. */
681 bool areAllTextureObjectsLoaded() const;
682
683
684 /** Gets the dirty flag for the current contextID. */
685 inline unsigned int& getTextureParameterDirty(unsigned int contextID) const
686 {
687 return _texParametersDirtyList[contextID];
688 }
689
690
691 /** Force a reset on next apply() of associated OpenGL texture
692 * parameters. */
693 void dirtyTextureParameters();
694
695 /** Force a manual allocation of the mipmap levels on the next apply() call.
696 * User is responsible for filling the mipmap levels with valid data.
697 * The OpenGL's glGenerateMipmapEXT function is used to generate the mipmap levels.
698 * If glGenerateMipmapEXT is not supported or texture's internal format is not supported
699 * by the glGenerateMipmapEXT, then empty mipmap levels will
700 * be allocated manually. The mipmap levels are also allocated if a non-mipmapped
701 * min filter is used. */
702 void allocateMipmapLevels();
703
704 /** Sets GL_TEXTURE_COMPARE_MODE_ARB to GL_COMPARE_R_TO_TEXTURE_ARB
705 * See http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow.txt. */
706 void setShadowComparison(bool flag) { _use_shadow_comparison = flag; }
707 bool getShadowComparison() const { return _use_shadow_comparison; }
708
709 enum ShadowCompareFunc {
710 NEVER = GL_NEVER,
711 LESS = GL_LESS,
712 EQUAL = GL_EQUAL,
713 LEQUAL = GL_LEQUAL,
714 GREATER = GL_GREATER,
715 NOTEQUAL = GL_NOTEQUAL,
716 GEQUAL = GL_GEQUAL,
717 ALWAYS = GL_ALWAYS
718 };
719
720 /** Sets shadow texture comparison function. */
721 void setShadowCompareFunc(ShadowCompareFunc func) { _shadow_compare_func = func; }
722 ShadowCompareFunc getShadowCompareFunc() const { return _shadow_compare_func; }
723
724 enum ShadowTextureMode {
725 LUMINANCE = GL_LUMINANCE,
726 INTENSITY = GL_INTENSITY,
727 ALPHA = GL_ALPHA,
728 NONE = GL_NONE
729 };
730
731 /** Sets shadow texture mode after comparison. */
732 void setShadowTextureMode(ShadowTextureMode mode) { _shadow_texture_mode = mode; }
733 ShadowTextureMode getShadowTextureMode() const { return _shadow_texture_mode; }
734
735 /** Sets the TEXTURE_COMPARE_FAIL_VALUE_ARB texture parameter. See
736 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow_ambient.txt. */
737 void setShadowAmbient(float shadow_ambient) { _shadow_ambient = shadow_ambient; }
738 float getShadowAmbient() const { return _shadow_ambient; }
739
740
741 /** Sets the texture image for the specified face. */
742 virtual void setImage(unsigned int face, Image* image) = 0;
743
744 template<class T> void setImage(unsigned int face, const ref_ptr<T>& image) { setImage(face, image.get()); }
745
746 /** Gets the texture image for the specified face. */
747 virtual Image* getImage(unsigned int face) = 0;
748
749 /** Gets the const texture image for specified face. */
750 virtual const Image* getImage(unsigned int face) const = 0;
751
752 /** Gets the number of images that can be assigned to this Texture. */
753 virtual unsigned int getNumImages() const = 0;
754
755
756 /** Set the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
757 void setReadPBuffer(GraphicsContext* context) { _readPBuffer = context; }
758
759 template<class T> void setReadPBuffer(const ref_ptr<T>& context) { setReadPBuffer(context.get()); }
760
761 /** Get the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
762 GraphicsContext* getReadPBuffer() { return _readPBuffer.get(); }
763
764 /** Get the const PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
765 const GraphicsContext* getReadPBuffer() const { return _readPBuffer.get(); }
766
767 /** Texture is a pure virtual base class, apply must be overridden. */
768 virtual void apply(State& state) const = 0;
769
770 /** Calls apply(state) to compile the texture. */
771 virtual void compileGLObjects(State& state) const;
772
773 /** Resize any per context GLObject buffers to specified size. */
774 virtual void resizeGLObjectBuffers(unsigned int maxSize);
775
776 /** If State is non-zero, this function releases OpenGL objects for
777 * the specified graphics context. Otherwise, releases OpenGL objects
778 * for all graphics contexts. */
779 virtual void releaseGLObjects(State* state=0) const;
780
781 /** Determine whether the given internalFormat is a compressed
782 * image format. */
783 static bool isCompressedInternalFormat(GLint internalFormat);
784
785 /** Determine the size of a compressed image, given the internalFormat,
786 * the width, the height, and the depth of the image. The block size
787 * and the size are output parameters. */
788 static void getCompressedSize(GLenum internalFormat, GLint width, GLint height, GLint depth, GLint& blockSize, GLint& size);
789
790
791 /** Helper method. Creates the texture, but doesn't set or use a
792 * texture binding. Note: Don't call this method directly unless
793 * you're implementing a subload callback. */
794 void applyTexImage2D_load(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height,GLsizei numMipmapLevels) const;
795
796 /** Helper method. Subloads images into the texture, but doesn't set
797 * or use a texture binding. Note: Don't call this method directly
798 * unless you're implementing a subload callback. */
799 void applyTexImage2D_subload(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height, GLint inInternalFormat, GLsizei numMipmapLevels) const;
800
801
802 /** Returned by mipmapBeforeTexImage() to indicate what
803 * mipmapAfterTexImage() should do */
804 enum GenerateMipmapMode
805 {
806 GENERATE_MIPMAP_NONE,
807 GENERATE_MIPMAP,
808 GENERATE_MIPMAP_TEX_PARAMETER
809 };
810
811 protected :
812
813 virtual ~Texture();
814
815 virtual void computeInternalFormat() const = 0;
816
817 /** Computes the internal format from Image parameters. */
818 void computeInternalFormatWithImage(const osg::Image& image) const;
819
820 /** Computes the texture dimension for the given Image. */
821 void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& numMipmapLevels) const;
822
823 /** Computes the internal format type. */
824 void computeInternalFormatType() const;
825
826 /** Helper method. Sets texture parameters. */
827 void applyTexParameters(GLenum target, State& state) const;
828
829 /** Returns true if _useHardwareMipMapGeneration is true and either
830 * glGenerateMipmapEXT() or GL_GENERATE_MIPMAP_SGIS are supported. */
831 bool isHardwareMipmapGenerationEnabled(const State& state) const;
832
833 /** Returns true if the associated Image should be released and it's safe to do so. */
834 bool isSafeToUnrefImageData(const State& state) const {
835 return (_unrefImageDataAfterApply && state.getMaxTexturePoolSize()==0 && areAllTextureObjectsLoaded());
836 }
837
838 /** Helper methods to be called before and after calling
839 * gl[Compressed][Copy]Tex[Sub]Image2D to handle generating mipmaps. */
840 GenerateMipmapMode mipmapBeforeTexImage(const State& state, bool hardwareMipmapOn) const;
841 void mipmapAfterTexImage(State& state, GenerateMipmapMode beforeResult) const;
842
843 /** Helper method to generate mipmap levels by calling of glGenerateMipmapEXT.
844 * If it is not supported, then call the virtual allocateMipmap() method */
845 void generateMipmap(State& state) const;
846
847 /** Allocate mipmap levels of the texture by subsequent calling of glTexImage* function. */
848 virtual void allocateMipmap(State& state) const = 0;
849
850 /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
851 int compareTexture(const Texture& rhs) const;
852
853 /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
854 int compareTextureObjects(const Texture& rhs) const;
855
856 typedef buffered_value<unsigned int> TexParameterDirtyList;
857 mutable TexParameterDirtyList _texParametersDirtyList;
858 mutable TexParameterDirtyList _texMipmapGenerationDirtyList;
859
860 WrapMode _wrap_s;
861 WrapMode _wrap_t;
862 WrapMode _wrap_r;
863
864 FilterMode _min_filter;
865 FilterMode _mag_filter;
866 float _maxAnisotropy;
867 float _minlod;
868 float _maxlod;
869 float _lodbias;
870 Vec4i _swizzle;
871 bool _useHardwareMipMapGeneration;
872 bool _unrefImageDataAfterApply;
873 bool _clientStorageHint;
874 bool _resizeNonPowerOfTwoHint;
875
876 Vec4d _borderColor;
877 GLint _borderWidth;
878
879 InternalFormatMode _internalFormatMode;
880 mutable InternalFormatType _internalFormatType;
881 mutable GLint _internalFormat;
882 mutable GLenum _sourceFormat;
883 mutable GLenum _sourceType;
884
885 bool _use_shadow_comparison;
886 ShadowCompareFunc _shadow_compare_func;
887 ShadowTextureMode _shadow_texture_mode;
888 float _shadow_ambient;
889
890 public:
891
892 struct OSG_EXPORT TextureProfile
893 {
894 inline TextureProfile(GLenum target):
895 _target(target),
896 _numMipmapLevels(0),
897 _internalFormat(0),
898 _width(0),
899 _height(0),
900 _depth(0),
901 _border(0),
902 _size(0) {}
903
904 inline TextureProfile(GLenum target,
905 GLint numMipmapLevels,
906 GLenum internalFormat,
907 GLsizei width,
908 GLsizei height,
909 GLsizei depth,
910 GLint border):
911 _target(target),
912 _numMipmapLevels(numMipmapLevels),
913 _internalFormat(internalFormat),
914 _width(width),
915 _height(height),
916 _depth(depth),
917 _border(border),
918 _size(0) { computeSize(); }
919
920
921 #define LESSTHAN(A,B) if (A<B) return true; if (B<A) return false;
922 #define FINALLESSTHAN(A,B) return (A<B);
923
924 bool operator < (const TextureProfile& rhs) const
925 {
926 LESSTHAN(_size,rhs._size);
927 LESSTHAN(_target,rhs._target);
928 LESSTHAN(_numMipmapLevels,rhs._numMipmapLevels);
929 LESSTHAN(_internalFormat,rhs._internalFormat);
930 LESSTHAN(_width,rhs._width);
931 LESSTHAN(_height,rhs._height);
932 LESSTHAN(_depth,rhs._depth);
933 FINALLESSTHAN(_border, rhs._border);
934 }
935
936 bool operator == (const TextureProfile& rhs) const
937 {
938 return _target == rhs._target &&
939 _numMipmapLevels == rhs._numMipmapLevels &&
940 _internalFormat == rhs._internalFormat &&
941 _width == rhs._width &&
942 _height == rhs._height &&
943 _depth == rhs._depth &&
944 _border == rhs._border;
945 }
946
947 inline void set(GLint numMipmapLevels,
948 GLenum internalFormat,
949 GLsizei width,
950 GLsizei height,
951 GLsizei depth,
952 GLint border)
953 {
954 _numMipmapLevels = numMipmapLevels;
955 _internalFormat = internalFormat;
956 _width = width;
957 _height = height;
958 _depth = depth;
959 _border = border;
960 computeSize();
961 }
962
963 inline bool match(GLenum target,
964 GLint numMipmapLevels,
965 GLenum internalFormat,
966 GLsizei width,
967 GLsizei height,
968 GLsizei depth,
969 GLint border)
970 {
971 return (_target == target) &&
972 (_numMipmapLevels == numMipmapLevels) &&
973 (_internalFormat == internalFormat) &&
974 (_width == width) &&
975 (_height == height) &&
976 (_depth == depth) &&
977 (_border == border);
978 }
979
980 void computeSize();
981
982 GLenum _target;
983 GLint _numMipmapLevels;
984 GLenum _internalFormat;
985 GLsizei _width;
986 GLsizei _height;
987 GLsizei _depth;
988 GLint _border;
989 unsigned int _size;
990 };
991
992 class OSG_EXPORT TextureObject : public GraphicsObject
993 {
994 public:
995
996 inline TextureObject(Texture* texture, GLuint id, GLenum target):
997 _id(id),
998 _profile(target),
999 _set(0),
1000 _previous(0),
1001 _next(0),
1002 _texture(texture),
1003 _allocated(false),
1004 _frameLastUsed(0),
1005 _timeStamp(0) {}
1006
1007 inline TextureObject(Texture* texture, GLuint id, const TextureProfile& profile):
1008 _id(id),
1009 _profile(profile),
1010 _set(0),
1011 _previous(0),
1012 _next(0),
1013 _texture(texture),
1014 _allocated(false),
1015 _frameLastUsed(0),
1016 _timeStamp(0) {}
1017
1018 inline TextureObject(Texture* texture,
1019 GLuint id,
1020 GLenum target,
1021 GLint numMipmapLevels,
1022 GLenum internalFormat,
1023 GLsizei width,
1024 GLsizei height,
1025 GLsizei depth,
1026 GLint border):
1027 _id(id),
1028 _profile(target,numMipmapLevels,internalFormat,width,height,depth,border),
1029 _set(0),
1030 _previous(0),
1031 _next(0),
1032 _texture(texture),
1033 _allocated(false),
1034 _frameLastUsed(0),
1035 _timeStamp(0) {}
1036
1037 inline bool match(GLenum target,
1038 GLint numMipmapLevels,
1039 GLenum internalFormat,
1040 GLsizei width,
1041 GLsizei height,
1042 GLsizei depth,
1043 GLint border)
1044 {
1045 return isReusable() &&
1046 _profile.match(target,numMipmapLevels,internalFormat,width,height,depth,border);
1047 }
1048
1049
1050 void bind();
1051
1052 inline GLenum id() const { return _id; }
1053 inline GLenum target() const { return _profile._target; }
1054
1055 inline unsigned int size() const { return _profile._size; }
1056
1057 inline void setTexture(Texture* texture) { _texture = texture; }
1058 inline Texture* getTexture() const { return _texture; }
1059
1060 inline void setTimeStamp(double timestamp) { _timeStamp = timestamp; }
1061 inline double getTimeStamp() const { return _timeStamp; }
1062
1063 inline void setAllocated(bool allocated=true) { _allocated = allocated; }
1064
1065 void setAllocated(GLint numMipmapLevels,
1066 GLenum internalFormat,
1067 GLsizei width,
1068 GLsizei height,
1069 GLsizei depth,
1070 GLint border);
1071
1072 inline bool isAllocated() const { return _allocated; }
1073
1074 inline bool isReusable() const { return _allocated && _profile._width!=0; }
1075
1076 /** release TextureObject to the orphan list to be reused or deleted.*/
1077 void release();
1078
1079 GLuint _id;
1080 TextureProfile _profile;
1081 TextureObjectSet* _set;
1082 TextureObject* _previous;
1083 TextureObject* _next;
1084 Texture* _texture;
1085 bool _allocated;
1086 unsigned int _frameLastUsed;
1087 double _timeStamp;
1088
1089 protected:
1090 virtual ~TextureObject();
1091
1092 };
1093
1094 typedef std::list< ref_ptr<TextureObject> > TextureObjectList;
1095
1096
1097 static osg::ref_ptr<TextureObject> generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target);
1098
1099 static osg::ref_ptr<TextureObject> generateTextureObject(const Texture* texture,
1100 unsigned int contextID,
1101 GLenum target,
1102 GLint numMipmapLevels,
1103 GLenum internalFormat,
1104 GLsizei width,
1105 GLsizei height,
1106 GLsizei depth,
1107 GLint border);
1108
1109 TextureObject* generateAndAssignTextureObject(unsigned int contextID, GLenum target) const;
1110
1111 TextureObject* generateAndAssignTextureObject(unsigned int contextID,
1112 GLenum target,
1113 GLint numMipmapLevels,
1114 GLenum internalFormat,
1115 GLsizei width,
1116 GLsizei height,
1117 GLsizei depth,
1118 GLint border) const;
1119
1120 protected:
1121
1122 typedef buffered_object< ref_ptr<TextureObject> > TextureObjectBuffer;
1123 mutable TextureObjectBuffer _textureObjectBuffer;
1124 mutable ref_ptr<GraphicsContext> _readPBuffer;
1125
1126};
1127
1128class OSG_EXPORT TextureObjectSet : public Referenced
1129{
1130public:
1131 TextureObjectSet(TextureObjectManager* parent, const Texture::TextureProfile& profile);
1132
1133 const Texture::TextureProfile& getProfile() const { return _profile; }
1134
1135 void handlePendingOrphandedTextureObjects();
1136
1137 void deleteAllTextureObjects();
1138 void discardAllTextureObjects();
1139 void flushAllDeletedTextureObjects();
1140 void discardAllDeletedTextureObjects();
1141 void flushDeletedTextureObjects(double currentTime, double& availableTime);
1142
1143 osg::ref_ptr<Texture::TextureObject> takeFromOrphans(Texture* texture);
1144 osg::ref_ptr<Texture::TextureObject> takeOrGenerate(Texture* texture);
1145 void moveToBack(Texture::TextureObject* to);
1146 void addToBack(Texture::TextureObject* to);
1147 void orphan(Texture::TextureObject* to);
1148 void remove(Texture::TextureObject* to);
1149 void moveToSet(Texture::TextureObject* to, TextureObjectSet* set);
1150
1151 unsigned int size() const { return _profile._size * _numOfTextureObjects; }
1152
1153 bool makeSpace(unsigned int& size);
1154
1155 bool checkConsistency() const;
1156
1157 TextureObjectManager* getParent() { return _parent; }
1158
1159 unsigned int computeNumTextureObjectsInList() const;
1160 unsigned int getNumOfTextureObjects() const { return _numOfTextureObjects; }
1161 unsigned int getNumOrphans() const { return static_cast<unsigned int>(_orphanedTextureObjects.size()); }
1162 unsigned int getNumPendingOrphans() const { return static_cast<unsigned int>(_pendingOrphanedTextureObjects.size()); }
1163
1164protected:
1165
1166 virtual ~TextureObjectSet();
1167
1168 OpenThreads::Mutex _mutex;
1169
1170 TextureObjectManager* _parent;
1171 unsigned int _contextID;
1172 Texture::TextureProfile _profile;
1173 unsigned int _numOfTextureObjects;
1174 Texture::TextureObjectList _orphanedTextureObjects;
1175 Texture::TextureObjectList _pendingOrphanedTextureObjects;
1176
1177 Texture::TextureObject* _head;
1178 Texture::TextureObject* _tail;
1179
1180};
1181
1182class OSG_EXPORT TextureObjectManager : public GraphicsObjectManager
1183{
1184public:
1185 TextureObjectManager(unsigned int contextID);
1186
1187
1188 void setNumberActiveTextureObjects(unsigned int size) { _numActiveTextureObjects = size; }
1189 unsigned int& getNumberActiveTextureObjects() { return _numActiveTextureObjects; }
1190 unsigned int getNumberActiveTextureObjects() const { return _numActiveTextureObjects; }
1191
1192 void setNumberOrphanedTextureObjects(unsigned int size) { _numOrphanedTextureObjects = size; }
1193 unsigned int& getNumberOrphanedTextureObjects() { return _numOrphanedTextureObjects; }
1194 unsigned int getNumberOrphanedTextureObjects() const { return _numOrphanedTextureObjects; }
1195
1196 void setCurrTexturePoolSize(unsigned int size) { _currTexturePoolSize = size; }
1197 unsigned int& getCurrTexturePoolSize() { return _currTexturePoolSize; }
1198 unsigned int getCurrTexturePoolSize() const { return _currTexturePoolSize; }
1199
1200 void setMaxTexturePoolSize(unsigned int size);
1201 unsigned int getMaxTexturePoolSize() const { return _maxTexturePoolSize; }
1202
1203 bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; }
1204 bool makeSpace(unsigned int size);
1205
1206 osg::ref_ptr<Texture::TextureObject> generateTextureObject(const Texture* texture, GLenum target);
1207 osg::ref_ptr<Texture::TextureObject> generateTextureObject(const Texture* texture,
1208 GLenum target,
1209 GLint numMipmapLevels,
1210 GLenum internalFormat,
1211 GLsizei width,
1212 GLsizei height,
1213 GLsizei depth,
1214 GLint border);
1215 void handlePendingOrphandedTextureObjects();
1216
1217 void deleteAllGLObjects();
1218 void discardAllGLObjects();
1219 void flushAllDeletedGLObjects();
1220 void discardAllDeletedGLObjects();
1221 void flushDeletedGLObjects(double currentTime, double& availableTime);
1222
1223 TextureObjectSet* getTextureObjectSet(const Texture::TextureProfile& profile);
1224
1225 void newFrame(osg::FrameStamp* fs);
1226 void resetStats();
1227 void reportStats(std::ostream& out);
1228 void recomputeStats(std::ostream& out) const;
1229 bool checkConsistency() const;
1230
1231 unsigned int& getFrameNumber() { return _frameNumber; }
1232 unsigned int& getNumberFrames() { return _numFrames; }
1233
1234 unsigned int& getNumberDeleted() { return _numDeleted; }
1235 double& getDeleteTime() { return _deleteTime; }
1236
1237 unsigned int& getNumberGenerated() { return _numGenerated; }
1238 double& getGenerateTime() { return _generateTime; }
1239
1240protected:
1241
1242 ~TextureObjectManager();
1243
1244 typedef std::map< Texture::TextureProfile, osg::ref_ptr<TextureObjectSet> > TextureSetMap;
1245
1246 unsigned int _numActiveTextureObjects;
1247 unsigned int _numOrphanedTextureObjects;
1248 unsigned int _currTexturePoolSize;
1249 unsigned int _maxTexturePoolSize;
1250 TextureSetMap _textureSetMap;
1251
1252 unsigned int _frameNumber;
1253
1254 unsigned int _numFrames;
1255 unsigned int _numDeleted;
1256 double _deleteTime;
1257
1258 unsigned int _numGenerated;
1259 double _generateTime;
1260};
1261}
1262
1263#endif