gfxGLTextureManager.cpp
Engine/source/gfx/gl/gfxGLTextureManager.cpp
Public Functions
Detailed Description
Public Functions
_textureUpload(const S32 width, const S32 height, const S32 bytesPerPixel, const GFXGLTextureObject * texture, const GFXFormat fmt, const U8 * data, const S32 mip, Swizzle< U8, 4 > * pSwizzle)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "gfx/gl/gfxGLTextureManager.h" 26#include "gfx/gl/gfxGLEnumTranslate.h" 27#include "gfx/gfxCardProfile.h" 28#include "core/util/safeDelete.h" 29#include "gfx/gl/gfxGLUtils.h" 30 31#include <squish.h> 32 33//----------------------------------------------------------------------------- 34// Constructor 35//----------------------------------------------------------------------------- 36GFXGLTextureManager::GFXGLTextureManager() 37{ 38} 39 40//----------------------------------------------------------------------------- 41// Destructor 42//----------------------------------------------------------------------------- 43GFXGLTextureManager::~GFXGLTextureManager() 44{ 45 SAFE_DELETE_ARRAY( mHashTable ); 46} 47 48//----------------------------------------------------------------------------- 49// createTexture 50//----------------------------------------------------------------------------- 51GFXTextureObject *GFXGLTextureManager::_createTextureObject( U32 height, 52 U32 width, 53 U32 depth, 54 GFXFormat format, 55 GFXTextureProfile *profile, 56 U32 numMipLevels, 57 bool forceMips, 58 S32 antialiasLevel, 59 GFXTextureObject *inTex ) 60{ 61 AssertFatal(format >= 0 && format < GFXFormat_COUNT, "GFXGLTextureManager::_createTexture - invalid format!"); 62 63 GFXGLTextureObject *retTex; 64 if ( inTex ) 65 { 66 AssertFatal( dynamic_cast<GFXGLTextureObject*>( inTex ), "GFXGLTextureManager::_createTexture() - Bad inTex type!" ); 67 retTex = static_cast<GFXGLTextureObject*>( inTex ); 68 retTex->release(); 69 retTex->reInit(); 70 } 71 else 72 { 73 retTex = new GFXGLTextureObject( GFX, profile ); 74 retTex->registerResourceWithDevice( GFX ); 75 } 76 77 innerCreateTexture(retTex, height, width, depth, format, profile, numMipLevels, forceMips); 78 79 return retTex; 80} 81 82//----------------------------------------------------------------------------- 83// innerCreateTexture 84//----------------------------------------------------------------------------- 85// This just creates the texture, no info is actually loaded to it. We do that later. 86void GFXGLTextureManager::innerCreateTexture( GFXGLTextureObject *retTex, 87 U32 height, 88 U32 width, 89 U32 depth, 90 GFXFormat format, 91 GFXTextureProfile *profile, 92 U32 numMipLevels, 93 bool forceMips) 94{ 95 // No 24 bit formats. They trigger various oddities because hardware (and Apple's drivers apparently...) don't natively support them. 96 if (format == GFXFormatR8G8B8) 97 format = GFXFormatR8G8B8A8; 98 else if (format == GFXFormatR8G8B8_SRGB) 99 format = GFXFormatR8G8B8A8_SRGB; 100 101 retTex->mFormat = format; 102 retTex->mIsZombie = false; 103 retTex->mIsNPoT2 = false; 104 105 GLenum binding = ( (height == 1 || width == 1) && ( height != width ) ) ? GL_TEXTURE_1D : ( (depth == 0) ? GL_TEXTURE_2D : GL_TEXTURE_3D ); 106 if((profile->testFlag(GFXTextureProfile::RenderTarget) || profile->testFlag(GFXTextureProfile::ZTarget)) && (!isPow2(width) || !isPow2(height)) && !depth) 107 retTex->mIsNPoT2 = true; 108 retTex->mBinding = binding; 109 110 // Bind it 111 PRESERVE_TEXTURE(binding); 112 glBindTexture(retTex->getBinding(), retTex->getHandle()); 113 114 // Create it 115 // @todo OPENGL - Creating mipmaps for compressed formats. Not supported on OpenGL ES and bugged on AMD. We use mipmaps present on file. 116 if( forceMips && !retTex->mIsNPoT2 && !ImageUtil::isCompressedFormat(format) ) 117 { 118 retTex->mMipLevels = numMipLevels > 1 ? numMipLevels : 0; 119 } 120 else if(profile->testFlag(GFXTextureProfile::NoMipmap) || profile->testFlag(GFXTextureProfile::RenderTarget) || numMipLevels == 1 || retTex->mIsNPoT2) 121 { 122 retTex->mMipLevels = 1; 123 } 124 else 125 { 126 retTex->mMipLevels = numMipLevels; 127 } 128 129 // @todo OPENGL - OpenGL ES2 not support mipmaps on NPOT textures 130#if 0 131 if(!retTex->mIsNPoT2) 132 { 133 if(!isPow2(width)) 134 width = getNextPow2(width); 135 if(!isPow2(height)) 136 height = getNextPow2(height); 137 if(depth && !isPow2(depth)) 138 depth = getNextPow2(depth); 139 } 140#endif 141 142 AssertFatal(GFXGLTextureInternalFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid internal format"); 143 AssertFatal(GFXGLTextureFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid format"); 144 AssertFatal(GFXGLTextureType[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid type"); 145 146 //calculate num mipmaps 147 if(retTex->mMipLevels == 0) 148 retTex->mMipLevels = getMaxMipmaps(width, height, 1); 149 150 glTexParameteri(binding, GL_TEXTURE_MAX_LEVEL, retTex->mMipLevels-1 ); 151 152 if( GFXGL->mCapabilities.textureStorage ) 153 { 154 if(binding == GL_TEXTURE_2D) 155 glTexStorage2D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], width, height ); 156 else if(binding == GL_TEXTURE_1D) 157 glTexStorage1D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], getMax(width, height) ); 158 else 159 glTexStorage3D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], width, height, depth ); 160 } 161 else 162 { 163 //If it wasn't for problems on amd drivers this next part could be really simplified and we wouldn't need to go through manually creating our 164 //mipmap pyramid and instead just use glGenerateMipmap 165 if(ImageUtil::isCompressedFormat(format)) 166 { 167 AssertFatal(binding == GL_TEXTURE_2D, 168 "GFXGLTextureManager::innerCreateTexture - Only compressed 2D textures are supported"); 169 170 U32 tempWidth = width; 171 U32 tempHeight = height; 172 U32 size = getCompressedSurfaceSize(format,height,width); 173 //Fill compressed images with 0's 174 U8 *pTemp = (U8*)dMalloc(sizeof(U8)*size); 175 dMemset(pTemp,0,size); 176 177 for(U32 i=0;i< retTex->mMipLevels;i++) 178 { 179 tempWidth = getMax( U32(1), width >> i ); 180 tempHeight = getMax( U32(1), height >> i ); 181 size = getCompressedSurfaceSize(format,width,height,i); 182 glCompressedTexImage2D(binding,i,GFXGLTextureInternalFormat[format],tempWidth,tempHeight,0,size,pTemp); 183 } 184 185 dFree(pTemp); 186 } 187 else 188 { 189 if(binding == GL_TEXTURE_2D) 190 glTexImage2D(binding, 0, GFXGLTextureInternalFormat[format], width, height, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL); 191 else if(binding == GL_TEXTURE_1D) 192 glTexImage1D(binding, 0, GFXGLTextureInternalFormat[format], (width > 1 ? width : height), 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL); 193 else 194 glTexImage3D(GL_TEXTURE_3D, 0, GFXGLTextureInternalFormat[format], width, height, depth, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL); 195 196 if(retTex->mMipLevels > 1) 197 glGenerateMipmap(binding); 198 } 199 } 200 201 // Complete the texture 202 // Complete the texture - this does get changed later but we need to complete the texture anyway 203 204 if(retTex->mMipLevels == 1) 205 glTexParameteri(binding, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 206 else 207 glTexParameteri(binding, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 208 glTexParameteri(binding, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 209 glTexParameteri(binding, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 210 glTexParameteri(binding, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 211 if(binding == GL_TEXTURE_3D) 212 glTexParameteri(binding, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 213 214 if(GFXGLTextureSwizzle[format]) 215 glTexParameteriv(binding, GL_TEXTURE_SWIZZLE_RGBA, GFXGLTextureSwizzle[format]); 216 217 // Get the size from GL (you never know...) 218 GLint texHeight, texWidth, texDepth = 0; 219 220 glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_WIDTH, &texWidth); 221 glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_HEIGHT, &texHeight); 222 if(binding == GL_TEXTURE_3D) 223 glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_DEPTH, &texDepth); 224 225 retTex->mTextureSize.set(texWidth, texHeight, texDepth); 226} 227 228//----------------------------------------------------------------------------- 229// loadTexture - GBitmap 230//----------------------------------------------------------------------------- 231 232static void _textureUpload(const S32 width, const S32 height,const S32 bytesPerPixel,const GFXGLTextureObject* texture, const GFXFormat fmt, const U8* data,const S32 mip=0, Swizzle<U8, 4> *pSwizzle = NULL) 233{ 234 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->getBuffer()); 235 U32 bufSize = width * height * bytesPerPixel; 236 glBufferData(GL_PIXEL_UNPACK_BUFFER, bufSize, NULL, GL_STREAM_DRAW); 237 238 if(pSwizzle) 239 { 240 PROFILE_SCOPE(Swizzle32_Upload); 241 U8* pboMemory = (U8*)dMalloc(bufSize); 242 pSwizzle->ToBuffer(pboMemory, data, bufSize); 243 glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, bufSize, pboMemory); 244 dFree(pboMemory); 245 } 246 else 247 { 248 PROFILE_SCOPE(SwizzleNull_Upload); 249 glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, bufSize, data); 250 } 251 252 if (texture->getBinding() == GL_TEXTURE_2D) 253 glTexSubImage2D(texture->getBinding(), mip, 0, 0, width, height, GFXGLTextureFormat[fmt], GFXGLTextureType[fmt], NULL); 254 else 255 glTexSubImage1D(texture->getBinding(), mip, 0, (width > 1 ? width : height), GFXGLTextureFormat[fmt], GFXGLTextureType[fmt], NULL); 256 257 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); 258} 259 260bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, GBitmap *pDL) 261{ 262 PROFILE_SCOPE(GFXGLTextureManager_loadTexture); 263 GFXGLTextureObject *texture = static_cast<GFXGLTextureObject*>(aTexture); 264 265 AssertFatal(texture->getBinding() == GL_TEXTURE_1D || texture->getBinding() == GL_TEXTURE_2D, 266 "GFXGLTextureManager::_loadTexture(GBitmap) - This method can only be used with 1D/2D textures"); 267 268 if(texture->getBinding() == GL_TEXTURE_3D) 269 return false; 270 271 // No 24bit formats. 272 if(pDL->getFormat() == GFXFormatR8G8B8) 273 pDL->setFormat(GFXFormatR8G8B8A8); 274 else if (pDL->getFormat() == GFXFormatR8G8B8_SRGB) 275 pDL->setFormat(GFXFormatR8G8B8A8_SRGB); 276 // Bind to edit 277 PRESERVE_TEXTURE(texture->getBinding()); 278 glBindTexture(texture->getBinding(), texture->getHandle()); 279 280 _textureUpload(pDL->getWidth(),pDL->getHeight(),pDL->getBytesPerPixel(),texture,pDL->getFormat(), pDL->getBits(), 0); 281 282 if(!ImageUtil::isCompressedFormat(pDL->getFormat())) 283 glGenerateMipmap(texture->getBinding()); 284 285 return true; 286} 287 288bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, DDSFile *dds) 289{ 290 GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture); 291 292 AssertFatal(texture->getBinding() == GL_TEXTURE_2D, 293 "GFXGLTextureManager::_loadTexture(DDSFile) - This method can only be used with 2D textures"); 294 295 if(texture->getBinding() != GL_TEXTURE_2D) 296 return false; 297 298 PRESERVE_TEXTURE(texture->getBinding()); 299 glBindTexture(texture->getBinding(), texture->getHandle()); 300 U32 numMips = dds->mSurfaces[0]->mMips.size(); 301 const GFXFormat fmt = texture->mFormat; 302 303 for(U32 i = 0; i < numMips; i++) 304 { 305 PROFILE_SCOPE(GFXGLTexMan_loadSurface); 306 307 if(ImageUtil::isCompressedFormat(texture->mFormat)) 308 { 309 if((!isPow2(dds->getWidth()) || !isPow2(dds->getHeight())) && GFX->getCardProfiler()->queryProfile("GL::Workaround::noCompressedNPoTTextures")) 310 { 311 U8* uncompressedTex = new U8[dds->getWidth(i) * dds->getHeight(i) * 4]; 312 ImageUtil::decompress(dds->mSurfaces[0]->mMips[i],uncompressedTex, dds->getWidth(i), dds->getHeight(i), fmt); 313 glTexSubImage2D(texture->getBinding(), i, 0, 0, dds->getWidth(i), dds->getHeight(i), GL_RGBA, GL_UNSIGNED_BYTE, uncompressedTex); 314 delete[] uncompressedTex; 315 } 316 else 317 glCompressedTexSubImage2D(texture->getBinding(), i, 0, 0, dds->getWidth(i), dds->getHeight(i), GFXGLTextureInternalFormat[fmt], dds->getSurfaceSize(dds->getHeight(), dds->getWidth(), i), dds->mSurfaces[0]->mMips[i]); 318 } 319 else 320 { 321 Swizzle<U8, 4> *pSwizzle = NULL; 322 if (fmt == GFXFormatR8G8B8A8 || fmt == GFXFormatR8G8B8X8 || fmt == GFXFormatR8G8B8A8_SRGB || fmt == GFXFormatR8G8B8A8_LINEAR_FORCE || fmt == GFXFormatB8G8R8A8) 323 pSwizzle = &Swizzles::bgra; 324 325 _textureUpload(dds->getWidth(i), dds->getHeight(i),dds->mBytesPerPixel, texture, fmt, dds->mSurfaces[0]->mMips[i],i, pSwizzle); 326 } 327 } 328 329 if(numMips !=1 && !ImageUtil::isCompressedFormat(texture->mFormat)) 330 glGenerateMipmap(texture->getBinding()); 331 332 return true; 333} 334 335bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, void *raw) 336{ 337 PROFILE_SCOPE(GFXGLTextureManager_loadTextureRaw); 338 if(aTexture->getDepth() < 1) 339 return false; 340 341 GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture); 342 343 PRESERVE_3D_TEXTURE(); 344 glBindTexture(texture->getBinding(), texture->getHandle()); 345 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, texture->getWidth(), texture->getHeight(), texture->getDepth(), GFXGLTextureFormat[texture->mFormat], GFXGLTextureType[texture->mFormat], raw); 346 347 return true; 348} 349 350bool GFXGLTextureManager::_freeTexture(GFXTextureObject *texture, bool zombify /*= false*/) 351{ 352 if(zombify) 353 static_cast<GFXGLTextureObject*>(texture)->zombify(); 354 else 355 static_cast<GFXGLTextureObject*>(texture)->release(); 356 357 return true; 358} 359 360bool GFXGLTextureManager::_refreshTexture(GFXTextureObject *texture) 361{ 362 U32 usedStrategies = 0; 363 GFXGLTextureObject* realTex = static_cast<GFXGLTextureObject*>(texture); 364 365 if(texture->mProfile->doStoreBitmap()) 366 { 367 if(realTex->isZombie()) 368 { 369 realTex->resurrect(); 370 innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels); 371 } 372 if(texture->mBitmap) 373 _loadTexture(texture, texture->mBitmap); 374 375 if(texture->mDDS) 376 return false; 377 378 usedStrategies++; 379 } 380 381 if(texture->mProfile->isRenderTarget() || texture->mProfile->isDynamic() || texture->mProfile->isZTarget() || !usedStrategies) 382 { 383 realTex->release(); 384 realTex->resurrect(); 385 innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels); 386 realTex->reloadFromCache(); 387 usedStrategies++; 388 } 389 390 AssertFatal(usedStrategies < 2, "GFXGLTextureManager::_refreshTexture - Inconsistent profile flags (store bitmap and dynamic/target"); 391 392 return true; 393} 394