gfxGLTextureObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "console/console.h"
  23. #include "gfx/gl/tGL/tGL.h"
  24. #include "math/mRect.h"
  25. #include "gfx/gl/gfxGLTextureObject.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gl/gfxGLEnumTranslate.h"
  28. #include "gfx/gl/gfxGLTextureManager.h"
  29. #include "gfx/gl/gfxGLUtils.h"
  30. #include "gfx/gfxCardProfile.h"
  31. GFXGLTextureObject::GFXGLTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile) :
  32. GFXTextureObject(aDevice, profile),
  33. mBinding(GL_TEXTURE_2D),
  34. mBytesPerTexel(4),
  35. mLockedRectRect(0, 0, 0, 0),
  36. mGLDevice(static_cast<GFXGLDevice*>(mDevice)),
  37. mZombieCache(NULL),
  38. mNeedInitSamplerState(true),
  39. mFrameAllocatorMark(0),
  40. mFrameAllocatorPtr(NULL)
  41. {
  42. AssertFatal(dynamic_cast<GFXGLDevice*>(mDevice), "GFXGLTextureObject::GFXGLTextureObject - Invalid device type, expected GFXGLDevice!");
  43. glGenTextures(1, &mHandle);
  44. glGenBuffers(1, &mBuffer);
  45. }
  46. GFXGLTextureObject::~GFXGLTextureObject()
  47. {
  48. glDeleteTextures(1, &mHandle);
  49. glDeleteBuffers(1, &mBuffer);
  50. delete[] mZombieCache;
  51. kill();
  52. }
  53. GFXLockedRect* GFXGLTextureObject::lock(U32 mipLevel, RectI *inRect)
  54. {
  55. AssertFatal(mBinding != GL_TEXTURE_3D, "GFXGLTextureObject::lock - We don't support locking 3D textures yet");
  56. U32 width = mTextureSize.x >> mipLevel;
  57. U32 height = mTextureSize.y >> mipLevel;
  58. if(inRect)
  59. {
  60. if((inRect->point.x + inRect->extent.x > width) || (inRect->point.y + inRect->extent.y > height))
  61. AssertFatal(false, "GFXGLTextureObject::lock - Rectangle too big!");
  62. mLockedRectRect = *inRect;
  63. }
  64. else
  65. {
  66. mLockedRectRect = RectI(0, 0, width, height);
  67. }
  68. mLockedRect.pitch = mLockedRectRect.extent.x * mBytesPerTexel;
  69. // CodeReview [ags 12/19/07] This one texel boundary is necessary to keep the clipmap code from crashing. Figure out why.
  70. U32 size = (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel;
  71. AssertFatal(!mFrameAllocatorMark && !mFrameAllocatorPtr, "");
  72. mFrameAllocatorMark = FrameAllocator::getWaterMark();
  73. mFrameAllocatorPtr = (U8*)FrameAllocator::alloc( size );
  74. mLockedRect.bits = mFrameAllocatorPtr;
  75. #if TORQUE_DEBUG
  76. mFrameAllocatorMarkGuard = FrameAllocator::getWaterMark();
  77. #endif
  78. if( !mLockedRect.bits )
  79. return NULL;
  80. return &mLockedRect;
  81. }
  82. void GFXGLTextureObject::unlock(U32 mipLevel)
  83. {
  84. if(!mLockedRect.bits)
  85. return;
  86. PRESERVE_TEXTURE(mBinding);
  87. glBindTexture(mBinding, mHandle);
  88. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
  89. glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, mFrameAllocatorPtr, GL_STREAM_DRAW);
  90. if(mBinding == GL_TEXTURE_2D)
  91. glTexSubImage2D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
  92. mLockedRectRect.extent.x, mLockedRectRect.extent.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  93. else if(mBinding == GL_TEXTURE_1D)
  94. glTexSubImage1D(mBinding, mipLevel, (mLockedRectRect.point.x > 1 ? mLockedRectRect.point.x : mLockedRectRect.point.y),
  95. (mLockedRectRect.extent.x > 1 ? mLockedRectRect.extent.x : mLockedRectRect.extent.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  96. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  97. mLockedRect.bits = NULL;
  98. #if TORQUE_DEBUG
  99. AssertFatal(mFrameAllocatorMarkGuard == FrameAllocator::getWaterMark(), "");
  100. #endif
  101. FrameAllocator::setWaterMark(mFrameAllocatorMark);
  102. mFrameAllocatorMark = 0;
  103. mFrameAllocatorPtr = NULL;
  104. }
  105. void GFXGLTextureObject::release()
  106. {
  107. glDeleteTextures(1, &mHandle);
  108. glDeleteBuffers(1, &mBuffer);
  109. mHandle = 0;
  110. mBuffer = 0;
  111. }
  112. void GFXGLTextureObject::reInit()
  113. {
  114. AssertFatal(!mHandle && !mBuffer,"Must release before reInit");
  115. glGenTextures(1, &mHandle);
  116. glGenBuffers(1, &mBuffer);
  117. }
  118. bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
  119. {
  120. if (!bmp)
  121. return false;
  122. // check format limitations
  123. // at the moment we only support RGBA for the source (other 4 byte formats should
  124. // be easy to add though)
  125. AssertFatal(mFormat == GFXFormatR8G8B8A8, "GFXGLTextureObject::copyToBmp - invalid format");
  126. AssertFatal(bmp->getFormat() == GFXFormatR8G8B8A8 || bmp->getFormat() == GFXFormatR8G8B8, "GFXGLTextureObject::copyToBmp - invalid format");
  127. if(mFormat != GFXFormatR8G8B8A8)
  128. return false;
  129. if(bmp->getFormat() != GFXFormatR8G8B8A8 && bmp->getFormat() != GFXFormatR8G8B8)
  130. return false;
  131. AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
  132. AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
  133. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  134. PRESERVE_TEXTURE(mBinding);
  135. glBindTexture(mBinding, mHandle);
  136. U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
  137. U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
  138. if(dstBytesPerPixel == srcBytesPerPixel)
  139. {
  140. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], bmp->getWritableBits());
  141. return true;
  142. }
  143. FrameAllocatorMarker mem;
  144. U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
  145. U8 *dest = bmp->getWritableBits();
  146. U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
  147. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
  148. for(int i = 0; i < srcPixelCount; ++i)
  149. {
  150. dest[0] = orig[0];
  151. dest[1] = orig[1];
  152. dest[2] = orig[2];
  153. if(dstBytesPerPixel == 4)
  154. dest[3] = orig[3];
  155. orig += srcBytesPerPixel;
  156. dest += dstBytesPerPixel;
  157. }
  158. return true;
  159. }
  160. void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
  161. {
  162. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  163. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  164. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  165. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  166. if(mBinding == GL_TEXTURE_3D)
  167. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  168. if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  169. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  170. mNeedInitSamplerState = false;
  171. mSampler = ssd;
  172. }
  173. void GFXGLTextureObject::bind(U32 textureUnit)
  174. {
  175. glActiveTexture(GL_TEXTURE0 + textureUnit);
  176. glBindTexture(mBinding, mHandle);
  177. GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
  178. if( gglHasExtension(ARB_sampler_objects) )
  179. return;
  180. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  181. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  182. if (!sb)
  183. return;
  184. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  185. if(mNeedInitSamplerState)
  186. {
  187. initSamplerState(ssd);
  188. return;
  189. }
  190. if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
  191. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  192. if(mSampler.magFilter != ssd.magFilter)
  193. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  194. if(mSampler.addressModeU != ssd.addressModeU)
  195. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  196. if(mSampler.addressModeV != ssd.addressModeV)
  197. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  198. if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
  199. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  200. if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  201. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  202. mSampler = ssd;
  203. }
  204. U8* GFXGLTextureObject::getTextureData( U32 mip )
  205. {
  206. AssertFatal( mMipLevels, "");
  207. mip = (mip < mMipLevels) ? mip : 0;
  208. const U32 dataSize = isCompressedFormat(mFormat)
  209. ? getCompressedSurfaceSize( mFormat, mTextureSize.x, mTextureSize.y, mip )
  210. : (mTextureSize.x >> mip) * (mTextureSize.y >> mip) * mBytesPerTexel;
  211. U8* data = new U8[dataSize];
  212. PRESERVE_TEXTURE(mBinding);
  213. glBindTexture(mBinding, mHandle);
  214. if( isCompressedFormat(mFormat) )
  215. glGetCompressedTexImage( mBinding, mip, data );
  216. else
  217. glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
  218. return data;
  219. }
  220. void GFXGLTextureObject::copyIntoCache()
  221. {
  222. PRESERVE_TEXTURE(mBinding);
  223. glBindTexture(mBinding, mHandle);
  224. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  225. if(mBinding == GL_TEXTURE_3D)
  226. cacheSize *= mTextureSize.z;
  227. cacheSize *= mBytesPerTexel;
  228. mZombieCache = new U8[cacheSize];
  229. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  230. }
  231. void GFXGLTextureObject::reloadFromCache()
  232. {
  233. if(!mZombieCache)
  234. return;
  235. if(mBinding == GL_TEXTURE_3D)
  236. {
  237. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  238. delete[] mZombieCache;
  239. mZombieCache = NULL;
  240. return;
  241. }
  242. PRESERVE_TEXTURE(mBinding);
  243. glBindTexture(mBinding, mHandle);
  244. if(mBinding == GL_TEXTURE_2D)
  245. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  246. else if(mBinding == GL_TEXTURE_1D)
  247. glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  248. if(GFX->getCardProfiler()->queryProfile("GL::Workaround::needsExplicitGenerateMipmap") && mMipLevels != 1)
  249. glGenerateMipmapEXT(mBinding);
  250. delete[] mZombieCache;
  251. mZombieCache = NULL;
  252. mIsZombie = false;
  253. }
  254. void GFXGLTextureObject::zombify()
  255. {
  256. if(mIsZombie)
  257. return;
  258. mIsZombie = true;
  259. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  260. copyIntoCache();
  261. release();
  262. }
  263. void GFXGLTextureObject::resurrect()
  264. {
  265. if(!mIsZombie)
  266. return;
  267. glGenTextures(1, &mHandle);
  268. glGenBuffers(1, &mBuffer);
  269. }
  270. F32 GFXGLTextureObject::getMaxUCoord() const
  271. {
  272. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  273. }
  274. F32 GFXGLTextureObject::getMaxVCoord() const
  275. {
  276. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  277. }
  278. const String GFXGLTextureObject::describeSelf() const
  279. {
  280. String ret = Parent::describeSelf();
  281. ret += String::ToString(" GL Handle: %i", mHandle);
  282. return ret;
  283. }