gfxGLTextureManager.cpp 17 KB

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