gfxGLTextureManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. }
  63. else
  64. {
  65. retTex = new GFXGLTextureObject( GFX, profile );
  66. retTex->registerResourceWithDevice( GFX );
  67. }
  68. innerCreateTexture(retTex, height, width, depth, format, profile, numMipLevels, forceMips);
  69. return retTex;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // innerCreateTexture
  73. //-----------------------------------------------------------------------------
  74. // This just creates the texture, no info is actually loaded to it. We do that later.
  75. void GFXGLTextureManager::innerCreateTexture( GFXGLTextureObject *retTex,
  76. U32 height,
  77. U32 width,
  78. U32 depth,
  79. GFXFormat format,
  80. GFXTextureProfile *profile,
  81. U32 numMipLevels,
  82. bool forceMips)
  83. {
  84. // No 24 bit formats. They trigger various oddities because hardware (and Apple's drivers apparently...) don't natively support them.
  85. if(format == GFXFormatR8G8B8)
  86. format = GFXFormatR8G8B8A8;
  87. retTex->mFormat = format;
  88. retTex->mIsZombie = false;
  89. retTex->mIsNPoT2 = false;
  90. GLenum binding = (depth == 0) ? GL_TEXTURE_2D : GL_TEXTURE_3D;
  91. if((profile->testFlag(GFXTextureProfile::RenderTarget) || profile->testFlag(GFXTextureProfile::ZTarget)) && (!isPow2(width) || !isPow2(height)) && !depth)
  92. retTex->mIsNPoT2 = true;
  93. retTex->mBinding = binding;
  94. // Bind it
  95. glActiveTexture(GL_TEXTURE0);
  96. PRESERVE_2D_TEXTURE();
  97. PRESERVE_3D_TEXTURE();
  98. glBindTexture(binding, retTex->getHandle());
  99. // Create it
  100. // TODO: Reenable mipmaps on render targets when Apple fixes their drivers
  101. if(forceMips && !retTex->mIsNPoT2)
  102. {
  103. glTexParameteri(binding, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  104. retTex->mMipLevels = 0;
  105. }
  106. else if(profile->testFlag(GFXTextureProfile::NoMipmap) || profile->testFlag(GFXTextureProfile::RenderTarget) || numMipLevels == 1 || retTex->mIsNPoT2)
  107. {
  108. retTex->mMipLevels = 1;
  109. }
  110. else
  111. {
  112. glTexParameteri(binding, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  113. retTex->mMipLevels = 0;
  114. }
  115. if(!retTex->mIsNPoT2)
  116. {
  117. if(!isPow2(width))
  118. width = getNextPow2(width);
  119. if(!isPow2(height))
  120. height = getNextPow2(height);
  121. if(depth && !isPow2(depth))
  122. depth = getNextPow2(depth);
  123. }
  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. if(binding != GL_TEXTURE_3D)
  128. glTexImage2D(binding, 0, GFXGLTextureInternalFormat[format], width, height, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  129. else
  130. glTexImage3D(GL_TEXTURE_3D, 0, GFXGLTextureInternalFormat[format], width, height, depth, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  131. // Complete the texture
  132. glTexParameteri(binding, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  133. glTexParameteri(binding, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  134. glTexParameteri(binding, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  135. glTexParameteri(binding, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  136. if(binding == GL_TEXTURE_3D)
  137. glTexParameteri(binding, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  138. // Get the size from GL (you never know...)
  139. GLint texHeight, texWidth, texDepth = 0;
  140. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_WIDTH, &texWidth);
  141. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_HEIGHT, &texHeight);
  142. if(binding == GL_TEXTURE_3D)
  143. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_DEPTH, &texDepth);
  144. retTex->mTextureSize.set(texWidth, texHeight, texDepth);
  145. }
  146. //-----------------------------------------------------------------------------
  147. // loadTexture - GBitmap
  148. //-----------------------------------------------------------------------------
  149. static void _fastTextureLoad(GFXGLTextureObject* texture, GBitmap* pDL)
  150. {
  151. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, texture->getBuffer());
  152. U32 bufSize = pDL->getWidth(0) * pDL->getHeight(0) * pDL->getBytesPerPixel();
  153. glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, bufSize, NULL, GL_STREAM_DRAW);
  154. U8* pboMemory = (U8*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
  155. if(pDL->getFormat() == GFXFormatR8G8B8A8 || pDL->getFormat() == GFXFormatR8G8B8X8)
  156. GFX->getDeviceSwizzle32()->ToBuffer(pboMemory, pDL->getBits(0), bufSize);
  157. else
  158. dMemcpy(pboMemory, pDL->getBits(0), bufSize);
  159. glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
  160. glTexSubImage2D(texture->getBinding(), 0, 0, 0, pDL->getWidth(0), pDL->getHeight(0), GFXGLTextureFormat[pDL->getFormat()], GFXGLTextureType[pDL->getFormat()], NULL);
  161. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  162. }
  163. static void _slowTextureLoad(GFXGLTextureObject* texture, GBitmap* pDL)
  164. {
  165. glTexSubImage2D(texture->getBinding(), 0, 0, 0, pDL->getWidth(0), pDL->getHeight(0), GFXGLTextureFormat[pDL->getFormat()], GFXGLTextureType[pDL->getFormat()], pDL->getBits(0));
  166. }
  167. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, GBitmap *pDL)
  168. {
  169. GFXGLTextureObject *texture = static_cast<GFXGLTextureObject*>(aTexture);
  170. AssertFatal(texture->getBinding() == GL_TEXTURE_2D,
  171. "GFXGLTextureManager::_loadTexture(GBitmap) - This method can only be used with 2D textures");
  172. if(texture->getBinding() != GL_TEXTURE_2D)
  173. return false;
  174. // No 24bit formats.
  175. if(pDL->getFormat() == GFXFormatR8G8B8)
  176. pDL->setFormat(GFXFormatR8G8B8A8);
  177. // Bind to edit
  178. glActiveTexture(GL_TEXTURE0);
  179. PRESERVE_2D_TEXTURE();
  180. glBindTexture(texture->getBinding(), texture->getHandle());
  181. if(pDL->getFormat() == GFXFormatR8G8B8A8 || pDL->getFormat() == GFXFormatR8G8B8X8)
  182. _fastTextureLoad(texture, pDL);
  183. else
  184. _slowTextureLoad(texture, pDL);
  185. glBindTexture(texture->getBinding(), 0);
  186. return true;
  187. }
  188. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, DDSFile *dds)
  189. {
  190. AssertFatal(!(dds->mFormat == GFXFormatDXT2 || dds->mFormat == GFXFormatDXT4), "GFXGLTextureManager::_loadTexture - OpenGL does not support DXT2 or DXT4 compressed textures");
  191. GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture);
  192. AssertFatal(texture->getBinding() == GL_TEXTURE_2D,
  193. "GFXGLTextureManager::_loadTexture(DDSFile) - This method can only be used with 2D textures");
  194. if(texture->getBinding() != GL_TEXTURE_2D)
  195. return false;
  196. glActiveTexture(GL_TEXTURE0);
  197. PRESERVE_2D_TEXTURE();
  198. glBindTexture(texture->getBinding(), texture->getHandle());
  199. U32 numMips = dds->mSurfaces[0]->mMips.size();
  200. if(GFX->getCardProfiler()->queryProfile("GL::Workaround::noManualMips"))
  201. numMips = 1;
  202. for(U32 i = 0; i < numMips; i++)
  203. {
  204. if(dds->mFormat == GFXFormatDXT1 || dds->mFormat == GFXFormatDXT3 || dds->mFormat == GFXFormatDXT5)
  205. {
  206. if((!isPow2(dds->getWidth()) || !isPow2(dds->getHeight())) && GFX->getCardProfiler()->queryProfile("GL::Workaround::noCompressedNPoTTextures"))
  207. {
  208. U32 squishFlag = squish::kDxt1;
  209. switch (dds->mFormat)
  210. {
  211. case GFXFormatDXT3:
  212. squishFlag = squish::kDxt3;
  213. break;
  214. case GFXFormatDXT5:
  215. squishFlag = squish::kDxt5;
  216. break;
  217. default:
  218. break;
  219. }
  220. U8* uncompressedTex = new U8[dds->getWidth(i) * dds->getHeight(i) * 4];
  221. squish::DecompressImage(uncompressedTex, dds->getWidth(i), dds->getHeight(i), dds->mSurfaces[0]->mMips[i], squishFlag);
  222. glTexSubImage2D(texture->getBinding(), i, 0, 0, dds->getWidth(i), dds->getHeight(i), GL_RGBA, GL_UNSIGNED_BYTE, uncompressedTex);
  223. delete[] uncompressedTex;
  224. }
  225. else
  226. 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]);
  227. }
  228. else
  229. glTexSubImage2D(texture->getBinding(), i, 0, 0, dds->getWidth(i), dds->getHeight(i), GFXGLTextureFormat[dds->mFormat], GFXGLTextureType[dds->mFormat], dds->mSurfaces[0]->mMips[i]);
  230. }
  231. glBindTexture(texture->getBinding(), 0);
  232. return true;
  233. }
  234. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, void *raw)
  235. {
  236. if(aTexture->getDepth() < 1)
  237. return false;
  238. GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture);
  239. glActiveTexture(GL_TEXTURE0);
  240. PRESERVE_3D_TEXTURE();
  241. glBindTexture(GL_TEXTURE_3D, texture->getHandle());
  242. glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, texture->getWidth(), texture->getHeight(), texture->getDepth(), GFXGLTextureFormat[texture->mFormat], GFXGLTextureType[texture->mFormat], raw);
  243. glBindTexture(GL_TEXTURE_3D, 0);
  244. return true;
  245. }
  246. bool GFXGLTextureManager::_freeTexture(GFXTextureObject *texture, bool zombify /*= false*/)
  247. {
  248. if(zombify)
  249. static_cast<GFXGLTextureObject*>(texture)->zombify();
  250. else
  251. static_cast<GFXGLTextureObject*>(texture)->release();
  252. return true;
  253. }
  254. bool GFXGLTextureManager::_refreshTexture(GFXTextureObject *texture)
  255. {
  256. U32 usedStrategies = 0;
  257. GFXGLTextureObject* realTex = static_cast<GFXGLTextureObject*>(texture);
  258. if(texture->mProfile->doStoreBitmap())
  259. {
  260. if(realTex->isZombie())
  261. {
  262. realTex->resurrect();
  263. innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels);
  264. }
  265. if(texture->mBitmap)
  266. _loadTexture(texture, texture->mBitmap);
  267. if(texture->mDDS)
  268. return false;
  269. usedStrategies++;
  270. }
  271. if(texture->mProfile->isRenderTarget() || texture->mProfile->isDynamic() || texture->mProfile->isZTarget() || !usedStrategies)
  272. {
  273. realTex->release();
  274. realTex->resurrect();
  275. innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels);
  276. realTex->reloadFromCache();
  277. usedStrategies++;
  278. }
  279. AssertFatal(usedStrategies < 2, "GFXGLTextureManager::_refreshTexture - Inconsistent profile flags (store bitmap and dynamic/target");
  280. return true;
  281. }