gfxGLTextureManager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. else if (format == GFXFormatR8G8B8_SRGB)
  89. format = GFXFormatR8G8B8A8_SRGB;
  90. retTex->mFormat = format;
  91. retTex->mIsZombie = false;
  92. retTex->mIsNPoT2 = false;
  93. GLenum binding = ( (height == 1 || width == 1) && ( height != width ) ) ? GL_TEXTURE_1D : ( (depth == 0) ? GL_TEXTURE_2D : GL_TEXTURE_3D );
  94. if((profile->testFlag(GFXTextureProfile::RenderTarget) || profile->testFlag(GFXTextureProfile::ZTarget)) && (!isPow2(width) || !isPow2(height)) && !depth)
  95. retTex->mIsNPoT2 = true;
  96. retTex->mBinding = binding;
  97. // Bind it
  98. PRESERVE_TEXTURE(binding);
  99. glBindTexture(retTex->getBinding(), retTex->getHandle());
  100. // Create it
  101. // @todo OPENGL - Creating mipmaps for compressed formats. Not supported on OpenGL ES and bugged on AMD. We use mipmaps present on file.
  102. if( forceMips && !retTex->mIsNPoT2 && !ImageUtil::isCompressedFormat(format) )
  103. {
  104. retTex->mMipLevels = numMipLevels > 1 ? numMipLevels : 0;
  105. }
  106. else if(profile->testFlag(GFXTextureProfile::NoMipmap) || numMipLevels == 1)
  107. {
  108. retTex->mMipLevels = 1;
  109. }
  110. else if (profile->testFlag(GFXTextureProfile::RenderTarget))
  111. {
  112. if (numMipLevels == 0) //auto
  113. numMipLevels = mFloor(mLog2(mMax(width, height))) + 1;
  114. else if (numMipLevels > 1) //capped
  115. numMipLevels = mMin(numMipLevels, mFloor(mLog2(mMax(width, height))) + 1);
  116. retTex->mMipLevels = mClampF(numMipLevels, 1, 13);
  117. }
  118. else
  119. {
  120. retTex->mMipLevels = numMipLevels;
  121. }
  122. // @todo OPENGL - OpenGL ES2 not support mipmaps on NPOT textures
  123. #if 0
  124. if(!retTex->mIsNPoT2)
  125. {
  126. if(!isPow2(width))
  127. width = getNextPow2(width);
  128. if(!isPow2(height))
  129. height = getNextPow2(height);
  130. if(depth && !isPow2(depth))
  131. depth = getNextPow2(depth);
  132. }
  133. #endif
  134. AssertFatal(GFXGLTextureInternalFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid internal format");
  135. AssertFatal(GFXGLTextureFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid format");
  136. AssertFatal(GFXGLTextureType[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid type");
  137. //calculate num mipmaps
  138. if(retTex->mMipLevels == 0)
  139. retTex->mMipLevels = getMaxMipmaps(width, height, 1);
  140. glTexParameteri(binding, GL_TEXTURE_MAX_LEVEL, retTex->mMipLevels-1 );
  141. if( GFXGL->mCapabilities.textureStorage )
  142. {
  143. if(binding == GL_TEXTURE_2D)
  144. glTexStorage2D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], width, height );
  145. else if(binding == GL_TEXTURE_1D)
  146. glTexStorage1D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], getMax(width, height) );
  147. else
  148. glTexStorage3D( retTex->getBinding(), retTex->mMipLevels, GFXGLTextureInternalFormat[format], width, height, depth );
  149. }
  150. else
  151. {
  152. //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
  153. //mipmap pyramid and instead just use glGenerateMipmap
  154. if(ImageUtil::isCompressedFormat(format))
  155. {
  156. AssertFatal(binding == GL_TEXTURE_2D,
  157. "GFXGLTextureManager::innerCreateTexture - Only compressed 2D textures are supported");
  158. U32 tempWidth = width;
  159. U32 tempHeight = height;
  160. U32 size = getCompressedSurfaceSize(format,height,width);
  161. //Fill compressed images with 0's
  162. U8 *pTemp = (U8*)dMalloc(sizeof(U8)*size);
  163. dMemset(pTemp,0,size);
  164. for(U32 i=0;i< retTex->mMipLevels;i++)
  165. {
  166. tempWidth = getMax( U32(1), width >> i );
  167. tempHeight = getMax( U32(1), height >> i );
  168. size = getCompressedSurfaceSize(format,width,height,i);
  169. glCompressedTexImage2D(binding,i,GFXGLTextureInternalFormat[format],tempWidth,tempHeight,0,size,pTemp);
  170. }
  171. dFree(pTemp);
  172. }
  173. else
  174. {
  175. if(binding == GL_TEXTURE_2D)
  176. glTexImage2D(binding, 0, GFXGLTextureInternalFormat[format], width, height, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  177. else if(binding == GL_TEXTURE_1D)
  178. glTexImage1D(binding, 0, GFXGLTextureInternalFormat[format], (width > 1 ? width : height), 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  179. else
  180. glTexImage3D(GL_TEXTURE_3D, 0, GFXGLTextureInternalFormat[format], width, height, depth, 0, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  181. if(retTex->mMipLevels > 1)
  182. glGenerateMipmap(binding);
  183. }
  184. }
  185. // Complete the texture
  186. // Complete the texture - this does get changed later but we need to complete the texture anyway
  187. if(retTex->mMipLevels == 1)
  188. glTexParameteri(binding, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  189. else
  190. glTexParameteri(binding, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  191. glTexParameteri(binding, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  192. glTexParameteri(binding, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  193. glTexParameteri(binding, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  194. if(binding == GL_TEXTURE_3D)
  195. glTexParameteri(binding, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  196. if(GFXGLTextureSwizzle[format])
  197. glTexParameteriv(binding, GL_TEXTURE_SWIZZLE_RGBA, GFXGLTextureSwizzle[format]);
  198. // Get the size from GL (you never know...)
  199. GLint texHeight, texWidth, texDepth = 0;
  200. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_WIDTH, &texWidth);
  201. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_HEIGHT, &texHeight);
  202. if(binding == GL_TEXTURE_3D)
  203. glGetTexLevelParameteriv(binding, 0, GL_TEXTURE_DEPTH, &texDepth);
  204. retTex->mTextureSize.set(texWidth, texHeight, texDepth);
  205. }
  206. //-----------------------------------------------------------------------------
  207. // loadTexture - GBitmap
  208. //-----------------------------------------------------------------------------
  209. static 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)
  210. {
  211. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->getBuffer());
  212. U32 bufSize = width * height * bytesPerPixel;
  213. glBufferData(GL_PIXEL_UNPACK_BUFFER, bufSize, NULL, GL_STREAM_DRAW);
  214. if(pSwizzle)
  215. {
  216. PROFILE_SCOPE(Swizzle32_Upload);
  217. U8* pboMemory = (U8*)dMalloc(bufSize);
  218. pSwizzle->ToBuffer(pboMemory, data, bufSize);
  219. glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, bufSize, pboMemory);
  220. dFree(pboMemory);
  221. }
  222. else
  223. {
  224. PROFILE_SCOPE(SwizzleNull_Upload);
  225. glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, bufSize, data);
  226. }
  227. if (texture->getBinding() == GL_TEXTURE_2D)
  228. glTexSubImage2D(texture->getBinding(), mip, 0, 0, width, height, GFXGLTextureFormat[fmt], GFXGLTextureType[fmt], NULL);
  229. else
  230. glTexSubImage1D(texture->getBinding(), mip, 0, (width > 1 ? width : height), GFXGLTextureFormat[fmt], GFXGLTextureType[fmt], NULL);
  231. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  232. }
  233. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, GBitmap *pDL)
  234. {
  235. PROFILE_SCOPE(GFXGLTextureManager_loadTexture);
  236. GFXGLTextureObject *texture = static_cast<GFXGLTextureObject*>(aTexture);
  237. AssertFatal(texture->getBinding() == GL_TEXTURE_1D || texture->getBinding() == GL_TEXTURE_2D,
  238. "GFXGLTextureManager::_loadTexture(GBitmap) - This method can only be used with 1D/2D textures");
  239. if(texture->getBinding() == GL_TEXTURE_3D)
  240. return false;
  241. // No 24bit formats.
  242. if(pDL->getFormat() == GFXFormatR8G8B8)
  243. pDL->setFormat(GFXFormatR8G8B8A8);
  244. else if (pDL->getFormat() == GFXFormatR8G8B8_SRGB)
  245. pDL->setFormat(GFXFormatR8G8B8A8_SRGB);
  246. // Bind to edit
  247. PRESERVE_TEXTURE(texture->getBinding());
  248. glBindTexture(texture->getBinding(), texture->getHandle());
  249. _textureUpload(pDL->getWidth(),pDL->getHeight(),pDL->getBytesPerPixel(),texture,pDL->getFormat(), pDL->getBits(), 0);
  250. if(!ImageUtil::isCompressedFormat(pDL->getFormat()))
  251. glGenerateMipmap(texture->getBinding());
  252. return true;
  253. }
  254. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, DDSFile *dds)
  255. {
  256. GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture);
  257. AssertFatal(texture->getBinding() == GL_TEXTURE_2D,
  258. "GFXGLTextureManager::_loadTexture(DDSFile) - This method can only be used with 2D textures");
  259. if(texture->getBinding() != GL_TEXTURE_2D)
  260. return false;
  261. PRESERVE_TEXTURE(texture->getBinding());
  262. glBindTexture(texture->getBinding(), texture->getHandle());
  263. U32 numMips = dds->mSurfaces[0]->mMips.size();
  264. const GFXFormat fmt = texture->mFormat;
  265. for(U32 i = 0; i < numMips; i++)
  266. {
  267. PROFILE_SCOPE(GFXGLTexMan_loadSurface);
  268. if(ImageUtil::isCompressedFormat(texture->mFormat))
  269. {
  270. if((!isPow2(dds->getWidth()) || !isPow2(dds->getHeight())) && GFX->getCardProfiler()->queryProfile("GL::Workaround::noCompressedNPoTTextures"))
  271. {
  272. U8* uncompressedTex = new U8[dds->getWidth(i) * dds->getHeight(i) * 4];
  273. ImageUtil::decompress(dds->mSurfaces[0]->mMips[i],uncompressedTex, dds->getWidth(i), dds->getHeight(i), fmt);
  274. glTexSubImage2D(texture->getBinding(), i, 0, 0, dds->getWidth(i), dds->getHeight(i), GL_RGBA, GL_UNSIGNED_BYTE, uncompressedTex);
  275. delete[] uncompressedTex;
  276. }
  277. else
  278. 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]);
  279. }
  280. else
  281. {
  282. Swizzle<U8, 4> *pSwizzle = NULL;
  283. if (fmt == GFXFormatR8G8B8A8 || fmt == GFXFormatR8G8B8X8 || fmt == GFXFormatR8G8B8A8_SRGB || fmt == GFXFormatR8G8B8A8_LINEAR_FORCE || fmt == GFXFormatB8G8R8A8)
  284. pSwizzle = &Swizzles::bgra;
  285. _textureUpload(dds->getWidth(i), dds->getHeight(i),dds->mBytesPerPixel, texture, fmt, dds->mSurfaces[0]->mMips[i],i, pSwizzle);
  286. }
  287. }
  288. if(numMips !=1 && !ImageUtil::isCompressedFormat(texture->mFormat))
  289. glGenerateMipmap(texture->getBinding());
  290. return true;
  291. }
  292. bool GFXGLTextureManager::_loadTexture(GFXTextureObject *aTexture, void *raw)
  293. {
  294. PROFILE_SCOPE(GFXGLTextureManager_loadTextureRaw);
  295. if(aTexture->getDepth() < 1)
  296. return false;
  297. GFXGLTextureObject* texture = static_cast<GFXGLTextureObject*>(aTexture);
  298. PRESERVE_3D_TEXTURE();
  299. glBindTexture(texture->getBinding(), texture->getHandle());
  300. glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, texture->getWidth(), texture->getHeight(), texture->getDepth(), GFXGLTextureFormat[texture->mFormat], GFXGLTextureType[texture->mFormat], raw);
  301. return true;
  302. }
  303. bool GFXGLTextureManager::_freeTexture(GFXTextureObject *texture, bool zombify /*= false*/)
  304. {
  305. if(zombify)
  306. static_cast<GFXGLTextureObject*>(texture)->zombify();
  307. else
  308. static_cast<GFXGLTextureObject*>(texture)->release();
  309. return true;
  310. }
  311. bool GFXGLTextureManager::_refreshTexture(GFXTextureObject *texture)
  312. {
  313. U32 usedStrategies = 0;
  314. GFXGLTextureObject* realTex = static_cast<GFXGLTextureObject*>(texture);
  315. if(texture->mProfile->doStoreBitmap())
  316. {
  317. if(realTex->isZombie())
  318. {
  319. realTex->resurrect();
  320. innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels);
  321. }
  322. if(texture->mBitmap)
  323. _loadTexture(texture, texture->mBitmap);
  324. if(texture->mDDS)
  325. return false;
  326. usedStrategies++;
  327. }
  328. if(texture->mProfile->isRenderTarget() || texture->mProfile->isDynamic() || texture->mProfile->isZTarget() || !usedStrategies)
  329. {
  330. realTex->release();
  331. realTex->resurrect();
  332. innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels);
  333. realTex->reloadFromCache();
  334. usedStrategies++;
  335. }
  336. AssertFatal(usedStrategies < 2, "GFXGLTextureManager::_refreshTexture - Inconsistent profile flags (store bitmap and dynamic/target");
  337. return true;
  338. }