| 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 | |
|---|
| 129 | void Texture2D::apply(State& state) const |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | const unsigned int contextID = state.getContextID(); |
|---|
| 137 | |
|---|
| 138 | Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); |
|---|
| 139 | ElapsedTime elapsedTime(&(tom->getApplyTime())); |
|---|
| 140 | tom->getNumberApplied()++; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 144 | |
|---|
| 145 | if (textureObject) |
|---|
| 146 | { |
|---|
| 147 | if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | computeInternalFormat(); |
|---|
| 151 | |
|---|
| 152 | GLsizei new_width, new_height, new_numMipmapLevels; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | computeRequiredTextureDimensions(state, *_image, new_width, new_height, new_numMipmapLevels); |
|---|
| 156 | |
|---|
| 157 | if (!textureObject->match(GL_TEXTURE_2D, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth)) |
|---|
| 158 | { |
|---|
| 159 | Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); |
|---|
| 160 | _textureObjectBuffer[contextID] = 0; |
|---|
| 161 | textureObject = 0; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (textureObject) |
|---|
| 167 | { |
|---|
| 168 | textureObject->bind(); |
|---|
| 169 | |
|---|
| 170 | if (getTextureParameterDirty(state.getContextID())) |
|---|
| 171 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 172 | |
|---|
| 173 | if (_subloadCallback.valid()) |
|---|
| 174 | { |
|---|
| 175 | _subloadCallback->subload(*this,state); |
|---|
| 176 | } |
|---|
| 177 | else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 178 | { |
|---|
| 179 | applyTexImage2D_subload(state,GL_TEXTURE_2D,_image.get(), |
|---|
| 180 | _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 184 | |
|---|
| 185 | } |
|---|
| 186 | else if (_readPBuffer.valid()) |
|---|
| 187 | { |
|---|
| 188 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | } |
|---|
| 192 | else if (_subloadCallback.valid()) |
|---|
| 193 | { |
|---|
| 194 | |
|---|
| 195 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_2D); |
|---|
| 196 | |
|---|
| 197 | textureObject->bind(); |
|---|
| 198 | |
|---|
| 199 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 200 | |
|---|
| 201 | _subloadCallback->load(*this,state); |
|---|
| 202 | |
|---|
| 203 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | if (_image.valid()) getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 213 | } |
|---|
| 214 | else if (_image.valid() && _image->data()) |
|---|
| 215 | { |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | osg::ref_ptr<osg::Image> image = _image; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | computeInternalFormat(); |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | computeRequiredTextureDimensions(state,*image,_textureWidth, _textureHeight, _numMipmapLevels); |
|---|
| 225 | |
|---|
| 226 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 227 | this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 228 | |
|---|
| 229 | textureObject->bind(); |
|---|
| 230 | |
|---|
| 231 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 232 | |
|---|
| 233 | if (textureObject->isAllocated()) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | applyTexImage2D_subload(state,GL_TEXTURE_2D,image.get(), |
|---|
| 237 | _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels); |
|---|
| 238 | } |
|---|
| 239 | else |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | applyTexImage2D_load(state,GL_TEXTURE_2D,image.get(), |
|---|
| 243 | _textureWidth, _textureHeight, _numMipmapLevels); |
|---|
| 244 | |
|---|
| 245 | textureObject->setAllocated(true); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | getModifiedCount(contextID) = image->getModifiedCount(); |
|---|
| 250 | |
|---|
| 251 | if (state.getMaxTexturePoolSize()==0 && |
|---|
| 252 | _unrefImageDataAfterApply && |
|---|
| 253 | areAllTextureObjectsLoaded() && |
|---|
| 254 | image->getDataVariance()==STATIC) |
|---|
| 255 | { |
|---|
| 256 | Texture2D* non_const_this = const_cast<Texture2D*>(this); |
|---|
| 257 | non_const_this->_image = 0; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) |
|---|
| 268 | { |
|---|
| 269 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 270 | this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); |
|---|
| 271 | |
|---|
| 272 | textureObject->bind(); |
|---|
| 273 | |
|---|
| 274 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | glTexImage2D( GL_TEXTURE_2D, 0, _internalFormat, |
|---|
| 278 | _textureWidth, _textureHeight, _borderWidth, |
|---|
| 279 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 280 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 281 | 0); |
|---|
| 282 | |
|---|
| 283 | if (_readPBuffer.valid()) |
|---|
| 284 | { |
|---|
| 285 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | else |
|---|
| 290 | { |
|---|
| 291 | glBindTexture( GL_TEXTURE_2D, 0 ); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID]) |
|---|
| 296 | { |
|---|
| 297 | generateMipmap(state); |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | void Texture2D::computeInternalFormat() const |
|---|
| 302 | { |
|---|
| 303 | if (_image.valid()) computeInternalFormatWithImage(*_image); |
|---|
| 304 | else computeInternalFormatType(); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | void Texture2D::copyTexImage2D(State& state, int x, int y, int width, int height ) |
|---|
| 308 | { |
|---|
| 309 | const unsigned int contextID = state.getContextID(); |
|---|
| 310 | |
|---|
| 311 | if (_internalFormat==0) _internalFormat=GL_RGBA; |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 315 | |
|---|
| 316 | if (textureObject) |
|---|
| 317 | { |
|---|
| 318 | if (width==(int)_textureWidth && height==(int)_textureHeight) |
|---|
| 319 | { |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | copyTexSubImage2D(state,0 ,0, x, y, width, height); |
|---|
| 325 | return; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | dirtyTextureObject(); |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | _image = NULL; |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_2D); |
|---|
| 344 | |
|---|
| 345 | textureObject->bind(); |
|---|
| 346 | |
|---|
| 347 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST); |
|---|
| 351 | bool hardwareMipMapOn = false; |
|---|
| 352 | if (needHardwareMipMap) |
|---|
| 353 | { |
|---|
| 354 | hardwareMipMapOn = isHardwareMipmapGenerationEnabled(state); |
|---|
| 355 | |
|---|
| 356 | if (!hardwareMipMapOn) |
|---|
| 357 | { |
|---|
| 358 | |
|---|
| 359 | notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch off mip mapping as hardware support not available."<<std::endl; |
|---|
| 360 | _min_filter = LINEAR; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | GenerateMipmapMode mipmapResult = mipmapBeforeTexImage(state, hardwareMipMapOn); |
|---|
| 365 | |
|---|
| 366 | glCopyTexImage2D( GL_TEXTURE_2D, 0, _internalFormat, x, y, width, height, 0 ); |
|---|
| 367 | |
|---|
| 368 | mipmapAfterTexImage(state, mipmapResult); |
|---|
| 369 | |
|---|
| 370 | _textureWidth = width; |
|---|
| 371 | _textureHeight = height; |
|---|
| 372 | |
|---|
| 373 | _numMipmapLevels = 1; |
|---|
| 374 | if (needHardwareMipMap) |
|---|
| 375 | { |
|---|
| 376 | for(int s=1; s<width || s<height; s <<= 1, ++_numMipmapLevels) {} |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | void Texture2D::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height ) |
|---|
| 386 | { |
|---|
| 387 | const unsigned int contextID = state.getContextID(); |
|---|
| 388 | |
|---|
| 389 | if (_internalFormat==0) _internalFormat=GL_RGBA; |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 393 | |
|---|
| 394 | if (textureObject) |
|---|
| 395 | { |
|---|
| 396 | |
|---|
| 397 | textureObject->bind(); |
|---|
| 398 | |
|---|
| 399 | applyTexParameters(GL_TEXTURE_2D,state); |
|---|
| 400 | |
|---|
| 401 | bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST); |
|---|
| 402 | bool hardwareMipMapOn = false; |
|---|
| 403 | if (needHardwareMipMap) |
|---|
| 404 | { |
|---|
| 405 | hardwareMipMapOn = isHardwareMipmapGenerationEnabled(state); |
|---|
| 406 | |
|---|
| 407 | if (!hardwareMipMapOn) |
|---|
| 408 | { |
|---|
| 409 | |
|---|
| 410 | notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch off mip mapping as hardware support not available."<<std::endl; |
|---|
| 411 | _min_filter = LINEAR; |
|---|
| 412 | } |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | GenerateMipmapMode mipmapResult = mipmapBeforeTexImage(state, hardwareMipMapOn); |
|---|
| 416 | |
|---|
| 417 | glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset, yoffset, x, y, width, height); |
|---|
| 418 | |
|---|
| 419 | mipmapAfterTexImage(state, mipmapResult); |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 423 | |
|---|
| 424 | } |
|---|
| 425 | else |
|---|
| 426 | { |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | copyTexImage2D(state,x,y,width,height); |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | void Texture2D::allocateMipmap(State& state) const |
|---|
| 434 | { |
|---|
| 435 | const unsigned int contextID = state.getContextID(); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 439 | |
|---|
| 440 | if (textureObject && _textureWidth != 0 && _textureHeight != 0) |
|---|
| 441 | { |
|---|
| 442 | |
|---|
| 443 | textureObject->bind(); |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | int width = _textureWidth; |
|---|
| 447 | int height = _textureHeight; |
|---|
| 448 | int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | width >>= 1; |
|---|
| 452 | height >>= 1; |
|---|
| 453 | |
|---|
| 454 | for( GLsizei k = 1; k < numMipmapLevels && (width || height); k++) |
|---|
| 455 | { |
|---|
| 456 | if (width == 0) |
|---|
| 457 | width = 1; |
|---|
| 458 | if (height == 0) |
|---|
| 459 | height = 1; |
|---|
| 460 | |
|---|
| 461 | glTexImage2D( GL_TEXTURE_2D, k, _internalFormat, |
|---|
| 462 | width, height, _borderWidth, |
|---|
| 463 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 464 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL); |
|---|
| 465 | |
|---|
| 466 | width >>= 1; |
|---|
| 467 | height >>= 1; |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|