| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/GLExtensions> |
|---|
| 14 | #include <osg/Texture2DArray> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | #include <string.h> |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | using namespace osg; |
|---|
| 22 | |
|---|
| 23 | Texture2DArray::Texture2DArray(): |
|---|
| 24 | _textureWidth(0), |
|---|
| 25 | _textureHeight(0), |
|---|
| 26 | _textureDepth(0), |
|---|
| 27 | _numMipmapLevels(0) |
|---|
| 28 | { |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | Texture2DArray::Texture2DArray(const Texture2DArray& text,const CopyOp& copyop): |
|---|
| 32 | Texture(text,copyop), |
|---|
| 33 | _textureWidth(text._textureWidth), |
|---|
| 34 | _textureHeight(text._textureHeight), |
|---|
| 35 | _textureDepth(text._textureDepth), |
|---|
| 36 | _numMipmapLevels(text._numMipmapLevels), |
|---|
| 37 | _subloadCallback(text._subloadCallback) |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | for (int i=0; i < text._textureDepth; i++) |
|---|
| 41 | { |
|---|
| 42 | _images.push_back(copyop(text._images[i].get())); |
|---|
| 43 | _modifiedCount.push_back(ImageModifiedCount()); |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | Texture2DArray::~Texture2DArray() |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | int Texture2DArray::compare(const StateAttribute& sa) const |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | COMPARE_StateAttribute_Types(Texture2DArray,sa) |
|---|
| 56 | |
|---|
| 57 | bool noImages = true; |
|---|
| 58 | for (int n=0; n < _textureDepth; n++) |
|---|
| 59 | { |
|---|
| 60 | if (noImages && _images[n].valid()) noImages = false; |
|---|
| 61 | if (noImages && rhs._images[n].valid()) noImages = false; |
|---|
| 62 | |
|---|
| 63 | if (_images[n]!=rhs._images[n]) |
|---|
| 64 | { |
|---|
| 65 | if (_images[n].valid()) |
|---|
| 66 | { |
|---|
| 67 | if (rhs._images[n].valid()) |
|---|
| 68 | { |
|---|
| 69 | int result = _images[n]->compare(*rhs._images[n]); |
|---|
| 70 | if (result!=0) return result; |
|---|
| 71 | } |
|---|
| 72 | else |
|---|
| 73 | { |
|---|
| 74 | return 1; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | else if (rhs._images[n].valid()) |
|---|
| 78 | { |
|---|
| 79 | return -1; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | if (noImages) |
|---|
| 86 | { |
|---|
| 87 | int result = compareTextureObjects(rhs); |
|---|
| 88 | if (result!=0) return result; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | int result = compareTexture(rhs); |
|---|
| 92 | if (result!=0) return result; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | COMPARE_StateAttribute_Parameter(_textureWidth) |
|---|
| 96 | COMPARE_StateAttribute_Parameter(_textureHeight) |
|---|
| 97 | COMPARE_StateAttribute_Parameter(_textureDepth) |
|---|
| 98 | COMPARE_StateAttribute_Parameter(_subloadCallback) |
|---|
| 99 | |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void Texture2DArray::setImage(unsigned int layer, Image* image) |
|---|
| 104 | { |
|---|
| 105 | |
|---|
| 106 | if (static_cast<int>(layer) >= _textureDepth) |
|---|
| 107 | { |
|---|
| 108 | |
|---|
| 109 | notify(WARN)<<"Warning: Texture2DArray::setImage(..) failed, the given layer number is bigger then the size of the texture array."<<std::endl; |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (_images[layer] == image) return; |
|---|
| 114 | |
|---|
| 115 | unsigned numImageRequireUpdateBefore = 0; |
|---|
| 116 | for (unsigned int i=0; i<getNumImages(); ++i) |
|---|
| 117 | { |
|---|
| 118 | if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateBefore; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | _images[layer] = image; |
|---|
| 123 | _modifiedCount[layer].setAllElementsTo(0); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | unsigned numImageRequireUpdateAfter = 0; |
|---|
| 127 | for (unsigned int i=0; i<getNumImages(); ++i) |
|---|
| 128 | { |
|---|
| 129 | if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateAfter; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if (numImageRequireUpdateBefore>0) |
|---|
| 133 | { |
|---|
| 134 | if (numImageRequireUpdateAfter==0) |
|---|
| 135 | { |
|---|
| 136 | setUpdateCallback(0); |
|---|
| 137 | setDataVariance(osg::Object::STATIC); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | else if (numImageRequireUpdateAfter>0) |
|---|
| 141 | { |
|---|
| 142 | setUpdateCallback(new Image::UpdateCallback()); |
|---|
| 143 | setDataVariance(osg::Object::DYNAMIC); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void Texture2DArray::setTextureSize(int width, int height, int depth) |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | _textureWidth = width; |
|---|
| 151 | _textureHeight = height; |
|---|
| 152 | setTextureDepth(depth); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void Texture2DArray::setTextureDepth(int depth) |
|---|
| 156 | { |
|---|
| 157 | |
|---|
| 158 | if (depth < _textureDepth) |
|---|
| 159 | { |
|---|
| 160 | _images.resize(depth); |
|---|
| 161 | _modifiedCount.resize(depth); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | if (depth > _textureDepth) |
|---|
| 166 | { |
|---|
| 167 | _images.resize(depth, ref_ptr<Image>(0)); |
|---|
| 168 | _modifiedCount.resize(depth, ImageModifiedCount()); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | _textureDepth = depth; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | Image* Texture2DArray::getImage(unsigned int layer) |
|---|
| 176 | { |
|---|
| 177 | return _images[layer].get(); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | const Image* Texture2DArray::getImage(unsigned int layer) const |
|---|
| 181 | { |
|---|
| 182 | return _images[layer].get(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | bool Texture2DArray::imagesValid() const |
|---|
| 186 | { |
|---|
| 187 | if (_textureDepth < 1) return false; |
|---|
| 188 | for (int n=0; n < _textureDepth; n++) |
|---|
| 189 | { |
|---|
| 190 | if (!_images[n].valid() || !_images[n]->data()) |
|---|
| 191 | return false; |
|---|
| 192 | } |
|---|
| 193 | return true; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void Texture2DArray::computeInternalFormat() const |
|---|
| 197 | { |
|---|
| 198 | if (imagesValid()) computeInternalFormatWithImage(*_images[0]); |
|---|
| 199 | else computeInternalFormatType(); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | void Texture2DArray::apply(State& state) const |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | const unsigned int contextID = state.getContextID(); |
|---|
| 208 | |
|---|
| 209 | Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); |
|---|
| 210 | ElapsedTime elapsedTime(&(tom->getApplyTime())); |
|---|
| 211 | tom->getNumberApplied()++; |
|---|
| 212 | |
|---|
| 213 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | if (!extensions->isTexture2DArraySupported() || !extensions->isTexture3DSupported()) |
|---|
| 217 | { |
|---|
| 218 | notify(WARN)<<"Warning: Texture2DArray::apply(..) failed, 2D texture arrays are not support by OpenGL driver."<<std::endl; |
|---|
| 219 | return; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 224 | |
|---|
| 225 | if (textureObject && _textureDepth>0) |
|---|
| 226 | { |
|---|
| 227 | const osg::Image* image = _images[0].get(); |
|---|
| 228 | if (image && getModifiedCount(0, contextID) != image->getModifiedCount()) |
|---|
| 229 | { |
|---|
| 230 | |
|---|
| 231 | computeInternalFormat(); |
|---|
| 232 | |
|---|
| 233 | GLsizei new_width, new_height, new_numMipmapLevels; |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | computeRequiredTextureDimensions(state, *image, new_width, new_height, new_numMipmapLevels); |
|---|
| 237 | |
|---|
| 238 | if (!textureObject->match(GL_TEXTURE_2D_ARRAY_EXT, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth)) |
|---|
| 239 | { |
|---|
| 240 | Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); |
|---|
| 241 | _textureObjectBuffer[contextID] = 0; |
|---|
| 242 | textureObject = 0; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | if (textureObject) |
|---|
| 249 | { |
|---|
| 250 | |
|---|
| 251 | textureObject->bind(); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | if (_subloadCallback.valid()) |
|---|
| 258 | { |
|---|
| 259 | _subloadCallback->subload(*this,state); |
|---|
| 260 | } |
|---|
| 261 | else |
|---|
| 262 | { |
|---|
| 263 | |
|---|
| 264 | for (GLsizei n=0; n < _textureDepth; n++) |
|---|
| 265 | { |
|---|
| 266 | osg::Image* image = _images[n].get(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | if (image && getModifiedCount(n,contextID) != image->getModifiedCount()) |
|---|
| 270 | { |
|---|
| 271 | applyTexImage2DArray_subload(state, image, _textureWidth, _textureHeight, n, _internalFormat, _numMipmapLevels); |
|---|
| 272 | getModifiedCount(n,contextID) = image->getModifiedCount(); |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | else if (_subloadCallback.valid()) |
|---|
| 281 | { |
|---|
| 282 | |
|---|
| 283 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_2D_ARRAY_EXT); |
|---|
| 284 | textureObject->bind(); |
|---|
| 285 | applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state); |
|---|
| 286 | _subloadCallback->load(*this,state); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | else if (imagesValid()) |
|---|
| 293 | { |
|---|
| 294 | |
|---|
| 295 | computeInternalFormat(); |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | computeRequiredTextureDimensions(state,*_images[0],_textureWidth, _textureHeight, _numMipmapLevels); |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | textureObject = generateTextureObject( |
|---|
| 302 | this, contextID,GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | textureObject->bind(); |
|---|
| 306 | applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, _internalFormat, |
|---|
| 310 | _textureWidth, _textureHeight, _textureDepth, |
|---|
| 311 | _borderWidth, |
|---|
| 312 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 313 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 314 | 0); |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | for (GLsizei n=0; n<_textureDepth; n++) |
|---|
| 318 | { |
|---|
| 319 | |
|---|
| 320 | osg::Image* image = _images[n].get(); |
|---|
| 321 | if (image) |
|---|
| 322 | { |
|---|
| 323 | |
|---|
| 324 | applyTexImage2DArray_subload(state, image, _textureWidth, _textureHeight, n, _internalFormat, _numMipmapLevels); |
|---|
| 325 | getModifiedCount(n,contextID) = image->getModifiedCount(); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 329 | |
|---|
| 330 | _textureObjectBuffer[contextID] = textureObject; |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded()) |
|---|
| 334 | { |
|---|
| 335 | Texture2DArray* non_const_this = const_cast<Texture2DArray*>(this); |
|---|
| 336 | for (int n=0; n<_textureDepth; n++) |
|---|
| 337 | { |
|---|
| 338 | if (_images[n].valid() && _images[n]->getDataVariance()==STATIC) |
|---|
| 339 | { |
|---|
| 340 | non_const_this->_images[n] = 0; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | else if ( (_textureWidth > 0) && (_textureHeight > 0) && (_textureDepth > 0) && (_internalFormat!=0) ) |
|---|
| 349 | { |
|---|
| 350 | |
|---|
| 351 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 352 | this, contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 353 | |
|---|
| 354 | textureObject->bind(); |
|---|
| 355 | applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state); |
|---|
| 356 | |
|---|
| 357 | extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, _internalFormat, |
|---|
| 358 | _textureWidth, _textureHeight, _textureDepth, |
|---|
| 359 | _borderWidth, |
|---|
| 360 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 361 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 362 | 0); |
|---|
| 363 | |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | else |
|---|
| 368 | { |
|---|
| 369 | glBindTexture( GL_TEXTURE_2D_ARRAY_EXT, 0 ); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID]) |
|---|
| 374 | { |
|---|
| 375 | generateMipmap(state); |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | void Texture2DArray::applyTexImage2DArray_subload(State& state, Image* image, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const |
|---|
| 380 | { |
|---|
| 381 | |
|---|
| 382 | if (!imagesValid()) |
|---|
| 383 | return; |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | const unsigned int contextID = state.getContextID(); |
|---|
| 388 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 389 | const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true); |
|---|
| 390 | GLenum target = GL_TEXTURE_2D_ARRAY_EXT; |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | computeInternalFormat(); |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | bool compressed_image = isCompressedInternalFormat((GLenum)image->getPixelFormat()); |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | if (indepth > extensions->maxLayerCount()) |
|---|
| 401 | { |
|---|
| 402 | |
|---|
| 403 | notify(WARN)<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) the given layer number exceeds the maximum number of supported layers."<<std::endl; |
|---|
| 404 | return; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | if( _resizeNonPowerOfTwoHint || !texExtensions->isNonPowerOfTwoTextureSupported(_min_filter) |
|---|
| 409 | || inwidth > extensions->max2DSize() |
|---|
| 410 | || inheight > extensions->max2DSize()) |
|---|
| 411 | image->ensureValidSizeForTexturing(extensions->max2DSize()); |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | if (image->s()!=inwidth || |
|---|
| 415 | image->t()!=inheight || |
|---|
| 416 | image->getInternalTextureFormat()!=inInternalFormat ) |
|---|
| 417 | { |
|---|
| 418 | notify(WARN)<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) given image do have wrong dimension or internal format."<<std::endl; |
|---|
| 419 | return; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking()); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | if( _min_filter == LINEAR || _min_filter == NEAREST ) |
|---|
| 426 | { |
|---|
| 427 | numMipmapLevels = 1; |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | if (!compressed_image) |
|---|
| 431 | { |
|---|
| 432 | extensions->glTexSubImage3D( target, 0, |
|---|
| 433 | 0, 0, indepth, |
|---|
| 434 | inwidth, inheight, 1, |
|---|
| 435 | (GLenum)image->getPixelFormat(), |
|---|
| 436 | (GLenum)image->getDataType(), |
|---|
| 437 | image->data() ); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | else if (extensions->isCompressedTexImage3DSupported()) |
|---|
| 442 | { |
|---|
| 443 | |
|---|
| 444 | numMipmapLevels = 1; |
|---|
| 445 | |
|---|
| 446 | GLint blockSize, size; |
|---|
| 447 | getCompressedSize(_internalFormat, inwidth, inheight, 1, blockSize,size); |
|---|
| 448 | |
|---|
| 449 | extensions->glCompressedTexSubImage3D(target, 0, |
|---|
| 450 | 0, 0, indepth, |
|---|
| 451 | inwidth, inheight, 1, |
|---|
| 452 | (GLenum)image->getPixelFormat(), |
|---|
| 453 | size, |
|---|
| 454 | image->data()); |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | }else |
|---|
| 459 | { |
|---|
| 460 | |
|---|
| 461 | if(!image->isMipmap()) |
|---|
| 462 | { |
|---|
| 463 | notify(WARN)<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) automagic mipmap generation is currently not implemented. Check texture's min/mag filters."<<std::endl; |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | }else |
|---|
| 467 | { |
|---|
| 468 | numMipmapLevels = image->getNumMipmapLevels(); |
|---|
| 469 | |
|---|
| 470 | int width = image->s(); |
|---|
| 471 | int height = image->t(); |
|---|
| 472 | |
|---|
| 473 | for( GLsizei k = 0 ; k < numMipmapLevels && (width || height ) ;k++) |
|---|
| 474 | { |
|---|
| 475 | |
|---|
| 476 | if (width == 0) |
|---|
| 477 | width = 1; |
|---|
| 478 | if (height == 0) |
|---|
| 479 | height = 1; |
|---|
| 480 | |
|---|
| 481 | extensions->glTexSubImage3D( target, k, 0, 0, indepth, |
|---|
| 482 | width, height, 1, |
|---|
| 483 | (GLenum)image->getPixelFormat(), |
|---|
| 484 | (GLenum)image->getDataType(), |
|---|
| 485 | image->getMipmapData(k)); |
|---|
| 486 | |
|---|
| 487 | width >>= 1; |
|---|
| 488 | height >>= 1; |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | void Texture2DArray::copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height ) |
|---|
| 497 | { |
|---|
| 498 | const unsigned int contextID = state.getContextID(); |
|---|
| 499 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | if (textureObject != 0) |
|---|
| 506 | { |
|---|
| 507 | textureObject->bind(); |
|---|
| 508 | |
|---|
| 509 | applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state); |
|---|
| 510 | extensions->glCopyTexSubImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, xoffset,yoffset,zoffset, x, y, width, height); |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 514 | |
|---|
| 515 | } |
|---|
| 516 | else |
|---|
| 517 | { |
|---|
| 518 | notify(WARN)<<"Warning: Texture2DArray::copyTexSubImage2DArray(..) failed, cannot not copy to a non existant texture."<<std::endl; |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | void Texture2DArray::allocateMipmap(State& state) const |
|---|
| 523 | { |
|---|
| 524 | const unsigned int contextID = state.getContextID(); |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 528 | |
|---|
| 529 | if (textureObject && _textureWidth != 0 && _textureHeight != 0 && _textureDepth != 0) |
|---|
| 530 | { |
|---|
| 531 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | textureObject->bind(); |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | int width = _textureWidth; |
|---|
| 539 | int height = _textureHeight; |
|---|
| 540 | int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | width >>= 1; |
|---|
| 544 | height >>= 1; |
|---|
| 545 | |
|---|
| 546 | for( GLsizei k = 1; k < numMipmapLevels && (width || height); k++) |
|---|
| 547 | { |
|---|
| 548 | if (width == 0) |
|---|
| 549 | width = 1; |
|---|
| 550 | if (height == 0) |
|---|
| 551 | height = 1; |
|---|
| 552 | |
|---|
| 553 | extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, k, _internalFormat, |
|---|
| 554 | width, height, _textureDepth, _borderWidth, |
|---|
| 555 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 556 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL); |
|---|
| 557 | |
|---|
| 558 | width >>= 1; |
|---|
| 559 | height >>= 1; |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 564 | } |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | typedef buffered_value< ref_ptr<Texture2DArray::Extensions> > BufferedExtensions; |
|---|
| 568 | static BufferedExtensions s_extensions; |
|---|
| 569 | |
|---|
| 570 | Texture2DArray::Extensions* Texture2DArray::getExtensions(unsigned int contextID,bool createIfNotInitalized) |
|---|
| 571 | { |
|---|
| 572 | if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); |
|---|
| 573 | return s_extensions[contextID].get(); |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | void Texture2DArray::setExtensions(unsigned int contextID,Extensions* extensions) |
|---|
| 577 | { |
|---|
| 578 | s_extensions[contextID] = extensions; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | Texture2DArray::Extensions::Extensions(unsigned int contextID) |
|---|
| 582 | { |
|---|
| 583 | setupGLExtensions(contextID); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | Texture2DArray::Extensions::Extensions(const Extensions& rhs): |
|---|
| 587 | Referenced() |
|---|
| 588 | { |
|---|
| 589 | _isTexture3DSupported = rhs._isTexture3DSupported; |
|---|
| 590 | _isTexture2DArraySupported = rhs._isTexture2DArraySupported; |
|---|
| 591 | |
|---|
| 592 | _max2DSize = rhs._max2DSize; |
|---|
| 593 | _maxLayerCount = rhs._maxLayerCount; |
|---|
| 594 | |
|---|
| 595 | _glTexImage3D = rhs._glTexImage3D; |
|---|
| 596 | _glTexSubImage3D = rhs._glTexSubImage3D; |
|---|
| 597 | _glCopyTexSubImage3D = rhs._glCopyTexSubImage3D; |
|---|
| 598 | _glCompressedTexImage3D = rhs._glCompressedTexImage3D; |
|---|
| 599 | _glCompressedTexSubImage3D = rhs._glCompressedTexSubImage3D;; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | void Texture2DArray::Extensions::lowestCommonDenominator(const Extensions& rhs) |
|---|
| 603 | { |
|---|
| 604 | if (!rhs._isTexture3DSupported) _isTexture3DSupported = false; |
|---|
| 605 | if (!rhs._isTexture2DArraySupported) _isTexture2DArraySupported = false; |
|---|
| 606 | if (rhs._max2DSize<_max2DSize) _max2DSize = rhs._max2DSize; |
|---|
| 607 | if (rhs._maxLayerCount<_maxLayerCount) _maxLayerCount = rhs._maxLayerCount; |
|---|
| 608 | |
|---|
| 609 | if (!rhs._glTexImage3D) _glTexImage3D = 0; |
|---|
| 610 | if (!rhs._glTexSubImage3D) _glTexSubImage3D = 0; |
|---|
| 611 | if (!rhs._glCompressedTexImage3D) _glTexImage3D = 0; |
|---|
| 612 | if (!rhs._glCompressedTexSubImage3D) _glTexSubImage3D = 0; |
|---|
| 613 | if (!rhs._glCopyTexSubImage3D) _glCopyTexSubImage3D = 0; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | void Texture2DArray::Extensions::setupGLExtensions(unsigned int contextID) |
|---|
| 617 | { |
|---|
| 618 | _isTexture3DSupported = OSG_GL3_FEATURES || isGLExtensionSupported(contextID,"GL_EXT_texture3D"); |
|---|
| 619 | _isTexture2DArraySupported = OSG_GL3_FEATURES || isGLExtensionSupported(contextID,"GL_EXT_texture_array"); |
|---|
| 620 | |
|---|
| 621 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_max2DSize); |
|---|
| 622 | glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS_EXT, &_maxLayerCount); |
|---|
| 623 | |
|---|
| 624 | setGLExtensionFuncPtr(_glTexImage3D, "glTexImage3D","glTexImage3DEXT"); |
|---|
| 625 | setGLExtensionFuncPtr(_glTexSubImage3D, "glTexSubImage3D","glTexSubImage3DEXT"); |
|---|
| 626 | setGLExtensionFuncPtr(_glCompressedTexImage3D, "glCompressedTexImage3D","glCompressedTexImage3DARB"); |
|---|
| 627 | setGLExtensionFuncPtr(_glCompressedTexSubImage3D, "glCompressedTexSubImage3D","glCompressedTexSubImage3DARB"); |
|---|
| 628 | setGLExtensionFuncPtr(_glCopyTexSubImage3D, "glCopyTexSubImage3D","glCopyTexSubImage3DEXT"); |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | void Texture2DArray::Extensions::glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) const |
|---|
| 632 | { |
|---|
| 633 | if (_glTexImage3D) |
|---|
| 634 | { |
|---|
| 635 | _glTexImage3D( target, level, internalFormat, width, height, depth, border, format, type, pixels); |
|---|
| 636 | } |
|---|
| 637 | else |
|---|
| 638 | { |
|---|
| 639 | notify(WARN)<<"Error: glTexImage3D not supported by OpenGL driver"<<std::endl; |
|---|
| 640 | } |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | void Texture2DArray::Extensions::glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) const |
|---|
| 644 | { |
|---|
| 645 | if (_glTexSubImage3D) |
|---|
| 646 | { |
|---|
| 647 | _glTexSubImage3D( target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); |
|---|
| 648 | } |
|---|
| 649 | else |
|---|
| 650 | { |
|---|
| 651 | notify(WARN)<<"Error: glTexSubImage3D not supported by OpenGL driver"<<std::endl; |
|---|
| 652 | } |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | void Texture2DArray::Extensions::glCompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *data) const |
|---|
| 656 | { |
|---|
| 657 | if (_glCompressedTexImage3D) |
|---|
| 658 | { |
|---|
| 659 | _glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, data); |
|---|
| 660 | } |
|---|
| 661 | else |
|---|
| 662 | { |
|---|
| 663 | notify(WARN)<<"Error: glCompressedTexImage3D not supported by OpenGL driver"<<std::endl; |
|---|
| 664 | } |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | void Texture2DArray::Extensions::glCompressedTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data ) const |
|---|
| 668 | { |
|---|
| 669 | if (_glCompressedTexSubImage3D) |
|---|
| 670 | { |
|---|
| 671 | _glCompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data); |
|---|
| 672 | } |
|---|
| 673 | else |
|---|
| 674 | { |
|---|
| 675 | notify(WARN)<<"Error: glCompressedTexImage2D not supported by OpenGL driver"<<std::endl; |
|---|
| 676 | } |
|---|
| 677 | } |
|---|
| 678 | |
|---|
| 679 | void Texture2DArray::Extensions::glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) const |
|---|
| 680 | { |
|---|
| 681 | if (_glCopyTexSubImage3D) |
|---|
| 682 | { |
|---|
| 683 | _glCopyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height); |
|---|
| 684 | } |
|---|
| 685 | else |
|---|
| 686 | { |
|---|
| 687 | notify(WARN)<<"Error: glCopyTexSubImage3D not supported by OpenGL driver"<<std::endl; |
|---|
| 688 | } |
|---|
| 689 | } |
|---|