| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/GLExtensions> |
|---|
| 15 | #include <osg/Texture2D> |
|---|
| 16 | #include <osg/State> |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | |
|---|
| 19 | using namespace osg; |
|---|
| 20 | |
|---|
| 21 | Texture2D::Texture2D(): |
|---|
| 22 | _textureWidth(0), |
|---|
| 23 | _textureHeight(0), |
|---|
| 24 | _numMipmapLevels(0) |
|---|
| 25 | { |
|---|
| 26 | setUseHardwareMipMapGeneration(true); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | Texture2D::Texture2D(Image* image): |
|---|
| 30 | _textureWidth(0), |
|---|
| 31 | _textureHeight(0), |
|---|
| 32 | _numMipmapLevels(0) |
|---|
| 33 | { |
|---|
| 34 | setUseHardwareMipMapGeneration(true); |
|---|
| 35 | setImage(image); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | Texture2D::Texture2D(const Texture2D& text,const CopyOp& copyop): |
|---|
| 39 | Texture(text,copyop), |
|---|
| 40 | _image(copyop(text._image.get())), |
|---|
| 41 | _textureWidth(text._textureWidth), |
|---|
| 42 | _textureHeight(text._textureHeight), |
|---|
| 43 | _numMipmapLevels(text._numMipmapLevels), |
|---|
| 44 | _subloadCallback(text._subloadCallback) |
|---|
| 45 | { |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | Texture2D::~Texture2D() |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | int Texture2D::compare(const StateAttribute& sa) const |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | COMPARE_StateAttribute_Types(Texture2D,sa) |
|---|
| 57 | |
|---|
| 58 | if (_image!=rhs._image) |
|---|
| 59 | { |
|---|
| 60 | if (_image.valid()) |
|---|
| 61 | { |
|---|
| 62 | if (rhs._image.valid()) |
|---|
| 63 | { |
|---|
| 64 | int result = _image->compare(*rhs._image); |
|---|
| 65 | if (result!=0) return result; |
|---|
| 66 | } |
|---|
| 67 | else |
|---|
| 68 | { |
|---|
| 69 | return 1; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | else if (rhs._image.valid()) |
|---|
| 73 | { |
|---|
| 74 | return -1; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if (!_image && !rhs._image) |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | int result = compareTextureObjects(rhs); |
|---|
| 86 | if (result!=0) return result; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | int result = compareTexture(rhs); |
|---|
| 90 | if (result!=0) return result; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | #if 1 |
|---|
| 94 | if (_textureWidth != 0 && rhs._textureWidth != 0) |
|---|
| 95 | { |
|---|
| 96 | COMPARE_StateAttribute_Parameter(_textureWidth) |
|---|
| 97 | } |
|---|
| 98 | if (_textureHeight != 0 && rhs._textureHeight != 0) |
|---|
| 99 | { |
|---|
| 100 | COMPARE_StateAttribute_Parameter(_textureHeight) |
|---|
| 101 | } |
|---|
| 102 | #endif |
|---|
| 103 | COMPARE_StateAttribute_Parameter(_subloadCallback) |
|---|
| 104 | |
|---|
| 105 | return 0; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void Texture2D::setImage(Image* image) |
|---|
| 109 | { |
|---|
| 110 | if (_image == image) return; |
|---|
| 111 | |
|---|
| 112 | if (_image.valid() && _image->requiresUpdateCall()) |
|---|
| 113 | { |
|---|
| 114 | setUpdateCallback(0); |
|---|
| 115 | setDataVariance(osg::Object::STATIC); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | _image = image; |
|---|
| 119 | _modifiedCount.setAllElementsTo(0); |
|---|
| 120 | |
|---|
| 121 | if (_image.valid() && _image->requiresUpdateCall()) |
|---|
| 122 | { |
|---|
| 123 | setUpdateCallback(new Image::UpdateCallback()); |
|---|
| 124 | setDataVariance(osg::Object::DYNAMIC); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | bool Texture2D::textureObjectValid(State& state) const |
|---|
| 129 | { |
|---|
| 130 | TextureObject* textureObject = getTextureObject(state.getContextID()); |
|---|
| 131 | if (!textureObject) return false; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | if (!_image) return true; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | computeInternalFormat(); |
|---|
| 138 | |
|---|
| 139 | GLsizei new_width, new_height, new_numMipmapLevels; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | computeRequiredTextureDimensions(state, *_image, new_width, new_height, new_numMipmapLevels); |
|---|
| 143 | |
|---|
| 144 | return textureObject->match(GL_TEXTURE_2D, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | void Texture2D::apply(State& state) const |
|---|
| 149 | { |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | const unsigned int contextID = state.getContextID(); |
|---|
| 156 | |
|---|
| 157 | Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); |
|---|
| 158 | ElapsedTime elapsedTime(&(tom->getApplyTime())); |
|---|
| 159 | tom->getNumberApplied()++; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 163 | if (textureObject) |
|---|
| 164 | { |
|---|
| 165 | bool textureObjectInvalidated = false; |
|---|
| 166 | if (_subloadCallback.valid()) |
|---|
| 167 | { |
|---|
| 168 | textureObjectInvalidated = !_subloadCallback->textureObjectValid(*this, state); |
|---|
| 169 | } |
|---|
| 170 | else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 171 | { |
|---|
| 172 | textureObjectInvalidated = !textureObjectValid(state); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | if (textureObjectInvalidated) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); |
|---|
| 179 | _textureObjectBuffer[contextID] = 0; |
|---|
| 180 | textureObject = 0; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if (textureObject) |
|---|
| 185 | { |
|---|
| 186 | textureObject->bind(); |
|---|
| 187 | |
|---|
| 188 | if (getTextureParameterDirty(state.getContextID())) |
|---|
| 189 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 190 | |
|---|
| 191 | if (_subloadCallback.valid()) |
|---|
| 192 | { |
|---|
| 193 | _subloadCallback->subload(*this,state); |
|---|
| 194 | } |
|---|
| 195 | else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 196 | { |
|---|
| 197 | applyTexImage2D_subload(state,GL_TEXTURE_2D,_image.get(), |
|---|
| 198 | _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | else if (_readPBuffer.valid()) |
|---|
| 205 | { |
|---|
| 206 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | } |
|---|
| 210 | else if (_subloadCallback.valid()) |
|---|
| 211 | { |
|---|
| 212 | _textureObjectBuffer[contextID] = textureObject = _subloadCallback->generateTextureObject(*this, state); |
|---|
| 213 | |
|---|
| 214 | textureObject->bind(); |
|---|
| 215 | |
|---|
| 216 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 217 | |
|---|
| 218 | _subloadCallback->load(*this,state); |
|---|
| 219 | |
|---|
| 220 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | if (_image.valid()) getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 230 | } |
|---|
| 231 | else if (_image.valid() && _image->data()) |
|---|
| 232 | { |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | osg::ref_ptr<osg::Image> image = _image; |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | computeInternalFormat(); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | computeRequiredTextureDimensions(state,*image,_textureWidth, _textureHeight, _numMipmapLevels); |
|---|
| 242 | |
|---|
| 243 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 244 | this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 245 | |
|---|
| 246 | textureObject->bind(); |
|---|
| 247 | |
|---|
| 248 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 249 | |
|---|
| 250 | if (textureObject->isAllocated() && image->supportsTextureSubloading()) |
|---|
| 251 | { |
|---|
| 252 | |
|---|
| 253 | applyTexImage2D_subload(state,GL_TEXTURE_2D,image.get(), |
|---|
| 254 | _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels); |
|---|
| 255 | } |
|---|
| 256 | else |
|---|
| 257 | { |
|---|
| 258 | |
|---|
| 259 | applyTexImage2D_load(state,GL_TEXTURE_2D,image.get(), |
|---|
| 260 | _textureWidth, _textureHeight, _numMipmapLevels); |
|---|
| 261 | |
|---|
| 262 | textureObject->setAllocated(true); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | getModifiedCount(contextID) = image->getModifiedCount(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | if (isSafeToUnrefImageData(state) && image->getDataVariance()==STATIC) |
|---|
| 270 | { |
|---|
| 271 | Texture2D* non_const_this = const_cast<Texture2D*>(this); |
|---|
| 272 | non_const_this->_image = NULL; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | } |
|---|
| 282 | else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) |
|---|
| 283 | { |
|---|
| 284 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 285 | this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 286 | |
|---|
| 287 | textureObject->bind(); |
|---|
| 288 | |
|---|
| 289 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | glTexImage2D( GL_TEXTURE_2D, 0, _internalFormat, |
|---|
| 293 | _textureWidth, _textureHeight, _borderWidth, |
|---|
| 294 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 295 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 296 | 0); |
|---|
| 297 | |
|---|
| 298 | if (_readPBuffer.valid()) |
|---|
| 299 | { |
|---|
| 300 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | } |
|---|
| 304 | else |
|---|
| 305 | { |
|---|
| 306 | glBindTexture( GL_TEXTURE_2D, 0 ); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID]) |
|---|
| 311 | { |
|---|
| 312 | generateMipmap(state); |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | void Texture2D::computeInternalFormat() const |
|---|
| 317 | { |
|---|
| 318 | if (_image.valid()) computeInternalFormatWithImage(*_image); |
|---|
| 319 | else computeInternalFormatType(); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | void Texture2D::copyTexImage2D(State& state, int x, int y, int width, int height ) |
|---|
| 323 | { |
|---|
| 324 | const unsigned int contextID = state.getContextID(); |
|---|
| 325 | |
|---|
| 326 | if (_internalFormat==0) _internalFormat=GL_RGBA; |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 330 | |
|---|
| 331 | if (textureObject) |
|---|
| 332 | { |
|---|
| 333 | if (width==(int)_textureWidth && height==(int)_textureHeight) |
|---|
| 334 | { |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | copyTexSubImage2D(state,0 ,0, x, y, width, height); |
|---|
| 340 | return; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | dirtyTextureObject(); |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | _image = NULL; |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST); |
|---|
| 360 | bool hardwareMipMapOn = false; |
|---|
| 361 | if (needHardwareMipMap) |
|---|
| 362 | { |
|---|
| 363 | hardwareMipMapOn = isHardwareMipmapGenerationEnabled(state); |
|---|
| 364 | |
|---|
| 365 | if (!hardwareMipMapOn) |
|---|
| 366 | { |
|---|
| 367 | |
|---|
| 368 | OSG_NOTICE<<"Warning: Texture2D::copyTexImage2D(,,,,) switch off mip mapping as hardware support not available."<<std::endl; |
|---|
| 369 | _min_filter = LINEAR; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | _textureWidth = width; |
|---|
| 374 | _textureHeight = height; |
|---|
| 375 | |
|---|
| 376 | _numMipmapLevels = 1; |
|---|
| 377 | if (needHardwareMipMap) |
|---|
| 378 | { |
|---|
| 379 | for(int s=1; s<width || s<height; s <<= 1, ++_numMipmapLevels) {} |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); |
|---|
| 383 | |
|---|
| 384 | textureObject->bind(); |
|---|
| 385 | |
|---|
| 386 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | GenerateMipmapMode mipmapResult = mipmapBeforeTexImage(state, hardwareMipMapOn); |
|---|
| 390 | |
|---|
| 391 | glCopyTexImage2D( GL_TEXTURE_2D, 0, _internalFormat, x, y, width, height, 0 ); |
|---|
| 392 | |
|---|
| 393 | mipmapAfterTexImage(state, mipmapResult); |
|---|
| 394 | |
|---|
| 395 | textureObject->setAllocated(true); |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | void Texture2D::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height ) |
|---|
| 402 | { |
|---|
| 403 | const unsigned int contextID = state.getContextID(); |
|---|
| 404 | |
|---|
| 405 | if (_internalFormat==0) _internalFormat=GL_RGBA; |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 409 | |
|---|
| 410 | if (textureObject) |
|---|
| 411 | { |
|---|
| 412 | |
|---|
| 413 | textureObject->bind(); |
|---|
| 414 | |
|---|
| 415 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 416 | |
|---|
| 417 | bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST); |
|---|
| 418 | bool hardwareMipMapOn = false; |
|---|
| 419 | if (needHardwareMipMap) |
|---|
| 420 | { |
|---|
| 421 | hardwareMipMapOn = isHardwareMipmapGenerationEnabled(state); |
|---|
| 422 | |
|---|
| 423 | if (!hardwareMipMapOn) |
|---|
| 424 | { |
|---|
| 425 | |
|---|
| 426 | OSG_NOTICE<<"Warning: Texture2D::copyTexImage2D(,,,,) switch off mip mapping as hardware support not available."<<std::endl; |
|---|
| 427 | _min_filter = LINEAR; |
|---|
| 428 | } |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | GenerateMipmapMode mipmapResult = mipmapBeforeTexImage(state, hardwareMipMapOn); |
|---|
| 432 | |
|---|
| 433 | glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset, yoffset, x, y, width, height); |
|---|
| 434 | |
|---|
| 435 | mipmapAfterTexImage(state, mipmapResult); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 439 | |
|---|
| 440 | } |
|---|
| 441 | else |
|---|
| 442 | { |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | copyTexImage2D(state,x,y,width,height); |
|---|
| 446 | } |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | void Texture2D::allocateMipmap(State& state) const |
|---|
| 450 | { |
|---|
| 451 | const unsigned int contextID = state.getContextID(); |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 455 | |
|---|
| 456 | if (textureObject && _textureWidth != 0 && _textureHeight != 0) |
|---|
| 457 | { |
|---|
| 458 | |
|---|
| 459 | textureObject->bind(); |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | int width = _textureWidth; |
|---|
| 463 | int height = _textureHeight; |
|---|
| 464 | int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | width >>= 1; |
|---|
| 468 | height >>= 1; |
|---|
| 469 | |
|---|
| 470 | for( GLsizei k = 1; k < numMipmapLevels && (width || height); k++) |
|---|
| 471 | { |
|---|
| 472 | if (width == 0) |
|---|
| 473 | width = 1; |
|---|
| 474 | if (height == 0) |
|---|
| 475 | height = 1; |
|---|
| 476 | |
|---|
| 477 | glTexImage2D( GL_TEXTURE_2D, k, _internalFormat, |
|---|
| 478 | width, height, _borderWidth, |
|---|
| 479 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 480 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL); |
|---|
| 481 | |
|---|
| 482 | width >>= 1; |
|---|
| 483 | height >>= 1; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 488 | } |
|---|
| 489 | } |
|---|