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