gfxGLTextureManager.cpp 16 KB

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