gfxGLTextureObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // I know this is in unlock, but in GL we actually do our submission in unlock.
  87. PROFILE_SCOPE(GFXGLTextureObject_lockRT);
  88. PRESERVE_TEXTURE(mBinding);
  89. glBindTexture(mBinding, mHandle);
  90. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
  91. glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, mFrameAllocatorPtr, GL_STREAM_DRAW);
  92. if(mBinding == GL_TEXTURE_2D)
  93. glTexSubImage2D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
  94. mLockedRectRect.extent.x, mLockedRectRect.extent.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  95. else if(mBinding == GL_TEXTURE_1D)
  96. glTexSubImage1D(mBinding, mipLevel, (mLockedRectRect.point.x > 1 ? mLockedRectRect.point.x : mLockedRectRect.point.y),
  97. (mLockedRectRect.extent.x > 1 ? mLockedRectRect.extent.x : mLockedRectRect.extent.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  98. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  99. mLockedRect.bits = NULL;
  100. #if TORQUE_DEBUG
  101. AssertFatal(mFrameAllocatorMarkGuard == FrameAllocator::getWaterMark(), "");
  102. #endif
  103. FrameAllocator::setWaterMark(mFrameAllocatorMark);
  104. mFrameAllocatorMark = 0;
  105. mFrameAllocatorPtr = NULL;
  106. }
  107. void GFXGLTextureObject::release()
  108. {
  109. glDeleteTextures(1, &mHandle);
  110. glDeleteBuffers(1, &mBuffer);
  111. mHandle = 0;
  112. mBuffer = 0;
  113. }
  114. void GFXGLTextureObject::reInit()
  115. {
  116. AssertFatal(!mHandle && !mBuffer,"Must release before reInit");
  117. glGenTextures(1, &mHandle);
  118. glGenBuffers(1, &mBuffer);
  119. }
  120. bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
  121. {
  122. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  123. if (!bmp)
  124. return false;
  125. // check format limitations
  126. // at the moment we only support RGBA for the source (other 4 byte formats should
  127. // be easy to add though)
  128. AssertFatal(mFormat == GFXFormatR8G8B8A8, "GFXGLTextureObject::copyToBmp - invalid format");
  129. AssertFatal(bmp->getFormat() == GFXFormatR8G8B8A8 || bmp->getFormat() == GFXFormatR8G8B8, "GFXGLTextureObject::copyToBmp - invalid format");
  130. if(mFormat != GFXFormatR8G8B8A8)
  131. return false;
  132. if(bmp->getFormat() != GFXFormatR8G8B8A8 && bmp->getFormat() != GFXFormatR8G8B8)
  133. return false;
  134. AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
  135. AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
  136. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  137. PRESERVE_TEXTURE(mBinding);
  138. glBindTexture(mBinding, mHandle);
  139. U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
  140. U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
  141. if(dstBytesPerPixel == srcBytesPerPixel)
  142. {
  143. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], bmp->getWritableBits());
  144. return true;
  145. }
  146. FrameAllocatorMarker mem;
  147. U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
  148. U8 *dest = bmp->getWritableBits();
  149. U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
  150. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
  151. PROFILE_START(GFXGLTextureObject_copyToBmp_pixCopy);
  152. for(int i = 0; i < srcPixelCount; ++i)
  153. {
  154. dest[0] = orig[0];
  155. dest[1] = orig[1];
  156. dest[2] = orig[2];
  157. if(dstBytesPerPixel == 4)
  158. dest[3] = orig[3];
  159. orig += srcBytesPerPixel;
  160. dest += dstBytesPerPixel;
  161. }
  162. PROFILE_END();
  163. return true;
  164. }
  165. void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
  166. {
  167. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  168. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  169. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  170. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  171. if(mBinding == GL_TEXTURE_3D)
  172. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  173. if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  174. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  175. mNeedInitSamplerState = false;
  176. mSampler = ssd;
  177. }
  178. void GFXGLTextureObject::bind(U32 textureUnit)
  179. {
  180. glActiveTexture(GL_TEXTURE0 + textureUnit);
  181. glBindTexture(mBinding, mHandle);
  182. GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
  183. if( gglHasExtension(ARB_sampler_objects) )
  184. return;
  185. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  186. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  187. if (!sb)
  188. return;
  189. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  190. if(mNeedInitSamplerState)
  191. {
  192. initSamplerState(ssd);
  193. return;
  194. }
  195. if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
  196. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  197. if(mSampler.magFilter != ssd.magFilter)
  198. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  199. if(mSampler.addressModeU != ssd.addressModeU)
  200. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  201. if(mSampler.addressModeV != ssd.addressModeV)
  202. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  203. if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
  204. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  205. if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  206. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  207. mSampler = ssd;
  208. }
  209. U8* GFXGLTextureObject::getTextureData( U32 mip )
  210. {
  211. AssertFatal( mMipLevels, "");
  212. mip = (mip < mMipLevels) ? mip : 0;
  213. const U32 dataSize = isCompressedFormat(mFormat)
  214. ? getCompressedSurfaceSize( mFormat, mTextureSize.x, mTextureSize.y, mip )
  215. : (mTextureSize.x >> mip) * (mTextureSize.y >> mip) * mBytesPerTexel;
  216. U8* data = new U8[dataSize];
  217. PRESERVE_TEXTURE(mBinding);
  218. glBindTexture(mBinding, mHandle);
  219. if( isCompressedFormat(mFormat) )
  220. glGetCompressedTexImage( mBinding, mip, data );
  221. else
  222. glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
  223. return data;
  224. }
  225. void GFXGLTextureObject::copyIntoCache()
  226. {
  227. PRESERVE_TEXTURE(mBinding);
  228. glBindTexture(mBinding, mHandle);
  229. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  230. if(mBinding == GL_TEXTURE_3D)
  231. cacheSize *= mTextureSize.z;
  232. cacheSize *= mBytesPerTexel;
  233. mZombieCache = new U8[cacheSize];
  234. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  235. }
  236. void GFXGLTextureObject::reloadFromCache()
  237. {
  238. if(!mZombieCache)
  239. return;
  240. if(mBinding == GL_TEXTURE_3D)
  241. {
  242. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  243. delete[] mZombieCache;
  244. mZombieCache = NULL;
  245. return;
  246. }
  247. PRESERVE_TEXTURE(mBinding);
  248. glBindTexture(mBinding, mHandle);
  249. if(mBinding == GL_TEXTURE_2D)
  250. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  251. else if(mBinding == GL_TEXTURE_1D)
  252. glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  253. if(GFX->getCardProfiler()->queryProfile("GL::Workaround::needsExplicitGenerateMipmap") && mMipLevels != 1)
  254. glGenerateMipmapEXT(mBinding);
  255. delete[] mZombieCache;
  256. mZombieCache = NULL;
  257. mIsZombie = false;
  258. }
  259. void GFXGLTextureObject::zombify()
  260. {
  261. if(mIsZombie)
  262. return;
  263. mIsZombie = true;
  264. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  265. copyIntoCache();
  266. release();
  267. }
  268. void GFXGLTextureObject::resurrect()
  269. {
  270. if(!mIsZombie)
  271. return;
  272. glGenTextures(1, &mHandle);
  273. glGenBuffers(1, &mBuffer);
  274. }
  275. F32 GFXGLTextureObject::getMaxUCoord() const
  276. {
  277. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  278. }
  279. F32 GFXGLTextureObject::getMaxVCoord() const
  280. {
  281. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  282. }
  283. const String GFXGLTextureObject::describeSelf() const
  284. {
  285. String ret = Parent::describeSelf();
  286. ret += String::ToString(" GL Handle: %i", mHandle);
  287. return ret;
  288. }